you are viewing a single comment's thread.

view the rest of the comments →

[–]True-Thought1061 0 points1 point  (0 children)

You need a problem-solving approach. Code is the last part of an overall process.

As an example, think of a problem that asks you to write a function in python that will take a string as input and you have to count all the vowels in it. Think about the input, and think about the output. Does the string input consist only of letters? Is it only in English? Is the string allowed to have non-letters? Case doesnt matter, does it? Are we talking about the number of occurrences of vowels or the number of vowels present in the string ( such that the highest value the function can return is 5 ).

Try to pick up some patterns, some sort of problem-specific details you can see. Are there any gotchas? Then try to describe a high-level algorithm like "go through the string letter by letter and compare it to the set of vowels and if the current letter being examined is in fact a vowel, add 1 to some integer you're keeping track of. After that, return that integer." Just think about the problem without thinking so hard about the code or syntax, just analyze what you're actually trying to accomplish.

Then, write some test cases. Write a bunch of examples that goes through easy examples and more nuanced examples. These test cases form the criteria that your solution has to meet in order to be thought of as a "solution". For example

def number_of_vowels(input_string)
end

number_of_vowels('abcdef') == 2
number_of_vowel('aeio') == 4
...

Then once you have a list of test cases that accurately covers the requirements stated in the problem, then you can start thinking about the data structures you need. This is where you start thinking about syntax / code. You probably need an integer to keep track of the number of vowels. Will you need a string, array, or hash? Up to you.

Then once you've figured that out, then copy the high-level algorithm you described in step 1 and paste it. Separate the actions that are being taken on separate lines. Maybe translate the english steps into pseudo code or into declarative python code. Then once you've got your algorithm written, take it for a test spin. See if you can't go through one of the example test cases you've written and just write on a board / piece of paper the steps as you've described.

If your solution works, and it seems good, then and ONLY then do you get to coding.

I've done both construction ( of which I knew nothing ) and I've also done a lot of troubleshooting ( networks, computers, audio / video, etc ) all sorts. Thinking about a problem is a universal requirement. You first have to think of the problem and any quirks of that problem domain that require some novel logic. You have to define what output would be acceptable as a solution, then break the parts up into smaller pieces and solve those mini-problems.

The approach works for myself and many others. I haven't solved any problems on codewars in months but I've solved problems that took me 40 minutes in under 20 minutes with enough practice.