Skip to content

Commit bb6afce

Browse files
author
Mia von Steinkirch
committed
add some exe
1 parent 47e5ee3 commit bb6afce

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/bin/python
2+
3+
"""
4+
Each round, players receive a score between 0 and 100, which you use to rank them from highest to lowest. So far you're using an algorithm that sorts in O(n\lg{n})O(nlgn) time, but players are complaining that their rankings aren't updated fast enough. You need a faster sorting algorithm.
5+
6+
Write a function that takes:
7+
8+
a list of unsorted_scores
9+
the highest_possible_score in the game
10+
and returns a sorted list of scores in less than O(n\lg{n})O(nlgn) time.
11+
"""
12+
13+
def sort_scores(unsorted_scores, highest_score):
14+
15+
score_counts = [0] * (highest_score+1)
16+
17+
for score in unsorted_scores:
18+
score_counts[score] += 1
19+
20+
sorted_scores = []
21+
22+
for score in range(len(score_counts)-1, -1, -1):
23+
count = score_counts[score]
24+
25+
for i in range(count):
26+
sorted_scores.append(score)
27+
28+
return sorted_scores
29+
30+
31+
32+
if __name__ == '__main__':
33+
34+
unsorted_scores = [37, 89, 41, 65, 91, 53]
35+
HIGHEST_POSSIBLE_SCORE = 100
36+
37+
print sort_scores(unsorted_scores, HIGHEST_POSSIBLE_SCORE)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/bin/python
2+
3+
"""
4+
Users on longer flights like to start a second movie right when their first one ends,
5+
but they complain that the plane usually lands before they can see the ending.
6+
So you're building a feature for choosing two movies whose total runtimes will equal the exact flight length.
7+
8+
Write a function that takes an integer flight_length (in minutes) and a
9+
list of integers movie_lengths (in minutes) and returns a boolean indicating
10+
whether there are two numbers in movie_lengths whose sum equals flight_length.
11+
12+
When building your function:
13+
14+
Assume your users will watch exactly two movies
15+
Don't make your users watch the same movie twice
16+
Optimize for runtime over memory
17+
"""
18+
19+
def is_there_two_movies(flight_length, movie_lengths):
20+
movie_lengths_seen = set()
21+
22+
for first_movie_length in movie_lengths:
23+
matching_second_movie_length = flight_length - first_movie_length
24+
if matching_second_movie_length in movie_lengths_seen:
25+
return True
26+
movie_lengths_seen.add(first_movie_length)
27+
28+
return False
29+
30+
31+
32+
if __name__ == '__main__':
33+
34+
flight_length = 10
35+
36+
movie_lengths = [2, 4, 7]
37+
print(is_there_two_movies(flight_length, movie_lengths))
38+
print("Should be True")
39+
40+
movie_lengths = [5, 6, 7, 8]
41+
print(is_there_two_movies(flight_length, movie_lengths))
42+
print("Should be False")
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/bin/python
2+
3+
"""
4+
Write a function to tell us if a full deck of cards shuffled_deck is a single riffle of two other halves half1 and half2.
5+
6+
We'll represent a stack of cards as a list of integers in the range 1..521..52 (since there are 5252 distinct cards in a deck).
7+
Why do I care? A single riffle is not a completely random shuffle. If I'm right, I can make more informed bets and get rich and finally prove to my ex that I am not a "loser with an unhealthy cake obsession" (even though it's too late now because she let me go and she's never getting me back).
8+
"""
9+
10+
def is_single_riffle(half1, half2, shuffled_deck,
11+
shuffled_deck_index=0, half1_index=0, half2_index=0):
12+
if shuffled_deck_index == len(shuffled_deck):
13+
return True
14+
15+
if ((half1_index < len(half1)) and
16+
half1[half1_index] == shuffled_deck[shuffled_deck_index]):
17+
half1_index += 1
18+
19+
elif ((half2_index < len(half2)) and
20+
half2[half2_index] == shuffled_deck[shuffled_deck_index]):
21+
half2_index += 1
22+
else:
23+
return False
24+
25+
shuffled_deck_index += 1
26+
return is_single_riffle(
27+
half1, half2, shuffled_deck, shuffled_deck_index,
28+
half1_index, half2_index)

0 commit comments

Comments
 (0)