MKisunko
BAN USERI used both, the original code and the one that Vishap came with as more reliable in terms of result solution.
I have not found yet how this happens but [2,4,2,5,7] with k=10 gives the wrong result.
The correct result is 31 (as 2^5-1) - 4(options [7],[5,7],[4,5,7],[4,7]) that is equal to 27.
Any idea on why?
def is_periodic(input_string, is_return_first=True):
input_length = len(input_string)
result = (False, 0, '')
pattern_length = 1
while pattern_length <= int(input_length / 2):
if input_length % pattern_length == 0:
is_pattern_equal = True
pattern_string = input_string[:pattern_length]
pattern_multiplier = int(input_length / pattern_length)
j = 0
while (j < pattern_length) and is_pattern_equal:
for k in range(1, pattern_multiplier):
if input_string[j] != input_string[k * pattern_length + j]:
is_pattern_equal = False
break
j += 1
if is_pattern_equal:
result = (True, pattern_multiplier, pattern_string)
if is_return_first:
return result
pattern_length += 1
return result
print(is_periodic('abababababababababababab'))
print(is_periodic('xxxxxx', False))
print(is_periodic('aabbaaabba'))
This is my first code in Python. At least I like simplicity and because of lack of practice in Python I have very small idea on efficiency of it.
pairs=[(1,4),(6,8),(2,4),(7,9),(10,15)]
hours = set()
for first,last in pairs:
i = first
while i<=last:
print(i)
hours.add(i)
i+=1
print("total hours=",len(hours))
I decided to get not only the number of subsets but also subsets themselves to check how stable solution is.
- MKisunko March 16, 2019