인프런 커뮤니티 질문&답변

노빠꾸님의 프로필 이미지

작성한 질문수

파이썬 무료 강의 (기본편) - 6시간 뒤면 나도 개발자

퀴즈 #4

퀴즈 4 다른 방법으로 풀었는데요

20.04.25 16:50 작성

·

105

0

my_list = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
my_list2 = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
from random import *

chicken = my_list
coffe = my_list2

# print(type(coffe),type(chicken))

print(chicken)
print(coffe)

shuffle(chicken)
shuffle(coffe)

print(chicken)
print(coffe)

print(set(sample(chicken,1)))
print(set(sample(coffe,3)))

loto1=set(sample(chicken,1))
loto2=set(sample(coffe,3))

print(loto1 - loto2)

loto3 = (loto1 - loto2)

# print((loto1 - loto2),loto2)

print"""
-- 당첨자 발표 --
""")

print("치킨 당첨자 " + str(loto3))
print("커피 당첨자 " + str(loto2))

print("-- 축하합니다--")
이렇게 풀어도 맞는 건가요?

답변 1

0

나도코딩님의 프로필 이미지
나도코딩
지식공유자

2020. 04. 29. 21:02

안녕하세요

답변이 늦어 죄송합니다.

set 과 차집합을 이용하는 것은 매우 좋은 접근입니다.

하지만 아래 코드에서는 각각 독립사건으로 sample 을 하기 때문에, 동일한 당첨자가 중복되어 나올 위험이 있습니다.

print(set(sample(chicken,1)))
print(set(sample(coffe,3)))

my_list 와 my_list2 을 1, 2, 3, 4, 5 정도의 작은 값으로 설정해두면 아마 중복 상황이 금방 발견되실 거에요

이 부분을 더 보완하시면 좋겠습니다. ^^

감사합니다.