def __init__(self, file): with open(file, "r") as f: self.cash, self.height, self.width = [int(x) for x in f.readline().split(" ")] self.waves = SinglyList() self.waves_num = 0 for line in iter(f.readline, ""): self.waves.add_head(Wave(*[int(x) for x in line.split(" ")])) self.waves_num += 1 self.game_over = False self.turn_number = 0 self.non_plants = 0 self.deck = Stack() for i in range(100): self.deck.push(random.randint(1, 5)) self.board = [] #SinglyList Next points to the next array. #Data points to the list that stores the queues for i in range(self.height): self.board.append([]) #; for z in range(self.width): self.board[i].append(Queue()) print(self.board[i][z])
def train(size, parity): out = SinglyList() if parity == 0: size = 2 * int(size / 2) elif size % 2 == 0: size -= 1 for x in range(size, 0, -2): out.add_tail(out.head, x) return out
def merge(train1, train2): list_head = SinglyList() list_head.add_head(Node("Test")) node = list_head.head num = 0 while train1 is not None or train2 is not None: if num % 2 == 0: node.next = train1 train1 = train1.next else: node.next = train2 train2 = train2.next node = node.next num += 1 return list_head
def merge(train1, train2): list_head = SinglyList() list_head.add_head(train1) train1 = train1.next node = list_head.head num = 1 while train1 is not None or train2 is not None: if num % 2 == 0 and train1 is not None: node.next = train1 train1 = train1.next elif train2 is not None: node.next = train2 train2 = train2.next node = node.next num += 1 return list_head
class Game(): def __init__(self, file): with open(file, "r") as f: self.cash, self.height, self.width = [int(x) for x in f.readline().split(" ")] self.waves = SinglyList() self.waves_num = 0 for line in iter(f.readline, ""): self.waves.add_head(Wave(*[int(x) for x in line.split(" ")])) self.waves_num += 1 self.game_over = False self.turn_number = 0 self.non_plants = 0 self.deck = Stack() for i in range(100): self.deck.push(random.randint(1, 5)) self.board = [] #SinglyList Next points to the next array. #Data points to the list that stores the queues for i in range(self.height): self.board.append([]) #; for z in range(self.width): self.board[i].append(Queue()) print(self.board[i][z]) def draw(self): print("Cash: $", self.cash, "\nWaves: ", self.waves_num, sep = "") s = " ".join([str(i) for i in range(self.width - 1)]) print(" ", s) for row in range(self.height): s = [] for col in range(self.width): if self.is_plant(row, col): char = "P" elif self.is_nonplant(row, col): size = self.board[row][col].size() char = str(size) if size < 10 else "#" else: char = "." s.append(char) print(row, " ", " ".join(s), "\n", sep="") print() def is_nonplant(self, row, col): print(self.board[row][col].isEmpty()) if type(self.board[row][col].front()) == Non_Plant: return True else: False def is_plant(self, row, col): if type(self.board[row][col]) == Plant: return True else: False def remove(self, row, col): print("Total amount of non_plants", self.non_plants) print("cash before removal", self.cash) if self.is_nonplant(row, col) == True: self.non_plants -= 1 self.cash += self.board[row][col].front().worth #else: # self.cash += 35 print("cash after removal", self.cash) self.board[row][col].dequeue() print("Total amount of non_plants", self.non_plants) def place_nonplant(self, row): nplant = Non_Plant() self.board[row][self.width - 1].enqueue(nplant) self.non_plants += 1 def place_plant(self, row, col): if col != self.width - 1 and self.is_nonplant(row, col) == False and self.is_plant(row, col) == False: plant = Plant() self.board.item[row][col].enqueue(plant) self.cash -= Plant.cost
from singly_list import SinglyList, Node from print_list import print_list def add_tail(list_head, val): node = list_head if not isinstance(list_head, Node): print("This is not Node!") return while node.next is not None: node = node.next node.next = Node(val) if __name__ == '__main__': s_list = SinglyList() a = Node(-1) s_list.add_head(a) # node = s_list.head # for i in range(10): # node.next = Node(i) # node = node.next node = s_list.head b = Node(5) c = Node(8) d = Node(7) a.next = b b.next = c c.next = d
from singly_list import SinglyList from node import Node disList = SinglyList() theFirstNode = Node("The First Node") theLastNode = Node("The Last Node") theLastNode.next = theFirstNode disList.add_head(theLastNode) for i in range(1,10): disList.add_head(Node(i)) disList.add_head(theFirstNode) ''' #00 Test PrintList.print_list(disList.head) disList.add_head(Node("lmao")) PrintList.print_list(None) PrintList.print_list(disList.head) #01 Test PrintList.print_list(disList.head) PrintList.add_tail(disList.head, 10) PrintList.print_list(disList.head) #02 Test SinglyList.print_list(disList.head) disList.remove([self], list_head=something, val=4) disList.remove(4) SinglyList.print_list(disList.head)
from node import Node from singly_list import SinglyList from print_list import print_list from remove import remove from sort_asc import sort_asc alist = SinglyList() node0 = Node(1) node1 = Node(5) node0.next = node1 anode0 = Node(2) anode1 = Node(3) node1.next = anode0 anode0.next = anode1 anode1.next = None alist.add_head(node0) sort_asc(alist) print_list(alist)
from has_cycle import has_cycle from node import Node from singly_list import SinglyList list = SinglyList() #list.add_head(Node("f")) node0 = Node("a") node1 = Node("b") node0.next = node1 #node1.next = node0 node1.next = None list.add_head(node1) list_head = list.head print(has_cycle(list_head))
from singly_list import SinglyList from node import Node import random trainWon = SinglyList() trainToo = SinglyList() for i in range(1, 10): if random.random() > 0.9: trainWon.add_head(Node(i)) else: trainToo.add_head(Node(i)) ''' #00 Test PrintList.print_list(disList.head) disList.add_head(Node("lmao")) PrintList.print_list(None) PrintList.print_list(disList.head) #01 Test PrintList.print_list(disList.head) PrintList.add_tail(disList.head, 10) PrintList.print_list(disList.head) #02 Test SinglyList.print_list(disList.head) disList.remove(disList.head, 4) SinglyList.print_list(disList.head) #03 Test print(SinglyList.has_cycle(disList.head))
from singly_list import SinglyList from node import Node import random disList = SinglyList() for i in range(10): disList.add_head(Node(random.randint(1,100))) ''' #00 Test PrintList.print_list(disList.head) disList.add_head(Node("lmao")) PrintList.print_list(None) PrintList.print_list(disList.head) #01 Test PrintList.print_list(disList.head) PrintList.add_tail(disList.head, 10) PrintList.print_list(disList.head) #02 Test SinglyList.print_list(disList.head) disList.remove(disList.head, 4) SinglyList.print_list(disList.head) #03 Test print(SinglyList.has_cycle(disList.head)) #04 Test SinglyList.print_list(trainWon.head)
def main(): trainWon = SinglyList() trainToo = SinglyList() for i in range(10): trainWon.add_head(Node(random.randint(1, 100))) for i in range(10): trainToo.add_head(Node(random.randint(1, 100))) ''' #00 Test PrintList.print_list(disList.head) disList.add_head(Node("lmao")) PrintList.print_list(None) PrintList.print_list(disList.head) #01 Test PrintList.print_list(disList.head) PrintList.add_tail(disList.head, 10) PrintList.print_list(disList.head) #02 Test SinglyList.print_list(disList.head) disList.remove(disList.head, 4) SinglyList.print_list(disList.head) #03 Test print(SinglyList.has_cycle(disList.head)) #04 Test SinglyList.print_list(trainWon.head) SinglyList.print_list(trainToo.head) SinglyList.print_list(SinglyList.merge(trainWon.head, trainToo.head)) #05 Test SinglyList.print_list(disList.head) disList.sort_dsc(disList.head) SinglyList.print_list(disList.head) ''' #06 "Test" SinglyList.print_list(trainWon.head) SinglyList.print_list(trainToo.head) trainWon.sort_dsc(trainWon.head) trainToo.sort_dsc(trainToo.head) SinglyList.print_list(SinglyList.merge(trainWon.head, trainToo.head))
from node import Node from singly_list import SinglyList from print_list import print_list from sort_asc import sort_asc from merge import merge from random import randint def merge_n_sort(train1, train2): n = merge(train1, train2) sort_asc(n) return n l1 = SinglyList() l1.add_head(Node(-1)) node = l1.head for i in range(50): new = Node(randint(0, 100)) node.n = new node = node.n l2 = SinglyList() l2.add_head(Node(-1)) node = l2.head for i in range(50): new = Node(randint(0, 100)) node.n = new node = node.n print_list(merge_n_sort(l1.head, l2.head))
from singly_list import SinglyList from random import randint def train(size, parity): out = SinglyList() if parity == 0: size = 2 * int(size / 2) elif size % 2 == 0: size -= 1 for x in range(size, 0, -2): out.add_tail(out.head, x) return out list_a = SinglyList() [list_a.add_tail(list_a.head, x) for x in range(8, 3, -1)] list_a.remove(list_a.head, 6) list_a.print_list(list_a.head) print('Cycle? ', end='') print('yes' if list_a.has_cycle(list_a.head) else 'no') tail = list_a.head while tail.next: tail = tail.next tail.next = list_a.head print('Cycle? ', end='') print('yes' if list_a.has_cycle(list_a.head) else 'no') tail.next = None list_a = train(10, 0) list_b = train(10, 1) print('Merge:')
from random import randint, getrandbits from singly_list import SinglyList def yard(alice, bob): cars = sorted([car for car in alice] + [car for car in bob]) return cars if __name__ == '__main__': alice = [] bob = [] for x in range(randint(10, 15)): who = alice if getrandbits(1) else bob who.append(randint(1,20)) print("Alice:", alice, sep=' ') print("Bob:", bob, sep=' ') carol = SinglyList() dave = SinglyList() [carol.add_tail(carol.h, x) for x in alice + bob] carol.sort() while carol.size() > 2: dave.add_tail(dave.h, carol.h) carol.remove(carol.h, carol.h) print("Carol:", carol, sep=' ') print("Dave:", dave, sep=' ')
from node import Node from singly_list import SinglyList from print_list import print_list from add_tail import add_tail list = SinglyList() list.add_head(Node("a")) list.add_head(Node("b")) list.add_head(Node("c")) #list.add_head(Node("d")) #list.add_head(Node("e")) #list.add_head(Node("f")) #list.add_head(Node("g")) add_tail(list.head, "1") print_list(list.head)
from singly_list import SinglyList from node import Node from print_list import PrintList disList = SinglyList() for i in range(0): disList.add_head(Node(i)) ''' #00 Test PrintList.print_list(disList.head) disList.add_head(Node("lmao")) PrintList.print_list(None) PrintList.print_list(disList.head) ''' #01 Test PrintList.print_list(disList.head) PrintList.add_tail(disList.head, 10) PrintList.print_list(disList.head)
from sort_asc import sort_asc def merge(train1, train2): node1 = train1 node2 = train2 if not isinstance(train1, Node) or not isinstance(train2, Node): print("This is not Node!") return while node1.next != None: node1 = node1.next node1.next = node2 return train1 s_list1 = SinglyList() a = Node(0) s_list1.add_head(a) node = s_list1.head b = Node(5) c = Node(8) d = Node(7) a.next = b b.next = c c.next = d s_list2 = SinglyList() x = Node(10) s_list2.add_head(x)
from singly_list import SinglyList from node import Node disList = SinglyList() disList.add_head(Node(4)) for i in range(1,10): disList.add_head(Node(i)) disList.add_head(Node(4)) disList.add_head(Node(5)) disList.add_head(Node(8)) disList.add_head(Node(4)) ''' #00 Test PrintList.print_list(disList.head) disList.add_head(Node("lmao")) PrintList.print_list(None) PrintList.print_list(disList.head) #01 Test PrintList.print_list(disList.head) PrintList.add_tail(disList.head, 10) PrintList.print_list(disList.head) ''' #02 Test SinglyList.print_list(disList.head) disList.remove(disList.head, 4) SinglyList.print_list(disList.head)
from node import Node from merge import merge from singly_list import SinglyList from print_list import print_list from remove import remove list = SinglyList() node0 = Node(1) node1 = Node(5) node0.next = node1 node1.next = None anode0 = Node(2) anode1 = Node(3) anode0.next = anode1 anode1.next = None print_list(merge(node0, anode0))