Solution to problem on slide 4 :
# make a dictionary for characters to count the number of characters in the first string, then subtract the count from the 2nd, both counts need to cancel out at the end -> check if all elements are 0
tracker = collections.defaultdict(int)
for x in s: tracker[x] += 1
for x in t: tracker[x] -= 1
return all(x == 0 for x in tracker.values())
Solution to problem on slide 5:
# basically label each occurence of each character in the string by the order of its first occurance. Then check if the final labels are equal. If they are equal then the two strings are isomorphic.
s=list(s)
t=list(t)
in_s = {}
in_t = {}
for i,c in enumerate(s):
if c not in in_s:
in_s[c]=len(in_s)
s[i]=in_s[c]
for i,c in enumerate(t):
if c not in in_t:
in_t[c]=len(in_t)
t[i]=in_t[c]
return t==s
Solution to problem on slide 7 (HW):
#store cards that have values the same suit as the opponent, then iterate through them to find the smallest card greator than the opponents and the smallest card in general. If you do have a card greator than the opponent, play the smallest one, if you have a same-suit card play the smallest, or play NONE
for i in range(5):
def get_card_val(char):
if char in ['1','2','3','4','5','6','7','8','9','10']:
return int(char)
elif char =='J':
return 11
elif char =='Q':
return 12
return 13
def get_val_card(card):
if card==11:
return 'J'
if card==12:
return 'Q'
if card==13:
return 'K'
return str(card)
cards= (input("Enter the cards: ")).split(", ")
opp_val = get_card_val(cards[0][0])
opp_suit = cards[0][1]
same_suit_card = []
for card in cards[1:]: # dealer's cards
val = get_card_val(card[0])
if card[1]==opp_suit:
same_suit_card.append((val,card[1]))
if len(same_suit_card)>0:
min_val = 1e9
min_max_val = None
for val,suit in same_suit_card:
if val < min_val:
min_val = val
if val>opp_val:
if min_max_val is None:
min_max_val = val
elif val < min_max_val:
min_max_val=val
if min_max_val is not None:
print(get_val_card(min_max_val)+opp_suit)
else:
print(get_val_card(min_val)+opp_suit)
else:
print('NONE')
Let me know if you have any questions about the slides or code in this post or need help understanding a problem or its solution.