Python daily exercise (23 Part Series)
1 Python Daily exercise 1: Generate a list of even number
2 Python daily exercise 2: string
… 19 more parts…
3 Python daily exercise 3: reverse a string
4 Python daily exercise 4: separate a string
5 Python Daily Exercise 5: Generate new list by removing unwanted object
6 Python Exercise 6: Remove Special character In String
7 Python exercise 7: Determine a string is a mixture of something
8 Python Exercise 8: generate a dictionary from different source
9 Python Exercise 9: find the key with the minimum value in a dictionary
10 Python Exercise 10: Generate a list based on odd or even index
11 Python Exercise 11: Count the number and generate new dictionary
12 Python exercise 12: find the hidden ball
13 Python Exercise 13: Scottish Screaming
14 Python Exercise 14: sum of fractions
15 Python Exercise 15 : Perform calculation on a string
16 Python Exercise 16: find the highest index alphabet
17 Python exercise 17: convert number to english word
18 Python exercise 18:Sales Season
19 Python Exercise 19:advanced list sort
20 Python Exercise 20:Find the pattern
21 Python Exercise 21: list comprehensions
22 Python exercise 22: ransomnote
23 Python Exercise 23: Two sum
Question
- write a program to transform a speech to a strong Scottish accent
- a strong Scottish accent makes every vowel similar to an “e”,
- so you should replace every vowel with an “e”.
-
Additionally, it is being screamed, so it should be in block capitals.
-
Create a function that takes a string and returns a string.
example
to_scottish_screaming("hello world") "HELLE WERLD"to_scottish_screaming("Mr. Fox was very naughty") "MR. FEX WES VERY NEEGHTY"to_scottish_screaming("Butterflies are beautiful!") "BETTERFLEES ERE BEEETEFEL!"to_scottish_screaming("hello world") "HELLE WERLD" to_scottish_screaming("Mr. Fox was very naughty") "MR. FEX WES VERY NEEGHTY" to_scottish_screaming("Butterflies are beautiful!") "BETTERFLEES ERE BEEETEFEL!"to_scottish_screaming("hello world") "HELLE WERLD" to_scottish_screaming("Mr. Fox was very naughty") "MR. FEX WES VERY NEEGHTY" to_scottish_screaming("Butterflies are beautiful!") "BETTERFLEES ERE BEEETEFEL!"
Enter fullscreen mode Exit fullscreen mode
My attempt
Fail attempt
- algorithm
>Turn a sentence to Scottish accent>>replace all the vowel in the string with e and capitalise the whole sentence>>>find every vowel in the stringset up a vowel listfor every letter equals to one of the vowel list it is a vowel>>>replace the vowel e>>>capitalise the string>Turn a sentence to Scottish accent >>replace all the vowel in the string with e and capitalise the whole sentence >>>find every vowel in the string set up a vowel list for every letter equals to one of the vowel list it is a vowel >>>replace the vowel e >>>capitalise the string>Turn a sentence to Scottish accent >>replace all the vowel in the string with e and capitalise the whole sentence >>>find every vowel in the string set up a vowel list for every letter equals to one of the vowel list it is a vowel >>>replace the vowel e >>>capitalise the string
Enter fullscreen mode Exit fullscreen mode
- code
<span>def</span> <span>to_scottish_screaming</span><span>(</span><span>string</span><span>:</span> <span>str</span><span>):</span><span>vowel_list</span> <span>=</span> <span>[</span><span>"</span><span>a</span><span>"</span><span>,</span> <span>"</span><span>e</span><span>"</span><span>,</span> <span>"</span><span>i</span><span>"</span><span>,</span> <span>"</span><span>o</span><span>"</span><span>,</span> <span>"</span><span>u</span><span>"</span><span>]</span><span>string</span> <span>=</span> <span>string</span><span>.</span><span>lower</span><span>()</span><span>for</span> <span>letter</span> <span>in</span> <span>string</span><span>:</span><span>if</span> <span>letter</span> <span>in</span> <span>vowel_list</span><span>:</span><span>scottish_scraming</span> <span>=</span> <span>string</span><span>.</span><span>replace</span><span>(</span><span>letter</span><span>,</span> <span>"</span><span>e</span><span>"</span><span>)</span><span>scottish_scraming</span> <span>=</span> <span>scottish_scraming</span><span>.</span><span>upper</span><span>()</span><span>return</span> <span>scottish_scraming</span><span>def</span> <span>to_scottish_screaming</span><span>(</span><span>string</span><span>:</span> <span>str</span><span>):</span> <span>vowel_list</span> <span>=</span> <span>[</span><span>"</span><span>a</span><span>"</span><span>,</span> <span>"</span><span>e</span><span>"</span><span>,</span> <span>"</span><span>i</span><span>"</span><span>,</span> <span>"</span><span>o</span><span>"</span><span>,</span> <span>"</span><span>u</span><span>"</span><span>]</span> <span>string</span> <span>=</span> <span>string</span><span>.</span><span>lower</span><span>()</span> <span>for</span> <span>letter</span> <span>in</span> <span>string</span><span>:</span> <span>if</span> <span>letter</span> <span>in</span> <span>vowel_list</span><span>:</span> <span>scottish_scraming</span> <span>=</span> <span>string</span><span>.</span><span>replace</span><span>(</span><span>letter</span><span>,</span> <span>"</span><span>e</span><span>"</span><span>)</span> <span>scottish_scraming</span> <span>=</span> <span>scottish_scraming</span><span>.</span><span>upper</span><span>()</span> <span>return</span> <span>scottish_scraming</span>def to_scottish_screaming(string: str): vowel_list = ["a", "e", "i", "o", "u"] string = string.lower() for letter in string: if letter in vowel_list: scottish_scraming = string.replace(letter, "e") scottish_scraming = scottish_scraming.upper() return scottish_scraming
Enter fullscreen mode Exit fullscreen mode
- This fail as the scottish_scraming only replace one letter when updated.
Attempt 2 (success)
- algorithm
>Turn a sentence to Scottish accent>>replace all the vowel in the string with e and capitalise the whole sentence>>>find every vowel in the stringinitialise a vowel listcheck the letter in the string one by oneif the letter is exist in the vowel list, then it is vowel>>>replace the vowel with e and add to a new stringinitialise an empty stringif the letter is a vowel, add "e" to the new string at same positionotherwise, add the original letter to the new string at same position>>>capitalise the new stringcall upper() method on the string>>>print out the result>Turn a sentence to Scottish accent >>replace all the vowel in the string with e and capitalise the whole sentence >>>find every vowel in the string initialise a vowel list check the letter in the string one by one if the letter is exist in the vowel list, then it is vowel >>>replace the vowel with e and add to a new string initialise an empty string if the letter is a vowel, add "e" to the new string at same position otherwise, add the original letter to the new string at same position >>>capitalise the new string call upper() method on the string >>>print out the result>Turn a sentence to Scottish accent >>replace all the vowel in the string with e and capitalise the whole sentence >>>find every vowel in the string initialise a vowel list check the letter in the string one by one if the letter is exist in the vowel list, then it is vowel >>>replace the vowel with e and add to a new string initialise an empty string if the letter is a vowel, add "e" to the new string at same position otherwise, add the original letter to the new string at same position >>>capitalise the new string call upper() method on the string >>>print out the result
Enter fullscreen mode Exit fullscreen mode
- code
<span>def</span> <span>to_scottish_screaming</span><span>(</span><span>string</span><span>:</span> <span>str</span><span>):</span><span>string</span> <span>=</span> <span>string</span><span>.</span><span>lower</span><span>()</span><span>vowel_list</span> <span>=</span> <span>[</span><span>"</span><span>a</span><span>"</span><span>,</span> <span>"</span><span>e</span><span>"</span><span>,</span> <span>"</span><span>i</span><span>"</span><span>,</span> <span>"</span><span>o</span><span>"</span><span>,</span> <span>"</span><span>u</span><span>"</span><span>]</span><span>scottish_scraming</span> <span>=</span> <span>str</span><span>()</span><span>for</span> <span>letter</span> <span>in</span> <span>string</span><span>:</span><span>scottish_scraming</span> <span>+=</span> <span>"</span><span>e</span><span>"</span> <span>if</span> <span>letter</span> <span>in</span> <span>vowel_list</span> <span>else</span> <span>letter</span><span>scottish_scraming</span> <span>=</span> <span>scottish_scraming</span><span>.</span><span>upper</span><span>()</span><span>return</span> <span>scottish_scraming</span><span>print</span><span>(</span><span>to_scottish_screaming</span><span>(</span><span>"</span><span>A, wonderful, day, don</span><span>'</span><span>t, you, think?</span><span>"</span><span>))</span><span>def</span> <span>to_scottish_screaming</span><span>(</span><span>string</span><span>:</span> <span>str</span><span>):</span> <span>string</span> <span>=</span> <span>string</span><span>.</span><span>lower</span><span>()</span> <span>vowel_list</span> <span>=</span> <span>[</span><span>"</span><span>a</span><span>"</span><span>,</span> <span>"</span><span>e</span><span>"</span><span>,</span> <span>"</span><span>i</span><span>"</span><span>,</span> <span>"</span><span>o</span><span>"</span><span>,</span> <span>"</span><span>u</span><span>"</span><span>]</span> <span>scottish_scraming</span> <span>=</span> <span>str</span><span>()</span> <span>for</span> <span>letter</span> <span>in</span> <span>string</span><span>:</span> <span>scottish_scraming</span> <span>+=</span> <span>"</span><span>e</span><span>"</span> <span>if</span> <span>letter</span> <span>in</span> <span>vowel_list</span> <span>else</span> <span>letter</span> <span>scottish_scraming</span> <span>=</span> <span>scottish_scraming</span><span>.</span><span>upper</span><span>()</span> <span>return</span> <span>scottish_scraming</span> <span>print</span><span>(</span><span>to_scottish_screaming</span><span>(</span><span>"</span><span>A, wonderful, day, don</span><span>'</span><span>t, you, think?</span><span>"</span><span>))</span>def to_scottish_screaming(string: str): string = string.lower() vowel_list = ["a", "e", "i", "o", "u"] scottish_scraming = str() for letter in string: scottish_scraming += "e" if letter in vowel_list else letter scottish_scraming = scottish_scraming.upper() return scottish_scraming print(to_scottish_screaming("A, wonderful, day, don't, you, think?"))
Enter fullscreen mode Exit fullscreen mode
Other solution
use re.sub() method
- the first argument is pattern or string
- the
[]
is used to indicate a set of characters,- so AIOU can be list individually,
- AIOU will become “A”, “I”, “O”, “U”
- i.e. replace “A”, “I”, “O”, “U” with ‘E’ in the upper-cased of the string
<span>import</span> <span>re</span><span>def</span> <span>to_scottish_screaming</span><span>(</span><span>string</span><span>:</span> <span>str</span><span>):</span><span>scottish_scraming</span> <span>=</span> <span>re</span><span>.</span><span>sub</span><span>(</span><span>"</span><span>[AIOU]</span><span>"</span><span>,</span> <span>"</span><span>E</span><span>"</span><span>,</span> <span>string</span><span>.</span><span>upper</span><span>())</span><span>return</span> <span>scottish_scraming</span><span>import</span> <span>re</span> <span>def</span> <span>to_scottish_screaming</span><span>(</span><span>string</span><span>:</span> <span>str</span><span>):</span> <span>scottish_scraming</span> <span>=</span> <span>re</span><span>.</span><span>sub</span><span>(</span><span>"</span><span>[AIOU]</span><span>"</span><span>,</span> <span>"</span><span>E</span><span>"</span><span>,</span> <span>string</span><span>.</span><span>upper</span><span>())</span> <span>return</span> <span>scottish_scraming</span>import re def to_scottish_screaming(string: str): scottish_scraming = re.sub("[AIOU]", "E", string.upper()) return scottish_scraming
Enter fullscreen mode Exit fullscreen mode
My reflection
- just keep praticing writing algorithm first to solve proble, a bit more familiar to work in this way.
- re.sub() method is much simple in replace characters in a string, I should remember next time.
Credit
- challenge find on edabit
Python daily exercise (23 Part Series)
1 Python Daily exercise 1: Generate a list of even number
2 Python daily exercise 2: string
… 19 more parts…
3 Python daily exercise 3: reverse a string
4 Python daily exercise 4: separate a string
5 Python Daily Exercise 5: Generate new list by removing unwanted object
6 Python Exercise 6: Remove Special character In String
7 Python exercise 7: Determine a string is a mixture of something
8 Python Exercise 8: generate a dictionary from different source
9 Python Exercise 9: find the key with the minimum value in a dictionary
10 Python Exercise 10: Generate a list based on odd or even index
11 Python Exercise 11: Count the number and generate new dictionary
12 Python exercise 12: find the hidden ball
13 Python Exercise 13: Scottish Screaming
14 Python Exercise 14: sum of fractions
15 Python Exercise 15 : Perform calculation on a string
16 Python Exercise 16: find the highest index alphabet
17 Python exercise 17: convert number to english word
18 Python exercise 18:Sales Season
19 Python Exercise 19:advanced list sort
20 Python Exercise 20:Find the pattern
21 Python Exercise 21: list comprehensions
22 Python exercise 22: ransomnote
23 Python Exercise 23: Two sum
暂无评论内容