Daily Coding Challenge: Capitalized Fibonacci
Challenge
Capitalized Fibonacci
Given a string, return a new string where each letter is capitalized if its index is a Fibonacci number, and lowercased otherwise.
The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones. The first 10 numbers in the sequence are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34.
- The first character is at index 0.
- If the index of non-letter characters is a Fibonacci number, leave it unchanged.
My solution
def fib(n): mylist = [] i = 0 while True: if i == 0: mylist.append(0) i += 1 elif i == 1: mylist.append(1) i += 1 elif i >= 2: num = mylist[i-1] + mylist[i-2] if num <= n: mylist.append(num) i += 1 else: return mylist def capitalize_fibonacci(s): s = s.lower() fibonacci_list = fib(len(s)) string = [] for index, char in enumerate(s): if index in fibonacci_list: string.append(char.upper()) else: string.append(char) return "".join(string) print(capitalize_fibonacci("hello world")) print(capitalize_fibonacci("The quick brown fox jumped over the lazy dog."))
HELLo woRld THE qUicK broWn fox jUmped over thE lazy dog.