LogFAQs > #955655703

LurkerFAQs, Active DB, DB1, DB2, DB3, DB4, DB5, DB6, DB7, Database 8 ( 02.18.2021-09-28-2021 ), DB9, DB10, DB11, DB12, Clear
Topic List
Page List: 1
TopicITT: Learning Python
SpiritSephiroth
07/01/21 9:46:31 PM
#36:


1337toothbrush posted...
In order to write code, you need to know two things:

1) What you want to achieve.
2) How to get there.

2) outlines the steps and 1) provides validation. Let's go over the steps in the code sample where you're replacing all vowels in a phrase with the letter "g" or "G" (matching case with the original vowel):

You need a variable to hold the result, so you declare: translation = "". Now this line is doing two important things. First, it's defining the existence of a string and second, it's assigning it the value of an empty string. When crafting algorithms, you need to account for different cases. Let's say the variable phrase is an empty string. In this case, you'd return an empty string as well. This is considered the base case (a trivial case that acts as a base). This is covered by starting translation off as "". Additionally, you need a data structure that can store the data that you append. You can append to that string as you go along, like you do in your code. What follows is the general case (what your algorithm is doing in general).

To translate, you need to go through phrase character by character, hence the line: for letter in phrase:. The inside of this loop is the critical part. What do you intend to do with every single character in phrase? Well, the algorithm checks against vowels and then does the appropriate action. Now why would you first set the letter to lower case? Because your string of vowels is also lower case and you want the algorithm to work on both lower-case and upper-case vowels in the string phrase. Things like that inform you on what functions you need to use and when.

Thanks man a lot of that made sense. Im gonna sleep on this and make another code similar to this tomorrow! Appreciate it.

But that first step you said, "what I want to achieve", I should be thinking more about this personally.

---
... Copied to Clipboard!
Topic List
Page List: 1