97 lines
1.9 KiB
Python
97 lines
1.9 KiB
Python
class Nod:
|
|
def __init__(self, e):
|
|
self.e = e
|
|
self.urm = None
|
|
|
|
class Lista:
|
|
def __init__(self):
|
|
self.prim = None
|
|
|
|
|
|
'''
|
|
crearea unei liste din valori citite pana la 0
|
|
'''
|
|
def creareLista():
|
|
lista = Lista()
|
|
lista.prim = creareLista_rec()
|
|
return lista
|
|
|
|
def creareLista_rec():
|
|
x = int(input("x="))
|
|
if x == 0:
|
|
return None
|
|
else:
|
|
nod = Nod(x)
|
|
nod.urm = creareLista_rec()
|
|
return nod
|
|
|
|
'''
|
|
tiparirea elementelor unei liste
|
|
'''
|
|
def tipar(lista):
|
|
tipar_rec(lista.prim)
|
|
|
|
def tipar_rec(nod):
|
|
if nod != None:
|
|
print (nod.e)
|
|
tipar_rec(nod.urm)
|
|
|
|
|
|
'''
|
|
program pentru test
|
|
'''
|
|
|
|
def gcd(a, b):
|
|
if b == 0:
|
|
return a
|
|
else:
|
|
return gcd(b, a%b)
|
|
|
|
def lcm(a, b):
|
|
return a*b//gcd(a, b)
|
|
|
|
def lcm_list(lista):
|
|
if lista.prim == None:
|
|
return 1
|
|
else:
|
|
new_list = Lista()
|
|
new_list.prim = lista.prim.urm
|
|
return lcm(lista.prim.e, lcm_list(new_list))
|
|
|
|
def f(lista, e, e1):
|
|
if lista.prim == None:
|
|
return Lista()
|
|
if lista.prim.e != e:
|
|
new_list = Lista()
|
|
new_list.prim = lista.prim.urm
|
|
lista_returnata = f(new_list, e, e1)
|
|
lista_de_returnat = Lista()
|
|
lista_de_returnat.prim = Nod(lista.prim.e)
|
|
lista_de_returnat.prim.urm = lista_returnata.prim
|
|
return lista_de_returnat
|
|
else:
|
|
new_list = Lista()
|
|
new_list.prim = lista.prim.urm
|
|
lista_returnata = f(new_list, e, e1)
|
|
lista_de_returnat = Lista()
|
|
lista_de_returnat.prim = Nod(e1)
|
|
lista_de_returnat.prim.urm = lista_returnata.prim
|
|
return lista_de_returnat
|
|
|
|
|
|
|
|
def main():
|
|
list = creareLista()
|
|
tipar(list)
|
|
print(lcm_list(list))
|
|
e=int(input("e="))
|
|
e1=int(input("e1="))
|
|
list1=f(list,e,e1)
|
|
tipar(list1)
|
|
|
|
main()
|
|
|
|
|
|
|
|
|
|
|