I am getting runtime error. However, the script produces correct output
without any error on my pc.
import re
patterns = []
list_of_permutations = []
def main():
global patterns
number_of_cases = int(input())
for i in range(number_of_cases):
number_of_patterns = int(input())
patterns = []
for j in range(number_of_patterns):
patterns.append(input())
size, characters = getSize(patterns)
data = [None] * size
is_found, string = combinations(
characters, data, 0, len(characters)-1, 0, size)
if is_found:
print(f'Case #{i+1}: {string}')
else:
print(f'Case #{i+1}: *')
def match_with_patterns(permutation, patterns):
for pattern in patterns:
pattern = re.compile('^' + pattern.replace('*', '.*') + '$')
matches = pattern.match(permutation)
if not matches:
return False
return True
def getSize(patterns):
uniqueCharacters = {}
for pattern in patterns:
for char in pattern:
if char != '*' and char not in uniqueCharacters.keys():
uniqueCharacters[char] = True
max_pattern_size = 0
for pattern in patterns:
pattern = pattern.replace('*', '')
max_pattern_size = max(max_pattern_size, len(pattern))
return max(len(uniqueCharacters), max_pattern_size),
list(uniqueCharacters.keys())
def combinations(arr, data, start, end, index, r):
global list_of_permutations
if index == r:
list_of_permutations = []
list_of_permutations = lop(sorted(''.join(list(data))))
for permutation in list_of_permutations:
is_a_match = match_with_patterns(permutation, patterns)
if is_a_match:
return True, permutation
return False, None
for i in range(start, end+1):
data[index] = arr[i]
is_a_match, permutation = combinations(arr, data, i, end, index+1,
r)
if is_a_match:
return True, permutation
return False, None
# lexicographic permutations
def lop(string):
result = []
result.append(''.join(string))
count = 0
while True:
for i in range(len(string)-1, -1, -1):
if i != 0 and string[i-1] < string[i]:
break
if i == 0:
break
for j in range(len(string)-1, -1, -1):
if string[i-1] < string[j]:
string[i-1], string[j] = string[j], string[i-1]
temp = string[i:]
string = string[:i]
string.extend(list(''.join(temp[::-1])))
result.append(''.join(string))
break
return result
main()
--
You received this message because you are subscribed to the Google Groups
"Google Code Jam" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/google-code/5cefa9f1-d1ef-4b9b-a480-1a5e3cec2b31%40googlegroups.com.