Objectives Achieved
This week I learnt to use a code visualizer to help with my learning and started solving coding within the Visual Studio Code editor application.
The following are the list of concepts I have gone through:
loops with conditions, building strings, concatenation, index, substring
slicing, in-operator, find( ) method, continue command, nested loops
helper variables, defining and calling functions, arguments, parameters
global variables, Visual Studio Code editor, visual debugger, breakpoint and the return statement
Some Statistics: 28% Completed
Parts Completed This Week: 3.1, 3.2, 3.3, 3.4, 4.1, 4.2
Here are some codes I did this week
A framed word: My Solution
This program asks the user for a string and then prints out a frame of *
characters with the word in the center. The width of the frame is 30 characters.
It works by using 3 print statements, 2 of which form the width of the frame with 30 *
characters.
A framed word: Model Solution
The model solution in my opinion is more neat as it uses an if-statement
to account for the odd word length.
Does it contain vowels: My Solution
This program asks the user to input a string and then prints out different messages if the string contains any of the vowels a, e or o.
We assume the input will be in lowercase entirely.
My solution uses 3 if-else statements
to consider the presence of a, e and o.
Does it contain vowels: Model Solution
The model solution is much more neat and clever because it defines vowels as the string “aeo” and makes use of the index command to determine whether any of a, e, o is found in the input.
The [ ] is the index command which allows user to locate and return the first occurrence of the specified value. In our case, we form a while loop and run through each letter in the input. The index += 1
lets us cycle through all letters.
Find the first substring: My Solution
This program asks the user to type in a string and a single character. It then prints the first three character slice which begins with the character specified by the user.
We assume the input string is at least three characters long. The program always prints out three characters, or else nothing.
Here I used the find
command in word.find(character)
. The code then works by using an if statement to ensure there are 3 characters to print out.
Find the first substring: Model Solution
The model solution is quite similar to the one I submitted. The only difference is the condition in the if-statement.
It only prints the characters if the index is not equal to 1 and the length of the input ‘word’ is greater than the index plus 3.
The second occurrence: My Solution
This program finds the second occurrence of a substring. If there is no second (or first) occurrence, the program prints out a message accordingly.
This code was quite challenging and it took me a few attempts. The tricky part is to account for when the substring does not occur twice in the string.
The model solution is much neater, as we shall see below.
The second occurrence: Model Solution
It’s much shorter than the solution I submitted.
The key point is that it uses the if index == -1 condition to account for when the substring does not occur twice.
In Python programming, index == -1 means the last element of the list!
Taking turns: My Solution
This program asks the user to type in a number. The program then prints out the positive integers between 1 and the number itself, alternating between the two ends of the range.
Here I sort of ‘cheated’ as I used math.ceil
and math.floor
functions which have not been introduced in the course.
Taking turns: Model Solution
The model solution is much more elementary as it uses variables left and right within the while loop to print out alternating numbers.
Chessboard: My Solution
This a function named chessboard
, which prints out a chessboard made out of ones and zeroes. The function takes an integer argument, which specifies the length of the side of the board.
As an example, we have a chessboard of size 6.
chessboard(6)
101010
010101
101010
010101
101010
010101
Again, I used math.floor
functions and multiple while
loops and if-elif
statements.
Chessboard: Model Solution
The model solution here defines the function to take two arguments, characters and size.
It uses the while loop to assemble the whole chessboard row by row. Eventually it prints out the whole board.
Same characters: My Solution
This function named same_chars
takes one string and two integers as arguments. The integers refer to indexes within the string.
The function returns True
if the two characters at the indexes specified are the same. Otherwise, and especially if either of the indexes falls outside the scope of the string, the function returns False
.
This code uses boolean values True
and False
.
Same characters: Model Solution
The model solution returns false if either a or b is out of range. It then returns str[a] == str[b]
.
This means if the character at index a is the same as the character at index b, it returns True
, otherwise it returns False
.
I didn’t know it would work like that!
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 🦁