CS2333/Homework/2024-1-15.py

119 lines
2.7 KiB
Python
Raw Permalink Normal View History

2024-01-15 18:39:16 -04:00
from util import *
def p1():
alphabet = ['a', 'b']
strings = get_all_strings_with_a_given_alphabet_and_length(alphabet, 3)
def number_of_a_is_even(s):
return s.count('a') % 2 == 0
strings = list(filter(number_of_a_is_even, strings))
return strings
def p2():
possible_m = [0, 1, 2, 3, 4]
possible_n = [0, 1, 2, 3, 4]
possible_p = [0, 1, 2, 3, 4]
strings = []
for m in possible_m:
for n in possible_n:
for p in possible_p:
# First rule
if m + n + p <= 4:
# Second rule
if m > n:
# Third rule
if n == p:
strings.append('a' * m + 'b' * n + 'c' * p)
return strings
def p3():
alphabet = ['a', 'b', 'c']
strings = get_all_strings_with_a_given_alphabet_and_length(alphabet, 4)
def number_of_a_is_greater_than_number_of_b(s):
return s.count('a') > s.count('b')
def number_of_b_is_equal_to_number_of_c(s):
return s.count('b') == s.count('c')
strings = list(filter(number_of_a_is_greater_than_number_of_b, strings))
strings = list(filter(number_of_b_is_equal_to_number_of_c, strings))
return strings
2024-01-18 17:25:55 -04:00
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
2024-01-15 18:39:16 -04:00
def main():
print_strings(p1())
print_strings(p2())
print_strings(p3())
2024-01-18 17:25:55 -04:00
print_strings(valid())
print_strings(p5())
print_strings(p6())
2024-01-15 18:39:16 -04:00
if __name__ == '__main__':
main()