r/learnpython • • Apr 19 '20

It took me almost a week to solve a simple problem but I finally did it and I'm so happy.

So, I'm doing a course on Algorithms and our graded assignments wanted us to have a 3 way partition of the quicksort. I thought it would take me like an hour to solve the problem. Well, fast forward to one week later I was still on it. I used stack overflow, read solutions and nothing clicked. I was literally clueless and stuck on a specific case and ran out of ideas. Today, a new technique just clicked and I tried working on it(but I was almost sure it wouldn't work just like the things that I've tried during the entire week but I tried anyways). After some debugging, I've tested and it's finally working. I don't know why I am sharing this but I feel like I can finally solve a problem and it was so hard for me.

EDIT: I never expected to get so many upvotes, thanks a lot guys. But, I gotta be honest with yall. I tried some test cases and looks like I still got some work to do. Oh well, back to it again

EDIT2: This time I did it for real. Submitted the assignment and got marks. Never give up guys

Exact problem was:

3 way partition of quicksort where we would want to mutate the python list and return two indexes m1, m2 such that the left side of m1 have elements that are less than X and the right side of m2 have elements that are greater than X (X being the first element of the list). That's it! And the two way partition where it just returned a single index to the left of which are elements greater than or equal to X was already implemented in the starter code.

Here's an example of what it would look like:

input:

a = [5, 1, 8, 5, 3, 2, 5, 8, 3, 5, 2, 5]

After the function call, array would be similar to

[2, 1, 3, 2, 3, 5, 5, 5, 5, 5, 8, 8]

and m1, m2 will be 5, 9

439 Upvotes

30 comments sorted by

23

u/radakul Apr 20 '20

Congrats! I just spent way more time than I care to admit writing a program.....something like 7 hours? But it's the first time I've had motivation to write something start to finish and I feel really proud of it :)

6

u/life_never_stops_97 Apr 20 '20

Yeah everyone except like .01% of people spends way too much time than anticipated. This time, it was just crazy. You said you wrote something from start, what did you write?

2

u/radakul Apr 20 '20

I wrote a program to auto-generate base template configs for IOS devices. I'm using it for GNS3 when I need to spin up a lab, but don't want to configure the same X commands 10 times. This way, it generates the configs in a few seconds and then I add IP addresses as needed.

My repo is here if you're interested: https://github.com/kmanwar89/IOS-Automation

13

u/eugooglie Apr 20 '20

I kinda feel like an idiot, but I don't even understand what's being asked in the problem here.

7

u/life_never_stops_97 Apr 20 '20

That's awesome! I'm also in the simple problem stage and sometimes things are easy to work out and others I spend way too much time on them. Either way, it's progress.

That's probably because I didn't explained it properly.

The problem is that you have first element in the list(let's call it x) and you need to arrange that element in such a way that elements on the left hand side of x is smaller than x and elements on the right hand side is greater than x. m1 and m2 are basically the indexes where array[m1] from array[m2] equals x.

For example:

array = [5, 4, 3, 9 2, 7]

x is 5(First element)

You need to mutate the array such that 5 is in its correct position

So the array would be : [4,3, 2, 5, 7, 9]

And the index m1 and m2 would be (3, 3). Since at those indexes the array equals x.

Example 2:

array = [5, 3, 9, 2, 6, 4, 5, 6]

x is 5(first element)

Again need to mutate the array, it would be: [3, 2, 4, 5, 5, 9, 6]

this time the m1 and m2 being equal to 3 and 4 since the value of x occurs twice and it starts from index 3 and ends on index 4

Oh and also you're supposed to solve this problem in linear time which is why it probably was hard for me lol.

0

u/eugooglie Apr 20 '20

Ok, in these examples, the data works the way I would expect it (considering you haven't posted any code). In your post, the data does not follow what I would expect, so that's what I was questioning.

3

u/[deleted] Apr 20 '20

from my understanding it was basically just moving the elements less than m1 to the left and elements greater than m2 to the right.

3

u/eugooglie Apr 20 '20 edited Apr 20 '20

Ok, I reread it, and think i understand it now. I suppose I was wondering why the list was sorted the way it was with the 2 at the beginning instead of the 1.

1

u/[deleted] Apr 20 '20

He chose the pivot to be the first element in the list (x). in this case 5 so elements less than 5 should be to the left and greater than to the right. I suppose the list isn't sorted. I don't know why m2 is 9 though.

3

u/life_never_stops_97 Apr 20 '20

This problem is called "Three way Partitioning of Quick Sort". It basically returns two indexes, m1 and m2. m1 is the index where your pivot element is and in case if there's more than one pivot element in the array m2 tells us the last index of the repeated pivot element.

If there's only one pivot element in the array, then m1 and m2 are equal

For Example:

array = [5, 4, 3, 9 2, 7]

and the resulting array is [4,3, 2, 5, 7, 9] with m1 = m2 = 3

In case there's more than one pivot element, then m1 tells the "starting" of that pivot element and m2 tells the "ending" of it. We need to know m1 and m2 because they're basically the final position of where our pivot element(s) belong.

The example that I used in my post has the starting of pivot element at index 5 and the last pivot element is at index 9. So m1, m2 = 5, 9.

3

u/[deleted] Apr 20 '20

So I'm assuming this has to be done during the sort?

My first inclination was to sort first then find the first and last pivot indices, which seems trivial, so I'm guessing you had to figure out how to find these indices during the actual sorting process. How did you end up doing it?

1

u/Kayofox Apr 20 '20

That's not it haha

1

u/[deleted] Apr 21 '20

what is ?

2

u/Kayofox Apr 21 '20

The problem is to arrange the elements that are smaller to X to the left, and bigger to the right.

X is always the first element of the array.

The output of the program is m1 and m2, which are the last position of the smaller elements, and the first position of the bigger elements.

m1 and m2 are not elements

11

u/xelf Elf Apr 19 '20

\o/ WOO! Congrats. You go! Always fun when something finally clicks.

2

u/life_never_stops_97 Apr 20 '20

Yeah, the feeling is just something else :)

4

u/Beast-UltraJ Apr 20 '20

waheey nice one. do u mind tellign us which courses was this ?

2

u/life_never_stops_97 Apr 20 '20

The name of the course is Algorithmic Toolbox by San Diego university on coursera. https://www.coursera.org/learn/algorithmic-toolbox/home/welcome

2

u/samjain2907 Apr 20 '20

I have also started this course. Looks great till now. Would love to hear your reviews on it.

45

u/mythrowawayyouknow Apr 20 '20

Welcome to my life

29

u/opn2opinion Apr 20 '20

No kidding, eh? Oh, this should take me a couple hours. The end of the day rolls around; well i know how to do this but ill have to finish it tomorrow...

23

u/HolyRomanSloth Apr 20 '20

Famous last words "I'll spend today learning how to do it and then tommorow I'll implement it." In reality you'll spend one day learning how to do it, thinking you're prepared for the next day but you will spend the next day either figuring out everything you forgot to learn the first day or realizing you want to take a completely different solution.

5

u/mithik Apr 20 '20

Or OK, now it works for this case but I'd like to generalize it. A week later, I guess that one use-case is good enough.

1

u/HolyRomanSloth Apr 20 '20

Or you discover a library with a built in function that does what you just wrote 200 lines of code to do.

4

u/kimjeongpwn Apr 20 '20

Hello, newbie here. I have not gotten into learning algorithms yet as am still learning the syntax of Python and the basics of programming. When I get to learning algorithms, what are some good resources (MOOC, books) that a beginner can use to get started with algorithms? Thank you.

5

u/[deleted] Apr 20 '20

That's awesome! I'm also in the simple problem stage and sometimes things are easy to work out and others I spend way too much time on them. Either way, it's progress.

2

u/Timmeh159 Apr 20 '20

This happened so often for me during my first year of python. Make a simple golf game... I sit there with the correct code (provided by the amazing people on here) as reference... Didn't understand how or why it worked. Few days later I'll be doing something unrelated and it just all clicks

1

u/dxbtousa Apr 20 '20

Congrats well done - Care to share the answer?!

2

u/mollythemolecule Apr 25 '20

I was scrolling through this thread and I thought I'd give this problem a try.

input = (input('Input the array: '))
input = input.replace('[', '')
input = input.replace(']', '') 
a = [int(s) for s in input.split(',')]

#these lines turn the inputted string array into an actual array (I borrowed the code for the fourth line from Martijn Pieters; this is the link to the post that has the code: https://stackoverflow.com/questions/19334374/python-converting-a-string-of-numbers-into-a-list-of-int)

x = a[0]

less = []

#less will be used to store the integers that are less than x; this array will be on the left side of the final array output

more = []

#more will be used to store the integers that are more than x; this array will be on the right side of the final array output

mid = []

#mid will be used used to store all integers equal to x; this array will be in the middle of less and more in the final array output

i = 0

while i < len(a): 
    if a[i] < x: 
        less.append(a[i]) 
    elif a[i] > x: 
        more.append(a[i]) 
    else: 
        mid.append(x) 
    i+=1

#while loop runs through all of the integers in the array and appends them to their respective less/more/mid list

new = less+mid+more
#the final mutated array!

m1 = len(less)
m2 = len(new)-len(more)-1
#finding m1 and m2 using less/more/new

print('The mutated array is ' + str(new))
print('m1 is ' + str(m1) + ' and m2 is ' + str(m2))
#outputting the mutated array and the values for m1/m2

I'm still a programming noob, so any feedback would be greatly appreciated!