Skip to content

Commit 1b336bf

Browse files
author
Mia von Steinkirch
committed
so easy
1 parent d99d1d2 commit 1b336bf

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/python
2+
3+
"""
4+
Write a function for doing an in-place shuffle of a list.
5+
6+
The shuffle must be "uniform," meaning each item in the original list must have the same probability of ending up in each spot in the final list.
7+
8+
Assume that you have a function get_random(floor, ceiling) for getting a random integer that is >= floor and <= ceiling.
9+
"""
10+
11+
import random
12+
13+
def get_random(floor, ceiling):
14+
return random.randrange(floor, ceiling + 1)
15+
16+
def shuffle(the_list):
17+
18+
if len(the_list) <= 1:
19+
return the_list
20+
21+
last_index_in_the_list = len(the_list) - 1
22+
23+
for i in range(len(the_list) - 1):
24+
random_choice_index = get_random(i,
25+
last_index_in_the_list)
26+
if random_choice_index != i:
27+
the_list[i], the_list[random_choice_index] = \
28+
the_list[random_choice_index], the_list[i]
29+
30+
31+
seed_list = [5, 2, 6, 2, 6]
32+
shuffle(seed_list)
33+
print seed_list
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/bin/python
2+
3+
"""
4+
I want to learn some big words so people think I'm smart.
5+
6+
I opened up a dictionary to a page in the middle and started flipping through, looking for words I didn't know. I put each word I didn't know at increasing indices in a huge list I created in memory. When I reached the end of the dictionary, I started from the beginning and did the same thing until I reached the page I started at.
7+
8+
Now I have a list of words that are mostly alphabetical, except they start somewhere in the middle of the alphabet, reach the end, and then start from the beginning of the alphabet. In other words, this is an alphabetically ordered list that has been "rotated." For example:
9+
10+
words = [
11+
'ptolemaic',
12+
'retrograde',
13+
'supplant',
14+
'undulate',
15+
'xenoepist',
16+
'asymptote', # <-- rotates here!
17+
'babka',
18+
'banoffee',
19+
'engender',
20+
'karpatka',
21+
'othellolagkage',
22+
]
23+
24+
Write a function for finding the index of the "rotation point," which is where I started working from the beginning of the dictionary. This list is huge (there are lots of words I don't know) so we want to be efficient here.
25+
"""
26+
27+
def find_index(words):
28+
29+
for i, word in enumerate(words):
30+
if word[0] > words[i+1][0]:
31+
return i+1, words[i+1]
32+
33+
return "Not found"
34+
35+
36+
37+
def find_index_bs(words):
38+
first_word = words[0]
39+
floor_index = 0
40+
ceiling_index = len(words) - 1
41+
42+
while floor_index < ceiling_index:
43+
guess_index = floor_index + ((ceiling_index - floor_index) / 2)
44+
45+
if words[guess_index] >= first_word:
46+
floor_index = guess_index
47+
else:
48+
ceiling_index = guess_index
49+
50+
if floor_index + 1 == ceiling_index:
51+
return ceiling_index
52+
53+
54+
words = [
55+
'ptolemaic',
56+
'retrograde',
57+
'supplant',
58+
'undulate',
59+
'xenoepist',
60+
'asymptote',
61+
'babka',
62+
'banoffee',
63+
'engender',
64+
'karpatka',
65+
'othellolagkage',
66+
]
67+
68+
print find_index(words)
69+
print
70+
print find_index_bs(words)

0 commit comments

Comments
 (0)