Rozwiązania¶
Zadanie 1¶
Zadanie 1.3¶
Zadanie 3¶
Zadanie 3.1¶
with open("anagram.txt") as file:
binary_list = file.read().split()
result1 = 0
result2 = 0
for binary in binary_list:
count0 = binary.count("0")
count1 = binary.count("1")
if count0 == count1:
result1 += 1
elif abs(count0 - count1) == 1:
result2 += 1
print("Zadanie 3.1")
print("Zrównoważone:", result1)
print("Prawie zrównoważone:", result2)
Zadanie 3.2¶
from math import factorial
with open("anagram.txt") as file:
binary_list = file.read().split()
max_anagrams = 0
for binary in binary_list:
if len(binary) != 8:
continue
count0 = binary.count("0")
count1 = binary.count("1")
anagrams_count = factorial(8) / (factorial(count0) * factorial(count1))
if count0 > 0:
anagrams_count -= factorial(7) / (factorial(count0 - 1) * factorial(count1))
if anagrams_count > max_anagrams:
max_anagrams = anagrams_count
print("Zadanie 3.2")
for binary in binary_list:
if len(binary) != 8:
continue
count0 = binary.count("0")
count1 = binary.count("1")
anagrams_count = factorial(8) / (factorial(count0) * factorial(count1))
if count0 > 0:
anagrams_count -= factorial(7) / (factorial(count0 - 1) * factorial(count1))
if anagrams_count == max_anagrams:
print(binary)
Zadanie 3.3¶
Zadanie 3.4¶
with open("anagram.txt") as file:
binary_list = file.read().split()
result1 = 0
max_distinct_sum = 0
for binary in binary_list:
decimal = int(binary, 2)
decimal_str = str(decimal)
if decimal_str.count("0") == 0:
result1 += 1
distinct_sum = sum(map(int, set(decimal_str)))
if distinct_sum > max_distinct_sum:
max_distinct_sum = distinct_sum
print("Zadanie 3.4")
print("a)", result1)
for binary in binary_list:
decimal = int(binary, 2)
decimal_str = str(decimal)
distinct_sum = sum(map(int, set(decimal_str)))
if distinct_sum == max_distinct_sum:
print("b)", decimal)
break