본문 바로가기

collections5

1023. No Loops 2 - You only need one No Loops Allowed You will be given an array a and a value x. All you need to do is check whether the provided array contains the value, without using a loop. Array can contain numbers or strings. x can be either. Return true if the array contains the value, false if not. With strings you will need to account for case. Looking for more, loop-restrained fun? Check out the other kata in the series:.. 2022. 10. 23.
Find the stray number You are given an odd-length array of integers, in which all of them are the same, except for one single number. Complete the method which accepts such an array, and returns that single different number. The input array will always be valid! (odd-length >= 3) Examples [1, 1, 2] ==> 2 [17, 17, 3, 17, 17, 17, 17] ==> 3 Solution: def stray(arr): import collections return [k for k, v in collections.C.. 2022. 8. 19.
Find the odd int Description: Given an array of integers, find the one that appears an odd number of times. There will always be only one integer that appears an odd number of times. Examples [7] should return 7, because it occurs 1 time (which is odd). [0] should return 0, because it occurs 1 time (which is odd). [1,1,2] should return 2, because it occurs 1 time (which is odd). [0,1,0,1,0] should return 0, beca.. 2022. 3. 30.
Python _ 리스트 요소 개수 세기(dictionary, collections) 리스트에 어떤 요소의 개수를 파악해야하는 때가 있습니다. (특히 코딩테스트에서 사용할 일이 많습니다) 그래서 오늘은 요소 개수를 세는 방법을 정리해보고자 합니다. dictionary 사용 # 요소를 세고 싶은 리스트 >>> list = [1,2,3,4,5,5,5,5,5,1,1] # 빈 딕셔너리를 생성 >>> dict = {} # 요소가 딕셔너리에 있다면 += 1, 없다면 = 1 >>> for num in list: if num in dict: # dict.get(num)로 대체가능 dict[num] += 1 else: dict[num] = 1 # {요소(키): 개수(값)} 형태의 딕셔너리 생성 >>> dict {1: 3, 2: 1, 3: 1, 4: 1, 5: 5} 기본 딕셔너리를 사용하는 베이직한 방법입.. 2022. 3. 6.
문자열 내 p와 y의 개수 문제 설명 대문자와 소문자가 섞여있는 문자열 s가 주어집니다. s에 'p'의 개수와 'y'의 개수를 비교해 같으면 True, 다르면 False를 return 하는 solution를 완성하세요. 'p', 'y' 모두 하나도 없는 경우는 항상 True를 리턴합니다. 단, 개수를 비교할 때 대문자와 소문자는 구별하지 않습니다. 예를 들어 s가 "pPoooyY"면 true를 return하고 "Pyy"라면 false를 return합니다. 제한 사항 문자열 s의 길이 : 50 이하의 자연수 문자열 s는 알파벳으로만 이루어져 있습니다. 해결 방법 1. s를 대문자로 만들어준다. 2. P와 Y의 개수를 각각 구한다. 3. 개수가 서로 일치하는지 확인한다. def solution(s): s = s.upper() p_cn.. 2022. 2. 2.