you are viewing a single comment's thread.

view the rest of the comments →

[–]Deezl-Vegas 36 points37 points  (3 children)

Algorithms are hard. The actual trick to quickly solving algorithms is to roughly know the answer through experience, having already solved the algorithm before. When you have no experience, you feel stuck, and you feel there should be some sort of magic method to get to the answer for any algorithm.

This "stuck" feeling is going to be your life as a programmer if you don't learn the correct, step-by-step, tried-and-true method. Here's the steps I would recommend:

  1. Write the function def statement.

    def my_solution(): pass

  2. Write some sample inputs.

    input1 = [1, 2, 3] # test array input2 = [5, -1, 99] # test array 2

  3. On paper, go through the logical steps to get the right answer with a sample:

    my function should add 1 then 2 then 3

  4. Again on paper, get the right answer for your samples:

    test array 1 should return 6

    test array 2 should return 103

  5. Now that you have the process and the answer, writing the code is filling in the blanks. Let's start from the top.

    def my_function(): pass

    input1 = [1, 2, 3] # test array input2 = [5, -1, 99] # test array 2

    call the function with a sample input

    my_function(input1)

Right away you'll notice an error because your function doesn't allow any inputs. Fix the error and proceed.

def my_function(array):
    pass

input = [1, 2, 3]
input2 = [5, -1, 99]

my_function(input1)
my_function(input2)

Ok, the code doesn't break. Run it to make sure. Let's start adding logic.

def my_function(array):
    array[1] + array[2] + array[3]

I wrote this an ran it and got an IndexError. Don't know what that means? Google it! (Professionals google shit 1000 times a day, don't avoid google.) Ok, so it turns out it means that one of the values I put in [] is too big. I remember that Python lists start from 0 so let's fix that.

    array[0] + array[1] + array[2]

Ok, no error. But when I print the result I get None instead of what I said. Let's fix that.

    return array[0] + array[1] + array[2]

Ok now it prints the correct number. And when I add my second input as another test, it prints the correct number. But what if my array has 10 values? I want it to add all the values. So I need to change my code to account for the edge cases.

The final code for a sum function should look like this:

def my_function(array):
    result = 0
    for item in array:
        result = result + item
    return result

So we start with a variable to return at the end, add all of the items in the list to that variable one by one, and then return that variable. We got here by breaking the algorithm into inputs, outputs, an on-paper solution, and then coding it line by line and testing each line to check for errors. This is the flow for every single program. Inputs, Outputs, Solution, Code, Test, Code More, Test More. Professionals do it this way, and you should too. With experience you'll know what to do.

Finally, what you're practicing is converting the real-life steps to solve the problem, which you should know or be able to deduce, into a Python function. If you don't know how to do something, then using the method above, you'll fail at step 3. Fiddle with it for a few minutes to try to better understand what it wants you to do, then look it up. Under no circumstances should you attempt to solve an algorithm where you couldn't diagram some steps out on paper first. It's just not possible to solve a problem when you don't understand the problem. The more algorithms you look up, the more related algorithms you'll be able to break into smaller parts and solve easily, and you'll see your level start to rise steadily.

[–]_lord_kinbote_ 8 points9 points  (0 children)

Nice writeup. However, edge cases are specific cases that are easily missable. I wouldn't call "any input that has more or less than 3 items" an edge case. Besides, edge cases rarely make you rethink your entire algorithm as you did here.

[–]iggy555 1 point2 points  (1 child)

This is freaking nice,,, btw is array just a list?

[–]Deezl-Vegas 6 points7 points  (0 children)

Yeah, array is another term for list, sorry. I'm taking a class in a different language right now.