Objectives Achieved
This week’s codes have increased in complexity and it now takes me a bit more time to work through each exercise. At times I would move on to the next parts come back to the more difficult problems at a later time. Besides certain codes are neater when we use a for loop instead of a while loop.
The following are the list of concepts I have gone through:
lists, items, elements, append, insert, pop, remove, sorting lists, sorted, max, min
sum, for loop, definite & indefinite iteration, range, list function, sep
keyword , end, newline character, format specifier, count, replace, isupper()
Some Statistics: 36% Completed
Parts Completed This Week: 4.3, 4.4, 4.5, 4.6
Here are some codes I did this week
Anagrams: My Solution
This function takes two strings as arguments. The function returns True
if the strings are anagrams of each other. Two words are anagrams if they contain exactly the same characters.
Here I made use of the sorted command to execute the code.
Anagrams: Model Solution
The model solution simply returns sorted(string1) == sorted(string2). This means if the condition is satisfied, it returns True. Otherwise it returns False.
Palindromes: My Solution
This function takes a string argument and returns True
if the string is a palindrome. Palindromes are words which are spelled exactly the same backwards and forwards.
My idea is to add each character of the given string from the last to the first to the variable y. This forms a string with all the letters spelled backwards.
Palindromes: Model Solution
The model solution uses a for loop and considers the string in 2 halves. It compares the first half of the string to the second half.
The sum of lists: My Solution
This function takes two lists of integers as arguments. It then returns a new list which contains the sums of the items at each index in the two original lists. We have assumed that both lists have the same number of items.
I used two helper variables i and j to run through each item within the lists. The sum k is appended into the new_list at each iteration of the while loop.
The sum of lists: Model Solution
The model solution simply uses a for loop and append the sum of the items in each list.
No vowels allowed: My Solution
This function which takes a string argument and returns a new string, which us the same as the original but with all vowels removed.
I sort of used a brute-force method as the code simply replaces the string ‘a’, ‘e’, ‘i’, ‘o’, ‘u’ with empty space.
This works only when the demand is simple.
No vowels allowed: Model Solution
The way the model solution removes the vowels is done by only adding letters that are not in the string called vowels.
This is a much more general approach as we can modify vowels = “aeiou” to return a new string with any arbitrary characters we want removed.
So that’s it for this week!
About Me
Hello I am Barry and I study mathematics at the University of Warwick. I also write on Medium.
If you want to reach out, please do so below 🦁