{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# ICS 104 - Introduction to Programming in Python and C\n", "## Loops - Lab 2 " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Lab Objectives\n", "- To understand nested loops\n", "- To process strings" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Exercises" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "## Exercise # 1:\n", "Write a phython program that, using the alphabet string and nested loops, will generate the patterns shown below in the sample runs. \n", "Your program will prompt the user for the number of lines (must be from 1 to 26). You need to validate the input as shown below in sample run 1 and 2." ] }, { "cell_type": "raw", "metadata": {}, "source": [ "Sample run 1\n", "Enter number of lines to generate: -1\n", "wrong input: lines must be between 1 and 26\n", "\n", "Sample run 2\n", "Enter number of lines to generate: 27\n", "wrong input: lines must be between 1 and 26\n", "\n", "Sample run 3\n", "Enter number of lines to generate: 5\n", "a,b,c,d,e,\n", "a,b,c,d,e,\n", "a,b,c,d,e,\n", "a,b,c,d,e,\n", "a,b,c,d,e,\n", "----------\n", "a,\n", "a,b,\n", "a,b,c,\n", "a,b,c,d,\n", "a,b,c,d,e,\n", "----------\n", "e,d,c,b,a,\n", "d,c,b,a,\n", "c,b,a,\n", "b,a,\n", "a," ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# YOUR CODE HERE\n", "alphabet='abcdefghijklmnopqrstuvwxyz'\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# YOUR CODE HERE" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "## Exercise # 2:\n", "Write a python program that extracts a substring from the alphabet string based on the starting letter and the step.\n", "The step cannot be zero. The starting letter can be entered as lowercase or upper case.\n", "You need to validate the input as shown in the sample runs.
\n", "Hint:use find method to get the index of the entered letter. " ] }, { "cell_type": "raw", "metadata": {}, "source": [ "Sample run 1:\n", "Enter starting letter: th\n", "You must enter one letter\n", "\n", "Sample run 2:\n", "Enter starting letter: $\n", "You must enter one letter\n", "\n", "Sample run 3:\n", "Enter starting letter: T\n", "Enter step: 0\n", "Step cannot be 0\n", "\n", "Sample run 4:\n", "Enter starting letter: M\n", "Enter step: 3\n", "Resulting substring is: mpsvy\n", "\n", "Sanmple run 5:\n", "Enter starting letter: t\n", "Enter step: -3\n", "Resulting substring is: tqnkheb" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "# YOUR CODE HERE\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Exercise # 3:\n", "Write a program that generates a random word made of 4 consonants and 4 vowels. Each randomly selected consonant is followed by a randomly selected vowel.\n", "Use the alphabet string given in Exercise 1 to create one string containing consonants and another one containing vowels. Use them for selecting the consonants and vowels.\n", "Hint: use randint function to generate a random index to be used to randomly select the consonant and vowel." ] }, { "cell_type": "raw", "metadata": {}, "source": [ "Sample run 1\n", "Cosntructed word is: wufixatu\n", "\n", "Sample run 2\n", "Cosntructed word is: yosigugi" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "# YOUR CODE HERE\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# End of the Loops - Lab 2\n", "Good luck..." ] } ], "metadata": { "celltoolbar": "Slideshow", "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.8" }, "rise": { "theme": "yellow" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": false, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": { "height": "789px", "left": "450px", "top": "145px", "width": "384px" }, "toc_section_display": false, "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 }