본문 바로가기

isinstance5

0207. Divide and Conquer Given a mixed array of number and string representations of integers, add up the non-string integers and subtract this from the total of the string integers. Return as a number. Solution: def div_con(arr): x = sum(i for i in arr if isinstance(i, int)) y = sum(int(i) for i in arr if isinstance(i, str)) return x - y 2023. 2. 8.
0129. Super Duper Easy Make a function that returns the value multiplied by 50 and increased by 6. If the value entered is a string it should return "Error". Solution: problem = lambda x: "Error" if isinstance(x, str) else x * 50 + 6 2023. 1. 29.
0128. Gauß needs help! (Sums of a lot of numbers). Due to another of his misbehaved, the primary school's teacher of the young Gauß, Herr J.G. Büttner, to keep the bored and unruly young schoolboy Karl Friedrich Gauss busy for a good long time, while he teaching arithmetic to his mates, assigned him the problem of adding up all the whole numbers from 1 through a given number n. Your task is to help the young Carl Friedrich to solve this problem .. 2023. 1. 28.
List Filtering In this kata you will create a function that takes a list of non-negative integers and strings and returns a new list with the strings filtered out. Example filter_list([1,2,'a','b']) == [1,2] filter_list([1,'a','b',0,15]) == [1,0,15] filter_list([1,2,'aasf','1','123',123]) == [1,2,123] Solution: def filter_list(l): return [i for i in l if isinstance(i, int)] 2022. 8. 11.
Python _ isinstance로 타입을 체크하자. 어떤 객체가 무슨 타입인지를 알려면 type() 메서드를 활용하면 됩니다. 하지만 어떤 타입이 맞는지 참/거짓으로 체크만 하고 싶다면 isinstance()를 활용할 수 있습니다. 사용법은 간단합니다. isinstance 메서드에 첫 번째 인자로 해당 객체를, 두번째 인자로 체크하고 싶은 타입을 넣어주면 됩니다. def 체크_문자열(객체): return isinstance(객체, str) print(체크_문자열('문자')) # >>> True print(체크_문자열(123)) # >>> False 문자열을 넣었을 때는 True, 숫자를 넣었을 때는 False를 반환합니다. def 체크_리스트(객체): return isinstance(객체, list) print(체크_리스트('문자')) # >>> Fals.. 2022. 3. 30.