Programming Exercises



Similar documents
Base Conversion written by Cathy Saxton

CS177 MIDTERM 2 PRACTICE EXAM SOLUTION. Name: Student ID:

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

Exercise 4 Learning Python language fundamentals

Algorithm Design and Recursion

Introduction to Python

Informatica e Sistemi in Tempo Reale

Chapter 4: Computer Codes

Python Lists and Loops

NUMBER SYSTEMS APPENDIX D. You will learn about the following in this appendix:

Pseudo code Tutorial and Exercises Teacher s Version

Lab 4.4 Secret Messages: Indexing, Arrays, and Iteration

CSI 333 Lecture 1 Number Systems

=

Exercise 1: Python Language Basics

Digital System Design Prof. D Roychoudhry Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

Number Systems and Radix Conversion

Section 1.4 Place Value Systems of Numeration in Other Bases

How do sort this list using one line? a_list.sort()

Grade 6 Math Circles. Binary and Beyond

I PUC - Computer Science. Practical s Syllabus. Contents

Introduction to Computer Science I Spring 2014 Mid-term exam Solutions

Python for Rookies. Example Examination Paper

EE 261 Introduction to Logic Circuits. Module #2 Number Systems

Python Loops and String Manipulation

VISUAL GUIDE to. RX Scripting. for Roulette Xtreme - System Designer 2.0

Lecture 10: Regression Trees

Positional Numbering System

Sources: On the Web: Slides will be available on:

COMSC 100 Programming Exercises, For SP15

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

1 Description of The Simpletron

Everything you wanted to know about using Hexadecimal and Octal Numbers in Visual Basic 6

Reading 13 : Finite State Automata and Regular Expressions

Cyber Security Workshop Encryption Reference Manual

Introduction to SQL for Data Scientists

What Is Recursion? Recursion. Binary search example postponed to end of lecture

CmpSci 187: Programming with Data Structures Spring 2015

Chapter 2. Binary Values and Number Systems

CDA 3200 Digital Systems. Instructor: Dr. Janusz Zalewski Developed by: Dr. Dahai Guo Spring 2012

The programming language C. sws1 1

Section IV.1: Recursive Algorithms and Recursion Trees

Test Driven Development

So far we have considered only numeric processing, i.e. processing of numeric data represented

PHP Tutorial From beginner to master

Lecture 11: Number Systems

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

6. Control Structures

The string of digits in the binary number system represents the quantity

Visual Logic Instructions and Assignments

Specimen 2015 am/pm Time allowed: 1hr 30mins

Chapter 7 Lab - Decimal, Binary, Octal, Hexadecimal Numbering Systems

Useful Number Systems

COMP 250 Fall 2012 lecture 2 binary representations Sept. 11, 2012

Moving from CS 61A Scheme to CS 61B Java

Stacks. Linear data structures

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

NUMBER SYSTEMS. William Stallings

Introduction to Java

Computer Science 281 Binary and Hexadecimal Review

Chapter 5. Recursion. Data Structures and Algorithms in Java

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

Unit 1 Number Sense. In this unit, students will study repeating decimals, percents, fractions, decimals, and proportions.

Chapter 2 Writing Simple Programs

Unix Shell Scripts. Contents. 1 Introduction. Norman Matloff. July 30, Introduction 1. 2 Invoking Shell Scripts 2

Sorting. Lists have a sort method Strings are sorted alphabetically, except... Uppercase is sorted before lowercase (yes, strange)

CS201: Architecture and Assembly Language

Lecture 2 Notes: Flow of Control

Solving Problems Recursively

6.080 / Great Ideas in Theoretical Computer Science Spring 2008

Number Representation

Sample CSE8A midterm Multiple Choice (circle one)

Subnetting Examples. There are three types of subnetting examples I will show in this document:

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

CSCE 110 Programming I Basics of Python: Variables, Expressions, and Input/Output

Chapter 11. Talk To Your Computer

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

This is great when speed is important and relatively few words are necessary, but Max would be a terrible language for writing a text editor.

Dynamic Programming. Lecture Overview Introduction

The C++ Language. Loops. ! Recall that a loop is another of the four basic programming language structures

VHDL Test Bench Tutorial

Introduction to Computers and Programming

Introduction to Python for Text Analysis

6.080/6.089 GITCS Feb 12, Lecture 3

Number Conversions Dr. Sarita Agarwal (Acharya Narendra Dev College,University of Delhi)

12-6 Write a recursive definition of a valid Java identifier (see chapter 2).

Accredited Specimen Mark Scheme

Introduction to Programming (in C++) Loops. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

1001ICT Introduction To Programming Lecture Notes

Example. Introduction to Programming (in C++) Loops. The while statement. Write the numbers 1 N. Assume the following specification:

MACM 101 Discrete Mathematics I

Binary Representation. Number Systems. Base 10, Base 2, Base 16. Positional Notation. Conversion of Any Base to Decimal.

Continuous Integration Part 2

Numeral Systems. The number twenty-five can be represented in many ways: Decimal system (base 10): 25 Roman numerals:

PL / SQL Basics. Chapter 3

Systems Programming & Scripting

Transcription:

s CMPS 5P (Professor Theresa Migler-VonDollen ): Assignment #8 Problem 6 Problem 1 Programming Exercises Modify the recursive Fibonacci program given in the chapter so that it prints tracing information. Specifically, have the function print a message when it is called and when it returns. For example, the output should contain lines like these: Computing fib(4)... Leaving fib(4) returning 3 Use your modified version of fib to compute fib(10) and count how many times fib(3) is computed in the process. First, copy and paste the program as it s written on the top of page 442 in your textbook. Second, you will need to add a print statement in three separate places: before you run the recursion conditions, beneath the condition if n < 3, and beneath the else statement. Remember to cast n into a character using the str function. Thirdly you will need to keep track of the number of times you run fib(3). This is not that hard. Simply declare a variable outside the function (I call mine numberofthrees) and assign it the value of 0. In the function, insert a condition to check if n is equal to 3 (implying we re running fib(3)). If so, add one to numberofthrees. But remember to include the line global numberofthrees at the beginning of fib so it knows to use that global variable instead of some local variable named numberofthrees and it will cause an UnboundLocalError. Finally, below the fib functions definition, run fib(10) and print out numberofthrees. numberofthrees = 0 def fib(n): global numberofthrees print("computing fib(" + str(n) + ")") if n is 3: numberofthrees +=1 if n < 3: print("leaving fib(" + str(n) + ") returning 1") return 1 output = fib(n-1) + fib(n-2) print("leaving fib(" + str(n) + ") returning " + str(output)) return output fib(10) print(numberofthrees) 4

s CMPS 5P (Professor Theresa Migler-VonDollen ): Assignment #8 Problem 2 Problem 3 A palindrome is a sentence that contains the same sequence of letters reading it either forwards or backwards. A classic example is: Able was I, ere I saw Elba. Write a recursive function that detects whether a string is a palindrome. I shortened the instructions to the basic idea. The very first thing you should do is check to make sure there aren t any punctuation marks, spaces, or capital letters to mess up your calculations. Use string.punctuation and remove every instance of a punctuation mark from the text using the replace function. Next, use replace to remove all spaces, and then use lower to turn all letters into lowercase (because, for example, a!= A). Make sure you only do this once by checking if the entire text is made of alphabet characters using not(quote.isalpha()). There are two base cases for this recursive function: if the remaining text is of length 1, and if the remaining text is of length 2. If it s length 1, return True (because that s how palindromes work). If it s length 2, then check if both letters are the same then return True if so. Return False otherwise. Finally, create a function that takes manual input from a user and determines if it is a palindrome. def ispalindrome(quote): #cleanup string if not(quote.isalpha()): for punc in string.punctuation: quote = quote.replace(punc, "") quote = quote.replace(" ", "") quote = quote.lower() print(quote) #base case if len(quote) is 1: return True if len(quote) is 2: if quote[0] is quote[1]: return True return False if quote[0] is quote[-1]: print(quote[1:-1]) return ispalindrome(quote[1:-1]) return False def main(): string = input("give me something on the next line, and I will determine if it is a palindrome.") return ispalindrome(string) main() 5

s CMPS 5P (Professor Theresa Migler-VonDollen ): Assignment #8 Problem 4 Problem 4 Write and test a recursive function max to find the largest number in a list. The max is the larger of the first item and the max of all the other items. The wording of the exercise is somewhat vague and max is a built-in function, so I m going to change it to maximum instead. What this exercise is trying to tell you is the following: maximum(list) = whatever is greater: list[0] or maximum[the list minus the first entry] The first thing you must do is find the base case, which in our case is... len(alist) == 2: return whichever item in alist is greater Now we look for a general case, which will be calculate the maximum of the list - without the first element. set it to the variable a. if the first element of the list is greater than a, return the first element else, return a. def maximum(alist): if len(alist) == 2: if alist[0] > alist[1]: return alist[0] return alist[1] a = maximum(alist[1:]) if alist[0] > a: return alist[0] return a 6

s CMPS 5P (Professor Theresa Migler-VonDollen ): Assignment #8 Problem 5 Problem 5 Computer scientists and mathematicians often use numbering systems other than base 10. Write a program that allows auger to enter a number and a base and then prints out the digits of the number in the new base. Use a recursive function baseconversion(num, base) to print the digits. Hint: Consider base 10. To get the rightmost digit of a base 10 number, simply look at the remainder after dividing by 10. For example, 153%10 is 3. To get the remaining digits, you repeat the process on 15, which is just 153/10. This same process works for any base. The only problem is that we get the digits in reverse order (right to left). Write a recursive function that first prints the digits of num//base and then prints the last digit, namely num%base. You should put a space between successive digits, since bases greater than ten will print out multi-character digits. For example, baseconversion(245, 16) should print out 15 5. I know this seems incredibly complicated, but it s not that hard. Remember when you had to learn about binary numbers? Those are numbers that use a base of 2 and only use the numbers 0 and 1 to count things. 01 is equal to 1, 1111 is equal to 15, etc. This question expands on that idea by illustrating an important fact: you can make a base out of any amount of numbers. The most frequently used ones are base 10 (digits), base 2 (binary), base 8 (octal), and base 16 (hexadecimal). But what you re being asked is to create a generalized function that can take any base 10 number and convert it into a different base. It takes a bit of thinking, but once you understand how the conversion works, you can make a recursive function. The base case for this recursive function is when num has a value less than the base you wish to translate to. When this happens, you just return str(num). I am using the str() function because although these are numbers, I want to output them in an order that s not normal. I can try using a list of numbers and outputting those separated by spaces, but that s a little too cumbersome for such a simple equation. The general case for this recursive function is when num has a greater value than base. When that occurs, we have to split num into two different values. If I refer to the hint provided in the textbook, the first portion will pass num // base into baseconversion, and the second portion will pass base%num into baseconversion. // probably looks unfamiliar to you. It s division that removes anything after the decimal point. So essentially floor(base / num). It s always a good idea to check your work by running through a recursive algorithm on pen and paper, because recursion is not that intuitive. However, it is amazingly elegant and simple, as you ll see below. def baseconversion(num, base): if num < base: return str(num) return baseconversion(num // base, base) + " " + baseconversion(num \% base, base) 7

s CMPS 5P (Professor Theresa Migler-VonDollen ): Assignment #8 Problem 6 Problem 6 Write a recursive function to print out the digits of a number in English. For example, if the number is 153, the output should be One Five Three. See the hint from the previous problem for help on how this might be done. This answer resembles the answer to the previous question. First, create a list of strings corresponding to the English words representing numbers. For instance: conversion = ["Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"] What s nice about that list above is that each word corresponds to its index. conversion[0] is Zero, conversion[1] is One, etc. Next, you need to convert your code to automatically handle base 10 numbers. Copy and paste the code from the previous problem into a new script. Anytime you see base, replace it with 10. The other thing you must do is change the base case. Instead of returning the str format of a number, you are going to return its word in str format. Also note that I initialized my list conversion outside of the function definition. In order for numberinwords (the name I used for this function) to work properly, I ll need to use the global keyword to indicate that I m using a variable from outside of the script. conversion = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"] def numberinwords(number): global conversion if number < 10: return conversion[number] return numberinwords(number // 10) + " " + numberinwords(number % 10) 8

s CMPS 5P (Professor Theresa Migler-VonDollen ): Assignment #8 Problem 7 Problem 10 Automated spell checkers are used to analyze documents and locate words that might be misspelled. These programs work by comparing each word in the document to a large dictionary (in the non-python sense) of words. If the word is not found in the dictionary, it is flagged as potentially incorrect. Write a program to perform spell-checking on a text file. To do this, you will need to get a large file of English words in alphabetical order. If you have a Unix or Linux system available, you might poke around for a file called words, usually located in /usr/dict or /usr/share/dict. Otherwise, a quick search on the Internet should turn up something usable. Your program should prompt for a file to analyze and then try to look up every word in the file using binary search. If a word is not found in the dictionary, print it on the screen as potentially incorrect. Problem 10 wants you to find a dictionary and use it to spell check words using binary search. Not an easy task. Start by finding a dictionary. I chose to make a separate function to handle this called uploaddictionary, so I can keep this task separate from other tasks in the program. Mac users will find the dictionary in /usr/share/dict/words, which I have uploaded using the built-in open function. I have found an equivalent for both Mac and Windows users at the following location: http://www-01.sil.org/linguistics/wordlists/english/wordlist/wordsen.txt Next, run a spell check to find every word in a text file using binary search. This is simpler than it seems because strings can use boolean operators like and to compare words alphabetically. For instance, "a" < "b" returns True because a is first in alphabetical order. And it works on entire words, too. For instance, "alphabet" > "boogie" returns False. I named my function spellcheck. An important note is that the instructions do not ask for a recursive implementation of binary search, so I used an iterative implementation instead. I ve copied the version found on page 432 of your textbook, with some modifications to account for the switch from numbers to strings. In the final function, which I have named problem10, we ll upload a text file, check its words, and stick any problem words into a list. We ll then output all the words that we think might be misspelled. Remember to remove all punctuation and turn all words into lower-case to prevent any mistakes in spell checking. For simplicity s sake, I m turning every word in the dictionary into lowercase. Remember that a is not the same value as A. Also somewhat surprisingly they do not include plural words (like words ), so do not worry if they appear in your results. Problem 10 continued on next page... 9

s CMPS 5P (Professor Theresa Migler-VonDollen ): Assignment Problem #8 10 (continued) import string def uploaddictionary(): textfile = open("/usr/share/dict/words", "r") words = textfile.read() dictionary = [] for eachword in words.split(): dictionary.append(eachword.lower()) return dictionary def spellcheck(word, alist): low = 0 high = len(alist) - 1 #print("next word: " + word) while low <= high: mid = (low + high) // 2 item = alist[mid] #print(" item: " + item) if word == item: return True elif word < item: high = mid - 1 elif word > item: low = mid + 1 return False #occurs only when we re done inside the loop def problem10(textfile): problemwords = [] doc = open(textfile, "r") text = doc.read() for punc in string.punctuation: text = text.replace(punc, "") text = text.lower() dictionary = uploaddictionary() print(text.split()) for eachword in text.split(): if not(spellcheck(eachword, dictionary)): problemwords.append(eachword) if len(problemwords) > 0: print("your document has " + str(len(problemwords)) + " spelling errors.") for eachword in problemwords: print(str(problemwords.index(eachword) + 1) + " ", end="") print(eachword) print("your document has no spelling errors!") 10