Ask HN: How do I solve problems/code outside of the book I used to learn python?
3 comments
Hello,
You need to break the problem into small steps. One step at the time. Then you unify the code. This is not a straight-forward process. For complex problems you might try to grab a problem from many different angles... That's why each user on Reddit gave a different solution. Not all solutions follow the same logic... But that's the good thing with programming, there are usually more than N solutions for each problem. Of course some are faster than others, some are better than others...
To give a practical example to the problem you're trying to solve:
1. Given a text, we need to separate the words. So we do need to 'split' a phrase into words.
2. It would be nice to have the words into an 'array'. Arrays are a very handy data structures for this kind of problem.
3. Now we need to iterate through the words and given a word, find the consonants. In order to do that, we need to split each word in to letters.
4. Then we need to iterate through each letter of the word, test if it's a consonant. How will you do that? I would create a list of consonants.. (Another Array! Too many Arrays you might think right?) and try to 'match' each letter with that 'array'.
4.1 Now if the 'letter' doesn't match with any item listed in 'consonants' array, do nothing. If it does match, substitute the consonant following the rules given from the problem.
6. Now we need to 'join' our letters together, with our substitutions.
7. Then we need to join the 'words' together adding spaces in between to re-form the phrase.
8. Then we need to print the final output.
That's a first version of this program which you can optimize later on... Until you end up with something that you like.
Later you might add exceptions and checks. Then you might find another way to make your program iterate through each letter/word faster using an algorithm (well python will do that for you probably, I wouldn't know as I am a ruby programmer but... I can guess it has already optimized functions for all these actions).
Good luck!
You need to break the problem into small steps. One step at the time. Then you unify the code. This is not a straight-forward process. For complex problems you might try to grab a problem from many different angles... That's why each user on Reddit gave a different solution. Not all solutions follow the same logic... But that's the good thing with programming, there are usually more than N solutions for each problem. Of course some are faster than others, some are better than others...
To give a practical example to the problem you're trying to solve:
1. Given a text, we need to separate the words. So we do need to 'split' a phrase into words.
2. It would be nice to have the words into an 'array'. Arrays are a very handy data structures for this kind of problem.
3. Now we need to iterate through the words and given a word, find the consonants. In order to do that, we need to split each word in to letters.
4. Then we need to iterate through each letter of the word, test if it's a consonant. How will you do that? I would create a list of consonants.. (Another Array! Too many Arrays you might think right?) and try to 'match' each letter with that 'array'.
4.1 Now if the 'letter' doesn't match with any item listed in 'consonants' array, do nothing. If it does match, substitute the consonant following the rules given from the problem.
6. Now we need to 'join' our letters together, with our substitutions.
7. Then we need to join the 'words' together adding spaces in between to re-form the phrase.
8. Then we need to print the final output.
That's a first version of this program which you can optimize later on... Until you end up with something that you like.
Later you might add exceptions and checks. Then you might find another way to make your program iterate through each letter/word faster using an algorithm (well python will do that for you probably, I wouldn't know as I am a ruby programmer but... I can guess it has already optimized functions for all these actions).
Good luck!
As another commenter (iovar) wrote, "try to solve this in your head with pseudo code do not overcomplicate it". There's actually nothing about this problem that requires splitting the input into words or storing the words in an array. You can just read one input character C at a time and either write CoC to the output (if a consonant) or copy C to the output (if any other character: vowel, white-space, punctuation, etc.). If a consonant character is capitalized, the one you write after the 'o' should be lower-cased. (Think of it as a Unix filter that copies a transformed version of its standard input stream to its standard output stream, like the Unix 'tr' command.)
Clickable link to original problem: https://www.reddit.com/r/dailyprogrammer/comments/341c03/201...
Clickable link to original problem: https://www.reddit.com/r/dailyprogrammer/comments/341c03/201...
Being a progtammer is being a problem solver. The language hardly matters.
Try to solve this in your head with pseudo code and do not overcomplicate it. It's a very simple problem. Also at your level, don't think of the language string handling facilities or other advanced Api's. Just control structures and arrays.
Finally, it's fine to ask for help when trying to build something, but when trying to learn, it's cheating yourself of the opportunity to have that moment were you actually understand the problem.
So my advice is, go back to solving the problem and don't come back here to see for possible answers until you have it and just want to compare solutions.
Try to solve this in your head with pseudo code and do not overcomplicate it. It's a very simple problem. Also at your level, don't think of the language string handling facilities or other advanced Api's. Just control structures and arrays.
Finally, it's fine to ask for help when trying to build something, but when trying to learn, it's cheating yourself of the opportunity to have that moment were you actually understand the problem.
So my advice is, go back to solving the problem and don't come back here to see for possible answers until you have it and just want to compare solutions.
A few things I would suggest to anyone learning to program:
1. Seek out peers to learn with. You don't need to go to school to find this, but it is a great place to start. Reddit, forums, local meetups, etc are all also good places to look.
The one thing I want to stress here is that you want quality over quantity. If you can find 1-3 people who you work with on a regular basis, that will have a much larger impact than meeting with 100 programmers. You generally want to find people who are around the same level as you are so that you can all learn together and teach one another. This is actually the most valuable aspect of college in my opinion - you start off with a group of peers who are (for the most part) at about the same level as you are, you make friends, and you progress at roughly the same rate. In doing that, you learn to teach one another, learn from each other, and you end up teaching each other nearly as much as the professor teaches.
If you do absolutely nothing else I mention, just do this. Find people to learn with.
2. Learn from as many sources as you can. You already read LPTHW, so now go to codecademy and try the python track, look for (and attempt to solve) more daily challenges on reddit, go to https://projecteuler.net/ and spoj.com to find problems and attempt to solve them. When you run into trouble, use those peers from (1), or if you don't have any yet, post to reddit explaining where you got stuck.
You can even try doing different things - you can try creating a website and follow tutorials on that. The point is you shouldn't read a single book, but you should read everything you can get your hands on.
3. Program constantly. Even if it seems like something you already know how to do, do it anyway. Make sure you can write that code without referencing a single book or snippet of code. If you have that, look for something else to code and see if you can do it. Tweak your code to see if you can solve the problem in another way.
My guess is if you aren't coding at least 15 hrs per week, you likely aren't coding enough. Programming isn't something you can do 1 day a week and get great at. People who get great at programming get consumed by it. Every great programmer I know spent 10+ hours every week working on side projects, learning new things, and simply coding. The bad programmers I know are the ones who only coded when they had homework or some other assignment, and the difference is astonishing.
4. Don't just program mundane things (eg formatting HTML and CSS), but try to solve new problems. This is why I mentioned spoj, project euler, and suggested learning from as many sources as you can. Solving new problems will teach you how to be a better problem solver, and you also need to practice expressing your solutions to a problem in code.
Now if you truly want to solve that problem, feel free to email me - my HN username @ gmail.com - and I will be happy to try to help you break it into smaller parts and solve it.
1. Seek out peers to learn with. You don't need to go to school to find this, but it is a great place to start. Reddit, forums, local meetups, etc are all also good places to look.
The one thing I want to stress here is that you want quality over quantity. If you can find 1-3 people who you work with on a regular basis, that will have a much larger impact than meeting with 100 programmers. You generally want to find people who are around the same level as you are so that you can all learn together and teach one another. This is actually the most valuable aspect of college in my opinion - you start off with a group of peers who are (for the most part) at about the same level as you are, you make friends, and you progress at roughly the same rate. In doing that, you learn to teach one another, learn from each other, and you end up teaching each other nearly as much as the professor teaches.
If you do absolutely nothing else I mention, just do this. Find people to learn with.
2. Learn from as many sources as you can. You already read LPTHW, so now go to codecademy and try the python track, look for (and attempt to solve) more daily challenges on reddit, go to https://projecteuler.net/ and spoj.com to find problems and attempt to solve them. When you run into trouble, use those peers from (1), or if you don't have any yet, post to reddit explaining where you got stuck.
You can even try doing different things - you can try creating a website and follow tutorials on that. The point is you shouldn't read a single book, but you should read everything you can get your hands on.
3. Program constantly. Even if it seems like something you already know how to do, do it anyway. Make sure you can write that code without referencing a single book or snippet of code. If you have that, look for something else to code and see if you can do it. Tweak your code to see if you can solve the problem in another way.
My guess is if you aren't coding at least 15 hrs per week, you likely aren't coding enough. Programming isn't something you can do 1 day a week and get great at. People who get great at programming get consumed by it. Every great programmer I know spent 10+ hours every week working on side projects, learning new things, and simply coding. The bad programmers I know are the ones who only coded when they had homework or some other assignment, and the difference is astonishing.
4. Don't just program mundane things (eg formatting HTML and CSS), but try to solve new problems. This is why I mentioned spoj, project euler, and suggested learning from as many sources as you can. Solving new problems will teach you how to be a better problem solver, and you also need to practice expressing your solutions to a problem in code.
Now if you truly want to solve that problem, feel free to email me - my HN username @ gmail.com - and I will be happy to try to help you break it into smaller parts and solve it.
For example:
I'm looking to try and solve this on /r/DailyProgrammer: http://redd.it/341c03 but I just can not do it for the life of me. I can go through all of codecademy and LPTHW but I can't figure out something this simple!? Damn it. In my head this is how I would solve it:
First you need to grab user input and store it Next you need to define what consonants are and create a list If user input contains any of the letters defined in the list (regardless of case) place an 'o' after the specific letter and add the consonant Return to above command and check for any other characters, once every character is checked return new output and exit.
---
But for some reason I just hit a blank wall and can't apply this to python, it's as if I learn the syntax but not problem solving, or its like I haven't really learned programming. Any advice at all would be appreciated as I really want to start being able to do interesting stuff!