본문 바로가기
프로그래밍 문제/[Python] CheckIO

[Check-IO] Sum by Type

by UltraLowTemp-Physics 2021. 12. 12.
728x90

[1] Problem: https://py.checkio.org/en/mission/sum-by-type/     
[2] My solution: 

def sum_by_types(items: list) -> Tuple[str, int]:
    # your code here
    first_arg = ''
    second_arg = 0 
    
    for var in items:
        if isinstance(var, str):
            first_arg = first_arg + var
        else:
            second_arg = second_arg + var
    
    return (first_arg, second_arg)

 

[3] Best solution

def sum_by_types(items):
    result = ['', 0]
    for item in items:
        result[isinstance(item, int)] += item
    return result

a. In our problem, the first and the second argument in the result are string and integer respectively. 
b. Making a list whose name is "result"
  - if the item is int, then isinstance(item, int) --> True ( or 1)
  - if the item is str, then isinstance(item, int) --> False ( or 0) 

728x90

'프로그래밍 문제 > [Python] CheckIO' 카테고리의 다른 글

[Check-IO] Follow Instructions  (0) 2021.12.18
[Check-IO] Common Words  (0) 2021.12.16
[Check-IO] Time converter  (0) 2021.12.10
[CheckIO] Goes Right After  (0) 2021.12.03
[Check-IO] Absolute Sorting  (0) 2021.11.14

댓글