https://www.hackerrank.com/challenges/between-two-sets/problem?isFullScreen=true
Between Two Sets | HackerRank
Find the number of integers that satisfies certain criteria relative to two sets.
www.hackerrank.com
위 문제를 푸는데 난 아래와 같은 솔루션을 만들었다.
def getTotalX(a, b):
# Write your code here
cnt = 0
for i in range(a[-1], b[0]+1):
flag = 1
for j in range(len(a)):
if i%a[j] != 0:
flag = 0
break
else:
for k in range(len(b)):
if b[k]%i != 0:
flag = 0
break
if flag == 0:
break
if flag == 1:
cnt += 1
return cnt
그런데 이렇게 간단히 쓸수 있더라..
def getTotalX(a, b):
# Write your code here
ans=0
for i in range(a[-1], b[0]+1):
if all(i%x==0 for x in a) and all(x%i==0 for x in b):
ans+=1
return ans
푸는 원리는 같은데, all()을 아냐 마나의 차이이다.
all을 안 쓰면 나처럼 flag만들어서 복잡하게 풀어야한다.
all과 any함수 잘 알아두자!
'코딩 연습 > Python' 카테고리의 다른 글
[leetcode] 56. Merge Intervals (0) | 2022.03.14 |
---|---|
[leetcode] 121. Best Time to Buy and Sell Stock (0) | 2022.03.14 |
백준_9020: 골드바흐의 추측 (0) | 2022.02.05 |
백준_11508: 2+1 세일 (0) | 2022.02.03 |
백준_11657: 타임머신 (0) | 2022.01.30 |