Skip to content

Commit fa83f7e

Browse files
author
Mia von Steinkirch
committed
add some int-cake problems
1 parent e046561 commit fa83f7e

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""
2+
In order to win the prize for most cookies sold, my friend Alice and
3+
# I are going to merge our Girl Scout Cookies orders and enter as one unit.
4+
# Each order is represented by an "order id" (an integer).
5+
We have our lists of orders sorted numerically already, in lists.
6+
Write a function to merge our lists of orders into one sorted list.
7+
"""
8+
9+
def merge_lists(my_list, alices_list):
10+
11+
result = []
12+
index_alice_list = 0
13+
index_my_list = 0
14+
15+
while index_alice_list < len(alices_list) and index_my_list < len(my_list):
16+
if alices_list[index_alice_list] < my_list[index_my_list]:
17+
result.append(alices_list[index_alice_list])
18+
index_alice_list += 1
19+
elif alices_list[index_alice_list] > my_list[index_my_list]:
20+
result.append(my_list[index_my_list])
21+
index_my_list += 1
22+
else:
23+
result.append(my_list[index_my_list])
24+
result.append(alices_list[index_alice_list])
25+
index_my_list += 1
26+
index_alice_list += 1
27+
28+
if index_alice_list < len(alices_list):
29+
result.extend(alices_list[index_alice_list:])
30+
31+
if index_my_list < len(my_list):
32+
result.extend(my_list[index_my_list:])
33+
34+
return result
35+
36+
37+
my_list = [3, 4, 6, 10, 11, 15]
38+
alices_list = [1, 5, 8, 12, 14, 19]
39+
40+
41+
print merge_lists(my_list, alices_list)
42+
print "Must be [1, 3, 4, 5, 6, 8, 10, 11, 12, 14, 15, 19]"
43+
44+
45+
# Or just using Timsort
46+
def merge_sorted_lists(arr1, arr2):
47+
return sorted(arr1 + arr2)
48+
49+
print merge_sorted_lists(my_list, alices_list)
50+
print "Must be [1, 3, 4, 5, 6, 8, 10, 11, 12, 14, 15, 19]"

interview_cake/palindrome.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""
2+
Write an efficient function that checks whether
3+
any permutation of an input string is a palindrome.
4+
"""
5+
6+
def has_palindrome_permutation(the_string):
7+
unpaired_characters = set()
8+
9+
for char in the_string:
10+
if char in unpaired_characters:
11+
unpaired_characters.remove(char)
12+
else:
13+
unpaired_characters.add(char)
14+
15+
return len(unpaired_characters) <= 1
16+
17+
str1 = "civic"
18+
print has_palindrome_permutation(str1)
19+
print "Should be True"
20+
21+
str2 = "ivilc"
22+
print has_palindrome_permutation(str2)
23+
print "Should be False"

interview_cake/reverse_in_place.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Write a function that takes a list of characters and reverses the letters in place.
2+
# O(n) time and O(1)O(1) space.
3+
4+
def reverse_in_place(char_list):
5+
return char_list[::-1]
6+
7+
8+
char_list = ['a', 'b', 'c', 'd', 'e', 'f']
9+
10+
11+
12+
print(char_list)
13+
print(reverse_in_place(char_list))

0 commit comments

Comments
 (0)