https://www.codewars.com/kata/523f5d21c841566fde000009/train/python
Codewars - Achieve mastery through coding practice and developer mentorship
A coding practice website for all programming levels – Join a community of over 3 million developers and improve your coding skills in over 55 programming languages!
www.codewars.com
랭크업 이라는 단계가 있길래 눌러봤더니 6 kyu 문제가 나왔다.
두개의 배열을 파라미터로 받고, 첫번째 배열과 두번째 배열의 같은 엘리먼트를 모두 빼고 남은 엘리먼트를 return 하면 되는거 같다.
검색을 좀 해서, python에도 not in 이라는게 있고, 앞서 숏코딩을 하던 사람들이 이용하던게
List 내 For 문 형태라는걸 알았다.
[실행문 + for i in (rang or list) + 조건문] 이런식으로 쓴다고 한다.
그래서 비슷하게 만들어봤다.
def array_diff(arrA, arrB):
return [a for a in arrA if a not in arrB]
제일 잘 쓴 답은 나와 같았고,
이런 답도 찾았다. 자바스크립트의 set 함수랑은 좀 달라보이는데.
def array_diff(a, b):
return [x for x in a if x not in set(b)]
찾아보니 집합 자료형을 만드는거라고한다.
print(set([1,2,3,4,5])) # {1, 2, 3, 4, 5}
왜 이걸했나 했더니 그냥 arrB를쓰면, arrB의 크기에 따라서 연산이 늘어난다.
하지만 이걸 set으로 해시 테이블로 변형하면, 크기에 상관없이 작업속도가 일정하다.
'알고리즘' 카테고리의 다른 글
[CodeWars] Square Every Digit - 7Kyu (0) | 2024.12.05 |
---|---|
[CodeWars] Does my number look big in this?- 6Kyu (0) | 2024.12.04 |
[CodeWars] Rock Paper Scissors! - 8Kyu (0) | 2024.12.04 |
[CodeWars] Counting sheep... - 8Kyu (1) | 2024.12.04 |
[CodeWars] Count by X - 8Kyu (0) | 2024.11.30 |