Skip to content

Commit 4a080ba

Browse files
committed
Solved task with game called Cards
1 parent fa88e9b commit 4a080ba

File tree

3 files changed

+205
-4
lines changed

3 files changed

+205
-4
lines changed

__pycache__/stdarray.cpython-36.pyc

4.05 KB
Binary file not shown.

array.py

+25-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import stdio
2+
import stdarray
3+
import random
24
# Start doing task from books/ Added stdio library, to
35
# use some code from book;
46
n = 4
@@ -50,13 +52,32 @@
5052

5153
# Иницализируем массив массив длиной 52 представляющего колоду карт,
5254
# используя лва только что определенных массива
53-
54-
desk = []
55+
deck = []
5556
for rank in RANKS:
5657
for suit in SUITS:
5758
card = rank + ' of ' + suit
58-
desk += [card] # Равносильно desk = desk + card ?)
59-
print(desk)
59+
deck += [card] # Равносильно desk = desk + card ?)
60+
61+
62+
# Обмен єлементов местами. Поменяем карті с индексами i и j
63+
# Только изменим порядок элементов массива на обратный
64+
# temp = deck[i]
65+
# deck[i] = deck[j]
66+
# deck[j] = temp
67+
# Меняется порядок элементов в массиве, но не сам набор эл. в массиве
68+
# Когда i и j равны то массив не изменяется
69+
70+
71+
# Перестановка колоды карт
72+
n = len(deck)
73+
for i in range(n):
74+
r = random.randrange(i, n)
75+
temp = deck[r]
76+
deck[r] = deck[i]
77+
deck[i] = temp
78+
print(temp)
79+
80+
6081

6182

6283
#----------------------------------------------------------

stdarray.py

+180
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
"""
2+
stdarray.py
3+
4+
The stdarray module defines functions related to creating, reading,
5+
and writing one- and two-dimensional arrays.
6+
"""
7+
8+
import stdio
9+
10+
#=======================================================================
11+
# Array creation functions
12+
#=======================================================================
13+
14+
def create1D(length, value=None):
15+
"""
16+
Create and return a 1D array containing length elements, each
17+
initialized to value.
18+
"""
19+
return [value] * length
20+
21+
#-----------------------------------------------------------------------
22+
23+
def create2D(rowCount, colCount, value=None):
24+
"""
25+
Create and return a 2D array having rowCount rows and colCount
26+
columns, with each element initialized to value.
27+
"""
28+
a = [None] * rowCount
29+
for row in range(rowCount):
30+
a[row] = [value] * colCount
31+
return a
32+
33+
#=======================================================================
34+
# Array writing functions
35+
#=======================================================================
36+
37+
def write1D(a):
38+
"""
39+
Write array a to sys.stdout. First write its length. bool objects
40+
are written as 0 and 1, not False and True.
41+
"""
42+
length = len(a)
43+
stdio.writeln(length)
44+
for i in range(length):
45+
# stdio.writef('%9.5f ', a[i])
46+
element = a[i]
47+
if isinstance(element, bool):
48+
if element == True:
49+
stdio.write(1)
50+
else:
51+
stdio.write(0)
52+
else:
53+
stdio.write(element)
54+
stdio.write(' ')
55+
stdio.writeln()
56+
57+
#-----------------------------------------------------------------------
58+
59+
def write2D(a):
60+
"""
61+
Write two-dimensional array a to sys.stdout. First write its
62+
dimensions. bool objects are written as 0 and 1, not False and True.
63+
"""
64+
rowCount = len(a)
65+
colCount = len(a[0])
66+
stdio.writeln(str(rowCount) + ' ' + str(colCount))
67+
for row in range(rowCount):
68+
for col in range(colCount):
69+
#stdio.writef('%9.5f ', a[row][col])
70+
element = a[row][col]
71+
if isinstance(element, bool):
72+
if element == True:
73+
stdio.write(1)
74+
else:
75+
stdio.write(0)
76+
else:
77+
stdio.write(element)
78+
stdio.write(' ')
79+
stdio.writeln()
80+
81+
#=======================================================================
82+
# Array reading functions
83+
#=======================================================================
84+
85+
def readInt1D():
86+
"""
87+
Read from sys.stdin and return an array of integers. An integer at
88+
the beginning of sys.stdin defines the array's length.
89+
"""
90+
count = stdio.readInt()
91+
a = create1D(count, None)
92+
for i in range(count):
93+
a[i] = stdio.readInt()
94+
return a
95+
96+
#-----------------------------------------------------------------------
97+
98+
def readInt2D():
99+
"""
100+
Read from sys.stdin and return a two-dimensional array of integers.
101+
Two integers at the beginning of sys.stdin define the array's
102+
dimensions.
103+
"""
104+
rowCount = stdio.readInt()
105+
colCount = stdio.readInt()
106+
a = create2D(rowCount, colCount, 0)
107+
for row in range(rowCount):
108+
for col in range(colCount):
109+
a[row][col] = stdio.readInt()
110+
return a
111+
112+
#-----------------------------------------------------------------------
113+
114+
def readFloat1D():
115+
"""
116+
Read from sys.stdin and return an array of floats. An integer at the
117+
beginning of sys.stdin defines the array's length.
118+
"""
119+
count = stdio.readInt()
120+
a = create1D(count, None)
121+
for i in range(count):
122+
a[i] = stdio.readFloat()
123+
return a
124+
125+
#-----------------------------------------------------------------------
126+
127+
def readFloat2D():
128+
"""
129+
Read from sys.stdin and return a two-dimensional array of floats.
130+
Two integers at the beginning of sys.stdin define the array's
131+
dimensions.
132+
"""
133+
rowCount = stdio.readInt()
134+
colCount = stdio.readInt()
135+
a = create2D(rowCount, colCount, 0.0)
136+
for row in range(rowCount):
137+
for col in range(colCount):
138+
a[row][col] = stdio.readFloat()
139+
return a
140+
141+
#-----------------------------------------------------------------------
142+
143+
def readBool1D():
144+
"""
145+
Read from sys.stdin and return an array of booleans. An integer at
146+
the beginning of sys.stdin defines the array's length.
147+
"""
148+
count = stdio.readInt()
149+
a = create1D(count, None)
150+
for i in range(count):
151+
a[i] = stdio.readBool()
152+
return a
153+
154+
#-----------------------------------------------------------------------
155+
156+
def readBool2D():
157+
"""
158+
Read from sys.stdin and return a two-dimensional array of booleans.
159+
Two integers at the beginning of sys.stdin define the array's
160+
dimensions.
161+
"""
162+
rowCount = stdio.readInt()
163+
colCount = stdio.readInt()
164+
a = create2D(rowCount, colCount, False)
165+
for row in range(rowCount):
166+
for col in range(colCount):
167+
a[row][col] = stdio.readBool()
168+
return a
169+
170+
#=======================================================================
171+
172+
def _main():
173+
"""
174+
For testing.
175+
"""
176+
write2D(readFloat2D())
177+
write2D(readBool2D())
178+
179+
if __name__ == '__main__':
180+
_main()

0 commit comments

Comments
 (0)