mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-02 06:46:18 -04:00
reorganize dir
Signed-off-by: Mia Steinkirch <mia.steinkirch@gmail.com>
This commit is contained in:
parent
1b6f705e7c
commit
a8e71c50db
276 changed files with 23954 additions and 0 deletions
|
@ -0,0 +1,51 @@
|
|||
#!/bin/python
|
||||
|
||||
"""
|
||||
Build a calendar.
|
||||
|
||||
A meeting is stored as a tuple of integers (start_time, end_time).
|
||||
These integers represent the number of 30-minute blocks past 9:00am.
|
||||
|
||||
For example:
|
||||
|
||||
(2, 3)# Meeting from 10:00-10:30 am
|
||||
(6, 9)# Meeting from 12:00-1:30 pm
|
||||
|
||||
Write a function merge_ranges() that takes a list of multiple meeting time ranges and returns a list of condensed ranges.
|
||||
|
||||
For example, given:
|
||||
|
||||
[(0, 1), (3, 5), (4, 8), (10, 12), (9, 10)]
|
||||
|
||||
your function would return:
|
||||
|
||||
[(0, 1), (3, 8), (9, 12)]
|
||||
|
||||
Do not assume the meetings are in order. The meeting times are coming from multiple teams.
|
||||
|
||||
Write a solution that's efficient even when we can't put a nice upper bound on the numbers representing our time ranges.
|
||||
Here we've simplified our times down to the number of 30-minute slots past 9:00 am.
|
||||
But we want the function to work even for very large numbers, like Unix timestamps.
|
||||
In any case, the spirit of the challenge is to merge meetings where start_time and end_time don't have an upper bound.
|
||||
"""
|
||||
|
||||
def merge_ranges(meetings):
|
||||
|
||||
sorted_meetings = sorted(meetings)
|
||||
merged_meetings = [sorted_meetings[0]]
|
||||
|
||||
for current_meeting_start, current_meeting_ending in sorted_meetings[1:]:
|
||||
last_merged_meeting_start, last_merged_meeting_end = merged_meetings[-1]
|
||||
|
||||
if (current_meeting_start <= last_merged_meeting_end):
|
||||
merged_meetings[-1] = (last_merged_meeting_start, max(last_merged_meeting_end, current_meeting_ending))
|
||||
else:
|
||||
merged_meetings.append((current_meeting_start, current_meeting_ending))
|
||||
|
||||
return merged_meetings
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
meetings = [(0, 1), (3, 5), (4, 8), (10, 12), (9, 10)]
|
||||
print(merge_ranges(meetings))
|
||||
print("Should return {}".format([(0, 1), (3, 8), (9, 12)]))
|
28
other_resources/interview_cake/strings/online_poker.py
Normal file
28
other_resources/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)
|
25
other_resources/interview_cake/strings/palindrome.py
Normal file
25
other_resources/interview_cake/strings/palindrome.py
Normal file
|
@ -0,0 +1,25 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
"""
|
||||
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"
|
17
other_resources/interview_cake/strings/reverse_in_place.py
Normal file
17
other_resources/interview_cake/strings/reverse_in_place.py
Normal file
|
@ -0,0 +1,17 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
"""
|
||||
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…
Add table
Add a link
Reference in a new issue