-
카테고리
-
세부 분야
프로그래밍 언어
-
해결 여부
미해결
퀴즈 4 다른 방법으로 풀었는데요
20.04.25 16:50 작성 조회수 92
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("-- 축하합니다--")
이렇게 풀어도 맞는 건가요?
답변을 작성해보세요.
0
나도코딩
지식공유자2020.04.29
안녕하세요
답변이 늦어 죄송합니다.
set 과 차집합을 이용하는 것은 매우 좋은 접근입니다.
하지만 아래 코드에서는 각각 독립사건으로 sample 을 하기 때문에, 동일한 당첨자가 중복되어 나올 위험이 있습니다.
print(set(sample(chicken,1)))
print(set(sample(coffe,3)))
my_list 와 my_list2 을 1, 2, 3, 4, 5 정도의 작은 값으로 설정해두면 아마 중복 상황이 금방 발견되실 거에요
이 부분을 더 보완하시면 좋겠습니다. ^^
감사합니다.
답변 1