Table of Contents

Cvičení 5: 2D pole

Dvojrozměrné pole

x = [[0, 1], ['a', True], [1, 1.0, 2, 4]]
>>> len(x)
3
>>> len(x[0])
2
>>> x[1][1]
True
>>> type(x)
<class 'list'>
>>> type(x[0])
<class 'list'>

x = []
for i in range(2):
   x.append([0] * 3)
>>> print(x)
[[0, 0, 0], [0, 0, 0]]

Výpis matice

Stopa matice

Násobení vektoru a matice

m=[[0,0,1],[0,1,0],[1,0,0]]
v=[2, 4, 6]

Načtení 2D pole ze souboru

1 2 3
4 0 5
7 -1 2

pole=[]
f=open('matice.txt','rt')
for line in f:
    pole.append(list(map(int, line.split())))
f.close()
print(pole)

[[1,2,3],[4,0,5],[7,-1,2]]

Načtení zakódovaného obrázku ze souboru

## | ------------------ Functions definition ------------------ |
 
# [You should understand this function]
# Function loading given file
def loadFile(filename):
    mat = []                   # create empty list
    f   = open(filename, 'rt') # open file with name 'filename' in 'read' and 'text' modes
    for line in f:             # iterate all lines in the file
        # parse single line from file:
        #  1) strip: remove spaces and new line characters from beginning and end of the line
        #  2) split: split line by spaces, returns list
        #  3) map:   maps each element to string
        #  4) list:  convert output of map to list of strings
        lst = list(map(str, line.strip().split(' ')))
        mat.append(lst) # append list to our 'mat' variable
    f.close()
    return mat
 
# [You don't need to understand this function]
# Function decoding our file
def decode(mat):
    mat_decoded = []  # create empty output list
    for lst in mat:   # for each list in 'mat' (list of lists)
        decoded = ''  # initialize the decoded string to empty value
        for s in lst: # for each encoded value in list 'lst'
            char = s.rstrip('0123456789') # cut the numbered part of our string out, example: #2 -> #
            num  = s[len(char):]          # retrieve the cut part, example: 2
            if char == 's':               # space is encoded as 's'
                char = ' '                # replace 's' by space character
            decoded = decoded + int(num) * char # decode and store into 'decoded' variable
        mat_decoded.append(decoded)             # append single decoded line into our output list
    return mat_decoded                          # return output list
 
## | -------------- Python starts processing here ------------- |
encoded = loadFile('encoded.txt') # load the encoded file
decoded = decode(encoded)         # decode the encoded file 
for line in decoded: # print our decoded file
    print(line)

Témata k procvičení

Implementujte následující úlohy:

Domácí úkol

Lehká varianta

import sys
sachovnice = open(sys.argv[1], "rt")

Příklad

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 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 3 0 0 0 0 0 0
1 0 0 0 0 0 0 0

Výstup:

a1 c3
Vysvětlení: Šachovnice obsahuje pouze dva kameny, jeden bílý, jeden černý. Bílý skočí černý kámen skokem z pole a1 na pole c3.

0 0 0 0 0 0 0 3
0 0 0 0 0 0 4 0
0 0 0 0 0 1 0 0
1 0 3 0 3 0 4 0
0 3 0 0 0 0 0 0
1 0 0 0 4 0 0 0
0 3 0 3 0 0 0 1
1 0 1 0 0 0 0 0

Výstup:

a1 c3
Vysvětlení: Šachovnice obsahuje pouze mnoho kamenů, ale kromě kamenu na poli a1. Například kamen z pole a3 nemá za černým kamenem na poli b4 volné pole c5, bílý kamen z pole h2 nemá černý kámen na poli g3, bílý kamen z pole f6 by mohl skákat pouze zpět a to není v České dámě dovoleno.

0 0 0 0 0 0 0 3
0 0 0 0 0 0 4 0
0 3 0 0 0 1 0 0
0 0 3 0 3 0 4 0
0 3 0 0 0 0 0 0
1 0 0 0 4 0 0 0
0 3 0 3 0 0 0 1
1 0 1 0 0 0 0 0

Výstup:

a1 c3 a5 c7
Vysvětlení: Kamenu z pole a1 skočí nejdříve na pole c3, dále pokračuje skokem na poli a5 a ještě může pokračovat skokem na pole c7.

Těžká varianta

Příklad

0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 3 0 0 0 0
0 0 0 0 0 0 0 0
0 3 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 3 0 2 0 0 0 0
0 0 1 0 0 0 0 0

Výstup:

c1 a3 c5 e7
Vysvětlení: Skok bílého kamene je delší než skok bílé dámy.

0 0 0 0 0 2 0 0
0 0 3 0 3 0 4 0
0 0 0 0 0 0 0 0
0 0 3 0 0 0 3 0
0 0 0 0 0 0 0 0
0 0 0 0 3 0 3 0
0 0 0 3 0 0 0 0
1 0 1 0 0 0 2 0

Výstup:

g1 d4 b6 d8 f6 h4 e1 c3 h8
Velmi dlouhý skok dámy.

0 0 0 0 0 4 0 0
0 0 3 0 0 0 3 0
0 2 0 0 0 0 0 0
0 0 0 0 3 0 0 0
0 0 0 0 0 0 0 0
0 0 3 0 0 0 3 0
0 1 0 0 0 0 0 1
0 0 0 0 0 0 0 0

Výstup: buď

b2 d4 f6 h8
nebo
h2 f4 d6 b8
Dva stejně dlouhé tahy, Váš program vytiskne buď první nebo druhý tah.