Es. Python tutorial - Funzioni 2

Es. Python tutorial - Funzioni 2 Es. Python tutorial - Funzioni 2

Es. Python tutorial - Funzioni 2

Versione italiana

Esercizi

  1. Esercizio 1: Potenza Scrivi una funzione potenza(base, esponente) che restituisce il risultato di base elevato a esponente.

  2. Esercizio 2: Numero primo Scrivi una funzione è_primo(n) che restituisce True se n è un numero primo, altrimenti False.

  3. Esercizio 3: Somma dei quadrati Scrivi una funzione somma_quadrati(n) che restituisce la somma dei quadrati dei numeri da 1 a n.

  4. Esercizio 4: Conta consonanti Scrivi una funzione conta_consonanti(stringa) che restituisce il numero di consonanti in una stringa.

  5. Esercizio 5: Lista dei quadrati Scrivi una funzione lista_quadrati(n) che restituisce una lista contenente i quadrati dei numeri da 1 a n.

  6. Esercizio 6: Rimuovi duplicati Scrivi una funzione rimuovi_duplicati(lista) che restituisce una lista senza duplicati.

  7. Esercizio 7: Anagramma Scrivi una funzione è_anagramma(parola1, parola2) che restituisce True se parola1 è un anagramma di parola2, altrimenti False.

  8. 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.

  9. Esercizio 9: Trova il secondo massimo Scrivi una funzione secondo_massimo(lista) che restituisce il secondo numero più grande in una lista.

  10. Esercizio 10: Stringa invertita Scrivi una funzione stringa_invertita(stringa) che restituisce la stringa invertita.

Risposte

  1. def potenza(base, esponente):
        return base ** esponente
  2. 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
  3. def somma_quadrati(n):
        return sum(i**2 for i in range(1, n + 1))
  4. def conta_consonanti(stringa):
        consonanti = 'bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ'
        return sum(1 for char in stringa if char in consonanti)
  5. def lista_quadrati(n):
        return [i**2 for i in range(1, n + 1)]
  6. def rimuovi_duplicati(lista):
        return list(set(lista))
  7. def è_anagramma(parola1, parola2):
        return sorted(parola1) == sorted(parola2)
  8. def somma_lista(lista):
        return sum(lista)
  9. def secondo_massimo(lista):
        if len(lista) < 2:
            return None
        primo = secondo = float('-inf')
        for numero in lista:
            if numero > primo:
                secondo = primo
                primo = numero
            elif primo > numero > secondo:
                secondo = numero
        return secondo if secondo != float('-inf') else None
  10. python def stringa_invertita(stringa): return stringa[::-1]

English version

Exercises

  1. Exercise 1: Power Write a function power(base, exponent) that returns the result of base raised to the power of exponent.

  2. Exercise 2: Prime Number Write a function is_prime(n) that returns True if n is a prime number, otherwise False.

  3. Exercise 3: Sum of Squares Write a function sum_squares(n) that returns the sum of the squares of the numbers from 1 to n.

  4. Exercise 4: Count Consonants Write a function count_consonants(string) that returns the number of consonants in a string.

  5. Exercise 5: List of squares Write a function list_squares(n) that returns a list containing the squares of the numbers 1 through n.

  6. Exercise 6: Remove duplicates Write a function remove_duplicates(list) that returns a list with no duplicates.

  7. Exercise 7: Anagram Write a function is_anagram(word1, word2) that returns True if word1 is an anagram of word2, otherwise False.

  8. 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.

  9. Exercise 9: Find the second maximum Write a function second_maximum(list) that returns the second largest number in a list.

  10. 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):
consonants = 'bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ'
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
first = second = float('-inf')
for number in list:
if number > first:
second = first
first = number
elif first > number > second:
second = number
return second if second != float('-inf') else None
def reversed_string(string):
return string[::-1]

Commenti