diff --git a/interview_cake/merge_sorted_list.py b/interview_cake/merge_sorted_list.py new file mode 100644 index 0000000..44473e8 --- /dev/null +++ b/interview_cake/merge_sorted_list.py @@ -0,0 +1,50 @@ +""" +In order to win the prize for most cookies sold, my friend Alice and +# I are going to merge our Girl Scout Cookies orders and enter as one unit. +# Each order is represented by an "order id" (an integer). +We have our lists of orders sorted numerically already, in lists. +Write a function to merge our lists of orders into one sorted list. +""" + +def merge_lists(my_list, alices_list): + + result = [] + index_alice_list = 0 + index_my_list = 0 + + while index_alice_list < len(alices_list) and index_my_list < len(my_list): + if alices_list[index_alice_list] < my_list[index_my_list]: + result.append(alices_list[index_alice_list]) + index_alice_list += 1 + elif alices_list[index_alice_list] > my_list[index_my_list]: + result.append(my_list[index_my_list]) + index_my_list += 1 + else: + result.append(my_list[index_my_list]) + result.append(alices_list[index_alice_list]) + index_my_list += 1 + index_alice_list += 1 + + if index_alice_list < len(alices_list): + result.extend(alices_list[index_alice_list:]) + + if index_my_list < len(my_list): + result.extend(my_list[index_my_list:]) + + return result + + +my_list = [3, 4, 6, 10, 11, 15] +alices_list = [1, 5, 8, 12, 14, 19] + + +print merge_lists(my_list, alices_list) +print "Must be [1, 3, 4, 5, 6, 8, 10, 11, 12, 14, 15, 19]" + + +# Or just using Timsort +def merge_sorted_lists(arr1, arr2): + return sorted(arr1 + arr2) + +print merge_sorted_lists(my_list, alices_list) +print "Must be [1, 3, 4, 5, 6, 8, 10, 11, 12, 14, 15, 19]" \ No newline at end of file diff --git a/interview_cake/palindrome.py b/interview_cake/palindrome.py new file mode 100644 index 0000000..ef7bcb0 --- /dev/null +++ b/interview_cake/palindrome.py @@ -0,0 +1,23 @@ +""" +Write an efficient function that checks whether +any permutation of an input string is a palindrome. +""" + +def has_palindrome_permutation(the_string): + unpaired_characters = set() + + for char in the_string: + if char in unpaired_characters: + unpaired_characters.remove(char) + else: + unpaired_characters.add(char) + + return len(unpaired_characters) <= 1 + +str1 = "civic" +print has_palindrome_permutation(str1) +print "Should be True" + +str2 = "ivilc" +print has_palindrome_permutation(str2) +print "Should be False" \ No newline at end of file diff --git a/interview_cake/reverse_in_place.py b/interview_cake/reverse_in_place.py new file mode 100644 index 0000000..3f1d7f4 --- /dev/null +++ b/interview_cake/reverse_in_place.py @@ -0,0 +1,13 @@ +# Write a function that takes a list of characters and reverses the letters in place. +# O(n) time and O(1)O(1) space. + +def reverse_in_place(char_list): + return char_list[::-1] + + +char_list = ['a', 'b', 'c', 'd', 'e', 'f'] + + + +print(char_list) +print(reverse_in_place(char_list)) \ No newline at end of file