Finish hw 2

This commit is contained in:
Isaac Shoebottom 2024-01-18 17:25:55 -04:00
parent 91d8538cc9
commit 50518cc3c3
2 changed files with 71 additions and 1 deletions

View File

@ -48,10 +48,70 @@ def p3():
return strings return strings
def valid():
alphabet = ['a', 'b']
strings = get_all_strings_with_a_given_alphabet_and_length(alphabet, 5)
def number_of_a_is_equal_to_3(s):
return s.count('a') == 3
def number_of_b_is_equal_to_3(s):
return s.count('b') == 3
# A game stops when a player has 3 a's or 3 b's, so there should be no trailing wins from the losing player
def valid_game(s):
if number_of_a_is_equal_to_3(s):
return not s.endswith('b')
elif number_of_b_is_equal_to_3(s):
return not s.endswith('a')
strings = list(filter(valid_game, strings))
return strings
def p5():
alphabet = ['a', 'b']
valid_strings = valid()
strings = get_all_strings_with_a_given_alphabet_and_length(alphabet, 5)
def number_of_a_or_b_is_equal_to_3(s):
return s.count('a') == 3 or s.count('b') == 3
strings = list(filter(number_of_a_or_b_is_equal_to_3, strings))
strings = [i for i in strings if i not in valid_strings] # strings - valid_strings
return strings
def p6():
alphabet = ['a', 'b']
valid_strings = valid()
strings = get_all_strings_with_a_given_alphabet_and_length(alphabet, 5)
def number_of_a_is_equal_to_3(s):
return s.count('a') == 3
def number_of_b_is_equal_to_3(s):
return s.count('b') == 3
def opposite_number_of_winner_is_less_than_3(s):
if number_of_a_is_equal_to_3(s):
return s.count('b') < 3
elif number_of_b_is_equal_to_3(s):
return s.count('a') < 3
strings = list(filter(opposite_number_of_winner_is_less_than_3, strings))
strings = [i for i in strings if i not in valid_strings] # strings - valid_strings
return strings
def main(): def main():
print_strings(p1()) print_strings(p1())
print_strings(p2()) print_strings(p2())
print_strings(p3()) print_strings(p3())
print_strings(valid())
print_strings(p5())
print_strings(p6())
if __name__ == '__main__': if __name__ == '__main__':

12
util.py
View File

@ -9,5 +9,15 @@ def get_all_strings_with_a_given_alphabet_and_length(alphabet, length):
return strings return strings
def replace_empty_string_with_symbol(strings):
symbol = 'ε'
for i in range(len(strings)):
if strings[i] == '':
strings[i] = symbol
return strings
def print_strings(strings): def print_strings(strings):
print(strings, len(strings)) strings = replace_empty_string_with_symbol(strings)
# print(len(strings))
print(strings)