코딩테스트/코딩테스트 Python

[CodeSignal] Collections Truthness (Python)

sseozytank 2022. 9. 2.

https://app.codesignal.com/arcade/python-arcade/meet-python/H9raD7Bdb3D2847f9

 

Collections Truthness | CodeSignal

What will be the value of res after the following snippet is executed: xs = [()] res = [False] * 2 if xs: res[0] = True if xs[0]: res[1] = True

app.codesignal.com

 

✔️문제 요약 

- 다음 코드를 거친 후 value 값으로 올바른 것을 고르시오

xs = [()]
res = [False] * 2
if xs:
	res[0] = True
if xs[0]:
	res[1] = True

 

✔️풀이 방법 

res=[True,False]

if xs: 라는 것은 값이 있으면 True, 값이 없으면 False를 출력해야 하는데, 

if xs: 는 리스트안에 () 튜플이라는 요소가 있기 때문에 True 이고 ,

if xs[0]: 은 튜플 안에 아무것도 없기 때문에 False 이다.

 

따라서 답은 True, False

    

 

댓글