# intretesare linii din fisiere in adt # prelucrare linii din adt # Se scrie in Python un program (utilizand threading) care va crea un fiser din # din intreteserea linie cu linie (spre exemplu o linie din primul, doua din al doilea,etc) # a patru fisier utilizand firele cu 'with'. Fisierele test vor fi create de programator cu # cateva linii fiecare. Fiecare linie citita din fiecare fisier de # intrare este depusa intr-un ADT unde cu ajutorul unei procesari lambda(vezi PyFunctional) # este filtrata (se elimina spatiile albe) import threading from functional import seq class Tailor(threading.Thread): def __init__(self, file1, file2, file3, file4): threading.Thread.__init__(self) self.file1 = file1 self.file2 = file2 self.file3 = file3 self.file4 = file4 self.lines = [] def sew_lines(self): with open(self.file1) as fis1: with open(self.file2) as fis2: with open(self.file3) as fis3: with open(self.file4) as fis4: carry_on = True while carry_on: carry_on = False line1 = fis1.readline() if len(line1) > 0: self.lines.append(line1) carry_on = True for _ in range(0, 2): line2 = fis2.readline() if len(line2) > 0: self.lines.append(line2) carry_on = True for _ in range(0, 3): line3 = fis3.readline() if len(line3) > 0: self.lines.append(line3) carry_on = True for _ in range(0, 4): line4 = fis4.readline() if len(line4) > 0: self.lines.append(line4) carry_on = True self.lines = seq(self.lines).map(func=lambda line: line.strip()) with open("result.txt", "w") as result: for line in self.lines: result.write(line) def run(self): self.sew_lines() if __name__ == '__main__': tailor = Tailor("fis1.txt", "fis2.txt", "fis3.txt", "fis4.txt") tailor.start() SEARCH AND REPLACE if __name__ == '__main__': search_text = input("sirul care trebuie cautat=") number_of_files = int(input("numarul de fisiere=")) fnames = [] for _ in range(number_of_files): file_name = input("nume fisier=") fnames.append(file_name) replace_flag = input("doresti sa inlocuiesti cu ceva textul(y/n)?\n").lower() == 'y' replace_text = "" if replace_flag: replace_text = input("noul text=") for name in fnames: file_name_written = False replaced_lines = [] with open(name, "r") as file: lines = file.readlines() replaced_lines = lines.copy() for line in lines: if search_text in line: if not file_name_written: file_name_written = True print(f"inside {name}") index=lines.index(line) print(f"at line {index}") if replace_flag: replaced_lines[index] = line.replace(search_text, replace_text) #this is bs if replace_flag: with open(name, "w") as file: file.writelines(replaced_lines) JOKES from random import choice class Dictionary: def __init__(self, file_name): self.joke_book = { "*": [], "**": [], "***": [] } with open(file_name, "r") as file: for line in file.readlines(): self.joke_book[line.split()[0]].append(line.strip()) def get_joke(self, joke_rank): return choice(self.joke_book[joke_rank]) class GoodJokeStrategy: def __init__(self, joke_collection): self.joke_collection = joke_collection def get_joke(self): return self.joke_collection.get_joke("*") class VeryGoodJokeStrategy: def __init__(self, joke_collection): self.joke_collection = joke_collection def get_joke(self): return self.joke_collection.get_joke("**") class TheBestJokeStrategy: def __init__(self, joke_collection): self.joke_collection = joke_collection def get_joke(self): return self.joke_collection.get_joke("***") class Student: def __init__(self, joke_book): self.joke_book = joke_book self.strategy = GoodJokeStrategy(self.joke_book) def get_feedback(self, feedback): if feedback == "sad": self.strategy = TheBestJokeStrategy(self.joke_book) if feedback == "serious": self.strategy = VeryGoodJokeStrategy(self.joke_book) if feedback == "happy": self.strategy = GoodJokeStrategy(self.joke_book) def tell_a_joke(self): return self.strategy.get_joke() def listen(self, joke): print(f"i listen to {joke}") joke_rank = joke.split()[0] if joke_rank == "*": return "sad" if joke_rank == "**": return "serious" if joke_rank == "***": return "happy" if __name__ == '__main__': joke_book = Dictionary("jokes.txt") student1 = Student(joke_book) student2 = Student(joke_book) feedback = student2.listen(student1.tell_a_joke()) print(feedback) student1.get_feedback(feedback) feedback = student2.listen(student1.tell_a_joke()) student1.get_feedback(feedback) print(feedback) AUTOMAT PAR/IMPAR from functional import seq class StateZero: def __init__(self, automat): self.automat = automat def operation(self): if(seq(self.automat.lista).non_empty()): print(self.automat.lista) self.automat.current_state = self.automat.states[1] else: self.automat.keep_running = False print("lista vida") class StateOne: def __init__(self, automat): self.automat = automat def operation(self): if(seq(self.automat.lista).exists(lambda number: number % 2 == 0)): self.automat.lista.remove(seq(self.automat.lista).\ find(lambda number: number % 2 == 0)) self.automat.current_state = self.automat.states[2] class StateTwo: def __init__(self, automat): self.automat = automat def operation(self): if(seq(self.automat.lista).exists(lambda number: number % 2 == 1)): self.automat.lista.remove(seq(self.automat.lista).\ find(lambda number: number % 2 == 1)) self.automat.current_state = self.automat.states[0] class Automat: def __init__(self, lista_numere): self.lista = lista_numere self.states = [StateZero(self),StateOne(self),StateTwo(self)] self.current_state = self.states[0] self.keep_running = True def start(self): while self.keep_running: self.current_state.operation() if __name__ == '__main__': automat = Automat([2, 3, 4, 5, 6, 7, 8]) automat.start() AUTOMAT NR PRIM from functional import seq #asa arata functia de detectie a unui numar prim isPrime = lambda number: all(number % divider != 0 for divider in range(2, int(number/2)+1)) #detecteaza daca un numar nu este prim isNotPrime = lambda number: any(number % divider == 0 for divider in range(2, int(number/2)+1)) class StateZero: def __init__(self, automat): self.automat = automat def operation(self): if(seq(self.automat.lista).non_empty()): print(self.automat.lista) self.automat.current_state = self.automat.states[1] else: self.automat.keep_running = False print("lista vida") class StateOne: def __init__(self, automat): self.automat = automat def operation(self): if(seq(self.automat.lista).exists(isPrime)): self.automat.lista.remove(seq(self.automat.lista).\ find(lambda number: all(number % divider != 0 for divider in range(2, int(number/2)+1)))) self.automat.current_state=self.automat.states[2] class StateTwo: def __init__(self, automat): self.automat = automat def operation(self): if(seq(self.automat.lista).exists(lambda num: not isPrime(num))): self.automat.lista.remove(seq(self.automat.lista).\ find(lambda number: any(number % divider == 0 for divider in range(2, int(number/2)+1)))) self.automat.current_state=self.automat.states[0] class Automat: def __init__(self, lista_numere): self.lista = lista_numere self.states = [StateZero(self), StateOne(self), StateTwo(self)] self.current_state = self.states[0] self.keep_running = True def start(self): while self.keep_running: self.current_state.operation() if __name__ == '__main__': automat = Automat([2, 3, 4, 5, 6, 7, 8]) automat.start() Threaduri min/max import threading def readFromFile(): pass def read(): thread_1 = threading.Thread(target=readFromFile) thread_2 = threading.Thread(target=readFromFile()) thread_3 = threading.Thread(target=readFromFile()) thread_4 = threading.Thread(target=readFromFile()) thread_5 = threading.Thread(target=readFromFile()) thread_1.start() thread_2.start() thread_3.start() thread_4.start() thread_5.start() thread_1.join() thread_2.join() thread_3.join() thread_4.join() thread_5.join() #if __name__ == '__main__': pass ''' Utilizand modelul fatada FACADE in cadrul unui program Python sa se creeze un obiect de tip boombox (acesta este compus din metode specifice: play, record, fast forward, rewind, radio, volume, batery status, .etc). Se vor desene diagrama de clase si de obiecte. Se va explica maniera de aplicare a principiilor SOLID. ''' from abc import abstractmethod, ABC class Option: @abstractmethod def f(self): pass class Play(Option): def f(self): print("BoomBox play! ") class Record(Option): def f(self): print("BoomBox record! ") class FastForward(Option): def f(self): print("BoomBox fast forward! ") class Rewind(Option): def f(self): print("BoomBox fast rewind! ") class BoomBox: play = Play() record = Record() fast_forward = FastForward() rewind = Rewind() def __init__(self): pass # if __name__ == '__main__': # player = BoomBox() # player.play.f() # player.record.f() # player.fast_forward.f() # player.rewind.f() ''' # Utilizand modelul POD pot sa se scrie un program Python care va desena in mod # grafic un triunghi, un cerc si un dreptunghi. Se vor desena diagrama de clase si de obiecte. # Se va explica maniera de aplicare a principiilor SOLID. ''' from tkinter import Tk, Canvas, Frame, BOTH import math class Color: def __init__(self, c): self.val = c class Shape: color = Color('#000') def draw(self, canvas): pass class Displayer(Frame): def __init__(self, root, **kw): super().__init__(**kw) self.root = root self.canvas = Canvas(root) self.shape = Shape() def addShape(self, shape): self.shape = shape def drawShape(self): self.shape.draw(self.canvas) class Rectangle(Shape): def __init__(self, top_left, bottom_right, color): self.top_left = top_left self.bottom_right = bottom_right self.color = color def draw(self, canvas): canvas.create_rectangle(self.top_left[0], self.top_left[1], self.bottom_right[0], self.bottom_right[1], fill=self.color.val) canvas.pack(fill=BOTH, expand=1) class Triangle(Shape): def __init__(self, l1, l2, l3, color): self.l1 = l1 self.l2 = l2 self.l3 = l3 self.color = color def draw(self, canvas): canvas.create_polygon(self.l1[0], self.l1[1], self.l2[0], self.l2[1], self.l3[0], self.l3[1], fill=self.color.val) canvas.pack(fill=BOTH, expand=1) def expand(self, times): self.l1 = self.l1 * times self.l2 = self.l2 * times self.l3 = self.l3 * times class Circle(Shape): def __init__(self, center, radius, color): self.radius = radius self.color = color self.center = center def draw(self, canvas): canvas.create_oval(self.center[0] - self.radius * math.sqrt(2), self.center[1] - self.radius * math.sqrt(2), self.center[0] + self.radius * math.sqrt(2), self.center[1] + self.radius * math.sqrt(2), fill=self.color.val) canvas.pack(fill=BOTH, expand=1) def expand(self, times): self.radius = self.radius * times def main(): root = Tk() root.title("cod maimuta") displayer = Displayer(root) displayer.addShape(Rectangle((10, 10), (50, 50), Color('#fff'))) displayer.drawShape() displayer.addShape(Triangle((20, 30), (75, 95), (100, 32), Color('#461'))) displayer.drawShape() displayer.addShape(Circle((200, 200), 20, Color('#2f6'))) displayer.drawShape() root.mainloop() # if __name__ == '__main__': main()