CSCI-141 Exam 1 Review September 19, 2015 Presented by the RIT Computer Science Community

Similar documents
MCS 260 Exam 2 13 November In order to get full credit, you need to show your work.

CS177 Fall Midterm 1. Wed 10/07 6:30p - 7:30p. There are 25 multiple choice questions. Each one is worth 4 points.

COMP 120. For any doubts in the following, contact Agam, Room. 023

CS-141 Exam 2 Review October 19, 2016 Presented by the RIT Computer Science Community

CSE 311: Foundations of Computing. Lecture 26: Cardinality

CSCI 239 Discrete Structures of Computer Science Lab 6 Vectors and Matrices

Part I: Definitions and Properties

DNHI Homework 2 Solutions Recursion

9A. Iteration with range. Topics: Using for with range Summation Computing Min s Functions and for-loops A Graphics Applications

8. More on Iteration with For

Topic Contents. Factoring Methods. Unit 3: Factoring Methods. Finding the square root of a number

CS Exam 1 Study Guide and Practice Exam

Math 230 Final Exam, Spring 2009

Turing Machines Part Three

Read all questions and answers carefully! Do not make any assumptions about the code other than those that are clearly stated.

You will be writing code in the Python programming language, which you may have learnt in the Python module.

THE LOGIC OF COMPOUND STATEMENTS

Outline. policies for the second part. with answers. MCS 260 Lecture 10.5 Introduction to Computer Science Jan Verschelde, 9 July 2014

Computer Science Introductory Course MSc - Introduction to Java

Recursive Definitions

COMP 204. Exceptions continued. Yue Li based on material from Mathieu Blanchette, Carlos Oliver Gonzalez and Christopher Cameron

Checkpoint Questions Due Monday, October 1 at 2:15 PM Remaining Questions Due Friday, October 5 at 2:15 PM

CS177 Spring Final exam. Fri 05/08 7:00p - 9:00p. There are 40 multiple choice questions. Each one is worth 2.5 points.

CMPSCI 311: Introduction to Algorithms Second Midterm Exam

Thomas Jefferson Invitational Open in Informatics

Introduction to Python

EDA045F: Program Analysis LECTURE 10: TYPES 1. Christoph Reichenbach

CMPSCI 105 Final Exam Spring 2016 Professor William T. Verts CMPSCI 105 Final Exam Spring 2016 May 5, 2016 Professor William T. Verts Solution Key

Discrete Structures, Final Exam

CS 177 Fall 2014 Midterm 1 exam - October 9th

Notater: INF3331. Veronika Heimsbakk December 4, Introduction 3

Lecture Notes on Inductive Definitions

Exercises for Discrete Maths

Problem Points Score Total 100

CSCI3390-Lecture 14: The class NP

Instructions. Do not open your test until instructed to do so!

A High Level Programming Language for Quantum Computing

Deterministic Finite Automaton (DFA)

Instructions. Do not open your test until instructed to do so!

2 - Strings and Binomial Coefficients

Theory Bridge Exam Example Questions

Introduction to Computer Programming, Spring Term 2018 Practice Assignment 5 Discussion: power(m,n) = m n

VPython Class 2: Functions, Fields, and the dreaded ˆr

L435/L555. Dept. of Linguistics, Indiana University Fall 2016

RMT 2014 Power Round February 15, 2014

Foundations of

Name CMSC203 Fall2008 Exam 2 Solution Key Show All Work!!! Page (16 points) Circle T if the corresponding statement is True or F if it is False.

Python. chrysn

Algebra Year 9. Language

Functions in Python L435/L555. Dept. of Linguistics, Indiana University Fall Functions in Python. Functions.

CSC236H Lecture 2. Ilir Dema. September 19, 2018

Written Homework #1: Analysis of Algorithms

ITI Introduction to Computing II

ITI Introduction to Computing II

Fibonacci mod k. In this section, we examine the question of which terms of the Fibonacci sequence have a given divisor k.

MATH141: Calculus II Exam #4 7/21/2017 Page 1

Functional Programming Laboratory 3

n CS 160 or CS122 n Sets and Functions n Propositions and Predicates n Inference Rules n Proof Techniques n Program Verification n CS 161

6.045J/18.400J: Automata, Computability and Complexity. Quiz 2. March 30, Please write your name in the upper corner of each page.

Johns Hopkins Math Tournament Proof Round: Automata

Fall 2017 Test II review problems

Name: Student ID: Instructions:

CS415 Compilers Syntax Analysis Top-down Parsing

CS 1110: Introduction to Computing Using Python Sequence Algorithms

Lisp Introduction. Dr. Neil T. Dantam. Spring CSCI-498/598 RPM, Colorado School of Mines. Dantam (Mines CSCI, RPM) Lisp Spring / 88

Lecture Notes on Inductive Definitions

Algebra Year 10. Language

Homework 3 Solutions, Math 55

Module 1: Analyzing the Efficiency of Algorithms

Outline. policies for the first part. with some potential answers... MCS 260 Lecture 10.0 Introduction to Computer Science Jan Verschelde, 9 July 2014

6. How Functions Work and Are Accessed. Topics: Modules and Functions More on Importing Call Frames

CIS 500 Software Foundations Midterm II Answer key November 17, 2004

Figure 1: NFA N. Figure 2: Equivalent DFA N obtained through function nfa2dfa

/Users/jenskremkow/Science/Courses/python-summerschool-berlin/faculty/Day2/examples numpy.py September 2,

E23: Hotel Management System Wen Yunlu Hu Xing Chen Ke Tang Haoyuan Module: EEE 101

MATH c UNIVERSITY OF LEEDS Examination for the Module MATH2210 (May/June 2004) INTRODUCTION TO DISCRETE MATHEMATICS. Time allowed : 2 hours

Scripting Languages Fast development, extensible programs

Mathematical Background. Unsigned binary numbers. Powers of 2. Logs and exponents. Mathematical Background. Today, we will review:

Math 230 Final Exam, Spring 2008

6.001 SICP September? Procedural abstraction example: sqrt (define try (lambda (guess x) (if (good-enuf? guess x) guess (try (improve guess x) x))))

CS1110. Lecture 20: Sequence algorithms. Announcements

Data Structures and Algorithms Chapter 2

INDUCTIVE DEFINITION

Problem Decomposition: One Professor s Approach to Coding

Simulation: Density FOR THE TEACHER

RMT 2014 Power Round Solutions February 15, 2014

Algorithms. Jordi Planes. Escola Politècnica Superior Universitat de Lleida

Structural Induction

DM507 - Algoritmer og Datastrukturer Project 1

Discrete Event Systems Exam

Please give details of your answer. A direct answer without explanation is not counted.

Please give details of your answer. A direct answer without explanation is not counted.

Take-home Lab 1. Arrays

Operational Semantics

Solving and Graphing Inequalities

Introduction to Python

Lecture 4 Modeling, Analysis and Simulation in Logic Design. Dr. Yinong Chen

Math 1 Summer Assignment 2017

University of New Mexico Department of Computer Science. Midterm Examination. CS 361 Data Structures and Algorithms Spring, 2003

CS 311 Sample Final Examination

Transcription:

CSCI-141 Exam 1 Review September 19, 2015 Presented by the RIT Computer Science Community http://csc.cs.rit.edu 1. Python basics (a) The programming language Python (circle the best answer): i. is primarily a scripting language... True / False T ii. supports the Object-Oriented paradigm... True / False T iii. features static typing... True / False F (b) Write an iterative function to add up all numbers from 1 to n (inclusive) and return the sum. 1 def f o o i t e r ( n ) : 2 t o t a l = 0 3 for num in range (1, n+1): 4 t o t a l+=num 5 return t o t a l (c) Re-write the function above using recursion. Is your answer tail recursive? Why? The answer described below is tail recursive because no additional information must be stored on the stack for each recursive call. 1 def f o o r e c ( l i m i t, num=1, t o t a l =0): 2 i f num > l i m i t : 3 return t o t a l 4 t o t a l+=num 5 num+=1 6 return f o o r e c ( l i m i t, num, t o t a l ) (d) How would you test the two functions you ve written above? Explain test cases you would want to analyze, and write a function to run the tests. Extraneous cases: n=none, n is not an integer Base cases: n = 0, n = 1 General case: n > 1 The above functions are fruitful (have return values), so we can write test cases using the expected outputs. This can be done with hard-coded values or using a separate algorithm to assert the validity of our function s return values. 1 def t e s t f o o f u n c t i o n s (n ) : 2 # below s u g g e s t s the formula f o r the sum 1+2+...+(n 1)+n 3 expected = ( n ( n + 1 ) ) // 2 4 i f f o o i t e r ( n )!= expected : 5 print ( i t e r a t i v e foo f u n c t i o n produced an i n c o r r e c t r e s u l t! ) 6 i f f o o r e c ( n )!= expected : 7 print ( r e c u r s i v e foo f u n c t i o n produced an i n c o r r e c t r e s u l t! ) 1

2. Write a recursive function that reverses a string (e.g. Racecar yields racecar ). 1 def r e v e r s e ( s t r i n g ) : 2 i f s t r i n g == : 3 return s t r i n g 4 else : 5 return r e v e r s e ( s t r i n g [ 1 : ] ) + s t r i n g [ 0 ] Other solutions are possible. 3. Perform a substitution trace on reverse( Doge ). r e v e r s e ( Doge ) = r e v e r s e ( oge ) + D = r e v e r s e ( ge ) + o + D = r e v e r s e ( e ) + g + o + D = r e v e r s e ( ) + e + g + o + D = + e + g + o + D = egod 4. What does the following evaluate to? 1 def writethatdown ( n ) : 2 i f n < 5 : 3 return n 4 return (2 n ) 5 6 def he ( n ) : 7 temp = n + 180 8 i f temp > 1 8 5 : 9 return temp 10 return n 11 12 def putsthefernback ( n ) : 13 return n 14 15 n = 20 16 n = he ( putsthefernback ( writethatdown ( n ) ) ) 17 print ( n ) -40 2

5. Define a function that takes an input string and rotates the sequence of letters in the string by n. For example: shift left( DEADBEEF, 3) will produce the output string DBEEFDEA. shift left( Giant Robot, 4) will produce t RobotGian shift left( X, 5) will produce X You should be able to shift a string by a value greater than the length of the string 1. Assume a function len( str ) which returns the length of a string is provided. (a) Design: Give brief description on how your function should accomplish this. Our implementation will grab the first part of the new string by slicing all characters at an index after the offset. The second part of the string will be all of the characters up to the offset point. We will then concatenate the two strings. Making it possible for the offset to be greater than the length of the string can be accomplished by making offset = offset (%) len(string). (b) Testing: Provide at least 3 test cases, using specific values for the input string and amount of shifting and what the expected output should be for each. shift left(, 5) should return shift left( A,5) should return A shift left( ABADCAFE,300) == shift left( ABADCAFE,4) shift left( FOO, 1) should return OOF (c) Implement the function in Python. 1 def s h i f t l e f t ( s t r i n g, o f f s e t ) : 2 i f len ( s t r i n g ) == 0 : 3 return s t r i n g 4 else : 5 o f f s e t = o f f s e t % len ( s t r i n g ) 6 f i r s t = s t r i n g [ o f f s e t : ] 7 l a s t = s t r i n g [ : o f f s e t ] 8 return f i r s t + l a s t (d) Implement the function shift right(), which rotates letters in the opposite direction. Use shift left() in your implementation 2. 1 def s h i f t r i g h t ( s t r i n g, o f f s e t ) : 2 return s h i f t l e f t ( s t r i n g, o f f s e t ) 1 The modulus (%) operator, which finds the remainder of a division operation, will be useful here. 2 The behavior of % with negative numbers varies by language. In Python, it works like this: 7%2=1 and -7%2=-1. 3

6. Write a function that takes in a file name, and returns the average size of a word in that file. Assume the files will only have 1 word per line, for example: No soup for you! which has an average length of: 3.25 Assume a function len( str ) which returns the length of a string is provided. 1 def average wordlength ( f i l e n a m e ) : 2 c h a r a c t e r s = 0 3 words = 0 4 for l i n e in open( f i l e n a m e ) : 5 words += 1 6 c h a r a c t e r s += len ( l i n e ) 7 return c h a r a c t e r s /words 7. Write a function that takes in a string representation of a number and returns the sum of all of digits in the string. For example, sum( 11235 ) returns 12. Assume a function len( str ) that returns the length of a string is provided. Further, assume there is a function int( str ) which, given a string representation of an integer, returns its integer value. (a) Recursively. 1 def sumrec ( numberstr ) : 2 i f len ( numberstr ) == 0 : 3 return 0 4 else : 5 return int ( numberstr [ 0 ] ) + sumrec ( numberstr [ 1 : ] ) (b) Iteratively 1 def sumit ( numberstr ) : 2 t o t a l = 0 3 for c in numberstr : 4 t o t a l += int ( c ) 5 return t o t a l Other solutions are possible. (c) How would you test this function? Empty string String length = 1 string length > 1 4

8. Assuming the turtle is facing East, write the Python code to draw the following picture given the proper depth as input: depth = 0 No output depth = 1 depth = 2 depth = 3 1 def drawsqaures ( length, depth ) : 2 i f depth <= 0 : 3 return 4 count = 4 5 while count > 0 : 6 t u r t l e. forward ( length ) 7 t u r t l e. l e f t ( 90 ) 8 drawsqaures ( length /2, depth 1 ) 9 t u r t l e. r i g h t ( 180 ) 10 count = 1 NOTE: Solution starts from top-left corner of the main box. 5

9. Connor is a big dweeb and loves keeping a count of things. You will be writing a tail-recursive function to satisfy his desires. Assume you are given the functions strhead() and strtail(), which run in constant time. strhead() returns the first character in the string and strtail() returns the rest of the string. (a) Write a tail recursive function corec, which takes a string and a character and returns the number of times the character appears in the string. For example, corec("eric is enjoying the weather.", "i") should return 3. Do not use the len() function. 1 def corec ( s t r i n g, char, counter =0): 2 i f ( s t r i n g== ) : 3 return counter 4 i f ( strhead ( s t r i n g)==char ) : 5 return corec ( s t r T a i l ( s t r i n g ), char, counter +1) 6 else : 7 return corec ( s t r T a i l ( s t r i n g ), char, counter ) (b) What is the complexity of the functions you wrote for 9a? It runs in O(N) time. 10. Write a function which performs basic string compression, using the counts of repeated characters. For example, the string abbbccccaaa would be compressed to: a1b3c4a3. Assume the function len( str ), which returns the length of a string, is provided. Further, assume you may use int( str ) and str( int ), which converts a string to its integer representation, and converts an integer to its string representation, respectively. 1 def compress ( s t r i n g ) : 2 new = 3 curchar = s t r i n g [ 0 ] 4 count = 0 5 for char in s t r i n g : 6 i f char == curchar : 7 count += 1 8 else : 9 new = new + curchar + s t r ( count ) 10 curchar = c har 11 count = 1 12 13 new = new + curchar + s t r ( count ) 14 return new 6

11. Although most in the industry will be reasonably forgiving when it comes to coding by hand, you should still be comfortable with the syntax of your language of choice. (a) Identify and fix the line(s) that have invalid syntax in the following code: If you are unsure about a specific line, just write a brief explanation of the error. Assume that incorrect lines do not affect the validity of other lines which may depend on them. 1 i n c l u d e math 2 3 int a = 0 4 b = 0 5 6 def f u n c t i o n ( int arg ) : 7 return ( arg + 2) 8 9 c = h e l l o 10 sum = b + math. s q r t ( c ) 11 t o t a l = a + ( b 0. 5 ) 1 This line should read import math, or, alternatively, from math import *. 3 Python is a dynamically-typed language, so you cannot specify a variable s type in this manner. 6 Like line 3, you cannot specify the type of a formal parameter in a dynamicallytyped language. 10 This line will cause a runtime error because sqrt tries to perform mathematical operations on a string, which is invalid. (b) Which of the following are keywords or standard functions in Python? import in from class define int str String True false where while or not isinstance new print range float char bool xor sum All of these are valid, except define, String, false, where, new, xor, char. 12. Write a function that takes a filename as its parameter and returns all the last names from the given file. Each line of the file follows the format of a first name, a space, a last name, a space, and miscellaneous extra data. 1 def getlastnames ( filename ) : 2 namelist = [ ] 3 4 with open( filename ) as f i l e : 5 for l i n e in f i l e : 6 l i n e = l i n e. s t r i p ( ) 7 f i e l d s = l i n e. s p l i t ( ) 8 namelist. add ( f i e l d s [ 1 ] ) 9 10 return namelist 7