Python programming exercise for beginners

Tweet


In order to learn Python programming, solve the following exercises from upper part to lower part. Run your source code, and check whether it works correctly. Use generative AI if you want to know the correct source code. Following exercises are orthodox exercises, so generative AI may answer the correct source code.


Multiplication

Final source code: About 4 lines

Print multiplicaiton table

Prototype

print('% 3d '%81,end='')
print()

Output

 1  2  3  4  5  6  7  8  9
 2  4  6  8 10 12 14 16 18
 3  6  9 12 15 18 21 24 27
 4  8 12 16 20 24 28 32 36
 5 10 15 20 25 30 35 40 45
 6 12 18 24 30 36 42 48 54
 7 14 21 28 35 42 49 56 63
 8 16 24 32 40 48 56 64 72
 9 18 27 36 45 54 63 72 81


Even odd

Final source code: About 5 lines

Judge even or odd

Prototype

number=int(input('Input integer: '))
print('Even')

Output

Input integer: 23
Odd

Output

Input integer: 20
Even


Sum

Final source code: About 7 lines

Summarize the numbers

Prototype

while True:
    number=int(input('1 or more integer (0 for quit): '))
    if number==0:
        break
print('Sum',0)

Output

1 or more integer (0 for quit): 3
1 or more integer (0 for quit): 14
1 or more integer (0 for quit): 7
1 or more integer (0 for quit): 26
1 or more integer (0 for quit): 0
Sum 50


Loot box

Final source code: About 6 lines

Draw 11 lot that contains 6% hit

Prototype

import random
for i in range(11):
    print('hit')

Output

miss
miss
miss
miss
miss
miss
miss
miss
miss
hit
miss


Rock paper scissors

Final source code: About 8 lines

Play rock paper scissors

Prototype

import random
player=int(input('0=rock, 1=scissors, 2=paper: '))
if 0<=player<=2:
    print('Computer','rock')

Output

0=rock, 1=scissors, 2=paper: 1
Computer rock
You lose


Element

Final source code: About 13 lines

Change damage rate depending on the element
2 times damage: From element 0 to element 1
2 times damage: From element 1 to element 0
2 times damage: From element 2 to element 3
2 times damage: From element 3 to element 4
2 times damage: From element 4 to element 2
0.5 times damage: From element 2 to element 4
0.5 times damage: From element 4 to element 3
0.5 times damage: From element 3 to element 2
1 times damage: Otherwise

Prototype

import random
attribute=['light','darkness','fire','wind','water']
player=random.randint(0,4)
enemy=random.randint(0,4)
print('From element',attribute[player],' to element',attribute[enemy])
print('Damage',1,'times')

Output

From element water  to element fire
Damage 2 times


HP

Final source code: About 8 lines

Battle with a single enemy

Prototype

import random
enemy=random.randint(100,200)
print('Enemy life',enemy)
damage=random.randint(1,50)
print('Damage',damage,'to the enemy')
print('Enemy down')

Output

Enemy life 141
Damage 47 to the enemy
Enemy life 94
Damage 29 to the enemy
Enemy life 65
Damage 39 to the enemy
Enemy life 26
Damage 28 to the enemy
Enemy down


EXP

Final source code: About 15 lines

Level up when you get EXP
EXP needed for level up increases depending on the level

Prototype

experiment=[100,120,140,160,180,200,220,240,260]
level=1
point=int(input('How many EXP? '))
print('For level',level+1,' you need',experiment[level-1],'EXP')
level+=1
print('Become level',level)
print('Remaining EXP',point)

Output

How many EXP? 500
For level 2  you need 100 EXP
Become level 2
Remaining EXP 400
For level 3  you need 120 EXP
Become level 3
Remaining EXP 280
For level 4  you need 140 EXP
Become level 4
Remaining EXP 140
For level 5  you need 160 EXP


Max min

Final source code: About 8 lines

Calculate max and min

Prototype

while True:
    number=int(input('1 or more integer (0 for quit): '))
    if number<=0:
        break
print('Max', 0)
print('Min', 0)

Output

1 or more integer (0 for quit): 34
1 or more integer (0 for quit): 77
1 or more integer (0 for quit): 32
1 or more integer (0 for quit): 67
1 or more integer (0 for quit): 79
1 or more integer (0 for quit): 0
Max 79
Min 32


High and low

Final source code: About 12 lines

Play high and low

Prototype

import random
player=random.randint(1,13)
enemy=random.randint(1,13)
guess=int(input('less than %d then 0, more than %d then 1: '%(enemy,enemy)))
if 0<=guess<=1:
    print('Your number',player)
    if player==enemy:
        print('Draw')

Output

less than 4 then 0, more than 4 then 1: 0
Your number 7
You lose


Prime

Final source code: About 6 lines

Judge prime number

Prototype

number=int(input('Input integer number: '))
print('It is prime number')

Output

Input intger number: 14
It is not prime number


Aho

Final source code: About 6 lines

Print aho when multiple of three or including three

Prototype

print('Print aho when multiple of three or including three')
number=int(input('Input integer number: '))
print('aho')

Output

Print aho when multiple of three or including three
Input integer number: 130
aho


Lottery

Final source code: About 18 lines

Check lottery winner

Prototype

import random
lottery4=random.randint(1000,9999)
lottery3=random.randint(100,999)
lottery2=random.randint(10,99)
lottery1=random.randint(1,9)
print('1st %04d lower 4 digits'%lottery4)
print('2nd *%03d lower 3 digits'%lottery3)
print('3rd **%02d lower 2 digits'%lottery2)
print('4th ***%01d lower 1 digits'%lottery1)
lotteryyours=int(input('Your lottery number: '))
print('?th win')

Output

1st 7866 lower 4 digits
2nd *658 lower 3 digits
3rd **24 lower 2 digits
4th ***8 lower 1 digits
Your lottery number: 3658
2nd win
4th win


Prime factor decomposition

Final source code: About 27 lines

Calculate prime factor decomposition

Prototype

number=int(input('Input integer number: '))
print('Prime factor decomposition')
print(1)

Output

Input integer number: 360
Prime factor decomposition
2
2
2
3
3
5


Sort

Final source code: About 8 lines

Sort input numbers

Prototype

print('Input 10 integer numbers')
for i in range(10):
    number=int(input())
print('Sorted from small number to large number')
print('[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]')

Output

Input 10 integer numbers
34
23
78
55
55
12
98
45
23
97
Sorted from small number to large number
[12, 23, 23, 34, 45, 55, 55, 78, 97, 98]


Race

Final source code: About 26 lines

Race considering speed and stamina

Prototype

import random
print('speed per second, stamina 1 second, half speed if stamina 0')
length=1600+random.randint(0,2)*400
print('Circuit length',length)
speed=[]
stamina=[]
run=[]
NUMBER=16
for i in range(NUMBER):
    speed.append(6+random.randint(0,7)*2)
    stamina.append(20+random.randint(0,9)*10)
    run.append(0)
    print(i,'speed',speed[i],'stamina',stamina[i])
print('START')

Output

speed per second, stamina 1 second, half speed if stamina 0
Circuit length 2000
0 speed 18 stamina 70
1 speed 14 stamina 30
2 speed 8 stamina 30
3 speed 20 stamina 40
4 speed 6 stamina 110
5 speed 8 stamina 60
6 speed 6 stamina 20
7 speed 6 stamina 80
8 speed 18 stamina 110
9 speed 16 stamina 60
10 speed 20 stamina 50
11 speed 12 stamina 80
12 speed 12 stamina 60
13 speed 14 stamina 70
14 speed 16 stamina 60
15 speed 10 stamina 80
START
1st 3 distance 20 speed 20 stamina 39
1st 3 distance 40 speed 20 stamina 38
1st 3 distance 60 speed 20 stamina 37
1st 3 distance 80 speed 20 stamina 36
1st 3 distance 100 speed 20 stamina 35
1st 3 distance 120 speed 20 stamina 34
1st 3 distance 140 speed 20 stamina 33
1st 3 distance 160 speed 20 stamina 32
1st 3 distance 180 speed 20 stamina 31
1st 3 distance 200 speed 20 stamina 30
1st 3 distance 220 speed 20 stamina 29
1st 3 distance 240 speed 20 stamina 28
1st 3 distance 260 speed 20 stamina 27
1st 3 distance 280 speed 20 stamina 26
1st 3 distance 300 speed 20 stamina 25
1st 3 distance 320 speed 20 stamina 24
1st 3 distance 340 speed 20 stamina 23
1st 3 distance 360 speed 20 stamina 22
1st 3 distance 380 speed 20 stamina 21
1st 3 distance 400 speed 20 stamina 20
1st 3 distance 420 speed 20 stamina 19
1st 3 distance 440 speed 20 stamina 18
1st 3 distance 460 speed 20 stamina 17
1st 3 distance 480 speed 20 stamina 16
1st 3 distance 500 speed 20 stamina 15
1st 3 distance 520 speed 20 stamina 14
1st 3 distance 540 speed 20 stamina 13
1st 3 distance 560 speed 20 stamina 12
1st 3 distance 580 speed 20 stamina 11
1st 3 distance 600 speed 20 stamina 10
1st 3 distance 620 speed 20 stamina 9
1st 3 distance 640 speed 20 stamina 8
1st 3 distance 660 speed 20 stamina 7
1st 3 distance 680 speed 20 stamina 6
1st 3 distance 700 speed 20 stamina 5
1st 3 distance 720 speed 20 stamina 4
1st 3 distance 740 speed 20 stamina 3
1st 3 distance 760 speed 20 stamina 2
1st 3 distance 780 speed 20 stamina 1
1st 3 distance 800 speed 20 stamina 0
1st 10 distance 820 speed 20 stamina 9
1st 10 distance 840 speed 20 stamina 8
1st 10 distance 860 speed 20 stamina 7
1st 10 distance 880 speed 20 stamina 6
1st 10 distance 900 speed 20 stamina 5
1st 10 distance 920 speed 20 stamina 4
1st 10 distance 940 speed 20 stamina 3
1st 10 distance 960 speed 20 stamina 2
1st 10 distance 980 speed 20 stamina 1
1st 10 distance 1000 speed 20 stamina 0
1st 10 distance 1010 speed 20 stamina 0
1st 10 distance 1020 speed 20 stamina 0
1st 10 distance 1030 speed 20 stamina 0
1st 10 distance 1040 speed 20 stamina 0
1st 10 distance 1050 speed 20 stamina 0
1st 10 distance 1060 speed 20 stamina 0
1st 10 distance 1070 speed 20 stamina 0
1st 10 distance 1080 speed 20 stamina 0
1st 10 distance 1090 speed 20 stamina 0
1st 10 distance 1100 speed 20 stamina 0
1st 10 distance 1110 speed 20 stamina 0
1st 10 distance 1120 speed 20 stamina 0
1st 0 distance 1134 speed 18 stamina 7
1st 0 distance 1152 speed 18 stamina 6
1st 0 distance 1170 speed 18 stamina 5
1st 0 distance 1188 speed 18 stamina 4
1st 0 distance 1206 speed 18 stamina 3
1st 0 distance 1224 speed 18 stamina 2
1st 0 distance 1242 speed 18 stamina 1
1st 0 distance 1260 speed 18 stamina 0
1st 8 distance 1278 speed 18 stamina 39
1st 8 distance 1296 speed 18 stamina 38
1st 8 distance 1314 speed 18 stamina 37
1st 8 distance 1332 speed 18 stamina 36
1st 8 distance 1350 speed 18 stamina 35
1st 8 distance 1368 speed 18 stamina 34
1st 8 distance 1386 speed 18 stamina 33
1st 8 distance 1404 speed 18 stamina 32
1st 8 distance 1422 speed 18 stamina 31
1st 8 distance 1440 speed 18 stamina 30
1st 8 distance 1458 speed 18 stamina 29
1st 8 distance 1476 speed 18 stamina 28
1st 8 distance 1494 speed 18 stamina 27
1st 8 distance 1512 speed 18 stamina 26
1st 8 distance 1530 speed 18 stamina 25
1st 8 distance 1548 speed 18 stamina 24
1st 8 distance 1566 speed 18 stamina 23
1st 8 distance 1584 speed 18 stamina 22
1st 8 distance 1602 speed 18 stamina 21
1st 8 distance 1620 speed 18 stamina 20
1st 8 distance 1638 speed 18 stamina 19
1st 8 distance 1656 speed 18 stamina 18
1st 8 distance 1674 speed 18 stamina 17
1st 8 distance 1692 speed 18 stamina 16
1st 8 distance 1710 speed 18 stamina 15
1st 8 distance 1728 speed 18 stamina 14
1st 8 distance 1746 speed 18 stamina 13
1st 8 distance 1764 speed 18 stamina 12
1st 8 distance 1782 speed 18 stamina 11
1st 8 distance 1800 speed 18 stamina 10
1st 8 distance 1818 speed 18 stamina 9
1st 8 distance 1836 speed 18 stamina 8
1st 8 distance 1854 speed 18 stamina 7
1st 8 distance 1872 speed 18 stamina 6
1st 8 distance 1890 speed 18 stamina 5
1st 8 distance 1908 speed 18 stamina 4
1st 8 distance 1926 speed 18 stamina 3
1st 8 distance 1944 speed 18 stamina 2
1st 8 distance 1962 speed 18 stamina 1
1st 8 distance 1980 speed 18 stamina 0
1st 8 distance 1989 speed 18 stamina 0
1st 8 distance 1998 speed 18 stamina 0
1st 8 distance 2007 speed 18 stamina 0


Tic tac toe

Final source code: About 49 lines

Play tic tac toe

Prototype

import random
board=[[0,0,0],[0,0,0],[0,0,0]]
graphic=['.','o','x']
print('You are o')
print(' 123')
for i in range(3):
    print(i+1,end='')
    for j in range(3):
        print(graphic[board[i][j]],end='')
    print()
puti=int(input('Row(1-3):'))-1
putj=int(input('Column(1-3):'))-1

Output

You are o
123
1...
2...
3...
Row(1-3):3
Column(1-3):1
123
1...
2...
3o..
123
1...
2...
3o.x
Row(1-3):3
Column(1-3):2
123
1...
2...
3oox
123
1..x
2...
3oox
Row(1-3):2
Column(1-3):1
123
1..x
2o..
3oox
123
1..x
2o.x
3oox
x win


Multiple enemies

Final source code: About 58 lines

Battle with multiple enemies

Prototype

enemyhp=[100,120,190]
enemyattack=[35,25,15]
enemydefense=[5,10,20]
playerhp=105
playerattackone=70
playerattackall=40
playerdefense=10
for i in range(3):
    if enemyhp[i] > 0:
        print('Enemy%d HP%d Attack%d Defense%d'%(i,enemyhp[i],enemyattack[i],enemydefense[i]))
print('You HP%d Strength%d Intelligence%d Defense%d'%(playerhp,playerattackone,playerattackall,playerdefense))
command=int(input('0=Single attack 1=All magic attack: '))
print('Target enemy number')
enemyid=int(input())
print('To enemy %d: Attack'%enemyid)
damage=0
print('To enemy %d: %d damage'% (enemyid,damage))
print('Enemy %d down'%enemyid)
print('To enemy all: Magic attack')
for i in range(3):
    damage=0
    print('To enemy %d: %d damage'%(i,damage))
    print('Enemy %d down'%i)
    print('You win')
for i in range(3):
    damage=0
    print('From enemy %d: %d damage'%(i,damage))
    print('You died')

Output

Enemy0 HP100 Attack35 Defense5
Enemy1 HP120 Attack25 Defense10
Enemy2 HP190 Attack15 Defense20
You HP105 Strength70 Intelligence40 Defense10
0=Single attack 1=All magic attack: 1
To enemy all: Magic attack
To enemy 0: 35 damage
To enemy 1: 30 damage
To enemy 2: 20 damage
From enemy 0: 25 damage
From enemy 1: 15 damage
From enemy 2: 5 damage
Enemy0 HP65 Attack35 Defense5
Enemy1 HP90 Attack25 Defense10
Enemy2 HP170 Attack15 Defense20
You HP60 Strength70 Intelligence40 Defense10
0=Single attack 1=All magic attack: 0
Target enemy number
1
To enemy 1: Attack
To enemy 1: 60 damage
From enemy 0: 25 damage
From enemy 1: 15 damage
From enemy 2: 5 damage
Enemy0 HP65 Attack35 Defense5
Enemy1 HP30 Attack25 Defense10
Enemy2 HP170 Attack15 Defense20
You HP15 Strength70 Intelligence40 Defense10
0=Single attack 1=All magic attack: 0
Target enemy number
2
To enemy 2: Attack
To enemy 2: 50 damage
From enemy 0: 25 damage
You died


Vending machine

Final source code: About 72 lines

Make vending machine

Prototype

print('How many coins included in vending machine?')
vending500=int(input('500 yen coin: '))
vending100=int(input('100 yen coin: '))
vending50=int(input('50 yen coin: '))
vending10=int(input('10 yen coin: '))
cost=int(input('Price of the product: '))
if cost%10!=0:
    print('Price should be a multiplication of 10')
    exit()
print('How many coins you put in to vending machine?')
pay500=int(input('500 yen coin: '))
pay100=int(input('100 yen coin: '))
pay50=int(input('50 yen coin: '))
pay10=int(input('10 yen coin: '))
back500=0
back100=0
back50=0
back10=0
print('Product and return',0,'yen')
print('500 yen coin:',back500)
print('100 yen coin:',back100)
print('50 yen coin:',back50)
print('10 yen coin:',back10)
print('Remaining coins inside vending machine')
print('500 yen coin:',vending500)
print('100 yen coin:',vending100)
print('50 yen coin:',vending50)
print('10 yen coin:',vending10)

Output

How many coins included in vending machine?
500 yen coin: 1
100 yen coin: 1
50 yen coin: 1
10 yen coin: 1
Price of the product: 130
How many coins you put in to vending machine?
500 yen coin: 0
100 yen coin: 1
50 yen coin: 0
10 yen coin: 3
Product and return 0 yen
500 yen coin: 0
100 yen coin: 0
50 yen coin: 0
10 yen 0:
Remaining coins inside vending machine
500 yen coin: 1
100 yen coin: 2
50 yen coin: 1
10 yen coin: 4

Output

How many coins included in vending machine?
500 yen coin: 1
100 yen coin: 1
50 yen coin: 1
10 yen coin: 1
Price of the product: 130
How many coins you put in to vending machine?
500 yen coin: 0
100 yen coin: 2
50 yen coin: 0
10 yen coin: 0
Cannot buy: Return 70 unavailable

Output

How many coins included in vending machine?
500 yen coin: 10
100 yen coin: 10
50 yen coin: 0
10 yen coin: 10
Price of the product: 130
How many coins you put in to vending machine?
500 yen coin: 0
100 yen coin: 2
50 yen coin: 0
10 yen coin: 0
Product and return 70 yen
500 yen coin: 0
100 yen coin: 0
50 yen coin: 0
10 yen coin: 7
Remaining coins inside vending machine
500 yen coin: 10
100 yen coin: 12
50 yen coin: 0
10 yen coin: 3

Output

How many coins included in vending machine?
500 yen coin: 10
100 yen coin: 10
50 yen coin: 10
10 yen coin: 10
Price of the product: 130
How many coins you put in to vending machine?
500 yen coin: 1
100 yen coin: 0
50 yen coin: 0
10 yen coin: 0
Product and return 370 yen
500 yen coin: 0
100 yen coin: 3
50 yen coin: 1
10 yen coin: 2
Remaining coins inside vending machine
500 yen coin: 11
100 yen coin: 7
50 yen coin: 9
10 yen coin: 8


Old maid

Final source code: About 48 lines

Play old maid

Prototype

import random
player=[[],[]]
allcard=[]
for i in range(1,14):
    for j in range(4):
        allcard.append(i)
allcard.append(0)
random.shuffle(allcard)
for i in range(len(allcard)):
    player[i%2].append(allcard[i])
player[0].sort()
player[1].sort()
for i in range(2):
    print(['You','Computer'][i],'card')
    for j in range(len(player[i])):
        if player[i][j]==0:
            print('Joker ',end='')
        else:
            print('%d '%player[i][j],end='')
    print()
while True:
    card=int(input('Select card (%d-%d): '%(1,len(player[1]))))-1
    if 0<=card<len(player[1]):
        break
card=random.randint(0,len(player[0])-1)
print('Computer selected card %d'%(card+1))

Output

You card
1 2 4 5 6 7 10 12 13
Computer card
Joker 1 2 4 5 6 7 10 12 13
Select card (1-10): 10
You card
1 2 4 5 6 7 10 12
Computer card
Joker 1 2 4 5 6 7 10 12
Computer selected card 6
You card
1 2 4 5 6 10 12
Computer card
Joker 1 2 4 5 6 10 12
Select card (1-8): 2
You card
2 4 5 6 10 12
Computer card
Joker 2 4 5 6 10 12
Computer selected card 6
You card
2 4 5 6 10
Computer card
Joker 2 4 5 6 10
Select card (1-6): 3
You card
2 5 6 10
Computer card
Joker 2 5 6 10
Computer selected card 4
You card
2 5 6
Computer card
Joker 2 5 6
Select card (1-4): 3
You card
2 6
Computer card
Joker 2 6
Computer selected card 2
You card
2
Computer card
Joker 2
Select card (1-2): 2
You card

Computer card
Joker
You win


Othello

Final source code: About 85 lines

Play othello

Prototype

import random
global board
board=[
    [0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0],
    [0,0,0,2,1,0,0,0],
    [0,0,0,1,2,0,0,0],
    [0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0]
]
def main():
    graphics=['.','o','x']
    print('You are o')
    print(' 12345678')
    for i in range(8):
        print('%d'%(i+1),end='')
        for j in range(8):
            print(graphics[board[i][j]],end='')
        print()
    youi=int(input('Row(1-8): '))-1
    youj=int(input('Column(1-8): '))-1
if __name__ == '__main__':
    main()

Output

You are o
12345678
1........
2........
3........
4...xo...
5...ox...
6........
7........
8........
Row(1-8): 4
Column(1-8): 3
12345678
1........
2........
3........
4..ooo...
5...ox...
6........
7........
8........
12345678
1........
2........
3..x.....
4..oxo...
5...ox...
6........
7........
8........
Row(1-8): 3
Column(1-8): 4
12345678
1........
2........
3..xo....
4..ooo...
5...ox...
6........
7........
8........
12345678
1........
2........
3..xo....
4..xoo...
5..xxx...
6........
7........
8........
Row(1-8): 6
Column(1-8): 3
12345678
1........
2........
3..xo....
4..xoo...
5..xox...
6..o.....
7........
8........
12345678
1........
2........
3..xo....
4..xxxx..
5..xox...
6..o.....
7........
8........
Row(1-8): 3
Column(1-8): 6
12345678
1........
2........
3..xo.o..
4..xxox..
5..xox...
6..o.....
7........
8........
12345678
1........
2........
3..xo.o..
4..xxox..
5..xxx...
6..o.x...
7........
8........
Row(1-8): 6
Column(1-8): 4
12345678
1........
2........
3..xo.o..
4..xoox..
5..xox...
6..oox...
7........
8........
12345678
1........
2.....x..
3..xo.x..
4..xoox..
5..xox...
6..oox...
7........
8........
Row(1-8): 6
Column(1-8): 6
12345678
1........
2.....x..
3..xo.x..
4..xoox..
5..xoo...
6..oooo..
7........
8........
12345678
1........
2.....x..
3..xo.x..
4..xoox..
5..xxxx..
6..oooo..
7........
8........
Row(1-8): 1
Column(1-8): 6
12345678
1.....o..
2.....o..
3..xo.o..
4..xooo..
5..xxxo..
6..oooo..
7........
8........
12345678
1.....o..
2.....o..
3..xo.o..
4..xooo..
5..xxxo..
6..ooxo..
7.....x..
8........
Row(1-8): 8
Column(1-8): 6
12345678
1.....o..
2.....o..
3..xo.o..
4..xooo..
5..xxxo..
6..ooxo..
7.....o..
8.....o..
12345678
1.....o..
2.....o..
3..xo.o..
4..xooo..
5..xxxo..
6..ooxxx.
7.....o..
8.....o..
Row(1-8): 6
Column(1-8): 8
12345678
1.....o..
2.....o..
3..xo.o..
4..xooo..
5..xxxo..
6..oooooo
7.....o..
8.....o..
12345678
1.....o..
2.....o..
3..xo.ox.
4..xoox..
5..xxxo..
6..oooooo
7.....o..
8.....o..
Row(1-8): 3
Column(1-8): 8
12345678
1.....o..
2.....o..
3..xo.ooo
4..xoox..
5..xxxo..
6..oooooo
7.....o..
8.....o..
12345678
1.....o..
2.....o..
3..xo.ooo
4..xoox..
5..xxxo..
6..ooxooo
7.....x..
8.....ox.
Row(1-8): 8
Column(1-8): 8
12345678
1.....o..
2.....o..
3..xo.ooo
4..xoox..
5..xxxo..
6..ooxooo
7.....x..
8.....ooo
12345678
1.....o..
2.....o..
3..xo.ooo
4..xoox..
5..xxxo.x
6..ooxoxo
7.....x..
8.....ooo
Row(1-8): 4
Column(1-8): 8
12345678
1.....o..
2.....o..
3..xo.ooo
4..xoox.o
5..xxxo.o
6..ooxoxo
7.....x..
8.....ooo
12345678
1.....o..
2.....o..
3..xo.ooo
4..xoox.o
5..xxxo.o
6..xoxoxo
7.x...x..
8.....ooo
Row(1-8): 8
Column(1-8): 1
12345678
1.....o..
2.....o..
3..xo.ooo
4..xoox.o
5..xoxo.o
6..ooxoxo
7.o...x..
8o....ooo
12345678
1.....o..
2.....o..
3..xxxooo
4..xxxx.o
5..xoxo.o
6..ooxoxo
7.o...x..
8o....ooo
Row(1-8): 8
Column(1-8): 5
12345678
1.....o..
2.....o..
3..xxxooo
4..xxxx.o
5..xoxo.o
6..ooxooo
7.o...o..
8o...oooo
12345678
1.....o..
2.....o..
3..xxxooo
4..xxxx.o
5..xoxo.o
6..ooxxoo
7.o...ox.
8o...oooo
Row(1-8): 7
Column(1-8): 8
12345678
1.....o..
2.....o..
3..xxxooo
4..xxxx.o
5..xoxo.o
6..ooxxoo
7.o...ooo
8o...oooo
12345678
1.....o..
2.....o..
3..xxxooo
4..xxxx.o
5..xoxo.o
6.xxxxxoo
7.o...ooo
8o...oooo
Row(1-8): 6
Column(1-8): 1
12345678
1.....o..
2.....o..
3..xxxooo
4..xxxx.o
5..xoxo.o
6oooooooo
7.o...ooo
8o...oooo
12345678
1.....o..
2.....ox.
3..xxxxoo
4..xxxx.o
5..xoxo.o
6oooooooo
7.o...ooo
8o...oooo
Row(1-8): 1
Column(1-8): 8
12345678
1.....o.o
2.....oo.
3..xxxooo
4..xxox.o
5..xoxo.o
6oooooooo
7.o...ooo
8o...oooo
12345678
1.....o.o
2.....oo.
3..xxxooo
4..xxox.o
5..xxxo.o
6oooxoooo
7.o.x.ooo
8o...oooo
Row(1-8): 4
Column(1-8): 7
12345678
1.....o.o
2.....oo.
3..xxxooo
4..xxoooo
5..xxxo.o
6oooxoooo
7.o.x.ooo
8o...oooo
12345678
1.....o.o
2.....oo.
3..xxxooo
4..xxoxoo
5..xxxxxo
6oooxoooo
7.o.x.ooo
8o...oooo
Row(1-8): 5
Column(1-8): 2
12345678
1.....o.o
2.....oo.
3..xxxooo
4..xxoxoo
5.ooooooo
6oooxoooo
7.o.x.ooo
8o...oooo
12345678
1.....o.o
2.....oox
3..xxxoxo
4..xxoxoo
5.ooooooo
6oooxoooo
7.o.x.ooo
8o...oooo
Row(1-8): 8
Column(1-8): 4
12345678
1.....o.o
2.....oox
3..xxxoxo
4..xxoxoo
5.ooooooo
6oooooooo
7.o.o.ooo
8o..ooooo
12345678
1.....oxo
2.....xxx
3..xxxoxo
4..xxoxoo
5.ooooooo
6oooooooo
7.o.o.ooo
8o..ooooo
Row(1-8): 2
Column(1-8): 5
12345678
1.....oxo
2....oxxx
3..xoooxo
4..oxoxoo
5.ooooooo
6oooooooo
7.o.o.ooo
8o..ooooo
12345678
1.....oxo
2....oxxx
3..xoooxo
4..xxoxoo
5.oxoxooo
6ooxxoooo
7.oxo.ooo
8o..ooooo
Row(1-8): 4
Column(1-8): 2
12345678
1.....oxo
2....oxxx
3..xoooxo
4.ooooxoo
5.oxoxooo
6ooxxoooo
7.oxo.ooo
8o..ooooo
12345678
1.....oxo
2....oxxx
3x.xoooxo
4.xoooxoo
5.oxoxooo
6ooxxoooo
7.oxo.ooo
8o..ooooo
Row(1-8): 3
Column(1-8): 2
12345678
1.....oxo
2....oxxx
3xoooooxo
4.ooooxoo
5.oxoxooo
6ooxxoooo
7.oxo.ooo
8o..ooooo
12345678
1....xxxo
2....xxxx
3xoooxoxo
4.oooxxoo
5.oxoxooo
6ooxxoooo
7.oxo.ooo
8o..ooooo
Row(1-8): 1
Column(1-8): 4
12345678
1...ooooo
2....oxxx
3xoooxoxo
4.oooxxoo
5.oxoxooo
6ooxxoooo
7.oxo.ooo
8o..ooooo
12345678
1...ooooo
2....oxxx
3xoooxoxo
4.oooxxoo
5xxxoxooo
6oxxxoooo
7.oxo.ooo
8o..ooooo
Row(1-8): 4
Column(1-8): 2
Pass
12345678
1...ooooo
2....oxxx
3xoooxoxo
4.oooxxoo
5xxxoxooo
6oxxxoooo
7.oxo.ooo
8o..ooooo
12345678
1...ooooo
2...xxxxx
3xoxxxoxo
4.xoxxxoo
5xxxxxooo
6oxxxoooo
7.oxo.ooo
8o..ooooo
Row(1-8): 7
Column(1-8): 1
12345678
1...ooooo
2...xxoxx
3xoxxooxo
4.xooxxoo
5xxoxxooo
6ooxxoooo
7ooxo.ooo
8o..ooooo
12345678
1...ooooo
2.x.xxoxx
3xxxxooxo
4.xooxxoo
5xxoxxooo
6ooxxoooo
7ooxo.ooo
8o..ooooo
Row(1-8): 7
Column(1-8): 5
12345678
1...ooooo
2.x.xxoxx
3xxxxooxo
4.xooxxoo
5xxoxxooo
6ooxooooo
7ooxooooo
8o..ooooo
12345678
1...ooooo
2.x.xxoxx
3xxxxooxo
4.xooxxoo
5xxoxxooo
6oxxooooo
7oxxooooo
8ox.ooooo
Row(1-8): 8
Column(1-8): 3
12345678
1...ooooo
2.x.xxoxx
3xxxxooxo
4.xooxxoo
5xxoxxooo
6oxoooooo
7oooooooo
8oooooooo
12345678
1...ooooo
2.x.xxoxx
3xxxxooxo
4.xooxxoo
5xxoxxooo
6oxoooooo
7oooooooo
8oooooooo
Row(1-8): 1
Column(1-8): 1
12345678
1o..ooooo
2.o.xxoxx
3xxoxooxo
4.xooxxoo
5xxoxxooo
6oxoooooo
7oooooooo
8oooooooo
12345678
1o.xooooo
2.x.xxoxx
3xxoxooxo
4.xooxxoo
5xxoxxooo
6oxoooooo
7oooooooo
8oooooooo
Row(1-8): 1
Column(1-8): 2
12345678
1oooooooo
2.o.xxoxx
3xooxooxo
4.oooxxoo
5xooxxooo
6oooooooo
7oooooooo
8oooooooo
12345678
1oooooooo
2xo.xxoxx
3xxoxooxo
4.oxoxxoo
5xooxxooo
6oooooooo
7oooooooo
8oooooooo
Row(1-8): 4
Column(1-8): 1
12345678
1oooooooo
2oo.xxoxx
3oxoxooxo
4ooxoxxoo
5oooxxooo
6oooooooo
7oooooooo
8oooooooo
12345678
1oooooooo
2ooxxxoxx
3oxxxooxo
4ooxoxxoo
5oooxxooo
6oooooooo
7oooooooo
8oooooooo
Row(1-8): 1
Column(1-8): 1
Pass
12345678
1oooooooo
2ooxxxoxx
3oxxxooxo
4ooxoxxoo
5oooxxooo
6oooooooo
7oooooooo
8oooooooo
12345678
1oooooooo
2ooxxxoxx
3oxxxooxo
4ooxoxxoo
5oooxxooo
6oooooooo
7oooooooo
8oooooooo


Puyo Puyo

Final source code: About 57 lines

Remove 4 or more connecting same number
(Recursive function is better than labeling algorithm)

Prototype

global field
field=[
    [4,0,0,0,0,0,0,0],
    [2,5,0,0,0,0,1,3],
    [5,1,1,0,0,0,1,3],
    [1,3,4,0,0,0,1,1],
    [2,2,2,0,0,0,4,2],
    [2,2,2,2,0,0,3,2],
    [2,2,2,3,0,0,2,1],
    [1,3,2,3,0,1,1,1],
    [5,1,1,1,5,1,4,1],
    [2,5,2,2,2,1,1,1]
]
global discard
discard=[[False]*8 for _ in range(10)]
def main():
    for i in range(10):
        for j in range(8):
            if discard[i][j]:
                print('*',end='')
            else:
                print(field[i][j],end='')
        print()
if __name__ == "__main__":
    main()

Output

40000000
250000*3
511000*3
134000**
***00042
****0032
***3002*
13*30***
51115*4*
25222***


Back