Skip to content
76 changes: 0 additions & 76 deletions programming_lab/lab301019/conta_elementi_lista.py

This file was deleted.

51 changes: 0 additions & 51 deletions programming_lab/lab301019/conta_occorrenze_lista.py

This file was deleted.

32 changes: 0 additions & 32 deletions programming_lab/lab301019/conta_righe.py

This file was deleted.

78 changes: 78 additions & 0 deletions programming_lab/lab301019/count_elements_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Oct 30 09:52:14 2019

@author: angelo
Write a function that takes a list and returns a new list made up of tuples.
Each element of the tuple has the first element which is an element of the input list and the second which is the number of times the element repeats starting from that position.
The same elements must be in the same order as the input list.

ex: [1,2,2,2,"b","a","a",2.0,2.0,2.0,"home","home",2.0]
returns
[(1,1),(2,3),("b",1),("a",2),(2.0,4),("home",2)]
"""


def count_elements_list(list):
dic = {}
for element in list:
dic[element] = list.count(element)
return list(dic.items())


def count_elements_list2(list):
dic = {}
for element in list:
if element not in dic:
dic[element] = list.count(element)
return list(dic.items())


def count_elements_list3(list):
dic = {}
for element in list:
if element not in dic:
dic[element] = 1
else:
dic[element] += 1
return list(dic.items())


def count_elements_list4(list):
dic = {}
for element in list:
t = element, type(element)
if t not in dic:
dic[t] = 1
else:
dic[t] += 1
list_ret = []
for tuple, repetitions in dic.items():
list_ret.append((tuple[0], repetitions))
return list_ret


def count_elements_list5(list):
dic = {(element, type(element)): 0 for element in list}
for element in list:
t = element, type(element)
dic[t] += 1
return [(tuple[0], dic[tuple]) for tuple in dic]


def count_elements_list6(list):
dic = {}
for element in list:
try:
dic[element] += 1
except:
dic[element] = 1
return list(dic.items())


def count_elements_list7(list):
dic = {}
for element in list:
dic[element] = dic.get(element, 0) + 1
return list(dic.items())
24 changes: 12 additions & 12 deletions programming_lab/lab301019/file_10.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,30 @@
Created on Wed Oct 30 16:25:54 2019

@author: angelo
Scriviamo una funzione che prende come parametro il nome di un file e ci stampa dentro tutti i numeri da 0 a 9 su righe diverse. Ritorna 0.
We write a function that takes the name of a file as a parameter and prints all the numbers from 0 to 9 on different lines. Returns 0.
"""


def file_10(nomefile):
f = open(nomefile, "w")
stringa = ""
def file_10(filename):
f = open(filename, "w")
string = ""
for i in range(10):
stringa += str(i) + "\n"
f.write(stringa)
string += str(i) + "\n"
f.write(string)
f.close()
return 0


def file_102(nomefile):
f = open(nomefile, "w")
def file_102(filename):
f = open(filename, "w")
for i in range(10):
f.write(str(i) + "\n")
f.close()
return 0


def file_103(nomefile):
with open(nomefile, "w") as f:
lista = [str(i) + "\n" for i in range(10)]
f.writelines(lista)
def file_103(filename):
with open(filename, "w") as f:
list = [str(i) + "\n" for i in range(10)]
f.writelines(list)
return 0
31 changes: 31 additions & 0 deletions programming_lab/lab301019/find_vowel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Oct 30 12:44:03 2019

@author: angelo
Write a function that takes a file name and returns the vowel that repeats the most times on the same line in the file. If there are vowels that are repeated the same number of times, the first one found returns.

ex:
rotto
topo
missili

returns i
"""


def find_vowel(filename):
with open(filename) as f:
maxvow = 0
voc = ""
for line in f:
for vowel in "aeiou":
cont = line.count(vowel)
if cont > maxvoc:
voc = vowel
maxvoc = cont
return voc


find_vowel("head_vowels.txt")
1 change: 1 addition & 0 deletions programming_lab/lab301019/foo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hi how are you?
7 changes: 7 additions & 0 deletions programming_lab/lab301019/head_vocals.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
aaaaaaaaaaaaa
top
broken
missile
horror
sticky
cicciociccio
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
Created on Wed Oct 30 17:49:25 2019

@author: angelo
Scrivere una funzione che prende una stringa che è il nome di un file, e ritorna la vocale che è presente più volte su una stessa riga del file
Write a function that takes a string that is the name of a file, and returns the vowel that occurs multiple times on the same line of the file

Es: se il file contiene
Ex: if the file contains
buffo
papa
pope
richiami

ritorna 'i'
return 'i'
"""


def vocale_massima(nomefile):
def maximum_vowel(filename):
ripvoc = 0
voc = ""
with open(nomefile) as f:
with open(filename) as f:
for line in f:
for v in "aeiou":
cont = line.count(v)
Expand Down
Loading