Hanwool Codes RSS Tag Admin Write Guestbook
전체 글 (45)
2023-01-29 22:03:44

Find the Runner-Up Score! | HackerRank

 

Find the Runner-Up Score! | HackerRank

For a given list of numbers, find the second largest number.

www.hackerrank.com

 

Goal

 

1. Give n scores

2. Store them in a list

3. find the score of the runner-up

 

so, you should print the runner-up score.

 

Solution

 

You can convert map into list

After then sort asending order and then convert list to set because you need to escape duplicate scores

You can print runner-up score with index -2 because negative indexing is used in Python to begins slicing from the end of list. So in our case, you need to read index -2.

# Find the Runner-Up Score!

if __name__ == '__main__':
    n = int(input())
    arr = map(int, input().split())
    
    list_arr = list(arr)
    list_arr.sort()
    
    convert_arr = list(set(list_arr))
    
    print(convert_arr[-2])
2023-01-28 05:21:45

List Comprehensions | HackerRank

 

List Comprehensions | HackerRank

You will learn about list comprehensions.

www.hackerrank.com

 

Goal

 

Print a list of all possible coordinates given by (i, j, k) on a 3D grid where the sum of i + j + k is not equal to n.

 

 

Solution

You can simply make a condition statement {(i + j + k) != n} to append all (i,j,k) combinations into list and print the list. 

 

There are two simple ways to solve this problem. You can use simply multiple loops, but you can use easily loop comprehensions. 

 

1) The for statement

 

# List Comprehensions

if __name__ == '__main__':
    x = int(input())
    y = int(input())
    z = int(input())
    n = int(input())

    results = []

    for i in range( x + 1):
        for j in range( y + 1):
            for k in range(z + 1):
                if (i + j + k) != n:
                    results.append([i, j, k])


    print(results)

 

2) List Comprehension

# List Comprehensions

if __name__ == '__main__':
    x = int(input())
    y = int(input())
    z = int(input())
    n = int(input())

    results = [[i,j,k] for i in range(x+1) for j in range(y+1) for k in range(z+1) if i+j+k != n]

    print(results)
2023-01-27 04:59:45

Print Function | HackerRank

 

Print Function | HackerRank

Learn to use print as a function

www.hackerrank.com

 

Goal

 

Print the list of integers from 1 through give n as a string, without spaces

 

 

Solution

 

To solve this problem, you can use for statement and str class to convert integer value to string object. 

 

# Print Function

if __name__ == '__main__':
    n = int(input())
    
    number = ""
    for i in range(1, n+1, 1):
        number += str(i)
        
    print(int(number))


Hanwool Codes. Designed by 코딩재개발.