Each snippet of the following code crashes. Run the code, scroll to the bottom of the error message, and read it. Just above the error message, you will also find the line of code where the error occurred!
Try to fix the errors! Don't copy your code from the previous labs, but make minor changes to the code here.
Hints:
TypeError
mentioning NoneType
, you likely forgot a return
statement somewhere (and your function returns None
).RecursionError
(infinite recursion), you likely forgot the base case or are calling the function f
with the same argument (e.g. n
or lst
) rather than a smaller element (e.g. n-1
or lst[1:]
).print
statements to your function to see what the values of the variables are (don't forget to remove them at the end!). Or ask Ken how to use a debugger :)Printing all elements in a nested list
def print_list(lst):
if type(lst) == list:
for i in lst:
print_list(lst[i])
else:
print(lst)
print_list([["hello"], "darkness", ["my", "old", ["friend"]]])
Removing all occurrences of a letter in a string
def removeLetter(s, letter):
if s[0] == letter:
return removeLetter(s[1:], letter)
return s[0] + removeLetter(s[1:], letter)
removeLetter('abcaba', 'a')
Summing up all elements in a nested list
def sum_list(lst):
if type(lst) == list:
summ = 0
for i in lst:
summ += sum_list(i)
else:
return lst
print(sum_list([1, 2, [3, [4, 0], 5]]))
The following code should remove spaces from a string and print the result for the input "Addis Coder"
and "Don't worry be happy"
. But this code does not work.
def remove_spaces(string):
new_string = ""
for letter in string:
if letter != " ":
new_string += letter
print(new_string)
remove_spaces("Addis Coder")
remove_spaces("Don't worry be happy")
Question 5
Write a code that counts the occurrences of each numbers in a given interger list using the following methods.
a) Using any method
# Given list
numbers = [1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4]
#Write your code here
b) Using Dictionary Comprehension Method
Hint: lst.count(1)
counts how often 1 appears in lst
.
# Given list
numbers = [1, 2, 2, 2, 3,3 , 3, 4, 4, 4, 4, 4]
#Write your code here
There is no question 6.
Question 7
Write a code that extracts the first character of each word in a sentence using list comprehensions.
Hint: You can use string.split()
to turn a string into a list of words.
# Given sentence
sentence = "AddisCoder is helping Ethiopian HighSchoolers to achieve their dreams."
# Write your code for method here
Question 8
Write a code that takes list of strings and creates a new list with the length of each string using list comprehension.
#Given list of strings
string_list = ["Heather", "Biniyam", "Ken", "Alex", "Yeabsira", "Hana"]
#Write your code here
Question 9
Write a code that finds all the common elements between two given lists using a list comprehension.
#Given two lists
list1 = [5, 2, 3, 4, 6, 8, 7, 9]
list2 = [4, 5, 6, 7, 8, 3, 1, 10]
#Write your code here
Question 10
Write a code that finds all anagrams of a given word from a list of words using the following two methods.
"An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once."
Look here for more info: https://en.wikipedia.org/wiki/Anagram
a) Using any method
# Write your code for method 'a' here
def is_anagram(word1, word2):
pass
# Given word and list of words
given_word = "listen"
word_list = ["enlist", "silent", "hello", "inlets", "world", "tinsel"]
# Print the list of anagrams
b) Using List Comprehensions
# Write your code for method 'b' here
def is_anagram(word1, word2):
pass
# Given word and list of words
given_word = "listen"
word_list = ["enlist", "silent", "hello", "inlets", "world", "tinsel"]
# Print the list of anagrams
Question 11
Write a code that finds a Transpose Matrix of a given Matrix by using the following two methods.
Transpose matrix is a matrix that filiped over its diagonal. This means the that is, the row and column indices of the matrix are switched.
Read to learn more about Transpose Matrix: https://en.wikipedia.org/wiki/Transpose
a) Use any method
matrix = [[1, 2, 3, 4], [4, 5, 6, 8]]
#Write your code here
b) List Comprehensions method
matrix = [[1, 2, 3, 4], [4, 5, 6, 8]]
#Write your code here