Es. Python tutorial - Funzioni
Versione italiana
Esercizi
Esercizio 1: Somma Scrivi una funzione
somma(a, b)
che restituisce la somma di due numeri.Esercizio 2: Massimo Scrivi una funzione
massimo(x, y)
che restituisce il valore massimo tra due numeri.Esercizio 3: Parole lunghe Scrivi una funzione
parole_lunghe(lista_parole, lunghezza)
che restituisce una lista di parole che hanno una lunghezza maggiore di un valore specificato.Esercizio 4: Fattoriale Scrivi una funzione
fattoriale(n)
che restituisce il fattoriale di un numero intero non negativo.Esercizio 5: Palindromo Scrivi una funzione
è_palindromo(parola)
che restituisceTrue
se la parola è un palindromo, altrimentiFalse
.Esercizio 6: Fibonacci Scrivi una funzione
fibonacci(n)
che restituisce il n-esimo numero della sequenza di Fibonacci.Esercizio 7: Conta vocali Scrivi una funzione
conta_vocali(stringa)
che restituisce il numero di vocali in una stringa.Esercizio 8: Inverso Scrivi una funzione
inverso(lista)
che restituisce una nuova lista con gli elementi in ordine inverso.Esercizio 9: Unione di liste Scrivi una funzione
unione_liste(lista1, lista2)
che restituisce una lista contenente gli elementi unici di entrambe le liste.Esercizio 10: Media Scrivi una funzione
media(lista_numeri)
che restituisce la media dei numeri in una lista.
Risposte
def somma(a, b): return a + b
def massimo(x, y): return x if x > y else y
def parole_lunghe(lista_parole, lunghezza): return [parola for parola in lista_parole if len(parola) > lunghezza]
def fattoriale(n): if n == 0: return 1 else: return n * fattoriale(n - 1)
def è_palindromo(parola): return parola == parola[::-1]
def fibonacci(n): if n <= 0: return 0 elif n == 1: return 1 else: return fibonacci(n - 1) + fibonacci(n - 2)
def conta_vocali(stringa): = 'aeiouAEIOU' vocali return sum(1 for char in stringa if char in vocali)
def inverso(lista): return lista[::-1]
def unione_liste(lista1, lista2): return list(set(lista1) | set(lista2))
python def media(lista_numeri): return sum(lista_numeri) / len(lista_numeri) if lista_numeri else 0
English version
Exercises
Exercise 1: Sum Write a function
sum(a, b)
that returns the sum of two numbers.Exercise 2: Maximum Write a function
maximum(x, y)
that returns the maximum value of two numbers.Exercise 3: Long Words Write a function
long_words(word_list, length)
that returns a list of words that have a length greater than a specified value.Exercise 4: Factorial Write a function
factorial(n)
that returns the factorial of a non-negative integer.Exercise 5: Palindrome Write a function
is_palindrome(word)
that returnsTrue
if the word is a palindrome, otherwiseFalse
.Exercise 6: Fibonacci Write a function
fibonacci(n)
that returns the nth number in the Fibonacci sequence.Exercise 7: Count Vowels Write a function
count_vowels(string)
that returns the number of vowels in a string.Exercise 8: Inverse Write a function
inverse(list)
that returns a new list with the elements in reverse order.Exercise 9: Union of Lists Write a function
union_lists(list1, list2)
that returns a list containing the unique elements of both lists.Exercise 10: Average Write a function
average(list_numbers)
that returns the average of the numbers in a list.
Answers
def sum(a, b):
return a + b
def max(x, y):
return x if x > y else y
def long_words(wordlist, length):
return [word for word in wordlist if len(word) > length]
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
def is_palindrome(word):
return word == word[::-1]
def fibonacci(n):
if n <= 0:
return 0
elif n == 1:
return 1
else:
return fibonacci(n - 1) + fibonacci(n - 2)
def voice_count(string):
= 'aeiouAEIOU'
vowels return sum(1 for char in string if char in vowels)
def inverse(list):
return list[::-1]
def union_lists(list1, list2):
return list(set(list1) | set(list2))
def average(list_numbers):
return sum(number_list) / len(number_list) if number_list else 0
Commenti
Posta un commento