{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Programmare in Python\n", "\n", "## Esercizi: Input - Output\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__1 - Usando le variabili `a = \"civette\"` e `b = \"comò\"`, scrivete sullo schermo \"Tre civette sul comò!\"__" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__2 - Scrivete una funzione `somma(x,y)` che quando si chiama somma(12,5) scriva sullo schermo: \"La somma di 12 più 5 vale 17\".__ " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__3 - Scrivete una funzione che prende come input un intero e restituisca la somma delle sue cifre dispari \n", "nella forma:__\n", "\n", "> La somma delle cifre dispari nel numero $n$ è $d$.\n", "\n", "__Test:__\n", "\n", "* Input: $-1381723$\n", "* Output: `La somma delle cifre dispari nel numero -1381723 è 15.` \n", " __($1+3+1+7+3 = 15$)__" ] }, { "cell_type": "markdown", "metadata": {}, "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__4 - Scrivete una funzione che prende come input un intero e restituisca la somma delle sue cifre di indice dispari (la cifra di indice 0 sia quella delle unità) nella forma:__\n", "\n", "> La somma delle cifre di indice dispari nel numero $n$ è $d$.\n", "\n", "__Test:__\n", "\n", "* Input: $-1381723$\n", "* Output: `La somma delle cifre di indice dispari nel numero -1381723 è 6.` \n", " __($2+1+3 = 6$)__" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__5 - Scrivere una funzione che restituisca la rappresentazione esadecimale di un numero intero e stampi un messaggio informativo sullo schermo.__" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Gli esercizi che seguono richiedono l'uso di file." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__6 - Scrivere una funzione che restituisca la rappresentazione decimale di un numero intero in forma esadecimale e stampi un messaggio informativo nel file `decimal.txt`.__" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__7 - Create un file `mydat.txt`. Usando il metodo `write` e le f-strings scrivete nel file la tabella:
__\n", "\n", " 0.7500 13.52 0.32\n", " 1.7885 12.11 0.92\n", " 2.8269 14.27 0.73\n", " 3.8654 16.60 2.06\n", " " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__8 - Scrivete una funzione che prenda come input il nome di un file e un numero intero non negativo `n`, legga nel file `n` numeri interi (uno per riga) e restituisca la somma delle cifre più grandi di ciascun numero nel file.\n", "Se la cifra più grande compare più volte deve essere sommata una volta sola ($1777$ aggiunge alla somma solo $7$ e non $7+7+7$).__\n", "\n", "__Test:__\n", "\n", "* Input: $3, -1719, 1777, 111$ ($3$ vuol dire che ci sono $3$ tre numeri)\n", "* Output: `17` \n", " ($9+7+1 = 17$)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__9 - Create un file `trees.csv` che contenga:__\n", "```\n", "#Name,Type,AverageHeight(m)\n", "maple,Deciduous,18\n", "pine,Conifer,27\n", "Oak,Deciduous,14\n", "```\n", "__Leggete i dati dal file e stampate il contenuto nella forma:__\n", "```\n", "The X is a Y tree. Its average height is Z meters.\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__Extra: perchè i programmatori confondono sempre Halloween (Oct 31) con Natale (Dec 25)?Hint: octal.__" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "hide_input": false, "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.12" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": true, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 4 }