mirror of
https://github.com/iritmaximus/tira.git
synced 2026-10-12 19:13:47 +03:00
did 5 ex.
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
import itertools
|
||||
|
||||
def create_words(word=''):
|
||||
perms = set()
|
||||
for permutation in itertools.permutations(word):
|
||||
prev = ""
|
||||
correct = True
|
||||
for char in permutation:
|
||||
if char == prev:
|
||||
correct = False
|
||||
break
|
||||
prev = char
|
||||
|
||||
if correct:
|
||||
perms.add("".join(permutation))
|
||||
|
||||
return sorted(list(perms))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(create_words("abc")) # ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']
|
||||
print(create_words("aab")) # ['aba']
|
||||
print(create_words("aaab")) # []
|
||||
|
||||
print(create_words("kala"))
|
||||
# ['akal', 'akla', 'alak', 'alka', 'kala', 'laka']
|
||||
|
||||
print(create_words("syksy"))
|
||||
# ['ksysy', 'kysys', 'skysy', 'syksy', 'sykys', 'sysky',
|
||||
# 'sysyk', 'yksys', 'ysksy', 'yskys', 'ysyks', 'ysysk']
|
||||
|
||||
print(len(create_words("aybabtu"))) # 660
|
||||
print(len(create_words("abcdefgh"))) # 40320
|
||||
@@ -0,0 +1,67 @@
|
||||
from typing import List
|
||||
|
||||
def min_count(weights: List[int], max_weight: int):
|
||||
if len(weights) == 0:
|
||||
return 0
|
||||
|
||||
weights = sorted(weights, reverse=True)
|
||||
|
||||
if weights[0] > max_weight:
|
||||
return -1
|
||||
|
||||
|
||||
result = 0
|
||||
result += weights.count(max_weight)
|
||||
weights = list(filter(lambda x: x != max_weight, weights))
|
||||
|
||||
nums: List[int] = []
|
||||
remaining_weights = weights.copy()
|
||||
while len(remaining_weights) != 0:
|
||||
for (i, num) in enumerate(remaining_weights.copy()):
|
||||
print(num, "n:", nums, "r:", remaining_weights, "w:", max_weight, result)
|
||||
|
||||
# Found perfect match
|
||||
if sum(nums) + num == max_weight:
|
||||
nums.append(num)
|
||||
result += 1
|
||||
# Found optimal match, prepare to loop again
|
||||
for rm in nums:
|
||||
remaining_weights.remove(rm)
|
||||
nums = []
|
||||
|
||||
# Found something closer
|
||||
elif sum(nums) + num < max_weight:
|
||||
# We tried summing this one before and it failed
|
||||
nums.append(num)
|
||||
|
||||
# Too far
|
||||
elif sum(nums) + num > max_weight:
|
||||
pass
|
||||
|
||||
|
||||
if len(nums) != 0:
|
||||
result += 1
|
||||
for rm in nums:
|
||||
remaining_weights.remove(rm)
|
||||
nums = []
|
||||
|
||||
|
||||
print(weights, result)
|
||||
print()
|
||||
return result
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
assert min_count([], 1) == 0
|
||||
assert min_count([1], 1) == 1
|
||||
assert min_count([2, 3, 3, 5], 4) == -1
|
||||
|
||||
assert min_count([2, 3, 3, 5], 7) == 2
|
||||
assert min_count([2, 3, 3, 5], 6) == 3
|
||||
assert min_count([2, 3, 3, 5], 5) == 3
|
||||
|
||||
assert min_count([1, 1, 1, 1], 1) == 4
|
||||
assert min_count([1, 1, 1, 1], 4) == 1
|
||||
|
||||
assert min_count([3, 4, 1, 2, 3, 3, 5, 9], 10) == 3
|
||||
@@ -0,0 +1,25 @@
|
||||
import itertools
|
||||
|
||||
|
||||
def count_combinations(cards, target):
|
||||
print()
|
||||
result = 0
|
||||
|
||||
for i in range(1, len(cards) + 1):
|
||||
for comb in itertools.combinations(cards, i):
|
||||
print(i, comb, sum(comb), cards)
|
||||
if sum(comb) == target:
|
||||
result += 1
|
||||
|
||||
return result
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(count_combinations([2, 1, 4, 6], 6)) # 2
|
||||
print(count_combinations([1, 1, 1, 1], 2)) # 6
|
||||
print(count_combinations([2, 1, 4, 6], 15)) # 0
|
||||
print(count_combinations([1], 1)) # 1
|
||||
print(count_combinations([5], 5)) # 1
|
||||
print(count_combinations([2,2], 4)) # 1
|
||||
# print(count_combinations([1, 2, 3, 4, 5], 5)) # 3
|
||||
# print(count_combinations([1, 1, 4, 1, 1], 4)) # 2
|
||||
# print(count_combinations([1] * 10, 5)) # 252
|
||||
@@ -0,0 +1,67 @@
|
||||
from typing import List
|
||||
import itertools
|
||||
|
||||
|
||||
def check_increasing(number: str):
|
||||
prev_num: int = -1
|
||||
if len(number) > 1 and int(number[0]) == 0:
|
||||
return False
|
||||
|
||||
for num in number:
|
||||
if int(num) >= prev_num:
|
||||
prev_num = int(num)
|
||||
else:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def lower_than_res(res: str, numbers: str):
|
||||
for (i, x) in enumerate(numbers):
|
||||
if x >= res[0]:
|
||||
return i
|
||||
|
||||
return 0
|
||||
|
||||
def count_numbers(length: int, numbers: str):
|
||||
result: List[str] = [x for x in numbers]
|
||||
while length-1> 0:
|
||||
for i, res in enumerate(result.copy()):
|
||||
# Remove previous step
|
||||
result.remove(res)
|
||||
|
||||
# Don't calculate unnecessary things
|
||||
i_lower = lower_than_res(res, numbers)
|
||||
for num in numbers[i_lower:]:
|
||||
if i != 0 and res[-1] == "0":
|
||||
continue
|
||||
if num >= res[-1] and res[-1] != "0":
|
||||
result.append(res + num)
|
||||
|
||||
length -= 1
|
||||
|
||||
print(len(result))
|
||||
return len(result)
|
||||
|
||||
|
||||
# Way too slow on the last test
|
||||
def count_numbers2(length: int, numbers: str):
|
||||
result: int = 0
|
||||
for comb in itertools.product(numbers, repeat=length):
|
||||
# print(comb, check_increasing(comb))
|
||||
if check_increasing(comb):
|
||||
result += 1
|
||||
|
||||
print(result)
|
||||
# print()
|
||||
return result
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
assert count_numbers(5, "1") == 1
|
||||
assert count_numbers(1, "0") == 1
|
||||
assert count_numbers(3, "123") == 10
|
||||
assert count_numbers(2, "0") == 0
|
||||
assert count_numbers(2, "137") == 6
|
||||
assert count_numbers(8, "25689") == 495
|
||||
assert count_numbers(10, "12") == 11
|
||||
assert count_numbers(10, "123456789") == 43758
|
||||
@@ -0,0 +1,44 @@
|
||||
def find_codes(pattern):
|
||||
result = []
|
||||
for char in pattern:
|
||||
print(result, char)
|
||||
if char == "?":
|
||||
if len(result) == 0:
|
||||
for num in range(1,10):
|
||||
if str(num) not in pattern:
|
||||
result.append(str(num))
|
||||
else:
|
||||
for i in range(len(result)):
|
||||
for num in range(1,10):
|
||||
if str(num) not in result[i] and str(num) not in pattern:
|
||||
result.append(result[i] + str(num))
|
||||
|
||||
else:
|
||||
if len(result) == 0:
|
||||
result = [char]
|
||||
else:
|
||||
for i in range(len(result)):
|
||||
result[i] += char
|
||||
print(result)
|
||||
print()
|
||||
|
||||
longest = len(result[-1])
|
||||
result = list(filter(lambda x : len(x) == longest, result))
|
||||
print("Final result:", result)
|
||||
print()
|
||||
return result
|
||||
|
||||
if __name__ == "__main__":
|
||||
codes = find_codes("3148")
|
||||
print(codes) # ['1', '2', '3', ...]
|
||||
|
||||
codes = find_codes("24?5")
|
||||
print(codes) # ['2415', '2435', '2465', '2475', '2485', '2495']
|
||||
|
||||
# codes = find_codes("1?2?")
|
||||
# print(codes[:5]) # ['1324', '1325', '1326', '1327', '1328']
|
||||
# print(len(codes)) # 42
|
||||
|
||||
# codes = find_codes("????")
|
||||
# print(codes[:5]) # ['1234', '1235', '1236', '1237', '1238']
|
||||
# print(len(codes)) # 3024
|
||||
@@ -0,0 +1,31 @@
|
||||
import itertools
|
||||
|
||||
|
||||
def calc_remaining_list(nums, comb):
|
||||
l = nums.copy()
|
||||
for x in comb:
|
||||
l.remove(x)
|
||||
return l
|
||||
|
||||
def check_sum(numbers):
|
||||
print()
|
||||
for i in range(1, len(numbers) + 1):
|
||||
for x in itertools.combinations(numbers, i):
|
||||
x = list(x)
|
||||
|
||||
remaining_list = calc_remaining_list(numbers, x)
|
||||
print(x, sum(x), remaining_list, sum(remaining_list))
|
||||
if (sum(x) == sum(remaining_list)):
|
||||
return True
|
||||
return False
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(check_sum([1, 2, 3, 4])) # True
|
||||
print(check_sum([1, 2, 3, 5])) # False
|
||||
print(check_sum([0])) # True
|
||||
print(check_sum([2, 2])) # True
|
||||
print(check_sum([2, 4])) # False
|
||||
print(check_sum([1, 5, 6, 3, 5])) # True
|
||||
print(check_sum([1, 5, 5, 3, 5])) # False
|
||||
print(check_sum([10**9, 2*10**9, 10**9])) # True
|
||||
print(check_sum([1, 1, 1, 1, 1, 1, 1, 1, 1, 123])) # False
|
||||
Reference in New Issue
Block a user