Nested Lists in Python - Hacker Rank Solution
Problem
Tutorial :
Let's implement a nested list! A nested list is a list that contains another list (i.e.: a list of lists). For example:nested_list = [['blue', 'green'], ['red', 'black'], ['blue', 'white']] print len(nested_list) # prints 3 print nested_list[1] # prints ['red', 'black'] print nested_list[1][0] # prints red
To go
through every element in a list, use a nested for loop.
Given the
names and grades for each student in a Physics class of N students, store them
in a nested list and print the name(s) of any student(s) having the second
lowest grade.
Note: If there are multiple students with the same grade, order their names
alphabetically and print each name on a new line.
Input Format :
The 2N subsequent lines describe each student over 2 lines; the first line
contains a student's name, and the second line contains their grade.
Constraints :
- 2 <= N <=5
- There will always be one or more students having the second lowest grade.
Output Format :
Print the name(s) of any student(s) having the second lowest grade in Physics;
if there are multiple students, order their names alphabetically and print
each one on a new line.
Sample Input :
5 Harry 37.21 Berry 37.21 Tina 37.2 Akriti 41 Harsh 39
Sample Output :
Berry Harry
Explanation :
The lowest grade of 37.2 belongs to Tina. The second lowest grade of 37.21
belongs to both Harry and Berry, so we order their names alphabetically and
print each name on a new line.
Solution :
1 2 3 4 5 6 7 8 9 10 11 12 | # Nested Lists in Python - Hacker Rank Solution |
Disclaimer :-
the above hole problem statement is given by hackerrank.com but the solution is generated by the codeworld19 authority if any of the query regarding this post or website fill the following contact form thank you.
it was showing me some error
this problem solution are in python2. you have need to select python2 editor on hackerrank site then it will be correct. i think you have try to run it on python3 that's why they have show an error.
sorry for late replay......
Thank You
Use \n ,not /n .
please solve this question in python 3
and also try to solve all the question in python 3
score_list = []
for _ in range(int(input())):
name = input()
score = float(input())
score_list.append([name, score])
second_highest = sorted(set([score for name, score in score_list]))[1]
print('\n'.join(sorted([name for name, score in score_list if score == second_highest])))
all same only use input. raw_input for python 2 and input for python3
if __name__ == '__main__':
records=[]
scores=[]
final=[]
n=int(input())
for _ in range(n):
name = input()
score = float(input())
records.append([name, score])
scores.append(score)
scores.sort()
for i in range(len(scores)):
if scores[i]<scores[i+1]:
x=scores[i+1]
break
for i in range(n):
if records[i][1]==x:
final.append(records[i][0])
final.sort()
for j in range(len(final)):
print(final[j])
my code is kinda big but can be understood by newbies
Yes im totally confused with the small code they have given.
that's right
Your code doesnt fulfill two test cases
if __name__ == '__main__':
res=[]
score_1=[]
name_1=[]
final=[]
n=int(input())
for i in range(n):
name = input()
score = float(input())
res.append([name,score])
name_1.append(name)
score_1.append(score)
score_1.sort()
temp=score_1[1]
for i in res:
if i[1]==temp:
final.append(i[0])
final.sort()
for i in final:
print(i)
Score_1[] ko sort karne se pahle
score_1=list(set(score_1))
Karo jisse duplicate remove ho jayenge to hame temp sahi milega sakhi condition me
if __name__ == '__main__':
score_list=[]
sc=[]
for _ in range(int(input())):
name = input()
score = float(input())
score_list.append([name,score])
sc.append(score)
sc=list(set(sc))
sc.sort()
score_list.sort()
sh = sc[1]
for name,score in score_list:
if score==sh:
print(name)
your code is very clearly understandable for a newbie
Thank you very much
what is __name__ = '__main__'
Entry point for the program
Thank You guys for giving alternate code in comment section.
This comment has been removed by the author.
if __name__ == '__main__':
scoreList = []
listx = []
for _ in range(int(input())):
name = input()
score = float(input())
scoreList.append(score)
listx.append([name,score])
scoreList= list(set(scoreList))
scoreList.sort()
secondLast=scoreList[1]
listx.sort()
for x in listx:
if (x[1] == secondLast ):
print(x[0])
Check this:
FOR Python 3:
if __name__ == '__main__':
L = []
scoreL = []
for _ in range(int(input())):
name = input()
score = float(input())
scoreL.append(score)
subL = [score,name]
L.append(subL)
scoreL.sort()
L.sort()
for i in range(len(scoreL)):
if scoreL[i] > scoreL[0]:
sg = scoreL[i]
break
for p in L:
if sg in p:
print(p[1])
What does "score for name" and "name for name" mean
records = []
scores = []
if __name__ == '__main__':
for _ in range(int(input())):
name = input()
score = float(input())
records.append([name, score])
scores.append(score)
second_highest = sorted(set(scores))[1]
names = []
for record in records:
if second_highest in record:
names.append(record[0])
names.sort()
print('\n'.join (names))
What does this " print('\n'.join (names)) " means ?
Its means join \n in your name .The output is : #for \n we get the in the new line
name
Thank you very much... Good Article!!
Python Online Course
Python Online Training in India
if __name__ == '__main__':
res=[]
score_1=[]
name_1=[]
final=[]
for _ in range(int(raw_input())):
name = raw_input()
score = float(raw_input())
res.append([name,score])
name_1.append(name)
score_1.append(score)
score_1.sort()
c=[]
for i in score_1:
if i>score_1[0]:
c.append(i)
c.sort()
temp=c[0]
for i in res:
if i[1]==temp:
final.append(i[0])
final.sort()
for i in final:
print(i)
my_dict = {}
for _ in range(int(input())):
name = input()
score = float(input())
my_dict[name] = score
for key, value in my_dict.items():
if sorted(my_dict.values())[2] == value:
print(key)
n=int(input())
a=[]
d=[]
e=[]
for i in range(0,n):
b=[]
name=input()
b.append(name)
score=(float(input()))
b.append(score)
a.append(b)
def Sort(a):
a.sort(key =lambda x:x[0])
a.sort(key=lambda x:x[1])
return a
b=Sort(a)
for i in range(n):
if b[i][1]>b[0][1]:
d.append(b[i])
break
for i in range(0,n):
if d[0][1]==a[i][1]:
e.append(a[i][0])
for i in range(len(e)):
print(e[i])
In the line of sorted_score = sorted(set([score for name, score in score_list]))[1]...
What is the use of (1) in the statement
if __name__ == '__main__':
list1=[]
for _ in range(int(input())):
name = input()
score = float(input())
list1.append([name,score])
x=list1[1][1]
list2=[]
n=len(list1)
for j in range(n):
if x==list1[j][1] :
list2.append(list1[j][0])
list2.sort(key=lambda x:x[0])
for i in list2:
print(i)
hope this help:
if __name__ == '__main__':
data=[]
for _ in range(int(input())):
name = input()
score = float(input())
data.append([name,score])
data.sort(key = lambda x :x [1]) #Sort the value from the largest to smallest
min_value = min(data, key = lambda item : item[1])
second_min_value= 0
for k in range(len(data)):
if data[k][1] > min_value[1]:
second_min_value = data[k][1]
break
#rearrage the alphabet
all_value=[]
for j in range(len(data)):
if second_min_value == data[j][1] :
all_value.append(data[j][0])
all_value.sort(key=lambda x:x[0])
for i in all_value:
print(i)