Es. Python tutorial - Funzioni 2
Versione italiana
Esercizi
Esercizio 1: Potenza Scrivi una funzione
potenza(base, esponente)
che restituisce il risultato dibase
elevato aesponente
.Esercizio 2: Numero primo Scrivi una funzione
è_primo(n)
che restituisceTrue
sen
è un numero primo, altrimentiFalse
.Esercizio 3: Somma dei quadrati Scrivi una funzione
somma_quadrati(n)
che restituisce la somma dei quadrati dei numeri da 1 an
.Esercizio 4: Conta consonanti Scrivi una funzione
conta_consonanti(stringa)
che restituisce il numero di consonanti in una stringa.Esercizio 5: Lista dei quadrati Scrivi una funzione
lista_quadrati(n)
che restituisce una lista contenente i quadrati dei numeri da 1 an
.Esercizio 6: Rimuovi duplicati Scrivi una funzione
rimuovi_duplicati(lista)
che restituisce una lista senza duplicati.Esercizio 7: Anagramma Scrivi una funzione
è_anagramma(parola1, parola2)
che restituisceTrue
separola1
è un anagramma diparola2
, altrimentiFalse
.Esercizio 8: Somma dei numeri in una lista Scrivi una funzione
somma_lista(lista)
che restituisce la somma di tutti i numeri in una lista.Esercizio 9: Trova il secondo massimo Scrivi una funzione
secondo_massimo(lista)
che restituisce il secondo numero più grande in una lista.Esercizio 10: Stringa invertita Scrivi una funzione
stringa_invertita(stringa)
che restituisce la stringa invertita.
Risposte
def potenza(base, esponente): return base ** esponente
def è_primo(n): if n <= 1: return False for i in range(2, int(n**0.5) + 1): if n % i == 0: return False return True
def somma_quadrati(n): return sum(i**2 for i in range(1, n + 1))
def conta_consonanti(stringa): = 'bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ' consonanti return sum(1 for char in stringa if char in consonanti)
def lista_quadrati(n): return [i**2 for i in range(1, n + 1)]
def rimuovi_duplicati(lista): return list(set(lista))
def è_anagramma(parola1, parola2): return sorted(parola1) == sorted(parola2)
def somma_lista(lista): return sum(lista)
def secondo_massimo(lista): if len(lista) < 2: return None = secondo = float('-inf') primo for numero in lista: if numero > primo: = primo secondo = numero primo elif primo > numero > secondo: = numero secondo return secondo if secondo != float('-inf') else None
python def stringa_invertita(stringa): return stringa[::-1]
English version
Exercises
Exercise 1: Power Write a function
power(base, exponent)
that returns the result ofbase
raised to the power ofexponent
.Exercise 2: Prime Number Write a function
is_prime(n)
that returnsTrue
ifn
is a prime number, otherwiseFalse
.Exercise 3: Sum of Squares Write a function
sum_squares(n)
that returns the sum of the squares of the numbers from 1 ton
.Exercise 4: Count Consonants Write a function
count_consonants(string)
that returns the number of consonants in a string.Exercise 5: List of squares Write a function
list_squares(n)
that returns a list containing the squares of the numbers 1 throughn
.Exercise 6: Remove duplicates Write a function
remove_duplicates(list)
that returns a list with no duplicates.Exercise 7: Anagram Write a function
is_anagram(word1, word2)
that returnsTrue
ifword1
is an anagram ofword2
, otherwiseFalse
.Exercise 8: Sum of numbers in a list Write a function
sum_list(list)
that returns the sum of all the numbers in a list.Exercise 9: Find the second maximum Write a function
second_maximum(list)
that returns the second largest number in a list.Exercise 10: Reversed string Write a function
reversed_string(string)
that returns the reversed string.
Answers
def power(base, exponent):
return base ** exponent
def is_first(n):
if n <= 1:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
def sum_squares(n):
return sum(i**2 for i in range(1, n + 1))
def count_consonants(string):
= 'bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ'
consonants return sum(1 for char in string if char in consonants)
def list_squares(n):
return [i**2 for i in range(1, n + 1)]
def remove_duplicates(list):
return list(set(list))
def is_anagram(word1, word2):
return sorted(word1) == sorted(word2)
def sum_list(list):
return sum(list)
def second_max(list):
if len(list) < 2:
return None
= second = float('-inf')
first for number in list:
if number > first:
= first
second = number
first elif first > number > second:
= number
second return second if second != float('-inf') else None
def reversed_string(string):
return string[::-1]
Commenti
Posta un commento