# Challenge Problems ## Fizzbuzz style problems ```python= # Return an array containing the numbers from 1 to N, where N is the parametered value. N will never be less than 1. # Replace certain values however if any of the following conditions are met: # If the value is a multiple of 3 & 5: use the value "FizzBuzz" instead # If the value is a multiple of 3: use the value "Fizz" instead # If the value is a multiple of 5: use the value "Buzz" instead def fizzbuzz(n): # empty list to return fb_list = [] # itereate up to n for i in range(1, n + 1): # use module to find if they are a multiple of 3 or 5 # if i % 3 == 0 and i % 5 == 0: if i % 15 == 0: fb_list.append('FizzBuzz') elif i % 3 == 0: fb_list.append('Fizz') elif i % 5 == 0: fb_list.append('Buzz') else: fb_list.append(i) return fb_list # If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. # Finish the solution so that it returns the sum of all the multiples of 3 or 5 below the number passed in. def solution(number): # running total total = 0 # for i in range(1, number): if i % 3 == 0 or i % 5 == 0: total += i return total def recursive_solve(number, total = 0, i = 0): # base case if i >= number: return total if i % 3 == 0 or i % 5 == 0: total += i recursive_solve(number, total, i + 1) # Given an array of integers, find the one that appears an odd number of times. # There will always be only one integer that appears an odd number of times. def find_it(seq): # hash table to store numbers hash = {} # count how many times each number appears for number in seq: if number not in hash: hash[number] = 1 else: hash[number] += 1 # find the one odd number for number in hash: if hash[number] % 2 != 0: return number ``` ## Array Problems ```python= # Sum Of Negatives And Count Of Positives # Write a function that takes in a list of numbers and gives you back a tuple or list with two items: the sum of all negative numbers in the list, and the count of all positive numbers. # For input [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -11, -12, -13, -14, -15], you should return [10, -65]. (There are 10 positive numbers, and the negative numbers add up to -65.) # Using a loop. def count_positives_sum_negatives(nums): count = 0 total = 0 for num in nums: if num > 0: count += 1 else: total += num return [count, total] # Using list comprehensions. def count_positives_sum_negatives(nums): return [len([num for num in nums if num > 0]), sum([num for num in nums if num < 0])] # Testing it! print(count_positives_sum_negatives([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -11, -12, -13, -14, -15])) ``` ```python= # Implement a function unique_in_order that will take in an argument either a string or an array and return a list of elements without any elements with the same # values next to each other, but preserving the original order. # unique_in_order('AAAABBBCCDAABBB') == ['A', 'B', 'C', 'D', 'A', 'B'] # unique_in_order('ABBCcAD') == ['A', 'B', 'C', 'c', 'A', 'D'] # unique_in_order([1,2,2,3,3]) == [1,2,3] # Whenever your value is not what the last one is, # update what the last one is, # and append the new value to our result. def unique_in_order(iterable): result = [] prev = None for element in iterable: if element != prev: result.append(element) prev = element return result # Or just check against the most recently thing added. def unique_in_order(iterable): result = [] for element in iterable: if len(result) == 0 or element != result[-1]: result.append(element) return result ``` ## String Problems ```python= # Sum Mixed Array: easiest # Given an array of integers as strings and numbers, return the sum of the array values as if all were numbers. # Return your answer as a number. def sum_mix(arr): # # keep a running total total = 0 # iterate over every item in the list for element in arr: # check the type if it is a string if type(element) == str: total += int(element) # add the item to our total else: total += element return total # solution using list comprehension # https://www.w3schools.com/python/python_lists_comprehension.asp # return sum(int(element) for element in arr) # Vowel Count: mediumest # Return the number (count) of vowels in the given string. # We will consider a, e, i, o, u as vowels for this Kata (but not y). # The input string will only consist of lower case letters and/or spaces. def get_count(input_str): num_vowels = 0 # loop over input_str vowels = 'aeiou' for letter in input_str: # check if the letter is in vowels if letter in vowels: num_vowels += 1 return num_vowels # Convert string to camel case: hardest # Complete the method/function so that it converts dash/underscore delimited words into camel casing. The first word within the output should be capitalized only if the original word was capitalized (known as Upper Camel Case, also often referred to as Pascal case). # Examples # "the-stealth-warrior" gets converted to "theStealthWarrior" # "The_Stealth_Warrior" gets converted to "TheStealthWarrior" def to_camel_case(text): # replace underscores with dashes so the strings can be split words = text.replace('_', '-') # split the string at dashes and make into a list words = words.split('-') # this will be the return string -- start it off with the first word so it doesn't get capitalized string = words[0] # loop over the rest of the words and and capitalize them (range 1 - list length) for i in range(1, len(words)): # add the capitalized words to the string string += words[i].capitalize() # return the string return string ```