mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-30 04:36:08 -04:00
add some int-cake problems
This commit is contained in:
parent
e04656170b
commit
fa83f7e42a
50
interview_cake/merge_sorted_list.py
Normal file
50
interview_cake/merge_sorted_list.py
Normal file
@ -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]"
|
23
interview_cake/palindrome.py
Normal file
23
interview_cake/palindrome.py
Normal file
@ -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"
|
13
interview_cake/reverse_in_place.py
Normal file
13
interview_cake/reverse_in_place.py
Normal file
@ -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))
|
Loading…
x
Reference in New Issue
Block a user