Skip to content

Instantly share code, notes, and snippets.

@joshvasquez
Created May 22, 2023 21:15
Show Gist options
  • Save joshvasquez/df981461d3e12e6e86daa3e48d595b41 to your computer and use it in GitHub Desktop.
Save joshvasquez/df981461d3e12e6e86daa3e48d595b41 to your computer and use it in GitHub Desktop.
# Traveling Minstrel
def total_song_knowers(num_villagers, attendees_lists):
song_list = []
villager_list ={}
# First, instantiate the villagers from the num_villagers
for i in range(num_villagers+1)[1:]: # index + 1
villager_list[i] = []
for song_name, nights_attendees in enumerate(attendees_lists):
if 0 in nights_attendees:
for villager in nights_attendees:
if villager in villager_list: # no minstrel allowed
villager_list[villager].append(song_name)
song_list.append(song_name)
else:
new_known_songs_list = []
for villager in villager_list:
for song in villager_list[villager]:
if not song in new_known_songs_list:
new_known_songs_list.append(song)
for villager in villager_list:
if villager in nights_attendees:
for song in new_known_songs_list:
if not song in villager_list[villager]:
villager_list[villager].append(song)
num_total_song_knowers = 0
for villager in villager_list:
if song_list == villager_list[villager]:
num_total_song_knowers += 1
return num_total_song_knowers
# OG Texting
def translate(words, digits):
phone_dictionary = {
'2': ['A','B','C'],
'3': ['D','E','F'],
'4': ['G','H','I'],
'5': ['J','K','L'],
'6': ['M','N','O'],
'7': ['P','Q','R','S'],
'8': ['T','U','V'],
'9': ['W','X','Y','Z']
}
split_digits = [*str(digits)] # split the digits into a list of individual numbers as strings
num_words = 0
for word in words: # iterate through the entire list of words
required_length = len(word)
count = 0
for index, char in enumerate(word): #iterate through each character
# print(index, char)
if char in phone_dictionary[split_digits[index]]:
count += 1
if count == required_length:
num_words += 1
return num_words
# Drain Pipes
def pipe_outputs(num_pipes, steps):
pipes = [8] * num_pipes
for step in steps:
pipe_index = step[0] -1
# first check is if it is a joiner (only 1 item in list) or if it it is a splitter (2 items)
if len(step) > 1: #
flow_diff = step[1]
left = flow_diff
right = pipes[pipe_index] - flow_diff
# left pipe replaces the value of the pipe at pipe_index, insert right_pipe at pipe_index+ 1
pipes[pipe_index] = left
pipes.insert(pipe_index+1, right)
else: # it's joining the pipe on the right
new_pipe_value = pipes[pipe_index] +pipes[pipe_index + 1]
pipes[pipe_index] = new_pipe_value
pipes.pop(pipe_index+1)
return pipes
# Dirty Laundry
def num_laundry_days(num_shirts, num_days, event_days):
shirts = [True] * num_shirts
laundry_days_counter =0
for day in range(num_days): # progress through the amount of days
# is there a clean shirt?
if True in shirts: # True = Clean False = Dirty so any True in shirts will mean they don't need to to do laundry but will flip a clean shirt to dirty shirt
shirts[shirts.index(True)] = False # flip the first Clean shirt to a dirty shirt since it's being used
# if not do laundry
else:
for i in range(len(shirts)):
shirts[i] = True # clean shirts = make entire array true
shirts[shirts.index(True)] = False # flip the first Clean shirt to a dirty shirt since it's being used
# increment laundry_days
laundry_days_counter+= 1
# is it an event day?
if day+1 in event_days: # event days aren't 0-indexed, so add 1 to match with event day list
# if it is an event day, then you get another clean shirt
shirts.append(True)
return laundry_days_counter
# Tide Difference
def tide_difference(measurements):
low_tide = min(measurements)
high_tide = max(measurements)
low_tide_index = measurements.index(low_tide)
high_tide_index = measurements.index(high_tide)
# first, if the high tide index is less than the low tide index, return None
if high_tide_index < low_tide_index:
return None
lowest_number = low_tide # set the low to be the low_tide
# iterate through the numbers between the min and max
for num in measurements[low_tide_index+1:high_tide_index]:
if num < lowest_number: # if anything between is higher, then it fails
return None
else: #otherwise it becomes the new loewest number
lowest_number = num
# if the data isn't disqualified, return the difference
return high_tide-low_tide
# DNA Tranformation
# Check if Strand 1 complements Strand 2 DNA
def is_matching_pair(strand1, strand2):
for code in range(len(strand1)):
if strand1[code] == 'G':
if not strand2[code] == 'C': # if string 1 is 'G' then anything but 'C' is wrong
return False
elif strand1[code] == 'C':
if not strand2[code] == 'G':
return False
elif strand1[code] == 'A':
if not strand2[code] == 'T':
return False
elif strand1[code] == 'T':
if not strand2[code] == 'A':
return False
return True
# takes a string and returns its complement G:C A:T
def complement(strand):
new_strand =""
for char in strand:
if(char == "G"):
new_strand += "C"
elif(char== "C"):
new_strand+= "G"
elif(char =="A"):
new_strand += "T"
elif(char == "T"):
new_strand += "A"
return new_strand
def t_for_u(strand): # replace T with U
new_strand =""
for char in strand:
if char == "T":
new_strand += "U"
else:
new_strand += char
return new_strand
def rna_transcription(dna_strand):
promoter = "TATAAT"
transcription_unit_start = dna_strand.find(promoter) + 10 # index is the promoter starting index + 10
# print(dna_strand)
rna_strand = dna_strand[transcription_unit_start:] #The search is iterating from the start of the transcription unit until the end of the list to find the terminator
substring_length = len(rna_strand)
for i in range(substring_length):
for j in range(i + 6, substring_length): # generate all possible substrings. i + 6 will start from generating a string of length 6 and end up to substring_length -6, aka the end of the string
terminator = rna_strand[i:j]
reversed_terminator = complement(terminator[::-1])
if reversed_terminator in rna_strand:
index = rna_strand.find(reversed_terminator)
transcription_unit = (rna_strand[0:index])
# you must match the terminator string with a reverse match
return t_for_u(complement(transcription_unit))
# Secret Code
def breakout_room(code, message):
code_count = 0
search_index= 0
for char in message:
search_char = code[search_index]
if char == search_char:
if search_index == len(code)-1: # if the search_index is on last character of the code, then we want to increment the code_count and reset to 0
code_count += 1
search_index = 0
else: # we found the next character in the code, but we haven't found the whole code, so switch to the next character in the code
search_index += 1
return code_count
# Flip Flop
def get_x_o_list(string):
temp_list =[*string]
for i,char in enumerate(temp_list):
if char== "X":
temp_list[i]= "O"
else:
temp_list[i] = "OX"
tempstring = "".join(temp_list)
return [*tempstring]
def calculate_num_letters(num_pushes):
num_x = 0
num_o = 0
x_o_list =[]
for i in range(num_pushes):
if not x_o_list: # if it's empty
x_o_list= ["X"]
else: # if it's not empty, iterate through the list
x_o_list = get_x_o_list("".join(x_o_list))
for i in range(len(x_o_list)):
if x_o_list[i] == "O":
num_o += 1
else:
num_x += 1
return num_x, num_o
# Gold Coin
def outcome(starting_player, matches):
coin_holder = [*starting_player]
for match in matches:
if coin_holder[0] in match:
coin_holder[0] = match[0]
return "".join(coin_holder)
# Slots
def calculate_plays(num_quarters):
slots = {
0: {
'plays_required': 27,
'payout': 20,
'play_count': 0
},
1: {
'plays_required': 100,
'payout': 50,
'play_count': 0
},
2: {
'plays_required': 8,
'payout': 7,
'play_count': 0
}
}
quarters_remaining = num_quarters
num_plays = 0
slot_index = 0
while quarters_remaining > 0: # play until you're broke
slots[slot_index]['play_count'] += 1 # increment the play of the slot being played
if slots[slot_index]['plays_required'] == slots[slot_index]['play_count']: # this means that play_count has reached plays_required
quarters_remaining += slots[slot_index]['payout'] # add the payout
slots[slot_index]['play_count'] = 0 # reset the play count to 0
if slot_index == 2:
slot_index=0
else:
slot_index+= 1
quarters_remaining-= 1
num_plays+= 1
return num_plays
# SAT
def best_student_and_score(answer_key):
algorithms = [
['A','B','C'], #Azami
['B','A','B','C'], # Baz
['A','A','C','C','B','B'] # Caris
]
length = len(answer_key)
# for i in range(length): # loop through the entire length of the answer key, generate 1 char per student's algorithm
guesses =[]
for algorithm in algorithms:
guesses.append([algorithm[i % len(algorithm)] for i in range(length)]) # i % len(algorithm) means it will return the remainder... which is the index you want to fill it with
scores=[0,0,0]
# iterate though
for i in range(len(guesses)): # tabulate each student's scores
for j in range(len(guesses[i])):
current_guess = guesses[i][j] # student's guess
if current_guess == answer_key[j]: #if the key is the same as the answer key, add 1
scores[i]+=1
max_score = max(scores)
max_score_index = scores.index(max_score)
if max_score_index == 0:
return scores[max_score_index], "Azami"
elif max_score_index == 1:
return scores[max_score_index], "Baz"
else:
return scores[max_score_index], "Caris"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment