push_swap
by bhagenlo
written for version 6
Hey hey hey, you did it! You arrived at your first 'real' algorithms project.
You'll get to know layering software and unit testing, as well as the one big chunk:
Learning how to think in algorithms in addition to thinking in code.
With that, let's already get started. It's gonna be fun ;)
As Wikipedia says, an algorithm is a specification of a sequence of instructions used to solve a problem or perform a computation. The code you'll be writing is 'just' the implementation of such an algorithm. This implementation can have errors, even if the algorithm is correct. However, if the algorithm has errors, then there's no way your implementation following your algorithm could ever possibly be correct.#Prerequisites
Make sure you understand the difference between algorithm and code.
It guarantees correct results and it has to be clear what it does on every possible input.
For that, you could watch the first 22 minutes of this talk, or read its accompanying article.
(As well as the difference between comparison and non-comparison algorithms - if you like :))
So, you did your research? Great! Let's finally start. For that, let's make a plan. After reading the subject, you probably were a little bit confused about what exactly you would have to do to solve this task. For me, it was quite confusing at the time. That's what we'd like to provide a map for. In order to do this project, we think it's best to do the following: Create yourself some bigger struct you can put all else in, and which most of your functions take as a parameter. This might look like that: Concerning the data structure inside of Implement the basic data type. More concretely: Implement the And only if you've come up with rules for these cases, then start to finally write the code. And then, when you – after some superficial or more profound testing – have decided that your functions work, write some unit tests, meaning: Write a test that inputs some 'normal' and all the special cases and compares the behaviour of your functions with the expected one. Run Or however you want to make it look ;) Implement your 'special' functionality on top of your general, well-tested one. And now, finally: You've arrived at the algorithmic part. There's a few branches opening up from here, but there are two big approaches most of the people at our school took. The general idea is that you take some established algorithm (or some essential concepts out of it) and adjust it to our scenario. Some promising algorithms to look at: And there's also a push_swap visualizer for testing your algorithmic attempts. (If you can't get the C++ version to run, try the python one. That should work.)#During
typedef struct s_state
{
t_stack *a;
t_stack *b;
...
} t_s;
typedef struct s_push_swap
{
t_state *s;
...
} t_ps;
t_stack
? Choose for yourself :)
A linked list probably works a bit better than an array – but choose whatever you'd like to practice more ^^
push
, pop
, swap
and rotate
functions on this 'stack'. (A real stack has only push
and pop
.)
For that, think for a moment what you'd like them to do.
NULL
be?
For that, it is helpful to write this additional testing functionality inside of another file (a test.c
, maybe?), and to add a make test
-rule to your Makefile
that compiles and runs the tests. That way, you can ensure that you do run the tests regularly.make test
:
$ make test
...
Testing simple stack functions...
Test 4 (push): {4, 2, 3} ≠ {4, 2, 3, NULL}
Test 11 (rotate): {2, 2, 3, NULL} ≠ {2, 3, NULL}
Passed 10 / Failed 2 (of 12)
pa
, pb
, sa
, sb
, ss
, ra
, rb
, rr
, rra
, rrb
and rrr
.
I won't guide you through an existing solution in here. But here's some general advice:
Well, not much to be done here. Make sure you tested your push_swap properly, ran it against the That should be it. Good luck!#Cleaning Up
checker
, and got enough points on that to pass.
To be honest, I fear that you're already quite fed up with doing anything like push_swap again. So, no algorithms or anything in here. Instead, again some of the basics: Re-implement your stack from scratch, following the same process as last time. Only this time, you choose the underlying data structure you didn't choose for push_swap. Think before you write, and also write unit tests. And if you're like: 'Bah, I don't need that.'? Trust us, it'll come in handy to know when to use an array over a linked list and vice versa. The latest in *shudder* #Aftercare
#Task
minishell
.
With that, you should be good to go. See you next time :)
And may your algorithms ever be sound.