Created
December 5, 2020 20:17
-
-
Save pjhoberman/3b78ba85a2cddf07169ddf1544c31b96 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#binary to int: | |
# int("001", 2) --> 1 | |
# int("101", 2) --> 5 | |
# seat = "FBFBBFFRLR" | |
# row = seat[:7] | |
# col = seat[7:] | |
def get_seat(seat): | |
row = int("".join(map(lambda letter: {"F": "0", "B": "1"}[letter], seat[:7])), 2) | |
col = int("".join(map(lambda letter: {"R": "1", "L": "0"}[letter], seat[7:])), 2) | |
return row * 8 + col | |
assert get_seat("FBFBBFFRLR") == 357 | |
def get_max(seats): | |
return max(get_seat(seat) for seat in seats) | |
def get_all_seats(seats): | |
return sorted([get_seat(seat) for seat in seats]) | |
def find_missing_seat(seats): | |
all_seats = get_all_seats(seats) | |
for i, seat in enumerate(all_seats): | |
if i > 0 and int(all_seats[i+1]) != int(all_seats[i]) + 1: | |
return int(all_seats[i]) + 1 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment