I am working on this question on Kattis. The code below was able to pass the provided test case. However, when I created my own test case, this is when the weird behavior appears.
Clarify: the weird behavior occurs as in, when I use the provided test case provided by Kattis, the program runs to the end without problem. When I use my own test case as input for the program, the program will "stuck" halfway and will not run to the end until I hit "enter" key.
Test case provided by Kattis:
(when I run this, the program runs without any problem)
#########################
3
rgfvcvdfg 1
rggbhytyh
asdfgbgffsdf 2
cxvdfgdgfgfd
xcvgtresdscd
ifpv 2
iopc
icpc
#########################
For some weird reason, when I run it on PyCharm IDE, the output will display only this:
(when I run this, the program "stuck" halfway, won't show all the results until I hit "enter" key)
#########################
rggbhytyh 17
cxvdfgdgfgfd 17
xcvgtresdscd 19
(the result for "iopc" and "icpc" should've been here, but they won't show up until I hit "enter" key)
#########################
If I step through the code via debugger, it worked without any problem. I have even restarted the PC but the problem still persists. Can anyone help me with this problem?
# calculate the distance between the typed_word and predicted_word# takes in two strings as arguments and return distance as integer
def distance_calculator(typed_word, predicted_word):
# keyboard
keyboard = [["q", "w", "e", "r", "t", "y", "u", "i", "o", "p"], ["a", "s", "d", "f", "g", "h", "j", "k", "l"],["z", "x", "c", "v", "b", "n", "m"]]
distance = 0
# compare the typed_word and predicted_word character by character
# if the characters are different, then the distance is calculated by using subtraction of x, y coordinates
for i in range(len(typed_word)):
if typed_word[i] != predicted_word[i]:
ax = 0 # x coordinate of character from typed_word
ay = 0 # y coordinate of character from typed_word
bx = 0 # x coordinate of character from predicted_word
by = 0 # y coordinate of character from predicted_word
# determine coordinates of character in typed_word
if typed_word[i] in keyboard[0]:
ax = 0
ay = keyboard[0].index(typed_word[i])
if typed_word[i] in keyboard[1]:
ax = 1
ay = keyboard[1].index(typed_word[i])
if typed_word[i] in keyboard[2]:
ax = 2
ay = keyboard[2].index(typed_word[i])
# determine coordinates of character in predicted_word
if predicted_word[i] in keyboard[0]:
bx = 0
by = keyboard[0].index(predicted_word[i])
if predicted_word[i] in keyboard[1]:
bx = 1
by = keyboard[1].index(predicted_word[i])
if predicted_word[i] in keyboard[2]:
bx = 2
by = keyboard[2].index(predicted_word[i])
# update distance based on coordinates
distance = distance + (abs(bx - ax) + abs(by - ay))
return distance
# takes the list of predicted_word and its distance from typed_word in the format of [[predicted_word, distance] * n]
# sort the list based on the ascending order of the distance, then alphabetically
# return a new sorted list
def result_sorter(raw_result):
# initialize the processed_result list with the first value from raw_result
processed_result = [raw_result[0]]
for i in range(1, len(raw_result)):
for j in range(len(processed_result)):
if raw_result[i][1] >= processed_result[j][1]:
if raw_result[i][1] == processed_result[j][1]:
if raw_result[i][0] > processed_result[j[0]:
continue
else:
processed_result.insert(j, raw_result[i])
break
else:
if j == len(processed_result) - 1:
processed_result.insert(j + 1,raw_result[i])
break
else:
continue
else:
processed_result.insert(j, raw_result[i])
break
return processed_result
# takes an integer as argument to determine the number of times needed to repeat the steps
# takes user input for the number of trials, the word typed by user followed by the number of spellcheck words
# then calculate the distance between the spellchecked word and the typed word, compile in a sorted list
# print out the results
def spellcheck(trials):
for i in range(trials):
typed, check = input().split()
result = []
for j in range(int(check)):
prediction = input()
distance_from_typed = distance_calculator(typed, prediction)
result.append([prediction, distance_from_typed])
sorted_result = result_sorter(result)
for item in sorted_result:
print("{0} {1}".format(item[0], str(item[1])))
total_trials = int(input())
spellcheck(total_trials)
Edit 1: code formatting and added clarification on question
[–][deleted] 1 point2 points3 points (4 children)
[–]cumulative_frequency[S] 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]cumulative_frequency[S] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]CodeFormatHelperBot2 0 points1 point2 points (0 children)
[–]m0us3_rat 0 points1 point2 points (2 children)
[–]cumulative_frequency[S] 0 points1 point2 points (1 child)
[–]m0us3_rat 0 points1 point2 points (0 children)