Created
August 1, 2022 16:22
-
-
Save gmakc-094423/d58021e77895f6b383ea52299db4d1fd to your computer and use it in GitHub Desktop.
Домашнее задание к семинару № 6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
def print_matrix(matrix): | |
for i in range(0, len(matrix)): | |
for i2 in range(0, len(matrix[i])): | |
print(matrix[i][i2], end=" ") | |
print() | |
def InputNumbers(): | |
is_OK = False | |
while not is_OK: | |
try: | |
number = int(input("Введите Х: ")) - 1, int(input("Введите Y: ")) - 1 | |
is_OK = True | |
except ValueError: | |
print("Это неправильные циферки!") | |
return number | |
def check_num(number, temp_area): | |
is_OK = False | |
while not is_OK: | |
if 0 <= number[0] < 3 and 0 <= number[1] < 3: | |
if [i for i in temp_area if i == number]: | |
print("Уже было") | |
number = InputNumbers() | |
else: | |
is_OK = True | |
else: | |
print("Вне пределов доски.") | |
number = InputNumbers() | |
return number | |
def get_o(temp_area): | |
number = (0, 0) | |
while [i for i in temp_area if i == number]: | |
number = (random.randint(0, 2), random.randint(0, 2)) | |
return number | |
def check_result(matrix, liter): | |
x = "" | |
if ( | |
matrix[0][0] == matrix[0][1] == matrix[0][2] == str(liter) | |
or matrix[1][0] == matrix[1][1] == matrix[1][2] == str(liter) | |
or matrix[2][0] == matrix[2][1] == matrix[2][2] == str(liter) | |
or matrix[0][0] == matrix[1][0] == matrix[2][0] == str(liter) | |
or matrix[0][1] == matrix[1][1] == matrix[2][1] == str(liter) | |
or matrix[0][2] == matrix[1][2] == matrix[2][2] == str(liter) | |
or matrix[0][0] == matrix[1][1] == matrix[2][2] == str(liter) | |
or matrix[0][2] == matrix[1][1] == matrix[2][0] == str(liter) | |
): | |
x = str(liter) | |
return x | |
area = [] | |
area = [["."] * 3 for i in range(3)] | |
fill_aria = area | |
temp_area = [] | |
print_matrix(area) | |
while len(temp_area) < 9: | |
num = InputNumbers() | |
num_x = check_num(num, temp_area) | |
temp_area.append(num_x) | |
fill_aria[num_x[0]][num_x[1]] = "x" | |
print_matrix(fill_aria) | |
result = check_result(fill_aria, "x") | |
if result == "x": | |
print("YOU WIN !") | |
break | |
if len(temp_area) == 9: | |
break | |
num_o = get_o(temp_area) | |
print(f"Ход противника: {num_o[0]+1, num_o[1]+1}") | |
temp_area.append(num_o) | |
fill_aria[num_o[0]][num_o[1]] = "o" | |
print_matrix(fill_aria) | |
result = check_result(fill_aria, "o") | |
if result == "o": | |
print("YOU LOST :(") | |
break | |
print("GAME OVER") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# formula = "1+2* 3" | |
formula = input("Введите выражение: ") | |
formula.replace(" ", "") | |
result = eval(formula) | |
print("Result: {}".format(result)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def input_filename(file_name="input_data.txt"): | |
path1 = file_name | |
data = open(file_name, "r") | |
text = data.read() | |
data.close() | |
return text | |
def write_file(file_name, text): | |
f = open(file_name, "w") | |
f.write(text) | |
f.close() | |
def rle_encode(data): | |
if not data: | |
return "" | |
encoding = "" | |
prev_char = data[0] | |
count = 0 | |
for i in data: | |
if i == prev_char: | |
count += 1 | |
else: | |
encoding += str(count) + prev_char | |
prev_char = i | |
count = 1 | |
encoding += str(count) + prev_char | |
return encoding | |
def rle_decoding(text): | |
decoding = "" | |
for i in range(len(text)): | |
if str(text[i]).isdigit(): | |
for j in range(int(text[i])): | |
decoding += text[i + 1] | |
return decoding | |
en_dec = input("Press 1(encoding) or 2(decoding):") | |
if en_dec == "1": | |
file_name = str(input("Input file name:")) | |
# file_name = 'input_data.txt' | |
text = input_filename(file_name) | |
encod_data = rle_encode(text) | |
print("Result = {}".format(encod_data)) | |
write_file(input("Input file name:"), encod_data) | |
elif en_dec == "2": | |
file_name = str(input("Input file name:")) | |
# file_name = 'output_data.txt' | |
encod_data = input_filename(file_name) | |
deco_data = rle_decoding(encod_data) | |
print("Decoding data = {}".format(deco_data)) | |
else: | |
print("Please retry") | |
# 'input_data.txt' | |
# 'output_data.txt' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment