비트교육_단기과정
숫자 야구 Python ver
- -
import random total = 0 for exeCCount in range(1000): randomNum = map(str, random.sample(range(10), 3)) # 중복되지않는 숫자 3개를 목표 숫자로 지정 goalNum = ''.join(s for s in randomNum) # for i in range(len(randomNum)): # goalNum += str(i) Qlist = [] for i in range(10): # 질문할 경우의 수 720개 for j in range(10): if not (i == j): for n in range(10): if not (j == n or i == n): Qlist.append(str(i) + str(j) + str(n)) cntQuesiton = 0 strikeCnt = 0 ballCnt = 0 print('목표 숫자 : ', goalNum) while True: strikeIndex = [0, 0, 0] strikeCnt = 0 ballCnt = 0 temp = random.choice(Qlist) # 리스트에서 랜덤 수 1개 추출 for i in range(len(temp)): if goalNum[i] == temp[i]: strikeIndex[i] += 1 strikeCnt += 1 for i in range(len(goalNum)): for j in range(len(temp)): if temp[j] == goalNum[i]: ballCnt += 1 ballCnt -= strikeCnt print('CPU => ', temp) print('Strike : ', strikeCnt, ' Ball : ', ballCnt) first = temp[0] second = temp[1] third = temp[2] Qlist.remove(temp) if ballCnt >= 1 and strikeCnt == 0: temp = list(Qlist) # 질문리스트를 복사 for i in range(len(Qlist)): if first == Qlist[i][0]: temp.remove(Qlist[i]) Qlist = list(temp) temp = list(Qlist) # 질문리스트를 복사 for i in range(len(Qlist)): if second == Qlist[i][1]: temp.remove(Qlist[i]) Qlist = list(temp) temp = list(Qlist) # 질문리스트를 복사 for i in range(len(Qlist)): if third == Qlist[i][2]: temp.remove(Qlist[i]) Qlist = list(temp) if ballCnt == 0 and strikeCnt == 0: temp = list(Qlist) # 질문리스트를 복사 for i in range(len(Qlist)): for j in range(len(goalNum)): if first == Qlist[i][j]: temp.remove(Qlist[i]) Qlist = list(temp) temp = list(Qlist) # 질문리스트를 복사 for i in range(len(Qlist)): for j in range(len(goalNum)): if second == Qlist[i][j]: temp.remove(Qlist[i]) Qlist = list(temp) temp = list(Qlist) # 질문리스트를 복사 for i in range(len(Qlist)): for j in range(len(goalNum)): if third == Qlist[i][j]: temp.remove(Qlist[i]) Qlist = list(temp) if strikeCnt > 0: if strikeIndex[0] == 1: temp = list(Qlist) # 질문리스트를 복사 for i in range(len(Qlist)): if not (first == Qlist[i][0]): temp.remove(Qlist[i]) Qlist = list(temp) if strikeIndex[1] == 1: temp = list(Qlist) # 질문리스트를 복사 for i in range(len(Qlist)): if not (second == Qlist[i][1]): temp.remove(Qlist[i]) Qlist = list(temp) if strikeIndex[2] == 1: temp = list(Qlist) # 질문리스트를 복사 for i in range(len(Qlist)): if not (third == Qlist[i][2]): temp.remove(Qlist[i]) Qlist = list(temp) if strikeCnt == 1 and ballCnt == 0: if strikeIndex[0] == 1: temp = list(Qlist) # 질문리스트를 복사 for i in range(len(Qlist)): if not (first == Qlist[i][0]): temp.remove(Qlist[i]) Qlist = list(temp) temp = list(Qlist) # 질문리스트를 복사 for i in range(len(Qlist)): for j in range(1, 3): if (second == Qlist[i][j]): temp.remove(Qlist[i]) Qlist = list(temp) temp = list(Qlist) # 질문리스트를 복사 for i in range(len(Qlist)): for j in range(1, 3): if (third == Qlist[i][j]): temp.remove(Qlist[i]) Qlist = list(temp) if strikeIndex[1] == 1: temp = list(Qlist) # 질문리스트를 복사 for i in range(len(Qlist)): if not (second == Qlist[i][1]): temp.remove(Qlist[i]) Qlist = list(temp) temp = list(Qlist) # 질문리스트를 복사 for i in range(len(Qlist)): for j in range(0, 3, 2): if (first == Qlist[i][j]): temp.remove(Qlist[i]) Qlist = list(temp) temp = list(Qlist) # 질문리스트를 복사 for i in range(len(Qlist)): for j in range(0, 3, 2): if (third == Qlist[i][j]): temp.remove(Qlist[i]) Qlist = list(temp) if strikeIndex[2] == 1: temp = list(Qlist) # 질문리스트를 복사 for i in range(len(Qlist)): if not (third == Qlist[i][2]): temp.remove(Qlist[i]) Qlist = list(temp) temp = list(Qlist) # 질문리스트를 복사 for i in range(len(Qlist)): for j in range(2): if (first == Qlist[i][j]): temp.remove(Qlist[i]) Qlist = list(temp) temp = list(Qlist) # 질문리스트를 복사 for i in range(len(Qlist)): for j in range(2): if (second == Qlist[i][j]): temp.remove(Qlist[i]) Qlist = list(temp) print('경우의 수 ', len(Qlist)) cntQuesiton += 1 if strikeCnt == 3: print('총 횟수 : ', cntQuesiton) total += cntQuesiton print('\n\n') break print('1000번 시행 했을 떄 평균 횟수 : ', total // 1000) | cs |
'비트교육_단기과정' 카테고리의 다른 글
성적관리프로그램 파이썬 (0) | 2022.07.29 |
---|---|
원형 큐 실습 (0) | 2022.07.26 |
로또 당첨 프로그램 Python ver (0) | 2022.07.26 |
별 찍기 for문 while문 Python ver (0) | 2022.07.26 |
정렬알고리즘(버블, 선택, 삽입, 쉘 정렬) (0) | 2022.07.18 |
Contents
소중한 공감 감사합니다