{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# CSE 160 Section 2 Worksheet: for Loops and Functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Problem 1\n", "\n", "Fill in the necessary code to count the number of times the letter “l” appears in the list words.\n", "```\n", "words = [‘hello’, ‘world’, ‘python’, ‘yellow’]\n", "l_count = 0\n", "\n", "\n", "\n", "\n", "\n", "print(l_count)\n", "```\n", "The output should be 5." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Problem 2\n", "\n", "Write a function, over_twenty(ages) to count the number of people over 20 years old in the list\n", "ages.\n", "```\n", " ages = [20, 21, 20, 22, 19, 18, 14, 35]\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", ".\n", " \n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Problem 3\n", "\n", "Write a function age_groups(ages) that calculates and prints out the number of people 30 years old and above, the number of people 20-29 years old, and the number of people under 20 years old in the list ages.\n", "```\n", "ages = [20, 21, 20, 22, 19, 18, 14, 35]\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", ".\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Problem 4\n", "\n", "Write a function odd(num) that returns True if a number is odd and False if a number is even. Your function should take in an integer num and return a boolean.\n", "```\n", "\n", "\n", "\n", "\n", "\n", ".\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Problem 5\n", "\n", "Write a function that calculates and returns the average of ages. You are not allowed to use python's built-in sum() function. Your function should take in the list ages as a parameter and return the average.\n", "```\n", "ages = [20, 21, 20, 22, 19, 18, 14, 35]\n", "\n", "\n", "\n", "\n", "\n", "\n", ".\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Problem 6\n", "\n", "Given a function get_height that computes the height of the student passed in, write a new function max_height that finds the maximum height of all the people in the class. Your function should take in a list of student names and return the maximum height. You can assume height is in inches and that the list of all students in the class is class_lst. get_height(‘nicholas’) will return 75.\n", "\n", "```\n", "\n", "\n", "\n", "\n", "\n", ".\n", "```\n", "What is the type of max_height(students)?\n", "\n", "Suppose the code was modified to print(max_height) instead of return max_height, what would be\n", "the type of max_height(students)?\n", "```\n", "\n", "\n", "\n", "\n", "\n", ".\n", "```" ] } ], "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.7.1" } }, "nbformat": 4, "nbformat_minor": 2 }