├── .gitignore ├── README.md ├── easy ├── 01_first_reverse.py ├── 02_first_factorial.py ├── 03_longest_word.py ├── 04_letter_changes.py ├── 05_simple_adding.py ├── 06_letter_capitalize.py ├── 07_simple_symbols.py ├── 08_check_nums.py ├── 09_time_convert.py ├── 10_alphabet_soup.py ├── 11_AB_check.py ├── 12_vowel_count.py ├── 13_word_count.py ├── 14_ex_oh.py ├── 15_palindrome.py ├── 16_arith_geo.py ├── 17_array_addition_I.py ├── 18_letter_count.py └── 19_second_great_letter.py └── medium ├── 01_prime_time.py ├── 02_run_length.py ├── 03_prime_mover.py ├── 04_palindrome_two.py ├── 05_division.py ├── 06_string_scramble.py ├── 07_arithgeoII.py ├── 08_array_addition.py ├── 10_letter_count.py ├── 11_caesar_cipher.py ├── 12_simple_mode.py ├── 13_consecutive.py ├── 15_counting_minutes.py ├── 16_permutation_step.py ├── 17_prime_checker.py ├── 18_dash_insert.py ├── 20_number_search.py ├── 21_triple_double.py ├── 30_say_look_sequence.py ├── 31_distinct_list.py └── 32_number_encoding.py /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # coderbyte 2 | My solutions to Coderbyte programming challenges (http://coderbyte.com/), written in Python. 3 | -------------------------------------------------------------------------------- /easy/01_first_reverse.py: -------------------------------------------------------------------------------- 1 | # Have the function FirstReverse(str) take the str parameter being passed and return the string in reversed order. 2 | # Use the Parameter Testing feature in the box below to test your code with different arguments. 3 | 4 | def FirstReverse(a_string): 5 | reversed_string = '' 6 | for char in a_string[::-1]: 7 | reversed_string += char 8 | return reversed_string 9 | 10 | 11 | # keep this function call here 12 | # to see how to enter arguments in Python scroll down 13 | print FirstReverse(raw_input()) 14 | -------------------------------------------------------------------------------- /easy/02_first_factorial.py: -------------------------------------------------------------------------------- 1 | # Have the function FirstFactorial(num) take the num parameter being passed and return the factorial of it 2 | #(ie. if num = 4, return (4 * 3 * 2 * 1)). For the test cases, the range will be between 1 and 18. 3 | # Use the Parameter Testing feature in the box below to test your code with different arguments. 4 | 5 | 6 | def FirstFactorial(num): 7 | if num == 0: 8 | return 1 9 | else: 10 | return FirstFactorial(num - 1) * num 11 | 12 | 13 | # keep this function call here 14 | # to see how to enter arguments in Python scroll down 15 | print FirstFactorial(int(raw_input())) -------------------------------------------------------------------------------- /easy/03_longest_word.py: -------------------------------------------------------------------------------- 1 | # Have the function LongestWord(sen) take the sen parameter being passed and return the largest word in the string. 2 | # If there are two or more words that are the same length, return the first word from the string with that length. 3 | # Ignore punctuation and assume sen will not be empty. 4 | 5 | 6 | def LongestWord(sentence): 7 | longest_word = '' 8 | for char in "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~": 9 | no_punct_sentence = sentence.replace(char, " ") 10 | split_sentence = no_punct_sentence.split() 11 | 12 | for word in split_sentence: 13 | if len(word) > len(longest_word): 14 | longest_word = word 15 | return longest_word 16 | 17 | 18 | 19 | # keep this function call here 20 | # to see how to enter arguments in Python scroll down 21 | print LongestWord(raw_input()) 22 | -------------------------------------------------------------------------------- /easy/04_letter_changes.py: -------------------------------------------------------------------------------- 1 | def LetterChanges(a_string): 2 | letters = 'abcdefghijklmnopqrstuvwxyz' 3 | encoded_string = '' 4 | for letter in a_string.lower(): 5 | if letter not in letters: 6 | encoded_string += letter 7 | elif letter == 'z': 8 | encoded_string += 'a' 9 | else: 10 | index = letters.index(letter) 11 | encoded_string += letters[index + 1] 12 | capitalized_encoded = '' 13 | 14 | for char in encoded_string: 15 | if char in 'aeiou': 16 | capitalized_encoded += char.upper() 17 | else: 18 | capitalized_encoded += char 19 | return capitalized_encoded 20 | 21 | 22 | # keep this function call here 23 | # to see how to enter arguments in Python scroll down 24 | print LetterChanges(raw_input()) -------------------------------------------------------------------------------- /easy/05_simple_adding.py: -------------------------------------------------------------------------------- 1 | # Have the function SimpleAdding(num) add up all the numbers from 1 to num. For the test cases, 2 | # the parameter num will be any number from 1 to 1000. 3 | 4 | # Use the Parameter Testing feature in the box below to test your code with different arguments. 5 | 6 | def SimpleAdding(num): 7 | total = 0 8 | for each_num in xrange(num + 1): 9 | total += each_num 10 | return total 11 | 12 | # keep this function call here 13 | # to see how to enter arguments in Python scroll down 14 | print SimpleAdding(int(raw_input())) -------------------------------------------------------------------------------- /easy/06_letter_capitalize.py: -------------------------------------------------------------------------------- 1 | 2 | # Have the function LetterCapitalize(str) take the str parameter being passed and capitalize the first letter of each 3 | # word. Words will be separated by only one space. 4 | 5 | 6 | def LetterCapitalize(a_string): 7 | capitalized = a_string.title() 8 | return capitalized 9 | 10 | 11 | # keep this function call here 12 | # to see how to enter arguments in Python scroll down 13 | print LetterCapitalize(raw_input()) 14 | -------------------------------------------------------------------------------- /easy/07_simple_symbols.py: -------------------------------------------------------------------------------- 1 | # Have the function SimpleSymbols(str) take the str parameter being passed and determine if it is an acceptable sequence 2 | # by either returning the string true or false. The str parameter will be composed of + and = symbols with several 3 | # letters between them (ie. ++d+===+c++==a) and for the string to be true each letter must be surrounded by a + symbol. 4 | # So the string to the left would be false. The string will not be empty and will have at least one letter. 5 | 6 | 7 | def SimpleSymbols(str): 8 | letters = 'abcdefghijklmnopqrstuvwxyz' 9 | for num in xrange(len(str)): 10 | if str[0] in letters or str[-1] in letters: 11 | return False 12 | elif str[num] in letters: 13 | if str[num - 1] != "+" or str[num + 1] != "+": 14 | return False 15 | return True 16 | 17 | 18 | 19 | # keep this function call here 20 | # to see how to enter arguments in Python scroll down 21 | print SimpleSymbols(raw_input()) -------------------------------------------------------------------------------- /easy/08_check_nums.py: -------------------------------------------------------------------------------- 1 | # Using the Python language, have the function CheckNums(num1,num2) take both parameters being passed and return the 2 | # string true if num2 is greater than num1, otherwise return the string false. If the parameter values are equal to 3 | # each other then return the string -1. 4 | 5 | def CheckNums(num1,num2): 6 | if num1 == num2: 7 | return -1 8 | return num2 > num1 -------------------------------------------------------------------------------- /easy/09_time_convert.py: -------------------------------------------------------------------------------- 1 | # Using the Python language, have the function TimeConvert(num) take the num parameter being passed and return the 2 | # number of hours and minutes the parameter converts to (ie. if num = 63 then the output should be 1:3). 3 | # Separate the number of hours and minutes with a colon. 4 | 5 | def CheckNums(num1,num2): 6 | if num1 == num2: 7 | return -1 8 | return num2 > num1 -------------------------------------------------------------------------------- /easy/10_alphabet_soup.py: -------------------------------------------------------------------------------- 1 | # Have the function AlphabetSoup(str) take the str string parameter being passed and return the string with the letters 2 | # in alphabetical order (ie. hello becomes ehllo). Assume numbers and punctuation symbols will not be included in the string. 3 | 4 | def AlphabetSoup(a_string): 5 | list_of_chars = [] 6 | 7 | for char in a_string.lower(): 8 | list_of_chars.append(char) 9 | 10 | ordered_list = sorted(list_of_chars) 11 | alphabetical_string = ''.join(ordered_list) 12 | 13 | return alphabetical_string 14 | 15 | 16 | -------------------------------------------------------------------------------- /easy/11_AB_check.py: -------------------------------------------------------------------------------- 1 | # Using the Python language, have the function ABCheck(str) take the str parameter being passed and return the string 2 | # true if the characters a and b are separated by exactly 3 places anywhere in the string at least once (ie. 3 | # "lane borrowed" would result in true because there is exactly three characters between a and b). Otherwise 4 | # return the string false. 5 | 6 | def ABCheck(a_string): 7 | 8 | for index in xrange(len(a_string) - 4): 9 | if a_string[index] == 'a': 10 | if a_string[index + 4] == 'b': 11 | return True 12 | elif a_string[index] == 'b': 13 | if a_string[index + 4] == 'a': 14 | return True 15 | return False 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /easy/12_vowel_count.py: -------------------------------------------------------------------------------- 1 | # Using the Python language, have the function VowelCount(str) take the str string parameter being passed and return 2 | # the number of vowels the string contains (ie. "All cows eat grass" would return 5). Do not count y as a vowel for 3 | # this challenge. 4 | 5 | def VowelCount(a_string): 6 | vowels = 'aeiou' 7 | vowel_count = 0 8 | for char in a_string.lower(): 9 | if char in vowels: 10 | vowel_count += 1 11 | return vowel_count 12 | 13 | -------------------------------------------------------------------------------- /easy/13_word_count.py: -------------------------------------------------------------------------------- 1 | # Using the Python language, have the function WordCount(str) take the str string parameter being passed and return the 2 | # number of words the string contains (ie. "Never eat shredded wheat" would return 4). Words will be separated by 3 | # single spaces. 4 | 5 | 6 | def WordCount(a_string): 7 | words = a_string.split() 8 | return len(words) 9 | -------------------------------------------------------------------------------- /easy/14_ex_oh.py: -------------------------------------------------------------------------------- 1 | # Using the Python language, have the function ExOh(str) take the str parameter being passed and return the string true 2 | # if there is an equal number of x's and o's, otherwise return the string false. Only these two letters will be entered 3 | # in the string, no punctuation or numbers. For example: if str is "xooxxxxooxo" then the output should return false 4 | # because there are 6 x's and 5 o's. 5 | 6 | def ExOh(a_string): 7 | x_count = 0 8 | o_count = 0 9 | for char in a_string: 10 | if char == "x": 11 | x_count += 1 12 | elif char == "o": 13 | o_count += 1 14 | if x_count == o_count: 15 | return "true" 16 | return "false" 17 | -------------------------------------------------------------------------------- /easy/15_palindrome.py: -------------------------------------------------------------------------------- 1 | # Have the function Palindrome(str) take the str parameter being passed and return the string true if the parameter is 2 | # a palindrome, (the string is the same forward as it is backward) otherwise return the string false. For example: 3 | # "racecar" is also "racecar" backwards. Punctuation and numbers will not be part of the string. 4 | 5 | 6 | def Palindrome(str): 7 | string_no_spaces = str.replace(" ", "") 8 | if string_no_spaces == string_no_spaces[::-1]: 9 | return "true" 10 | return "false" -------------------------------------------------------------------------------- /easy/16_arith_geo.py: -------------------------------------------------------------------------------- 1 | # Using the Python language, have the function ArithGeo(arr) take the array of numbers stored in arr and return the 2 | # string "Arithmetic" if the sequence follows an arithmetic pattern or return "Geometric" if it follows a geometric 3 | # pattern. If the sequence doesn't follow either pattern return -1. An arithmetic sequence is one where the difference 4 | # between each of the numbers is consistent, where as in a geometric sequence, each term after the first is multiplied 5 | # by some constant or common ratio. Arithmetic example: [2, 4, 6, 8] and Geometric example: [2, 6, 18, 54]. 6 | # Negative numbers may be entered as parameters, 0 will not be entered, and no array will contain all the same elements. 7 | 8 | def ArithGeo(arr): 9 | if is_arithmetic(arr): 10 | return "Arithmetic" 11 | elif is_geometric(arr): 12 | return "Geometric" 13 | else: 14 | return -1 15 | 16 | 17 | def is_arithmetic(arr): 18 | first_diff = arr[1] - arr[0] 19 | for num in xrange(len(arr)- 1): 20 | if arr[num + 1] - arr[num] != first_diff: 21 | return False 22 | return True 23 | 24 | 25 | def is_geometric(arr): 26 | first_multiplier = arr[1] // arr[0] 27 | for num in xrange(len(arr)- 1): 28 | if arr[num + 1] // arr[num] != first_multiplier: 29 | return False 30 | return True 31 | 32 | -------------------------------------------------------------------------------- /easy/17_array_addition_I.py: -------------------------------------------------------------------------------- 1 | # Using the Python language, have the function ArrayAdditionI(arr) take the array of numbers stored in arr and return 2 | # the string true if any combination of numbers in the array can be added up to equal the largest number in the array, 3 | # otherwise return the string false. For example: if arr contains [4, 6, 23, 10, 1, 3] the output should return true 4 | # because 4 + 6 + 10 + 3 = 23. The array will not be empty, will not contain all the same elements, and may contain 5 | # negative numbers. 6 | 7 | def ArrayAdditionI(arr): 8 | max_num = max(arr) 9 | possible_totals = [] 10 | arr.remove(max_num) 11 | 12 | for num in arr: 13 | possible_totals.append(num) 14 | num_total = num 15 | arr.remove(num) 16 | 17 | for other_num in arr: 18 | possible_totals.append(num + other_num) 19 | num_total += other_num 20 | possible_totals.append(num_total) 21 | 22 | if max_num in possible_totals: 23 | return "true" 24 | return "false" 25 | 26 | -------------------------------------------------------------------------------- /easy/18_letter_count.py: -------------------------------------------------------------------------------- 1 | # Using the Python language, have the function LetterCountI(str) take the str parameter being passed and return the 2 | # first word with the greatest number of repeated letters. For example: "Today, is the greatest day ever!" should return 3 | # greatest because it has 2 e's (and 2 t's) and it comes before ever which also has 2 e's. If there are no words with 4 | # repeating letters return -1. Words will be separated by spaces. 5 | 6 | 7 | def repeated_letter_hist(word): 8 | histogram = {} 9 | for letter in word: 10 | if letter not in "abcdefghijklmnopqrstuvwxyz": 11 | continue 12 | else: 13 | histogram[letter] = histogram.get(letter, 0) + 1 14 | maximum = max(histogram.values()) 15 | return maximum 16 | 17 | 18 | def LetterCountI(a_str): 19 | split_string = a_str.split() 20 | max_count_repeats = 1 21 | word_with_most = '' 22 | 23 | for word in split_string: 24 | if repeated_letter_hist(word) > max_count_repeats: 25 | max_count_repeats = repeated_letter_hist(word) 26 | word_with_most = word 27 | 28 | if max_count_repeats > 1: 29 | return word_with_most 30 | return -1 -------------------------------------------------------------------------------- /easy/19_second_great_letter.py: -------------------------------------------------------------------------------- 1 | # Using the Python language, have the function SecondGreatLow(arr) take the array of numbers stored in arr and return 2 | # the second lowest and second greatest numbers, respectively, separated by a space. For example: if arr contains 3 | # [7, 7, 12, 98, 106] the output should be 12 98. The array will not be empty and will contain at least 2 numbers. 4 | # It can get tricky if there's just two numbers! 5 | 6 | def SecondGreatLow(arr): 7 | multiples_removed_list = list(set(arr)) 8 | sorted_list = sorted(multiples_removed_list) 9 | 10 | second_greatest = sorted_list[-2] 11 | second_lowest = sorted_list[1] 12 | 13 | return "%d %d" % (second_lowest, second_greatest) -------------------------------------------------------------------------------- /medium/01_prime_time.py: -------------------------------------------------------------------------------- 1 | # Have the function PrimeTime(num) take the num parameter being passed and return the string true if the parameter is 2 | # a prime number, otherwise return the string false. The range will be between 1 and 2^16. 3 | 4 | import math 5 | 6 | def PrimeTime(num): 7 | if num <= 1: 8 | return False 9 | elif num == 2 or num == 3: 10 | return True 11 | else: 12 | if num % 2 == 0: 13 | return False 14 | else: 15 | for potential_factor in range(3, int(math.sqrt(num) + 1), 2): 16 | if num % potential_factor == 0: 17 | return False 18 | return True 19 | 20 | 21 | # keep this function call here 22 | # to see how to enter arguments in Python scroll down 23 | print PrimeTime(int(raw_input())) 24 | -------------------------------------------------------------------------------- /medium/02_run_length.py: -------------------------------------------------------------------------------- 1 | # Have the function RunLength(str) take the str parameter being passed and return a compressed version of the string 2 | # using the Run-length encoding algorithm. This algorithm works by taking the occurrence of each repeating character 3 | # and outputting that number along with a single character of the repeating sequence. For example: "wwwggopp" would 4 | # return 3w2g1o2p. The string will not contain any numbers, punctuation, or symbols. 5 | 6 | 7 | def RunLength(a_str): 8 | compressed_string = '' 9 | start_indices = get_new_letter_start_indices(a_str) 10 | string_end_index = len(a_str) 11 | start_indices.append(string_end_index) 12 | 13 | for index in range(len(start_indices) - 1): 14 | num_occurrences = start_indices[index + 1] - start_indices[index] 15 | letter = a_str[start_indices[index]] 16 | compressed_string += (str(num_occurrences) + letter) 17 | 18 | return compressed_string 19 | 20 | 21 | def get_new_letter_start_indices(a_str): 22 | letter_start_indices = [0] 23 | for index in xrange(1, len(a_str)): 24 | if a_str[index] != a_str[index - 1]: 25 | letter_start_indices.append(index) 26 | return letter_start_indices 27 | 28 | 29 | 30 | # keep this function call here 31 | # to see how to enter arguments in Python scroll down 32 | print RunLength(raw_input()) 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /medium/03_prime_mover.py: -------------------------------------------------------------------------------- 1 | # Have the function PrimeMover(num) return the numth prime number. The range will be from 1 to 10^4. 2 | # For example: if num is 16 the output should be 53 as 53 is the 16th prime number. 3 | 4 | import math 5 | 6 | 7 | def PrimeMover(num): 8 | primes = get_primes() 9 | return primes[num - 1] 10 | 11 | 12 | def get_primes(): 13 | primes = [2] 14 | for num in xrange(3, 1000): 15 | if is_prime(num): 16 | primes.append(num) 17 | return primes 18 | 19 | 20 | def is_prime(num): 21 | for potential_factor in xrange(2, int(math.sqrt(num) + 1)): 22 | if num % potential_factor == 0: 23 | return False 24 | return True 25 | -------------------------------------------------------------------------------- /medium/04_palindrome_two.py: -------------------------------------------------------------------------------- 1 | # Have the function PalindromeTwo(str) take the str parameter being passed and return the string true if the parameter 2 | # is a palindrome, (the string is the same forward as it is backward) otherwise return the string false. The parameter 3 | # entered may have punctuation and symbols but they should not affect whether the string is in fact a palindrome. 4 | # For example: "Anne, I vote more cars race Rome-to-Vienna" should return true. 5 | 6 | import string 7 | 8 | def PalindromeTwo(a_string): 9 | punctuation = string.punctuation 10 | no_spaces_string = a_string.replace(" ", "").lower() 11 | no_punct_string = '' 12 | 13 | for char in no_spaces_string: 14 | if char in punctuation: 15 | continue 16 | else: 17 | no_punct_string += char 18 | 19 | return no_punct_string == no_punct_string[::-1] 20 | 21 | 22 | 23 | print PalindromeTwo("paigegiap") -------------------------------------------------------------------------------- /medium/05_division.py: -------------------------------------------------------------------------------- 1 | # Have the function Division(num1,num2) take both parameters being passed and return the Greatest Common Factor. 2 | # That is, return the greatest number that evenly goes into both numbers with no remainder. For example: 12 and 16 both 3 | # are divisible by 1, 2, and 4 so the output should be 4. The range for both parameters will be from 1 to 10^3. 4 | 5 | def Division(num1, num2): 6 | for num in xrange(max(num1, num2), 0, -1): 7 | if num1 % num == 0 and num2 % num == 0: 8 | return num 9 | 10 | -------------------------------------------------------------------------------- /medium/06_string_scramble.py: -------------------------------------------------------------------------------- 1 | # Using the Python language, have the function StringScramble(str1,str2) take both parameters being passed and return 2 | # the string true if a portion of str1 characters can be rearranged to match str2, otherwise return the string false. 3 | # For example: if str1 is "rkqodlw" and str2 is "world" the output should return true. Punctuation and symbols will 4 | # not be entered with the parameters. 5 | 6 | def StringScramble(str1, str2): 7 | outer_string = str1 8 | inner_string = str2 9 | for char in inner_string: 10 | if char not in outer_string: 11 | return False 12 | updated_outer = outer_string.replace(char, "", 1) 13 | outer_string = updated_outer 14 | return True 15 | -------------------------------------------------------------------------------- /medium/07_arithgeoII.py: -------------------------------------------------------------------------------- 1 | # Have the function ArithGeoII(arr) take the array of numbers stored in arr and return the string "Arithmetic" if the 2 | # sequence follows an arithmetic pattern or return "Geometric" if it follows a geometric pattern. If the sequence 3 | # doesn't follow either pattern return -1. An arithmetic sequence is one where the difference between each of the 4 | # numbers is consistent, where as in a geometric sequence, each term after the first is multiplied by some constant 5 | # or common ratio. Arithmetic example: [2, 4, 6, 8] and Geometric example: [2, 6, 18, 54]. Negative numbers may be 6 | # entered as parameters, 0 will not be entered, and no array will contain all the same elements. 7 | 8 | # Use the Parameter Testing feature in the box below to test your code with different arguments. 9 | 10 | 11 | def ArithGeoII(arr): 12 | if is_arithmetic(arr): 13 | return "Arithmetic" 14 | elif is_geometric(arr): 15 | return "Geometric" 16 | else: 17 | return -1 18 | 19 | 20 | def is_arithmetic(arr): 21 | first_diff = arr[1] - arr[0] 22 | for num in xrange(len(arr)- 1): 23 | if arr[num + 1] - arr[num] != first_diff: 24 | return False 25 | return True 26 | 27 | 28 | def is_geometric(arr): 29 | first_multiplier = arr[1] // arr[0] 30 | for num in xrange(len(arr)- 1): 31 | if arr[num + 1] // arr[num] != first_multiplier: 32 | return False 33 | return True 34 | 35 | 36 | # keep this function call here 37 | # to see how to enter arguments in Python scroll down 38 | print ArithGeoII(raw_input()) 39 | -------------------------------------------------------------------------------- /medium/08_array_addition.py: -------------------------------------------------------------------------------- 1 | # Using the Python language, have the function ArrayAddition(arr) take the array of numbers stored in arr and return 2 | # the string true if any combination of numbers in the array can be added up to equal the largest number in the array, 3 | # otherwise return the string false. For example: if arr contains [4, 6, 23, 10, 1, 3] the output should return true 4 | # because 4 + 6 + 10 + 3 = 23. The array will not be empty, will not contain all the same elements, and may contain 5 | # negative numbers. 6 | 7 | 8 | def ArrayAddition(arr): 9 | max_num = max(arr) 10 | possible_totals = [] 11 | arr.remove(max_num) 12 | 13 | for num in arr: 14 | possible_totals.append(num) 15 | num_total = num 16 | arr.remove(num) 17 | 18 | for other_num in arr: 19 | possible_totals.append(num + other_num) 20 | num_total += other_num 21 | possible_totals.append(num_total) 22 | 23 | return max_num in possible_totals 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /medium/10_letter_count.py: -------------------------------------------------------------------------------- 1 | # Have the function LetterCount(str) take the str parameter being passed and return the first word with the greatest 2 | # number of repeated letters. For example: "Today, is the greatest day ever!" should return greatest because it has 3 | # 2 e's (and 2 t's) and it comes before ever which also has 2 e's. If there are no words with repeating letters 4 | # return -1. Words will be separated by spaces. 5 | 6 | def repeated_letter_hist(word): 7 | histogram = {} 8 | for letter in word: 9 | if letter not in "abcdefghijklmnopqrstuvwxyz": 10 | continue 11 | else: 12 | histogram[letter] = histogram.get(letter, 0) + 1 13 | maximum = max(histogram.values()) 14 | return maximum 15 | 16 | 17 | def LetterCount(a_str): 18 | split_string = a_str.split() 19 | max_count_repeats = 1 20 | word_with_most = '' 21 | 22 | for word in split_string: 23 | if repeated_letter_hist(word) > max_count_repeats: 24 | max_count_repeats = repeated_letter_hist(word) 25 | word_with_most = word 26 | 27 | if max_count_repeats > 1: 28 | return word_with_most 29 | return -1 30 | 31 | 32 | 33 | # keep this function call here 34 | # to see how to enter arguments in Python scroll down 35 | print LetterCount(raw_input()) -------------------------------------------------------------------------------- /medium/11_caesar_cipher.py: -------------------------------------------------------------------------------- 1 | # Using the Python language, have the function CaesarCipher(str,num) take the str parameter and perform a Caesar Cipher 2 | # shift on it using the num parameter as the shifting number. A Caesar Cipher works by shifting each letter in the 3 | # string N places down in the alphabet (in this case N will be num). Punctuation, spaces, and capitalization should 4 | # remain intact. For example if the string is "Caesar Cipher" and num is 2 the output should be "Ecguct Ekrjgt". 5 | 6 | def CaesarCipher(a_str, num): 7 | lower_letters = 'abcdefghijklmnopqrstuvwxyz' 8 | upper_letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 9 | encoded_string = '' 10 | 11 | for char in a_str: 12 | if char not in lower_letters and char not in upper_letters: 13 | encoded_string += char 14 | elif char.isupper(): 15 | index = upper_letters.index(char) 16 | new_index = (index + num) % 25 17 | if (index + num) > 25: 18 | new_index = new_index - 1 19 | encoded_string += upper_letters[new_index] 20 | else: 21 | index = lower_letters.index(char) 22 | new_index = (index + num) % 25 23 | if (index + num) > 25: 24 | new_index = new_index - 1 25 | encoded_string += lower_letters[new_index] 26 | return encoded_string 27 | 28 | 29 | -------------------------------------------------------------------------------- /medium/12_simple_mode.py: -------------------------------------------------------------------------------- 1 | # Using the Python language, have the function SimpleMode(arr) take the array of numbers stored in arr and return the 2 | # number that appears most frequently (the mode). For example: if arr contains [10, 4, 5, 2, 4] the output should be 4. 3 | # If there is more than one mode return the one that appeared in the array first (ie. [5, 10, 10, 6, 5] should return 5 4 | # because it appeared first). If there is no mode return -1. The array will not be empty. 5 | 6 | def make_histogram(an_array): 7 | histogram = {} 8 | for num in an_array: 9 | histogram[num] = histogram.get(num, 0) + 1 10 | return histogram 11 | 12 | def SimpleMode(an_array): 13 | histogram = make_histogram(an_array) 14 | 15 | maximum = max(histogram.values()) 16 | if maximum == 1: 17 | return -1 18 | maxes = [] 19 | 20 | for key in histogram.keys(): 21 | if histogram[key] == maximum: 22 | maxes.append(key) 23 | 24 | for num in an_array: 25 | if num in maxes: 26 | return num 27 | 28 | 29 | -------------------------------------------------------------------------------- /medium/13_consecutive.py: -------------------------------------------------------------------------------- 1 | # Using the Python language, have the function Consecutive(arr) take the array of integers stored in arr and return the 2 | # minimum number of integers needed to make the contents of arr consecutive from the lowest number to the highest 3 | # number. For example: If arr contains [4, 8, 6] then the output should be 2 because two numbers need to be added to the 4 | # array (5 and 7) to make it a consecutive array of numbers from 4 to 8. Negative numbers may be entered as parameters 5 | # and no array will have less than 2 elements. 6 | 7 | 8 | def Consecutive(an_array): 9 | an_array.sort() 10 | maximum = max(an_array) 11 | minimum = min(an_array) 12 | 13 | missing_nums = [] 14 | for num in xrange(minimum, maximum): 15 | if num not in an_array: 16 | missing_nums.append(num) 17 | 18 | return len(missing_nums) 19 | 20 | -------------------------------------------------------------------------------- /medium/15_counting_minutes.py: -------------------------------------------------------------------------------- 1 | # Using the Python language, have the function CountingMinutes(str) take the str parameter being passed which will be 2 | # two times (each properly formatted with a colon and am or pm) separated by a hyphen and return the total number of 3 | # minutes between the two times. The time will be in a 12 hour clock format. For example: if str is 9:00am-10:00am 4 | # then the output should be 60. If str is 1:00pm-11:00am the output should be 1320. 5 | 6 | 7 | def CountingMinutes(a_string): 8 | times = a_string.split("-") 9 | two_times = [] 10 | 11 | for time in times: 12 | split_time = time.split(":") 13 | hours = int(split_time[0]) 14 | minutes = int(split_time[1][:2]) 15 | 16 | if time[-2:] == "pm": 17 | hours += 12 18 | time_in_minutes = hours * 60 + minutes 19 | two_times.append(time_in_minutes) 20 | 21 | if times[0][-2:] == "pm" and times[1][-2:] == "am": 22 | return (1440 - two_times[0]) + two_times[1] 23 | elif two_times[0] < two_times[1]: 24 | return two_times[1] - two_times[0] 25 | else: 26 | return 1440 - (two_times[0] - two_times[1]) 27 | 28 | # OR: 29 | 30 | def CountingMinutes2(times): 31 | total = 24 * 60 32 | start, end = tuple(times.split('-')) 33 | 34 | def minutes_from_midnight(time): 35 | colon = time.index(':') 36 | meridian = time[colon + 3:] 37 | hour = int(time[:colon]) 38 | mins = int(time[colon + 1: colon + 3]) 39 | from_midnight = (60 * hour) + mins 40 | 41 | if meridian == 'pm': 42 | from_midnight += 12 * 60 43 | 44 | return from_midnight 45 | 46 | start = minutes_from_midnight(start) 47 | end = minutes_from_midnight(end) 48 | 49 | if start < end: 50 | return end - start 51 | return (total - start) + end 52 | 53 | -------------------------------------------------------------------------------- /medium/16_permutation_step.py: -------------------------------------------------------------------------------- 1 | # Using the Python language, have the function PermutationStep(num) take the num parameter being passed and return the 2 | # next number greater than num using the same digits. For example: if num is 123 return 132, if it's 12453 return 12534. 3 | # If a number has no greater permutations, return -1 (ie. 999). 4 | 5 | import itertools 6 | 7 | 8 | def PermutationStep(num): 9 | perms = [int(''.join(perm)) for perm in itertools.permutations(str(num))] 10 | 11 | list_perms = list(set(perms)) 12 | list_perms.sort() 13 | print list_perms 14 | 15 | num_index = list_perms.index(num) 16 | try: 17 | if list_perms[num_index + 1] > num: 18 | return list_perms[num_index + 1] 19 | return -1 20 | except IndexError: 21 | return -1 22 | 23 | -------------------------------------------------------------------------------- /medium/17_prime_checker.py: -------------------------------------------------------------------------------- 1 | # Have the function PrimeChecker(num) take num and return 1 if any arrangement of num comes out to be a prime number, 2 | # otherwise return 0. For example: if num is 910, the output should be 1 because 910 can be arranged into 109 or 019, 3 | # both of which are primes. 4 | 5 | import itertools 6 | import math 7 | 8 | 9 | def is_prime(num): 10 | if num == 1: 11 | return False 12 | elif num == 2: 13 | return True 14 | for potential_factor in xrange(2, int(math.sqrt(num) + 1)): 15 | if num % potential_factor == 0: 16 | return False 17 | return True 18 | 19 | def PrimeChecker(num): 20 | perms = [int(''.join(perm)) for perm in itertools.permutations(str(num))] 21 | 22 | list_perms = list(set(perms)) 23 | list_perms.sort() 24 | 25 | for potential_prime in list_perms: 26 | if is_prime(potential_prime): 27 | return 1 28 | return 0 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /medium/18_dash_insert.py: -------------------------------------------------------------------------------- 1 | # Using the Python language, have the function DashInsertII(str) insert dashes ('-') between each two odd numbers and 2 | # insert asterisks ('*') between each two even numbers in str. For example: if str is 4546793 the output should be 3 | # 454*67-9-3. Don't count zero as an odd or even number. 4 | 5 | def DashInsertII(num_str): 6 | encoded_string = '' 7 | for index in xrange(len(num_str) - 1): 8 | encoded_string += num_str[index] 9 | if is_odd(int(num_str[index])) and is_odd(int(num_str[index + 1])): 10 | encoded_string += '-' 11 | elif is_even(int(num_str[index])) and is_even(int(num_str[index + 1])): 12 | encoded_string += '*' 13 | encoded_string += num_str[-1] 14 | return encoded_string 15 | 16 | 17 | def is_even(num): 18 | if num == 0: 19 | return False 20 | return num % 2 == 0 21 | 22 | 23 | def is_odd(num): 24 | return num % 2 != 0 -------------------------------------------------------------------------------- /medium/20_number_search.py: -------------------------------------------------------------------------------- 1 | # Have the function NumberSearch(str) take the str parameter, search for all the numbers in the string, add them 2 | # together, then return that final number divided by the total amount of letters in the string. For example: if str is 3 | # "Hello6 9World 2, Nic8e D7ay!" the output should be 2. First if you add up all the numbers, 6 + 9 + 2 + 8 + 7 you get 4 | # 32. Then there are 17 letters in the string. 32 / 17 = 1.882, and the final answer should be rounded to the nearest 5 | # whole number, so the answer is 2. Only single digit numbers separated by spaces will be used throughout the whole 6 | # string (So this won't ever be the case: hello44444 world). Each string will also have at least one letter. 7 | 8 | 9 | def NumberSearch(a_str): 10 | num_total = 0 11 | num_letters = 0 12 | for char in a_str: 13 | if char in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz": 14 | num_letters += 1 15 | elif char in "0123456789": 16 | num_total += int(char) 17 | return round(float(num_total)/num_letters) 18 | -------------------------------------------------------------------------------- /medium/21_triple_double.py: -------------------------------------------------------------------------------- 1 | # Using the Python language, have the function TripleDouble(num1,num2) take both parameters being passed, and return 1 2 | # if there is a straight triple of a number at any place in num1 and also a straight double of the same number in num2. 3 | # For example: if num1 equals 451999277 and num2 equals 41177722899, then return 1 because in the first parameter you 4 | # have the straight triple 999 and you have a straight double, 99, of the same number in the second parameter. 5 | # If this isn't the case, return 0. 6 | 7 | 8 | def TripleDouble(num1, num2): 9 | num1_triples = find_triples(num1) 10 | num2_doubles = find_doubles(num2) 11 | 12 | for num in num1_triples: 13 | if num in num2_doubles: 14 | return 1 15 | return 0 16 | 17 | 18 | def find_triples(a_num): 19 | triple_nums = [] 20 | 21 | consecutive_count = 1 22 | current_num = str(a_num)[0] 23 | 24 | for index in xrange(1, len(str(a_num))): 25 | if str(a_num)[index] == current_num: 26 | consecutive_count += 1 27 | if consecutive_count == 3: 28 | triple_nums.append(current_num) 29 | else: 30 | consecutive_count = 1 31 | current_num = str(a_num)[index] 32 | return triple_nums 33 | 34 | 35 | def find_doubles(a_num): 36 | double_nums = [] 37 | 38 | consecutive_count = 1 39 | current_num = str(a_num)[0] 40 | 41 | for index in xrange(1, len(str(a_num))): 42 | if str(a_num)[index] == current_num: 43 | consecutive_count += 1 44 | if consecutive_count == 2: 45 | double_nums.append(current_num) 46 | else: 47 | consecutive_count = 1 48 | current_num = str(a_num)[index] 49 | return double_nums 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /medium/30_say_look_sequence.py: -------------------------------------------------------------------------------- 1 | # Using the Python language, have the function LookSaySequence(num) take the num parameter being passed and return the 2 | # next number in the sequence according to the following rule: to generate the next number in a sequence read off the 3 | # digits of the given number, counting the number of digits in groups of the same digit. For example, the sequence 4 | # beginning with 1 would be: 1, 11, 21, 1211, ... The 11 comes from there being "one 1" before it and the 21 comes 5 | # from there being "two 1's" before it. So your program should return the next number in the sequence given num. 6 | 7 | def LookSaySequence(num): 8 | digits_list = [] 9 | 10 | starting_char = str(num)[0] 11 | char_count = 1 12 | str_num = str(num) + "x" 13 | 14 | for char in str_num[1:]: 15 | if char == starting_char: 16 | char_count += 1 17 | else: 18 | digits_list.append(str(char_count)) 19 | digits_list.append(starting_char) 20 | starting_char = char 21 | char_count = 1 22 | 23 | joined_string = int("".join(digits_list)) 24 | return joined_string 25 | 26 | 27 | -------------------------------------------------------------------------------- /medium/31_distinct_list.py: -------------------------------------------------------------------------------- 1 | # Using the Python language, have the function DistinctList(arr) take the array of numbers stored in arr and determine 2 | # the total number of duplicate entries. For example if the input is [1, 2, 2, 2, 3] then your program should output 2 3 | # because there are two duplicates of one of the elements. 4 | 5 | def DistinctList(an_array): 6 | distinct_array = list(set(an_array)) 7 | num_duplicates = len(an_array) - len(distinct_array) 8 | return num_duplicates 9 | 10 | 11 | -------------------------------------------------------------------------------- /medium/32_number_encoding.py: -------------------------------------------------------------------------------- 1 | # Using the Python language, have the function NumberEncoding(str) take the str parameter and encode the message 2 | # according to the following rule: encode every letter into its corresponding numbered position in the alphabet. 3 | # Symbols and spaces will also be used in the input. For example: if str is "af5c a#!" then your program should 4 | # return 1653 1#!. 5 | 6 | def NumberEncoding(a_string): 7 | letter_num_encoder = create_letter_num_dict() 8 | encoded_string ='' 9 | for char in a_string.lower(): 10 | if char.isalpha(): 11 | encoded_string += str(letter_num_encoder[char]) 12 | else: 13 | encoded_string += char 14 | return encoded_string 15 | 16 | 17 | def create_letter_num_dict(): 18 | letters = "abcdefghijklmnopqrstuvwxyz" 19 | nums = range(1, 27) 20 | letter_num_dict = {key: value for key, value in zip(letters, nums)} 21 | return letter_num_dict 22 | --------------------------------------------------------------------------------