Practical Questions CSCA48 Week 6

Similar documents
Lecture Chapter 6 Recursion as a Problem Solving Technique

Computer Science E-119 Fall Problem Set 1. Due before lecture on Wednesday, September 26

Discrete Structures. Fall Homework3

Test #2 October 8, 2015

CS 115 Exam 2 (Section 1) Spring 2017 Thu. 03/31/2017

Announcements. Recursion and why study it. Recursive programming. Recursion basic idea

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Excerpt from "Art of Problem Solving Volume 1: the Basics" 2014 AoPS Inc.

CSE 143 Lecture 10. Recursion

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Exercise: Using Numbers

CS150 - Sample Final

Types, lists & functions

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

Text Input and Conditionals

CS150 Sample Final. Name: Section: A / B

COMP 202 Recursion. CONTENTS: Recursion. COMP Recursion 1

CSE 143. Lecture 9: introduction to recursion reading: 12.1

CS150 Sample Final Solution

Functionally Modular. Self-Review Questions

cs1114 REVIEW of details test closed laptop period

Lecture 19: Recursion

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

You will not be tested on JUnit or the Eclipse debugger. The exam does not cover interfaces.

CS 115 Data Types and Arithmetic; Testing. Taken from notes by Dr. Neil Moore

Assignment #1 Simple C++

ENGR 101 Engineering Design Workshop

15-110: Principles of Computing Sample Exam #1

CS 115 Lecture 4. More Python; testing software. Neil Moore

Lecture 3 (02/06, 02/08): Condition Statements Decision, Operations & Information Technologies Robert H. Smith School of Business Spring, 2017

Structure and Interpretation of Computer Programs

CMSC 201 Spring 2018

int a; int b = 3; for (a = 0; a < 8 b < 20; a++) {a = a + b; b = b + a;}

Problem One: A Quick Algebra Review

You must bring your ID to the exam.

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

Conditionals and Recursion. Python Part 4

CSE413 Midterm. Question Max Points Total 100

Midterm Exam 2B Answer key


STREAMS AND REVIEW 12

CS 115 Exam 3, Spring 2014

CS212 Midterm. 1. Read the following code fragments and answer the questions.

(f) d={ alchemist :( a, t ), shaman : ( s, n ), wizard : ( w, z )} d[ shaman ][1]

RACKET BASICS, ORDER OF EVALUATION, RECURSION 1

Loops and Conditionals. HORT Lecture 11 Instructor: Kranthi Varala

Computer Science 236 Fall Nov. 11, 2010

Copied from: on 3/20/2017

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Practice Midterm Exam #1

Final Exam Practice Questions

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

CSE 20 DISCRETE MATH. Winter

Chapter 2, Part III Arithmetic Operators and Decision Making

(c) ((!(a && b)) == (!a!b)) TRUE / FALSE. (f) ((!(a b)) == (!a &&!b)) TRUE / FALSE. (g) (!(!a) && (c-d > 0) && (b!b))

In addition to the correct answer, you MUST show all your work in order to receive full credit.

RECURSION AND LINKED LISTS 3

Practice Midterm Exam #2

Recursion Problems. Warm Ups. Enumeration 1 / 7

CSCA08 Winter 2018 Week 2: Variables & Functions. Marzieh Ahmadzadeh, Brian Harrington University of Toronto Scarborough

Honors Computer Science Python Mr. Clausen Programs 4A, 4B, 4C, 4D, 4E, 4F

CS 170 Exam 2. Version: A Fall Name (as in OPUS) (print): Instructions:

CS 115 Lecture 8. Selection: the if statement. Neil Moore

Submit your answers to these questions to the Curator under Quizzes as HW08 by the posted due date and time. No late submissions will be accepted.

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

Lecture #7: Recursion (and a data structure)

CPSC 217 Assignment 3

Fundamentals: Expressions and Assignment

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

CSCI 101 Midterm Sample Questions

CSE 115. Introduction to Computer Science I

CS Prelim 2 Review Fall 2018

4. Write a sum-of-products representation of the following circuit. Y = (A + B + C) (A + B + C)

CmpSci 187: Programming with Data Structures Spring 2015

Control Flow: Loop Statements

Control Structures 1 / 17

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

RECURSION, RECURSION, (TREE) RECURSION! 2

Lecture. Loops && Booleans. Richard E Sarkis CSC 161: The Art of Programming

CSE 2123 Recursion. Jeremy Morris

CS-141 Final Exam Review December 8, 2017 Presented by the RIT Computer Science Community

6.001 Notes: Section 4.1

Python 1: Intro! Max Dougherty Andrew Schmitt

Lecture 02 Making Decisions: Conditional Execution

Week - 03 Lecture - 18 Recursion. For the last lecture of this week, we will look at recursive functions. (Refer Slide Time: 00:05)

The Big Python Guide

Midterm Exam 2A Principles of Computing Fall November 10, 2014

CS 1301 Exam 1 Fall 2010

Thomas Jefferson Invitational Open in Informatics 2012

Language Basics. /* The NUMBER GAME - User tries to guess a number between 1 and 10 */ /* Generate a random number between 1 and 10 */

Formulas in Microsoft Excel

CSC 1351: Quiz 6: Sort and Search

CSE143 Midterm Summer Name of Student: Section (e.g., AA): Student Number:

PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops

CPSC 217 Midterm (Python 3 version)

Syntax-Directed Translation. Lecture 14

Lecture 04 More Iteration, Nested Loops. Meet UTA Jarrett s dog Greta, lying in her nest

Review 4. Lists and Sequences

Q1 Q2 Q3 Q4 Q5 Total 1 * 7 1 * 5 20 * * Final marks Marks First Question

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Transcription:

Practical Questions CSCA48 Week 6 Trace the following functions with a variety of input, and describe their functionality in a single sentence. 1. def mystery(n): if n == 4: result = n result = 2 * mystery(n+1) 2. def mystery(a, b): if b < 1: return 0 if b % 2 == 0: return mystery(a+a, b/2) return mystery(a+a, b/2) + a 3. def mystery(s): if len(s) == 0: result = elif len(s) == 1: result = s elif s[0] == s[1]: result = mystery(s[1:]) result = s[0] + mystery(s[1:]) 4. def mystery(x, y): if y == 0: result = 0 result = x + mystery(x, y-1) What does mystery do? Describe in one English sentence.

5. (Challenge) (Hint: do part 4 first.) def mystery(x, y): if y == 0: result = 0 result = x + mystery(x, y-1) def mystery2(a, b): if b == 0: result = 1 result = mystery(a, mystery2(a, b-1)) 6. (Challenge) from math import * def mystery(a, b, r): if b >= a: return [] # return empty list c = ceil(a + (a-b)*r) - 1 d = floor(a + (b-a)*r) return mystery(c, a, r) + [(a, b)] + mystery(b, d+1, r) + [(c, d)] What is the output of mystery(20, 10, 0.2), mystery(7, 1, 0.5), and mystery(6, 2, 0.6)?

Complete the following functions. All functions must work recursively. a) def rec_len(l): '''Return the total number of non-list elements within nested list L. For example, rec_len([1, two, [[], [[5]]]]) should return 3. ''' b) def nest_level(l): '''Return the maximum "nesting level" of nested list L, that is, how many levels of sub-lists are in L, at the "deepest" point within L. For example, nest_level([1, [[], [[2]]], three ]) should return 4 because element 2 is in a list ([2]) within a list ([[2]]) within a list ([[], [[2]]]) within the original list. ''' c) def rev(n): '''Return the result of reversing the digits in integer n. For example, rev(512) should return 215. ''' d) def add_commas(n): '''Return a string version of integer n with commas added. For example, add_comma(15866321) should return 15,866,321. ''' e) def flatten(l): '''Takes a list as a parameter and flattens it: if any items are themselves lists, their contents are retrieved from those nested lists. For example, flatten([1,[2[3,4]],5]) should return [1, 2, 3, 4, 5]. '''

f) def num1s(n): '''Given a nonnegative int n, yields the number of 1's in its binary representation. For example, suppose n = 17. The binary representation of 17 is 10001, so the function would return 2. The following facts may be useful: (1) The rightmost bit of the binary representation of n is 1 if and only if n is odd. (2) For n > 0, the binary representation of n is: (binary representation of n/2) followed by (the rightmost bit of the binary representation of n).''' g) Write a function that given a integer K, print the first K rows of Pascal s Triangle. Print each row with values separated by space. For example, K = 5, then it prints 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 h) The parentheses below line up for each boolean subexpression. Note that the entire body is a single return statement. Fill in the blanks with or, and, subl, seql, subl[1:], and seql[1:] so that the code works as advertised. You may use each of them as often as you need to. def is_sub_sequence(subl, seql): '''Return whether the items in list subl appear in list seql in the same order. A list is a sub-sequence of itself, and the empty list if a sub-sequence of every list. For examples: is_sub_sequence([1, 3], [1, 2, 3, 4]) should evaluate to True. is_sub_sequence([3, 1], [1, 2, 3, 4]) should evaluate to False. ''' return ( len(subl) == 0 (len(seql) > 0 (is_sub_sequence(, ) (subl[0] == seql[0] is_sub_sequence(, ) ) ) ) )

i) A word is considered elfish if it contains the letters: e, l, and f in it, in any order. For example, we would say that the following words are elfish: whiteleaf, tasteful, unfriendly, and waffles, because they each contain those letters. Write a function called elfish, given a word and return true if that word is elfish, false otherwise. j) Write a recursive function called to_words that takes a single int parameter and returns a str that contains the digits in the int in English. For example, the call to_words(23561) would result in the following: two three five six one Assume the following list of strs is defined in the file that contains the function you are writing. NUMBERS = [ zero, one, two, three, four, five, six, seven, eight, nine ] Hint: Use the / and % operators. k) (Challenge) We define super digit of an integer x using the following rules: Iff x has only 1 digit, then its upper digit is x Otherwise, the super digit of x is equal to the super digit of the digit-sum of x. Here, digit-sum of a number us defined as the sum of its digits. For example, super digit of 9875 will be calculated as: super-digit(9875) = super-digit(9+8+7+5) = super-digit(29) = super-digit(2+9) = super-digit(11) = super-digit(1+1) = super-digit(2) = 2.

Write a function that calculate and return the super digit of P, given two integer n and k. P is created when number n is concatenated k times. That is, if n = 148 and k = 3, so P = 148148148 super-digit(9875) = super-digit(148148148) = super-digit(1+4+8+1+4+8+1+4+8) = super-digit(39) = super-digit(3+9) = super-digit(12) = super-digit(1+2) = super-digit(3) = 3. l) (Challenge) Consider the following puzzle. You have exactly two containers to use: one can hold at most three liters of water and the other can hold at most five liters of water. The containers have no markings on them to tell you how much water they currently contain. You have an infinite supply of water at your disposal with which to fill the containers. Both containers start empty. How can you get exactly four liters of water in the file-liter container? Write a recursive function that solves this puzzle by printing out all possible solutions. None of your solutions should allow the same state space to be reached more than once (i.e. you should never enter infinite recursion). Note that I do not want you to actually give me a list of moves that represent a solution to the puzzle (that s that computers are for). Discussion Questions 1) When we create a recursive function that doesn t have or misses its base case, the exception we get is called a stack overflow. Why do we use that name? What does this have to do (if anything) with the stacks we ve been learning about in lecture? 2) Can every program we write recursively also be written non-recursively? What about the other way around? Can you come up with a general algorithm to convert from one to the other? 3) Is recursion more or less efficient than iterative code? Can you come up with an example where you could write a recursive and a non-recursive version of the same code? Test them to see which one runs faster (hint: Try some of the easy ones we ve been doing).

Logic Question (Challenge) In the game Tetris, each piece has exactly 4 squares, and each row is 10 squares long. So in theory, it s possible to completely clear the board after your first 5 pieces are placed. How many arrangements of pieces can complete this task? That is, how many ways are there to arrange the classic Tetris pieces into a square that is 10 blocks wide by 2 blocks high?