Reorganize

This commit is contained in:
2024-01-26 15:05:29 -04:00
parent 9990c0e8a1
commit 487859e9d6
7 changed files with 19 additions and 9 deletions

69
Homework/2024-1-12.py Normal file
View File

@ -0,0 +1,69 @@
from util import *
def p1():
# Language rule 1, n is less than or equal to 2
n = 2
# Language rule 2, m is less than or equal to n
m = n
strings = []
# Language rule 3, n and m are both non-negative integers
for i in range(0, n + 1):
for j in range(0, m + 1):
if j > i:
continue
strings.append('0' * j + '1' * i)
return strings
def p2():
possible_n = [2, 3, 4, 5, 6]
strings = []
for i in possible_n:
integer_divisors = []
for j in range(1, i + 1):
if i % j == 0:
integer_divisors.append(j)
for j in integer_divisors:
strings.append('a' * j + 'b' * i)
return strings
def p3():
possible_n = [1, 2, 3]
strings = []
for i in possible_n:
an = i
bn = 2 * i - 1
cn = 3 * i - 2
strings.append('a' * an + 'b' * bn + 'c' * cn)
return strings
def p4():
alphabet = ['a', 'b']
# include empty string
strings = get_all_strings_with_a_given_alphabet_and_length(alphabet, 3)
def is_palindrome(s):
return s == s[::-1]
strings = list(filter(is_palindrome, strings))
return strings
def main():
print_strings(p1())
print_strings(p2())
print_strings(p3())
print_strings(p4())
if __name__ == '__main__':
main()

118
Homework/2024-1-15.py Normal file
View File

@ -0,0 +1,118 @@
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
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():
print_strings(p1())
print_strings(p2())
print_strings(p3())
print_strings(valid())
print_strings(p5())
print_strings(p6())
if __name__ == '__main__':
main()

23
Homework/util.py Normal file
View File

@ -0,0 +1,23 @@
def get_all_strings_with_a_given_alphabet_and_length(alphabet, length):
strings = ['']
for i in range(length):
s = [s + c for s in strings for c in alphabet]
strings += s
# Remove duplicates (set() isn't as nice as it doesn't preserve order, at least for verification purposes)
strings = list(dict.fromkeys(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):
strings = replace_empty_string_with_symbol(strings)
# print(len(strings))
print(strings)