Coding Exercises: Loops and Algorithms
Implement the following functions, including unit tests. A few example tests are shown. Add at least 2 more tests of your own for each function.
Sum of Squares
1) -> 1
sum_of_squares(2) -> 1+4 = 5
sum_of_squares(3) -> 1+4+9 = 14 sum_of_squares(
Count Occurences
"Mississippi", "iss") -> 2
count_occurrences("banananana", "na") -> 4 count_occurrences(
Reverse String
"bad") -> "dab"
reverse("Hello, world!") -> "!dlrow ,olleH"
reverse("tacocat") -> "tacocat" reverse(
Factorial
0) -> 1
factorial(1) -> 1
factorial(2) -> 2*1 = 2
factorial(3) -> 3*2*1 = 6
factorial(4) -> 4*3*2*1 = 24
factorial(5) -> 5*4*3*2*1 = 120 factorial(
Interlace Two Strings
"abc", "123") -> "a1b2c3"
interlace("bed", "ras") -> "breads" interlace(
Find 2nd βaβ
"banana") -> 3
find_2nd("happy birthday") -> 12 find_2nd(
Add βnaβ suffix
0) -> "ba"
add_na(1) -> "bana"
add_na(2) -> "banana"
add_na(3) -> "bananana" add_na(
Calculate Sum of Powers of 2
0) -> 0
sum_powers(1) -> 0+1 = 1
sum_powers(2) -> 0+1+2 = 3
sum_powers(3) -> 0+1+2+4 = 7
sum_powers(4) -> 0+1+2+4+8 = 15 sum_powers(