last lab
This commit is contained in:
56
labPython/lab16/task_5.py
Normal file
56
labPython/lab16/task_5.py
Normal file
@@ -0,0 +1,56 @@
|
||||
# task: ListWork62 / Создание убывающего списка из файла
|
||||
|
||||
import random
|
||||
|
||||
|
||||
class Node:
|
||||
def __init__(self, data):
|
||||
self.data = data
|
||||
self.next = None
|
||||
|
||||
|
||||
def insert_desc(head, x):
|
||||
node = Node(x)
|
||||
if not head or x > head.data:
|
||||
node.next = head
|
||||
return node
|
||||
cur = head
|
||||
while cur.next and cur.next.data > x:
|
||||
cur = cur.next
|
||||
node.next = cur.next
|
||||
cur.next = node
|
||||
return head
|
||||
|
||||
|
||||
def print_list(h):
|
||||
while h:
|
||||
print(h.data, end=" ")
|
||||
h = h.next
|
||||
print()
|
||||
|
||||
|
||||
while True:
|
||||
try:
|
||||
print("\n1 - создать файл")
|
||||
print("2 - обработать файл")
|
||||
print("0 - выход")
|
||||
c = input(">> ")
|
||||
|
||||
if c == "1":
|
||||
n = int(input("N: "))
|
||||
arr = [random.randint(1, 100) for _ in range(n)]
|
||||
with open("data.txt", "w") as f:
|
||||
f.write(str(n) + "\n")
|
||||
f.write(" ".join(map(str, arr)))
|
||||
elif c == "2":
|
||||
with open("data.txt") as f:
|
||||
n = int(f.readline())
|
||||
arr = list(map(int, f.readline().split()))
|
||||
head = None
|
||||
for x in arr:
|
||||
head = insert_desc(head, x)
|
||||
print_list(head)
|
||||
elif c == "0":
|
||||
break
|
||||
except:
|
||||
print("Ошибка")
|
||||
Reference in New Issue
Block a user