Hanwool Codes RSS Tag Admin Write Guestbook
전체 글 (45)
2023-02-02 05:26:03

Lists | HackerRank

 

Lists | HackerRank

Perform different list operations.

www.hackerrank.com

 

Goal

 

read different commands, operate commands and print the results.

 

 

Solution

 

you can create if statement to operate each command.

But important is to read the values after commands.

I use *line to read values.

 

# Lists

if __name__ == '__main__':
    N = int(input())
    
    results = []
    
    for _ in range(N):
        name, *line = input().split()
        
        if name == "insert":
            results.insert(int(line[0]), int(line[1]))
        
        elif name == "append":
            results.append(int(line[0]))
            
        elif name == "sort":
            results.sort()
            
        elif name == "print":
            print(results)
        
        elif name == "pop":
            results.pop()
            
        elif name == "remove" :
            results.remove(int(line[0]))
            
        else: #name == reverse
            results.reverse()
2023-02-01 04:46:40

Finding the percentage | HackerRank

 

Finding the percentage | HackerRank

Store a list of students and marks in a dictionary, and find the average marks obtained by a student.

www.hackerrank.com

 

Goal

 

Print the average of the marks array for the given student name and showing 2 places after the decimal.

 

 

Solution

 

First, we need to get average score from the give student marks.

Secondly, we need to print out only 2 places after the decimal.

I use {:.2f} format to show 2 places after the decimal. But there are other ways too.

 

# Finding the percentage

if __name__ == '__main__':
    n = int(input())
    student_marks = {}
    for _ in range(n):
        name, *line = input().split()
        scores = list(map(float, line))
        student_marks[name] = scores
    query_name = input()
    
    query_scores = [x for x in student_marks[query_name]]
    
    print("{:.2f}".format(float(sum(query_scores)) / len(query_scores)))

 

2023-01-31 05:30:54

Nested Lists | HackerRank

 

Nested Lists | HackerRank

In a classroom of N students, find the student with the second lowest grade.

www.hackerrank.com

Goal

 

Given the names and grades for each student in a class of  N students, store them in a nested list and print the name(s) of any student(s) having the second lowest grade.

 

Solution

 

I use two lists to solve this problem. One list is for student name and score. The other list is only for score.

So, we need to sort score list and then find second lowest score.

After then, we sort student by name and find students which have second lowest score and print them alphabetically

 

# Nested Lists

if __name__ == '__main__':
    
    students = []
    scores = []
    
    for _ in range(int(input())):
        name = input()
        score = float(input())
        
        students.append([name, score])
        scores.append(score)
        
    scores.sort()
    mini = scores[0]
    second_min = 0
    students.sort()
    
    for score in scores:
        if score != mini:
            second_min = score
            break
        
    
    for student in students:
        if student[1] == second_min:
            print(student[0])


Hanwool Codes. Designed by 코딩재개발.