파이썬 연습¶

PizzBuzz 찍기¶

** 규칙
1에서 100까지 출력
3의 배수는 Fizz 출력
5의 배수는 Buzz 출력
3과 5의 공배수는 FizzBuzz 출력

숫자 맞추기 게임¶

** 규칙
1~100사이의 Random한 값 1개 맞추기
맞출때까지 계속 진행
몇번만에 맞췄는 지 출력

숫자 찍기¶

11111
22222
33333
44444
55555

숫자 찍기¶

1  2  3  4  5 
6  7  8  9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25

별 찍기¶

*****
*****
*****
*****
*****

별 찍기¶

*
**
***
****
*****

별 찍기¶

*****
****
***
**
*

별 찍기¶

     *
    ***
   *****
  *******
 *********

별 찍기¶

 *********
  *******
   *****
    ***
     *

별 찍기¶

     *
    ***
   *****
  *******
 *********
***********
 *********
  *******
   *****
    ***
     *

주사위 눈 구하기¶

** 규칙
두 개의 주사위를 던졌을 때 눈의 합이 6이 되는 모든 경우의 수

2x+4y=10 방정식 풀기¶

** 규칙
2x+4y=10 방정식의 해  x y, 
범위 0<=x<=10, 0<=y<=10

정렬(버블 정렬)¶

리스트에 Random 한 0 ~ 9의 숫자를 10개 넣어서 정렬하기(오름차순)

로또번호 뽑기¶

set 자료형을 사용해서 로또번호 6개 (1~45, 중복불가) 뽑기
1장 뽑기
5장 뽑기
In [56]:
import random

lottoOne = set()
while True:
    lottoOne.add(random.randint(1,45))
    if len(lottoOne) == 6:
        break

lottoOne
Out[56]:
{16, 25, 28, 30, 38, 41}
In [63]:
import random

lottoOne = set()
lottoFive = list()
while True:
    lottoOne.add(random.randint(1,45))
    if len(lottoOne) == 6:
        lottoFive.append(lottoOne)
        lottoOne = set()
    if len(lottoFive) == 5:
        break

lottoFive
Out[63]:
[{1, 2, 3, 22, 25, 32},
 {6, 16, 25, 33, 40, 43},
 {14, 15, 25, 32, 35, 37},
 {5, 6, 9, 18, 23, 32},
 {3, 19, 29, 33, 37, 40}]

로또번호 뽑기¶

list 자료형을 사용해서 로또번호 6개 (1~45, 중복불가) 뽑기
1장 뽑기
5장 뽑기
In [64]:
import random

lottoBox = []
for i in range(45):
    lottoBox.append(i+1)

for i in range(777):
    shuffle = random.randint(0,44)
    tmp = lottoBox[0]
    lottoBox[0] = lottoBox[shuffle]
    lottoBox[shuffle] = tmp
    
lottoOne = lottoBox[:6]
lottoOne
Out[64]:
[36, 25, 4, 30, 44, 13]
In [65]:
import random

lottoBox = []
for i in range(45):
    lottoBox.append(i+1)

lottoFive = []
while True:
    for i in range(777):
        shuffle = random.randint(0,44)
        tmp = lottoBox[0]
        lottoBox[0] = lottoBox[shuffle]
        lottoBox[shuffle] = tmp
    lottoOne = lottoBox[:6]
    lottoOne.sort()
    lottoFive.append(lottoOne)
    if len(lottoFive) == 5:
        break
lottoFive
Out[65]:
[[6, 19, 20, 28, 33, 37],
 [15, 18, 25, 30, 38, 40],
 [2, 3, 15, 18, 24, 44],
 [10, 18, 25, 26, 32, 35],
 [8, 14, 24, 31, 33, 36]]

답¶

In [4]:
for i in range(1,6):
    print('*'*i)
*
**
***
****
*****
In [5]:
for i in range(5,0,-1):
    print('*'*i)
*****
****
***
**
*
In [24]:
end = 9
for i in range(1,10,2):
    space = end // 2
    print(space*' ','*'*i, sep='')
    end -= 2
    *
   ***
  *****
 *******
*********
In [21]:
start = 1
for i in range(9,0,-2):
    space = start // 2
    print(space*' ','*'*i, sep='')
    start += 2
*********
 *******
  *****
   ***
    *
In [22]:
end = 11
end2 = end
for i in range(1,end+1,2):
    space = end // 2
    print(space*' ','*'*i, sep='')
    end -= 2
start = 3
for i in range(end2-2,0,-2):
    space = start // 2
    print(space*' ','*'*i, sep='')
    start += 2
     *
    ***
   *****
  *******
 *********
***********
 *********
  *******
   *****
    ***
     *
In [52]:
import random

listNum = []
for i in range(10):
    listNum.append(random.randint(0,9))
In [36]:
listNum
Out[36]:
[6, 6, 3, 6, 2, 0, 1, 0, 3, 3]
In [37]:
for i in range(9):
    if listNum[i] > listNum[i+1]:
        tmp = listNum[i]
        listNum[i] = listNum[i+1]
        listNum[i+1] = tmp
listNum
Out[37]:
[6, 3, 6, 2, 0, 1, 0, 3, 3, 6]
In [42]:
print(listNum)
for j in range(9):
    for i in range(9):
        if listNum[i] > listNum[i+1]:
            tmp = listNum[i]
            listNum[i] = listNum[i+1]
            listNum[i+1] = tmp
    print(listNum)
[0, 1, 2, 6, 7, 8, 8, 8, 9, 9]
[0, 1, 2, 6, 7, 8, 8, 8, 9, 9]
[0, 1, 2, 6, 7, 8, 8, 8, 9, 9]
[0, 1, 2, 6, 7, 8, 8, 8, 9, 9]
[0, 1, 2, 6, 7, 8, 8, 8, 9, 9]
[0, 1, 2, 6, 7, 8, 8, 8, 9, 9]
[0, 1, 2, 6, 7, 8, 8, 8, 9, 9]
[0, 1, 2, 6, 7, 8, 8, 8, 9, 9]
[0, 1, 2, 6, 7, 8, 8, 8, 9, 9]
[0, 1, 2, 6, 7, 8, 8, 8, 9, 9]
In [44]:
print(listNum)
for j in range(9):
    for i in range(9-j):
        if listNum[i] > listNum[i+1]:
            tmp = listNum[i]
            listNum[i] = listNum[i+1]
            listNum[i+1] = tmp
    print(listNum)
[4, 8, 5, 6, 9, 9, 3, 8, 5, 7]
[4, 5, 6, 8, 9, 3, 8, 5, 7, 9]
[4, 5, 6, 8, 3, 8, 5, 7, 9, 9]
[4, 5, 6, 3, 8, 5, 7, 8, 9, 9]
[4, 5, 3, 6, 5, 7, 8, 8, 9, 9]
[4, 3, 5, 5, 6, 7, 8, 8, 9, 9]
[3, 4, 5, 5, 6, 7, 8, 8, 9, 9]
[3, 4, 5, 5, 6, 7, 8, 8, 9, 9]
[3, 4, 5, 5, 6, 7, 8, 8, 9, 9]
[3, 4, 5, 5, 6, 7, 8, 8, 9, 9]
In [53]:
print(listNum)
for j in range(9):
    isChange = False
    for i in range(9-j):
        if listNum[i] > listNum[i+1]:
            tmp = listNum[i]
            listNum[i] = listNum[i+1]
            listNum[i+1] = tmp
            isChange = True
    print(listNum,isChange)
    if not isChange:
        break
    
[8, 9, 5, 4, 3, 0, 3, 8, 9, 9]
[8, 5, 4, 3, 0, 3, 8, 9, 9, 9] True
[5, 4, 3, 0, 3, 8, 8, 9, 9, 9] True
[4, 3, 0, 3, 5, 8, 8, 9, 9, 9] True
[3, 0, 3, 4, 5, 8, 8, 9, 9, 9] True
[0, 3, 3, 4, 5, 8, 8, 9, 9, 9] True
[0, 3, 3, 4, 5, 8, 8, 9, 9, 9] False
In [ ]: