mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 20:26:07 -04:00
add some exe
This commit is contained in:
parent
47e5ee3918
commit
bb6afce467
37
interview_cake/data_structures/angry_bird.py
Normal file
37
interview_cake/data_structures/angry_bird.py
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
#!/bin/python
|
||||||
|
|
||||||
|
"""
|
||||||
|
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.
|
||||||
|
|
||||||
|
Write a function that takes:
|
||||||
|
|
||||||
|
a list of unsorted_scores
|
||||||
|
the highest_possible_score in the game
|
||||||
|
and returns a sorted list of scores in less than O(n\lg{n})O(nlgn) time.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def sort_scores(unsorted_scores, highest_score):
|
||||||
|
|
||||||
|
score_counts = [0] * (highest_score+1)
|
||||||
|
|
||||||
|
for score in unsorted_scores:
|
||||||
|
score_counts[score] += 1
|
||||||
|
|
||||||
|
sorted_scores = []
|
||||||
|
|
||||||
|
for score in range(len(score_counts)-1, -1, -1):
|
||||||
|
count = score_counts[score]
|
||||||
|
|
||||||
|
for i in range(count):
|
||||||
|
sorted_scores.append(score)
|
||||||
|
|
||||||
|
return sorted_scores
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
|
||||||
|
unsorted_scores = [37, 89, 41, 65, 91, 53]
|
||||||
|
HIGHEST_POSSIBLE_SCORE = 100
|
||||||
|
|
||||||
|
print sort_scores(unsorted_scores, HIGHEST_POSSIBLE_SCORE)
|
42
interview_cake/data_structures/inflight_entrain.py
Normal file
42
interview_cake/data_structures/inflight_entrain.py
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
#!/bin/python
|
||||||
|
|
||||||
|
"""
|
||||||
|
Users on longer flights like to start a second movie right when their first one ends,
|
||||||
|
but they complain that the plane usually lands before they can see the ending.
|
||||||
|
So you're building a feature for choosing two movies whose total runtimes will equal the exact flight length.
|
||||||
|
|
||||||
|
Write a function that takes an integer flight_length (in minutes) and a
|
||||||
|
list of integers movie_lengths (in minutes) and returns a boolean indicating
|
||||||
|
whether there are two numbers in movie_lengths whose sum equals flight_length.
|
||||||
|
|
||||||
|
When building your function:
|
||||||
|
|
||||||
|
Assume your users will watch exactly two movies
|
||||||
|
Don't make your users watch the same movie twice
|
||||||
|
Optimize for runtime over memory
|
||||||
|
"""
|
||||||
|
|
||||||
|
def is_there_two_movies(flight_length, movie_lengths):
|
||||||
|
movie_lengths_seen = set()
|
||||||
|
|
||||||
|
for first_movie_length in movie_lengths:
|
||||||
|
matching_second_movie_length = flight_length - first_movie_length
|
||||||
|
if matching_second_movie_length in movie_lengths_seen:
|
||||||
|
return True
|
||||||
|
movie_lengths_seen.add(first_movie_length)
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
|
||||||
|
flight_length = 10
|
||||||
|
|
||||||
|
movie_lengths = [2, 4, 7]
|
||||||
|
print(is_there_two_movies(flight_length, movie_lengths))
|
||||||
|
print("Should be True")
|
||||||
|
|
||||||
|
movie_lengths = [5, 6, 7, 8]
|
||||||
|
print(is_there_two_movies(flight_length, movie_lengths))
|
||||||
|
print("Should be False")
|
28
interview_cake/strings/online_poker.py
Normal file
28
interview_cake/strings/online_poker.py
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
#!/bin/python
|
||||||
|
|
||||||
|
"""
|
||||||
|
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.
|
||||||
|
|
||||||
|
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).
|
||||||
|
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).
|
||||||
|
"""
|
||||||
|
|
||||||
|
def is_single_riffle(half1, half2, shuffled_deck,
|
||||||
|
shuffled_deck_index=0, half1_index=0, half2_index=0):
|
||||||
|
if shuffled_deck_index == len(shuffled_deck):
|
||||||
|
return True
|
||||||
|
|
||||||
|
if ((half1_index < len(half1)) and
|
||||||
|
half1[half1_index] == shuffled_deck[shuffled_deck_index]):
|
||||||
|
half1_index += 1
|
||||||
|
|
||||||
|
elif ((half2_index < len(half2)) and
|
||||||
|
half2[half2_index] == shuffled_deck[shuffled_deck_index]):
|
||||||
|
half2_index += 1
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
shuffled_deck_index += 1
|
||||||
|
return is_single_riffle(
|
||||||
|
half1, half2, shuffled_deck, shuffled_deck_index,
|
||||||
|
half1_index, half2_index)
|
Loading…
x
Reference in New Issue
Block a user