Python 3.8.6 | packaged by conda-forge | (default, Oct 7 2020, 19:08:05)
Type "copyright", "credits" or "license" for more information.
IPython 7.19.0 -- An enhanced Interactive Python.
In [1]: def MCD(x,y):
...: if x == y:
...: return x
...: elif x < y:
...: return MCD( x, y-x)
...: else:
...: return MCD(x-y, y)
...:
In [2]: MCD(75, 97)
Out[2]: 1
In [3]: MCD(75, 90)
Out[3]: 15
In [4]: from rtrace import *
In [5]: def MCD(x,y):
...: if x == y:
...: return x
...: elif x < y:
...: return MCD( x, y-x)
...: else:
...: return MCD(x-y, y)
In [6]: @trace
...: def MCD(x,y):
...: if x == y:
...: return x
...: elif x < y:
...: return MCD( x, y-x)
...: else:
...: return MCD(x-y, y)
In [7]: MCD.trace(75, 90)
------------------- Starting recursion -------------------
entering MCD(75, 90)
|-- entering MCD(75, 15)
|--|-- entering MCD(60, 15)
|--|--|-- entering MCD(45, 15)
|--|--|--|-- entering MCD(30, 15)
|--|--|--|--|-- entering MCD(15, 15)
|--|--|--|--|-- exiting MCD(15, 15) returns 15
|--|--|--|-- exiting MCD(30, 15) returns 15
|--|--|-- exiting MCD(45, 15) returns 15
|--|-- exiting MCD(60, 15) returns 15
|-- exiting MCD(75, 15) returns 15
exiting MCD(75, 90) returns 15
-------------------- Ending recursion --------------------
Num calls: 6
Out[7]: 15
In [8]: def stampa_lista(L):
...: if not L: # if len(L) == 0 # if L == []
...: pass
...: else:
...: print(L[0])
...: stampa_lista(L[1:])
...:
In [9]: stampa_lista([1,2,3,4,5])
1
2
3
4
5
In [10]: @trace
...: def stampa_lista(L):
...: if not L: # if len(L) == 0 # if L == []
...: pass
...: else:
...: print(L[0])
...: stampa_lista(L[1:])
In [11]: stampa_lista([1,2,3,4,5])
1
2
3
4
5
In [12]: stampa_lista.trace([1,2,3,4,5])
------------------- Starting recursion -------------------
entering stampa_lista([1, 2, 3, 4, 5],)
1
|-- entering stampa_lista([2, 3, 4, 5],)
2
|--|-- entering stampa_lista([3, 4, 5],)
3
|--|--|-- entering stampa_lista([4, 5],)
4
|--|--|--|-- entering stampa_lista([5],)
5
|--|--|--|--|-- entering stampa_lista([],)
|--|--|--|--|-- exiting stampa_lista([],) returns None
|--|--|--|-- exiting stampa_lista([5],) returns None
|--|--|-- exiting stampa_lista([4, 5],) returns None
|--|-- exiting stampa_lista([3, 4, 5],) returns None
|-- exiting stampa_lista([2, 3, 4, 5],) returns None
exiting stampa_lista([1, 2, 3, 4, 5],) returns None
-------------------- Ending recursion --------------------
Num calls: 6
In [13]: @trace
...: def stampa_lista_a_rovescio(L):
...: if not L: # if len(L) == 0 # if L == []
...: pass
...: else:
...: stampa_lista(L[1:])
...: print(L[0])
...:
In [14]: stampa_lista([1,2,3,4,5])
1
2
3
4
5
In [15]: @trace
...: def stampa_lista_a_rovescio(L):
...: if not L: # if len(L) == 0 # if L == []
...: pass
...: else:
...: stampa_lista_a_rovescio(L[1:])
...: print(L[0])
In [16]: stampa_lista_a_rovescio([1,2,3,4,5])
5
4
3
2
1
In [17]: def cerca_file
File "<ipython-input-17-ca9658955ef2>", line 1
def cerca_file
^
SyntaxError: invalid syntax
In [18]: def cerca_file
File "<ipython-input-18-ca9658955ef2>", line 1
def cerca_file
^
SyntaxError: invalid syntax
In [19]: import os
...: def cerca_file(D, ext):
...: os.list
...:
In [20]: import os
...: def cerca_file(D, ext):
...: os.listdir(D)
...:
In [21]: os.listdir('.')
Out[21]: ['__pycache__', 'lezione15.py', 'rtrace.py']
In [22]: import os
...: def cerca_file(D, ext):
...: for name in os.listdir(D):
...: fullname = os.path.join(D,name) # Linux '/' Windows '\' OsX '/'
...: fullname = D + '/' + name
...: if os.path.isfile(fullname):
...: if name.split('.')[-1] == ext:
...: print(fullname)
...: else:
...: cerca_file(fullname, ext)
...:
In [23]: cerca_file('.','py')
./lezione15.py
./rtrace.py
In [24]: cerca_file('.','pyc')
./__pycache__/rtrace.cpython-38.pyc
In [25]: import os
...: def dimensioni_file(D, ext, dizio=None):
...: "torniamo un dizionario 'nome_file' -> dimensione solo dei file con una certa estensione"
...: if dizio is None:
...: dizio = {}
...: for name in os.listdir(D):
...: fullname = os.path.join(D,name) # Linux '/' Windows '\' OsX '/'
...: fullname = D + '/' + name
...: if os.path.isfile(fullname):
...: if name.split('.')[-1] == ext:
...: print(os.stat(fullname))
...: else:
...: dimensioni_file(fullname, ext, dizio)
...:
In [26]: dimensioni_file('.','py')
os.stat_result(st_mode=33277, st_ino=15891655, st_dev=2051, st_nlink=1, st_uid=1000, st_gid=1000, st_size=671, st_atime=1606212612, st_mtime=1606212611, st_ctime=1606212611)
os.stat_result(st_mode=33188, st_ino=15891653, st_dev=2051, st_nlink=1, st_uid=1000, st_gid=1000, st_size=1563, st_atime=1606210537, st_mtime=1605819740, st_ctime=1606210523)
In [27]: import os
...: def dimensioni_file(D, ext, dizio=None):
...: "torniamo un dizionario 'nome_file' -> dimensione solo dei file con una certa estensione"
...: if dizio is None:
...: dizio = {}
...: for name in os.listdir(D):
...: fullname = os.path.join(D,name) # Linux '/' Windows '\' OsX '/'
...: fullname = D + '/' + name
...: if os.path.isfile(fullname):
...: if name.split('.')[-1] == ext:
...: dizio[fullname] = os.stat(fullname).st_size
...: else:
...: return dimensioni_file(fullname, ext, dizio)
In [28]: import os
...: def dimensioni_file(D, ext, dizio=None):
...: "torniamo un dizionario 'nome_file' -> dimensione solo dei file con una certa estensione"
...: if dizio is None:
...: dizio = {}
...: for name in os.listdir(D):
...: fullname = os.path.join(D,name) # Linux '/' Windows '\' OsX '/'
...: fullname = D + '/' + name
...: if os.path.isfile(fullname):
...: if name.split('.')[-1] == ext:
...: dizio[fullname] = os.stat(fullname).st_size
...: else:
...: return dimensioni_file(fullname, ext, dizio)
...: return dizio
...:
In [29]: dimensioni_file('.','py')
Out[29]: {}
In [30]: import os
...: def dimensioni_file(D, ext, dizio=None):
...: "torniamo un dizionario 'nome_file' -> dimensione solo dei file con una certa estensione"
...: if dizio is None:
...: dizio = {}
...: for name in os.listdir(D):
...: fullname = os.path.join(D,name) # Linux '/' Windows '\' OsX '/'
...: fullname = D + '/' + name
...: if os.path.isfile(fullname):
...: if name.split('.')[-1] == ext:
...: dizio[fullname] = os.stat(fullname).st_size
...: else:
...: return dimensioni_file(fullname, ext, dizio)
...: return dizio
...:
In [31]: dimensioni_file('.','py')
Out[31]: {}
In [32]: debugfile('/home/andrea/Documents/Uni/Didattica/Prog1/2020-21/Lezioni/lezione15-24-11-20/lezione15.py', wdir='/home/andrea/Documents/Uni/Didattica/Prog1/2020-21/Lezioni/lezione15-24-11-20')
Reloaded modules: rtrace
> /home/andrea/Documents/Uni/Didattica/Prog1/2020-21/Lezioni/lezione15-24-11-20/lezione15.py(3)<module>()
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
----> 3 """
4 Created on Tue Nov 24 10:32:45 2020
5
ipdb> continue
> /home/andrea/Documents/Uni/Didattica/Prog1/2020-21/Lezioni/lezione15-24-11-20/lezione15.py(42)dimensioni_file()
40 def dimensioni_file(D, ext, dizio=None):
41 "torniamo un dizionario 'nome_file' -> dimensione solo dei file con una certa estensione"
11-> 42 if dizio is None:
43 dizio = {}
44 for name in os.listdir(D):
--Call--
--Return--
ipdb>
In [33]: import os
...: def dimensioni_file(D, ext, dizio=None):
...: "torniamo un dizionario 'nome_file' -> dimensione solo dei file con una certa estensione"
...: if dizio is None:
...: dizio = {}
...: for name in os.listdir(D):
...: fullname = os.path.join(D,name) # Linux '/' Windows '\' OsX '/'
...: fullname = D + '/' + name
...: if os.path.isfile(fullname):
...: if name.split('.')[-1] == ext:
...: dizio[fullname] = os.stat(fullname).st_size
...: else:
...: dizio = dimensioni_file(fullname, ext, dizio)
...: return dizio
...:
In [34]: dimensioni_file('.','py')
Out[34]: {'./lezione15.py': 1257, './rtrace.py': 1563}
In [35]: def merge(L1, L2):
...: # caso base, controllo se le liste sono vuote
...: if not L1: return L2
...: #if not L2: return L1
...: # inserisco un invariante per stare sul sicuro
...: assert len(L1)>0 and len(L2)>0, "una delle due liste è vuota"
...: # sono sicuro che esistano due primi elementi
...: A = L1[0]
...: B = L2[0]
...: if A < B:
...: # torno il valore A seguito dal merge del resto di L1 e di L2
...: return [A] + merge(L1[1:], L2)
...: else:
...: return [B] + merge(L1, L2[1:])
...:
In [36]: L1 = [2, 4, 6, 8, 10]
In [37]: L2 = [1, 3, 5, 7, 9]
In [38]: merge(L1, L2)
Traceback (most recent call last):
File "<ipython-input-38-e75ad75fba81>", line 1, in <module>
merge(L1, L2)
File "<ipython-input-35-38b80b89071b>", line 14, in merge
return [B] + merge(L1, L2[1:])
File "<ipython-input-35-38b80b89071b>", line 12, in merge
return [A] + merge(L1[1:], L2)
File "<ipython-input-35-38b80b89071b>", line 14, in merge
return [B] + merge(L1, L2[1:])
File "<ipython-input-35-38b80b89071b>", line 12, in merge
return [A] + merge(L1[1:], L2)
File "<ipython-input-35-38b80b89071b>", line 14, in merge
return [B] + merge(L1, L2[1:])
File "<ipython-input-35-38b80b89071b>", line 12, in merge
return [A] + merge(L1[1:], L2)
File "<ipython-input-35-38b80b89071b>", line 14, in merge
return [B] + merge(L1, L2[1:])
File "<ipython-input-35-38b80b89071b>", line 12, in merge
return [A] + merge(L1[1:], L2)
File "<ipython-input-35-38b80b89071b>", line 14, in merge
return [B] + merge(L1, L2[1:])
File "<ipython-input-35-38b80b89071b>", line 6, in merge
assert len(L1)>0 and len(L2)>0, "una delle due liste è vuota"
AssertionError: una delle due liste è vuota
In [39]: def merge(L1, L2):
...: # caso base, controllo se le liste sono vuote
...: if not L1: return L2
...: if not L2: return L1
...: # inserisco un invariante per stare sul sicuro
...: assert len(L1)>0 and len(L2)>0, "una delle due liste è vuota"
...: # sono sicuro che esistano due primi elementi
...: A = L1[0]
...: B = L2[0]
...: if A < B:
...: # torno il valore A seguito dal merge del resto di L1 e di L2
...: return [A] + merge(L1[1:], L2)
...: else:
...: return [B] + merge(L1, L2[1:])
...:
In [40]: merge(L1, L2)
Out[40]: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
In [41]: @trace
...: def merge(L1, L2):
...: # caso base, controllo se le liste sono vuote
...: if not L1: return L2
...: if not L2: return L1
...: # inserisco un invariante per stare sul sicuro
...: assert len(L1)>0 and len(L2)>0, "una delle due liste è vuota"
...: # sono sicuro che esistano due primi elementi
...: A = L1[0]
...: B = L2[0]
...: if A < B:
...: # torno il valore A seguito dal merge del resto di L1 e di L2
...: return [A] + merge(L1[1:], L2)
...: else:
...: return [B] + merge(L1, L2[1:])
In [42]: merge.trace(L1, L2)
------------------- Starting recursion -------------------
entering merge([2, 4, 6, 8, 10], [1, 3, 5, 7, 9])
|-- entering merge([2, 4, 6, 8, 10], [3, 5, 7, 9])
|--|-- entering merge([4, 6, 8, 10], [3, 5, 7, 9])
|--|--|-- entering merge([4, 6, 8, 10], [5, 7, 9])
|--|--|--|-- entering merge([6, 8, 10], [5, 7, 9])
|--|--|--|--|-- entering merge([6, 8, 10], [7, 9])
|--|--|--|--|--|-- entering merge([8, 10], [7, 9])
|--|--|--|--|--|--|-- entering merge([8, 10], [9])
|--|--|--|--|--|--|--|-- entering merge([10], [9])
|--|--|--|--|--|--|--|--|-- entering merge([10], [])
|--|--|--|--|--|--|--|--|-- exiting merge([10], []) returns [10]
|--|--|--|--|--|--|--|-- exiting merge([10], [9]) returns [9, 10]
|--|--|--|--|--|--|-- exiting merge([8, 10], [9]) returns [8, 9, 10]
|--|--|--|--|--|-- exiting merge([8, 10], [7, 9]) returns [7, 8, 9, 10]
|--|--|--|--|-- exiting merge([6, 8, 10], [7, 9]) returns [6, 7, 8, 9, 10]
|--|--|--|-- exiting merge([6, 8, 10], [5, 7, 9]) returns [5, 6, 7, 8, 9, 10]
|--|--|-- exiting merge([4, 6, 8, 10], [5, 7, 9]) returns [4, 5, 6, 7, 8, 9, 10]
|--|-- exiting merge([4, 6, 8, 10], [3, 5, 7, 9]) returns [3, 4, 5, 6, 7, 8, 9, 10]
|-- exiting merge([2, 4, 6, 8, 10], [3, 5, 7, 9]) returns [2, 3, 4, 5, 6, 7, 8, 9, 10]
exiting merge([2, 4, 6, 8, 10], [1, 3, 5, 7, 9]) returns [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
-------------------- Ending recursion --------------------
Num calls: 10
Out[42]: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
In [43]: from random import choices
In [44]: A = choices(range(0, 1000), k=30)
In [45]: A
Out[45]:
[726,
575,
893,
180,
881,
685,
354,
689,
890,
425,
919,
428,
72,
674,
934,
857,
760,
850,
838,
905,
32,
538,
11,
587,
522,
128,
630,
356,
894,
727]
In [46]: %pprint
Pretty printing has been turned OFF
In [47]: A
Out[47]: [726, 575, 893, 180, 881, 685, 354, 689, 890, 425, 919, 428, 72, 674, 934, 857, 760, 850, 838, 905, 32, 538, 11, 587, 522, 128, 630, 356, 894, 727]
In [48]: from lezione15 import *
In [49]: mergesort(A)
Out[49]: [11, 32, 72, 128, 180, 354, 356, 425, 428, 522, 538, 575, 587, 630, 674, 685, 689, 726, 727, 760, 838, 850, 857, 881, 890, 893, 894, 905, 919, 934]
In [50]: import importlib
In [51]: importlib.reload(lezione15)
Traceback (most recent call last):
File "<ipython-input-51-113e10460fb2>", line 1, in <module>
importlib.reload(lezione15)
NameError: name 'lezione15' is not defined
In [52]: import lezione15
In [53]: A = [ -56, -90, 45, 81, -32 ]
In [54]: lezione15.mergesort.trace(A)
------------------- Starting recursion -------------------
entering mergesort([-56, -90, 45, 81, -32],)
|-- entering mergesort([-56, -90],)
|--|-- entering mergesort([-56],)
|--|-- exiting mergesort([-56],) returns [-56]
|--|-- entering mergesort([-90],)
|--|-- exiting mergesort([-90],) returns [-90]
|-- exiting mergesort([-56, -90],) returns [-90, -56]
|-- entering mergesort([45, 81, -32],)
|--|-- entering mergesort([45],)
|--|-- exiting mergesort([45],) returns [45]
|--|-- entering mergesort([81, -32],)
|--|--|-- entering mergesort([81],)
|--|--|-- exiting mergesort([81],) returns [81]
|--|--|-- entering mergesort([-32],)
|--|--|-- exiting mergesort([-32],) returns [-32]
|--|-- exiting mergesort([81, -32],) returns [-32, 81]
|-- exiting mergesort([45, 81, -32],) returns [-32, 45, 81]
exiting mergesort([-56, -90, 45, 81, -32],) returns [-90, -56, -32, 45, 81]
-------------------- Ending recursion --------------------
Num calls: 9
Out[54]: [-90, -56, -32, 45, 81]
In [55]: import fsnode
In [56]: fsnode.FSNode('.', False)
Traceback (most recent call last):
File "<ipython-input-56-370869468251>", line 1, in <module>
fsnode.FSNode('.', False)
File "/home/andrea/Documents/Uni/Didattica/Prog1/2020-21/Lezioni/lezione15-24-11-20/fsnode.py", line 20, in __init__
self.content = esplora_dir(filename)
File "/home/andrea/Documents/Uni/Didattica/Prog1/2020-21/Lezioni/lezione15-24-11-20/fsnode.py", line 39, in esplora_dir
D = FSNode(fullname, False) # creo l'oggetto per la directory
File "/home/andrea/Documents/Uni/Didattica/Prog1/2020-21/Lezioni/lezione15-24-11-20/fsnode.py", line 20, in __init__
self.content = esplora_dir(filename)
File "/home/andrea/Documents/Uni/Didattica/Prog1/2020-21/Lezioni/lezione15-24-11-20/fsnode.py", line 36, in esplora_dir
F = FSNode(fullname, True) # creo l'oggeto per il file
File "/home/andrea/Documents/Uni/Didattica/Prog1/2020-21/Lezioni/lezione15-24-11-20/fsnode.py", line 18, in __init__
self.content = F.read()
File "/opt/anaconda3/envs/F20/lib/python3.8/codecs.py", line 322, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe3 in position 16: invalid continuation byte
In [57]: importlib.reload(fsnode)
Out[57]: <module 'fsnode' from '/home/andrea/Documents/Uni/Didattica/Prog1/2020-21/Lezioni/lezione15-24-11-20/fsnode.py'>
In [58]: fsnode.FSNode('.', False)
Traceback (most recent call last):
File "/opt/anaconda3/envs/F20/lib/python3.8/site-packages/IPython/core/formatters.py", line 693, in __call__
return repr(obj)
File "/home/andrea/Documents/Uni/Didattica/Prog1/2020-21/Lezioni/lezione15-24-11-20/fsnode.py", line 27, in __repr__
risultato += '\n\t' + f
TypeError: can only concatenate str (not "FSNode") to str
In [59]: importlib.reload(fsnode)
Out[59]: <module 'fsnode' from '/home/andrea/Documents/Uni/Didattica/Prog1/2020-21/Lezioni/lezione15-24-11-20/fsnode.py'>
In [60]: fsnode.FSNode('.', False)
Out[60]:
FSNode(., False)
FSNode(./__pycache__, False)
FSNode(./__pycache__/rtrace.cpython-38.pyc, True)
FSNode(./__pycache__/fsnode.cpython-38.pyc, True)
FSNode(./__pycache__/lezione15.cpython-38.pyc, True)
FSNode(./lezione15.py, True)
FSNode(./rtrace.py, True)
FSNode(./fsnode.py, True)
In [61]: