this time with no legal info
This commit is contained in:
37
2/Ex2.py
Normal file
37
2/Ex2.py
Normal file
@@ -0,0 +1,37 @@
|
||||
ranQ = []
|
||||
|
||||
count = 0
|
||||
|
||||
with open("input.txt") as file:
|
||||
input = file.read().strip()
|
||||
|
||||
ranges = input.split(",")
|
||||
|
||||
for ran in ranges:
|
||||
start, end = ran.split("-")
|
||||
if len(start) == len(end):
|
||||
ranQ.append((int(start), int(end)))
|
||||
else:
|
||||
if len(start) == 1:
|
||||
ranQ.append((10, int(end)))
|
||||
else:
|
||||
ranQ.append((int(start), 10**len(start)- 1))
|
||||
ranQ.append((10**len(start), int(end)))
|
||||
|
||||
for start, end in ranQ:
|
||||
done = []
|
||||
print(f"Processing range: {start} to {end}")
|
||||
for i in range(1, len(str(start)) // 2 + 1):
|
||||
if len(str(start)) % i != 0:
|
||||
continue
|
||||
start_prefix = str(start)[:i]
|
||||
end_prefix = str(end)[:i]
|
||||
num_repeats = len(str(start)) // i
|
||||
for prefix in range(int(start_prefix), int(end_prefix) + 1):
|
||||
candidate = int(str(prefix) * num_repeats)
|
||||
if start <= candidate <= end and candidate not in done:
|
||||
print(f"Adding: {candidate}")
|
||||
count += int(candidate)
|
||||
done.append(candidate)
|
||||
|
||||
print(count)
|
||||
Reference in New Issue
Block a user