Created
March 16, 2022 17:42
-
-
Save C10udburst/1458796418dcb68df5ffafca126594e4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import product | |
from functools import reduce | |
liczby_str = open("liczbyplus.txt", "r", encoding='utf-8').read().strip().splitlines() | |
liczby_str.sort(reverse=True) | |
liczby = [int(l) for l in liczby_str] | |
liczby_max = reduce(max, liczby) | |
fibonacci = [1, 2] | |
while fibonacci[-1] < liczby_max: | |
fibonacci.append(fibonacci[-1] + fibonacci[-2]) | |
def find_palindromes(text): | |
slices = product(range(0, len(text)), range(1, len(text) + 1)) # wszystkie mozliwe fragmenty tekstu | |
slices = list(filter(lambda a: (a[1] - a[0]) >= 2, slices)) # tylko o len()>=2 | |
# slices.sort(key=lambda a: (a[1] - a[0]), reverse=True) # od najdluzyszych do najkrotszych | |
sliced = [] | |
for start, end in slices: | |
txtslice = text[start:end] | |
if txtslice == txtslice[::-1]: # czy palindrom | |
sliced.append(txtslice) | |
return sliced | |
def fib_product(num): | |
nums = [] | |
for fib in fibonacci[::-1]: | |
if fib <= num: | |
num -= fib | |
nums.append(fib) | |
if num < 1: | |
return nums | |
print() | |
print("Zad. 2") | |
count = 0 | |
for liczba in liczby_str: | |
suma = str(int(liczba) + int(liczba[::-1])) | |
if suma == suma[::-1]: | |
count += 1 | |
print(f"Ilosc: {count}") | |
print() | |
print("Zad. 5") | |
for i, liczba in enumerate(liczby[:10]): | |
products = fib_product(liczba) | |
print(f"{i+1}: {liczba} = {' + '.join([str(x) for x in products])}") | |
print() | |
print("Zad. 7") | |
maximum = 0 | |
for liczba in liczby_str: | |
palindromy = find_palindromes(liczba) | |
if not palindromy: | |
continue | |
maximum = max(maximum, int(reduce(lambda a, b: max(int(a), int(b)), palindromy))) | |
print(maximum) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment