{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Banana'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# ESERCIZIO 8.1 Fate degli esperimenti con alcuni metodi per assicurarvi di avere capito come funzionano. \n", "# strip e replace sono particolarmente utili.\n", "\n", "parola = \"banana\"\n", "parola.capitalize()" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'bracadabr'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# ESERCIZIO 8.1 \n", "# str.strip([chars]). Return a copy of the string with the leading and trailing characters removed.\n", "parola = \"abracadabra\"\n", "parola.strip(\"a\")" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'www.example.it'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#ESERCIZIO 8.1 \n", "#str.replace(old, new[, count]) Return a copy of the string with all occurrences of substring old replaced by new.\n", "\n", "#ESEMPIO: Sostituire \".com\" con \".it\"\n", "website = \"www.example.com\"\n", "website.replace(\"com\", \"it\")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Fython Fython Python Python'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# ESERCIZIO 8.1 \n", "# str.replace(old, new[, count]). Return a copy of the string with all occurrences of substring old replaced by new. \n", "# If the optional argument count is given, only the first count occurrences are replaced.\n", "stringa = \"Python Python Python Python\"\n", "stringa.replace(\"P\", \"F\", 2)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n" ] } ], "source": [ "# ESERCIZIO 8.2 Esiste un metodo delle stringhe di nome count che è simile alla funzione del Paragrafo 8.7. \n", "# Leggete la documentazione del metodo e scrivete un’invocazione che conti il numero di a in 'banana'.\n", "\n", "parola = 'banana'\n", "conta = 0\n", "for lettera in parola: # Questa è la funzione del paragrafo 8.7\n", " if lettera == 'a':\n", " conta = conta + 1\n", "print(conta) " ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Opzione n°2 usando str.count(sub[, start[, end]])\n", "frutto = \"banana\"\n", "frutto.count(\"a\")" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# ESERCIZIO 8.3 Lo slicing [::-1] genera una stringa scritta al contrario. \n", "# Usate questo costrutto per scrivere una variante di una sola riga della funzione palindromo dell’Esercizio 6.3.\n", "\n", "def reverse(parola): \n", " parola=parola[::-1]\n", " return parola" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "kayak\n" ] } ], "source": [ "parola = \"kayak\" \n", "print (reverse(parola))" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "# ... ma cosa fa la funzione \"reverse\"?\n", "# definiamo NOI la funzione riversa che \"ribalta\" una parola\n", "def riversa(parola):\n", " j=len(parola)-1\n", " newparola=''\n", " while(j>=0):\n", " newparola=newparola+parola[j]\n", " j=j-1\n", " parola=newparola\n", " print(parola)\n", " return" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ananab\n" ] } ], "source": [ "riversa('banana')" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "# ... ma cosa fa la funzione \"reverse\"?\n", "# definiamo NOI la funzione riversa che \"ribalta\" una parola\n", "def riversa_l(parola):\n", " i=0\n", " j=len(parola)-1\n", " while(j>i):\n", " temp = parola[i]\n", " parola[i]=parola[j]\n", " parola[j]=temp\n", " i=i+1\n", " j=j-1\n", " print(parola)\n", " return" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['o', 'a', 'i', 'c']\n" ] } ], "source": [ "riversa_l(['c','i','a','o'])" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ananab\n" ] } ], "source": [ "#s.reverse() = it reverses the items of s in place\n", "frutto = \"banana\" \n", "print (reverse(frutto))" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "30203\n" ] } ], "source": [ "#Reverse() con i palindromi\n", "numero = \"30203\"\n", "print (reverse(numero))" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "# ESERCIZIO 8.4 Tutte le funzioni che seguono dovrebbero controllare se una stringa contiene \n", "# almeno una lettera minuscola, ma qualcuna di esse è sbagliata. Per ogni funzione, descrivete cosa fa in realtà\n", "\n", "def una_minuscola1(s):\n", " for c in s:\n", " if c.islower(): \n", " return True\n", " else:\n", " return False" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = \"Frutto\"\n", "una_minuscola1(s)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = \"frutto\"\n", "una_minuscola1(s)\n", "\n", "# La Funzione 1 è sbagliata perché restituisce \"TRUE\" solo quando tutti i caratteri della stringa sono minuscoli" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s =\"FRUTTO\"\n", "una_minuscola1(s)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "def una_minuscola2(s):\n", " for c in s:\n", " if 'c'.islower():\n", " return 'True'\n", " else:\n", " return 'False' # Anche la funzione 2 non è corretta\n", "\n", "# C'è un errore nella scrittura della funzione e nell'uso degli apici. " ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'True'" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = \"CIAO\"\n", "una_minuscola2(s)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'True'" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "S = \"ciao\"\n", "una_minuscola2(s)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'True'" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = \"Ciao\"\n", "una_minuscola2(s)\n", "\n", "# La funzione 2 restituisce TRUE in tutti e 3 i casi e, dunque, non è corretta. " ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "# La funzione n°3 funziona meglio delle due precedenti. Dovrebbe essere corretta. \n", "\n", "def una_minuscola3(s):\n", " for c in s:\n", " flag = c.islower()\n", " return flag " ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = \"Python\"\n", "una_minuscola3(s)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = \"PYTHON\"\n", "una_minuscola3(s)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "# La funzione n°4 è giusta\n", "def una_minuscola4(s):\n", " flag = False\n", " for c in s:\n", " flag = flag or c.islower()\n", " return flag" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = \"Jupyter\"\n", "una_minuscola4(s)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = \"JUPYTER\"\n", "una_minuscola4(s)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = \"jupyter\"\n", "una_minuscola4(s)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "# La funzione n°5 non è corretta. \n", "# L'output è TRUE solo quando tutte le lettere della stringa sono minuscole. \n", "def una_minuscola5(s):\n", " for c in s:\n", " if not c.islower():\n", " return False\n", " return True" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = \"Informatica\"\n", "una_minuscola5(s)" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = \"INFORMATICA\"\n", "una_minuscola5(s)" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = \"informatica\"\n", "una_minuscola5(s)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.5" } }, "nbformat": 4, "nbformat_minor": 5 }