I need help with my homework.
C200 Loops and iterables Dr. M.M. Dalkilic Edited by Geoffrey Brown Computer Science School of Informatics, Computing, and Engineering Indiana University, Bloomington, IN, USA September 21, 2021 Contents Introduction 3 Problem 1: Blocks and Squares without Loops 4 Deliverables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Problem 2: King Midas 7 Precious Metal Prices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Output for Problem 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Deliverables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Problem 3: Count of Min 11 Output for Problem 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Deliverables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Problem 4: Brute Force Search 13 Output for Problem 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Deliverables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Problem 5: Monotonic Functions 15 Deliverables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Problem 6: Counting Letters 17 Deliverables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Problem 7: Toward Data Structures 18 Deliverables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Problem 8: Determing the Age of Trees 22 Deliverables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Assignment 4 Page 1 Problem 9: Lists 23 Deliverables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Problem 10: Linear Modeling 26 Deliverables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Problem 11: Practice with Iterables and Loops 30 Deliverables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Assignment 4 Page 2 Introduction This week you’ll be working on loops and iterables. • Please start this homework early, particularly if you have other homework. • Make sure to commit your work (often) and before 11PM Tuesday, September 28th, 2021. If your timestamp is 11:00PM or greater, the homework cannot be graded. So do not wait until 10:59PM to turn it in. • Unless explicitly listed, you cannot use functions we have not discussed. For example, list.count(). In some problems we state that you are not allowed to use particular ap- proaches e.g., slicing. If you do not adhere to these requirements, the problem will not be awarded points. This is to have you think about how to solve it, rather than relying on a function or program that someone else wrote. We want to give you this experience firsthand! This homework requires you to read and contemplate passages that describe computation. This is excellent practice in translating real-world problems to programming. This also means the description will be (understandably) sometimes imprecise, and that’s okay! Making it work is our job. Assignment 4 Page 3 Problem 1: Shapes without Loops In this problem you will write several functions that take non-negative integers and return strings that, when printed, form blocks and squares. The requirement is that you cannot use any kind of loops. You must rely, instead, on the algebraic properties of strings as discussed in class. The functions return strings. The block function takes a non-negative integer n and returns a string whose dimensions are n × n. Included in the string are \n which, to remind you, causes the printer to return (also called newline). Here is a brief interactive session: 1 >>> x = "*********" 2 >>> pr in t ( x ) 3 ********* 4 >>> x = "***\n***\n***\n" 5 >>> x 6 ’ ***\n***\n***\n ’ 7 >>> pr in t ( x ) 8 *** 9 *** 10 *** 11 12 >>> x = 3*"***\n" 13 >>> x 14 ’ ***\n***\n***\n ’ 15 >>> pr in t ( x ) 16 *** 17 *** 18 *** 19 >>> Listing 1: block function output 1 f o r i in range (5 ) : 2 pr in t ( "Block o f s i z e {0}" . format ( i ) ) 3 pr in t ( b lock ( i ) ) produces output: 1 Block o f s i z e 0 2 3 Block o f s i z e 1 4 * 5 6 Block o f s i z e 2 Assignment 4 Page 4 7 ** 8 ** 9 10 Block o f s i z e 3 11 *** 12 *** 13 *** 14 15 Block o f s i z e 4 16 **** 17 **** 18 **** 19 **** The other function square, is like block, but only has the outline. Listing 2: square function output 1 f o r i in range (5 ) : 2 pr in t ( "Square o f s i z e {0}" . format ( i ) ) 3 pr in t ( square ( i ) ) produces output: 1 Square o f s i z e 0 2 3 Square o f s i z e 1 4 * 5 6 Square o f s i z e 2 7 ** 8 ** 9 10 Square o f s i z e 3 11 *** 12 * * 13 *** 14 15 Square o f s i z e 4 16 **** 17 * * 18 * * 19 **** Assignment 4 Page