 # Hackerrank problem: https://www.hackerrank.com/challenges/nested-list/problem

So I know answering this will most likely take you off-site so if it’s too much of a hassle don’t bother.

The problem description is the link in the title.
my code is:

``````if __name__ == '__main__':
my_names = []
my_scores = []
#creating lists to store the inputs, later going to zip them together.
for _ in range(int(input())):
name = input()
score = float(input())
my_names.append(name)
my_scores.append(score)

#sorting the zip list to keep scores and names grouped.
sorted_zip = sorted(zip(my_scores, my_names), key=lambda x: x)

x = []
y = []
#unzipping the list so that the scores can be compared. Was unsure if I could
# compare tuples.

for i in sorted_zip:
x.append(i)
y.append(i)

#locating second lowest score and printing the corresponding name.

for i in range(len(x)+1):
if x[i] < x[i+1]:
for z in range(i+2,len(x)):
if x[i+1] == x[z]:
print(y[z])
print(y[i+1])
break
#This is putrid code...
``````

So I realize that this isn’t the most elegant code but I could use some assistance on figuring out why it fails the case where the input is:

``````3
Test1
52
Test2
53
Test3
53
``````

For all other test cases it passes with flying colors. I’m assuming it has to do with the second nested loop and the fact that the list length is only 3 with the second lowest scores being the final two. I don’t know how to fix the indexing without receiving an index error Do i miss something obvious?

i wrote the case you struggle with:

``````abc = [["Test1", 52], ["Test2", 53], ["Test3", 53]];
``````

then for the most part i used your sorting algorithm:

``````abc.sort(key=lambda x: x)
``````

then you can simple grab the second and third element, get the names and sort them?

apparently not that simple, or i am just simply not paying attention

what a difficult challenge, so many corner cases its difficult to write code which will work if you increase the size of the list.

okay, i normally wouldn’t share my solution, but here you go:

``````if __name__ == '__main__':
abc = []
#creating lists to store the inputs, later going to zip them together.
for _ in range(int(input())):
name = input()
score = float(input())
abc.append([name, score])

abc.sort(key=lambda x: x)
# get the lowest score
score = abc
for index, person in enumerate(abc):
# keep looping over the list until we encounter second lowest score
if score != person:
# if this is last element in the list, just print the name
if index == len(abc) - 1:
print(abc[index])
# check if this score and the next score are equal
elif abc[index] == abc[index + 1]:
# if so, sort by name
result = sorted(abc[index:index + 2], key=lambda x:x)
# and print the names
print("{}\n{}".format(result, result))
else:
# print the name of the person with second lowest score
print("{}".format(abc[index]))
break;
``````

I am wondering why my code doesn’t perform that function. It needs to be generalized to pass all 10 tests on hackerrank. My code passes 9 out of the 10 and I can’t figure out how to make it pass this particular case.

I could write a separate if statement to catch this specific case but I want it to catch all scenarios on its own merit.

Like I said, I think it has to do with the second loop that’s nested but am unsure how to debug it without giving and indexing error.

i tried to see what i could with your code, but i end up writing my own. (see my reply i posted while you where typing )

i end up catching most corner cases with conditions which i tried to keep generic as possible.

that is the frustrating part about this challenge, then you think you are close because only one test case is missing, but then to solve it a nice way, you have to make changes in which you risk breaking other test cases.