For this exercise, you may work alone or with one partner.
Solve at least one of the following problems.
Be sure to include docstrings describing what the function does.
Let us know if you need help. The teaching team will come to you.
Once you have completed,
please ask the teaching team to record your participation and you may leave.
Write a function (called count_sum) that takes a list of integers.
Count how many adjacent numbers sum to 10, and report this number.
For example, for a list [2,7,3,1,9,5,5,4,6,8], the function would return 4
(as there are 4 pairs: 7,3 1,9 5,5 4,6).
You may not use any built-in functions/methods besides len().
Examples:
print(count_sum([])) # 0 print(count_sum([1])) # 0 print(count_sum([5, 5])) # 1 print(count_sum([5, 5, 6, 4, 8])) # 2 print(count_sum([2, 7, 3, 1, 9, 5, 5, 4, 6, 8])) # 4
Sample solution def count_sum(lst): list1 = lst count = 0 for i in range(0, len(list1) - 1): if(list1[i] + list1[i + 1] == 10): count += 1 return count print(count_sum([])) print(count_sum([1])) print(count_sum([5, 5])) print(count_sum([5, 5, 6, 4, 8])) print(count_sum([2, 7, 3, 1, 9, 5, 5, 4, 6, 8]))
Write a function (named replace) that takes a string and a letter.
Replace every character of the string that matches the incoming letter
with a charcter *, without using any built-in functions/methods besides len()
(i.e. you may NOT use .index() or replace()).
Examples:
print(replace("bird", "c")) # bird print(replace("bird", "i")) # b*rd print(replace("badapple", "a")) # b*d*pple print(replace("bidipple", "i")) # b*d*pple
Sample solution def replace(s, ss): result = "" for i in s: if i == ss: result += "*" else: result += i return result # another possible solution def replace(s, ss): string1 = s letter = ss count = 0 length = len(string1) while count < length: if string1[count] == letter: string1 = string1[:count] + '*' + string1[count + 1:] count += len(letter) count += 1 length = len(string1) return string1 print(replace("bird", "c")) print(replace("bird", "i")) print(replace("badapple", "a")) print(replace("bidipple", "i"))
Write a function (called count_occurrence) that takes a string and a substring.
Return the number of times the substring appears in the string
without using any built-in functions/methods besides len()
(i.e. you may NOT use .index() or count()).
The substring will be at most three characters long.
You may assume no space or special symbol in the substring
Examples:
print(count_occurrence("mississippi", "i")) # 4 print(count_occurrence("mississippi", "is")) # 2 print(count_occurrence("mississippi", "sis")) # 1 print(count_occurrence("mississippi", "a")) # 0 print(count_occurrence("mississippi", "sit")) # 0
Sample solution def count_occurrence(s, ss): string1 = s substring = ss count = 0 for i in range(0, len(string1)): if (string1[i: i + len(substring)] == substring): count += 1 return count print(count_occurrence("mississippi", "i")) # 4 print(count_occurrence("mississippi", "is")) # 2 print(count_occurrence("mississippi", "sis")) # 1 print(count_occurrence("mississippi", "a")) # 0 print(count_occurrence("mississippi", "sit")) # 0