Day 2
Day 2
It has been a long time since I wrote any Python, so I decided to use it today to shake off the rust (pun intended). The syntax felt a bit foreign after focusing on C and Java recently, but the string manipulation features were exactly what I needed for this problem.
how i approached the problem
The task was to clean up a database of product IDs. We were given a list of ranges (like 11-22) and had to find invalid IDs within them. An ID is invalid if it consists entirely of a repeating sequence of digits, like 123123 (repeating 123) or 1111 (repeating 1).
Since the input format was just a long string of ranges separated by commas, Python's split method made parsing easy. I just needed to iterate through every number in every range and check its properties.
checking for repetitions
To determine if a number was "invalid," I converted it to a string. Then, I checked every possible substring length size from 1 up to half the length of the string.
If the total length of the string was divisible by size, I took the first size characters, repeated them enough times to match the full length, and compared the result to the original string. If they matched, the ID was a repetition, and I added it to the total sum.
here is my code in python:
data = input()
parts = data.split(",")
def checkRepeat(start, end):
total = 0
for i in range(start, end):
s = str(i)
L = len(s)
repeated = False
for size in range(1, L // 2 + 1):
if L % size != 0:
continue
block = s[:size]
if block * (L // size) == s:
repeated = True
break
if repeated:
total += i
return total
result = 0
for p in parts:
start, end = p.split("-")
start = int(start)
end = int(end)
result += checkRepeat(start, end + 1)
print(result)