Problem Solving with Algorithms

반응형

www.hackerrank.com/domains/python

 

 

해커랭크(hackerrank) 에서 파이썬 공부하기

 

 

 

 

// 깔끔하게 integer 나누기

/ 소수 나누기

 

 

n이 5일때, 12345 를 출력하기

if __name__ == '__main__':

    print(*range(1, int(input())+1), sep='')

 

 

>>> s = set("Hacker")

>>> print s.difference({"Rank":1})

set(['a', 'c', 'e', 'H', 'k', 'r'])

 

>>> s - set("Rank")

set(['H', 'c', 'r', 'e'])

 

 

To All Beginners Overthere!!

Don't try to copy the style of others . Shorten the code as long as you are able to understand that and always remember that writing a loop in a single line will never improve the time complexity.

So if you are a beginner first be comfortable with all the underlying concepts and then move ahead...

n1 = int(input())
set_1 = set(map(int,input().split()))
n2 = int(input())
set_2 = set(map(int,input().split()))
print(len(set_1-set_2))

 

 

 

코드를 좀 더 깔끔하게 해야할텐데

if __name__ == '__main__':
    first_names = []
    second_names = []
    first = float("inf")
    second = float("inf")
    for _ in range(int(input())):
        name = input()
        score = float(input())
        
        if score < first:
            second = first
            second_names = first_names
            first = score
            first_names = []
            first_names.append(name)
        elif score == first:
            first_names.append(name)
        elif score < second:
            second = score
            second_names = []
            second_names.append(name)
        elif score == second:
            second_names.append(name)
    
    for name in sorted(second_names):
        print(name)
        

 

 

더 깔끔하게

 

from collections import defaultdict
if __name__ == '__main__':
    first = float("inf")
    second = float("inf")
    table = defaultdict(list)
    for _ in range(int(input())):
        name = input()
        score = float(input())
        table[score].append(name)
        if score < first:
            second = first
            first = score
        elif score < second and score != first:
            second = score
    for name in sorted(table[second]):
        print(name)

 

 

 

12월 18일 여기까지...

인터뷰빗에 비하며 코스가 너무 긴것 같다.

반응형
반응형

공유하기

facebook twitter kakaoTalk kakaostory naver band