SELECT
DISTINCT city
FROM
station
WHERE
id%2=0
;
→ point: exclude duplicates from the answer
N % M
= 0MOD(N,M)
= 0N MOD M
= 0SELECT
COUNT(city)-COUNT(DISTINCT city)
FROM
station
;
(
SELECT
city
, LENGTH(city)
FROM
station
ORDER BY
LENGTH(city)
, city
LIMIT 1
)
UNION ALL
(
SELECT
city
, LENGTH(city)
FROM
station
ORDER BY
LENGTH(city) DESC
, city
LIMIT 1
)
;
def solution(cards1, cards2, goal):
answer = ''
for word in goal:
if len(cards1) != 0 and word == cards1[0]:
cards1.pop(0)
elif len(cards2) != 0 and word == cards2[0]:
cards2.pop(0)
else:
answer='No'
break
answer='Yes'
return answer
from collections import deque
def solution(cards1, cards2, goal):
answer = 'Yes'
cards1 = deque(cards1)
cards2 = deque(cards2)
for i in goal:
if (len(cards1) != 0) and (i == cards1[0]):
cards1.popleft()
elif (len(cards2) != 0) and (i == cards2[0]):
cards2.popleft()
else:
answer = 'No'
break
return answer
from collections import deque
answer = 'Yes'
cards1 = deque(cards1) cards2 = deque(cards2)
for i in goal
if (len(cards1) != 0) and (i == cards1[0])
cards1.popleft()
elif (len(cards2) != 0) and (i == cards2[0])
cards2.popleft()
else
answer = 'No'
break
def solution(cards1, cards2, goal):
answer = []
n = len(cards1)
m = len(cards2)
i = j = 0
for word in goal:
if i < n and word == cards1[i]:
answer.append(cards1[i])
i += 1
if j < m and word == cards2[j]:
answer.append(cards2[j])
j += 1
return 'Yes' if answer == goal else 'No'
from collections import deque
def solution2(cards1, cards2, goal):
cards1 = deque(cards1)
cards2 = deque(cards2)
for word in goal:
if card1 and word == cards1[0]: cards1.popleft()
if card2 and word == cards2[0]: cards2.popleft()
else: return 'No'
return 'Yes'
cards1[0] 또는 cards2[0]에 접근하려면 cards1 또는 cards2가 빈 리스트가 아닌 상태여야 한다.
단순히 if i == cards1[0]만 작성하면 cards1에 요소가 하나도 없을 때 IndexError가 발생하게 된다.
그렇기 때문에 리스트에 값이 존재하는 경우에 True가 되므로 그때 [0]번 인덱스와 비교하는 것
def solution(cards1, cards2, goal):
for i in goal:
if cards1 and i == cards1[0]:
cards1.pop(0)
elif cards2 and i == cards2[0]:
cards2.pop(0)
else:
return 'No'
return 'Yes'
cards1 = ["i", "drink", "water"], cards2 = ["want", "to"], goal = ["i", "want", "to", "drink", "water"]이면 goal의 요소를 for문으로 하나씩 불러온다.
'i'가 cards1의 0번째 요소와 같으므로 cards1의 0번째 요소인 'i'를 지우고 다음 요소를 비교한다. → cards1 = ["drink", "water"], cards2 = ["want", "to"]
'want'는 cards2의 0번째 요소와 같으므로 cards2의 0번째 요소인 'want'를 지우고 다음 요소를 비교한다. → cards1 = ["drink", "water"], cards2 = ["to"]
'to'는 cards2의 0번째 요소와 같으므로 cards2의 0번째 요소인 'to'를 지우고 다음 요소를 비교한다. → cards1 = ["drink", "water"], cards2 = []
'drink'는 cards1의 0번째 요소와 같으므로 cards1의 0번째 요소인 'drink'를 지우고 다음 요소를 비교한다. → cards1 = ["water"], cards2 = []
'water'는 cards1의 0번째 요소와 같으므로 cards1의 0번째 요소인 'water'를 지우고 반복문을 마친다. → cards1 = [], cards2 = []
for문을 문제없이 마무리하면 Yes를 리턴한다.
하지만, 중간에 cards1 또는 cards2의 0번째 요소와 다르다면 순서대로 만들어진 문장이 아니므로 No를 리턴한다.
def solution(cards1, cards2, goal):
for g in goal:
if len(card1)>0 and g == cards1[0]:
cards1.pop(0)
elif len(card1)>0 and g == cards2[0]:
cards2.pop(0)
else:
return 'No'
return 'Yes'
※ TypeError: 'builtin_function_or_method' object is unsubscriptable