1 Object-Oriented Programming 50:198:113 (Spring 2017) Homework: Due Date: Office: 1 2/6/2017 321 BSB Professor: E-mail: URL: Phone: Suneeta Ramaswami [email protected] http://crab.rutgers.edu/~rsuneeta (856)-225-6439 Homework Assignment 1 The assignment is due by 11:59PM of the due date. The point value is indicated in square braces next to each problem. Each solution must be the student’s own work. Assistance should be sought or accepted only from the course instructor or the TA. Any violation of this rule will be dealt with harshly. The goal of this assignment is to review fundamental material from the first programming course; it involves string and file processing. In this and all future assignments, you are graded not only on the correctness of the code, but also on clarity and readability. Hence, I will deduct points for poor indentation, poor choice of object names, and lack of documentation. For documentation, use a common sense approach. While I do not expect every line of code to be explained, all code blocks that carry out a significant task should be documented briefly in clear English. Please read the submission guidelines at the end of this document before you start your work. Important note: When writing each of the following programs, it is important that you name the functions exactly as described because I will assume you are doing so when testing your programs. If your program produces errors because the functions do not satisfy the stated prototype, points will be deducted. In particular, 1. After importing the problem1 module, I will type palindrome generator() in the Python shell to test problem1.py , and 2. After importing the problem2 module, I will type ubbidubbi translator() in the Python shell to test problem2.py. 3. After importing the problem3 module, I will type evaluate essay() (with a suitable file name as parameter) in the Python shell to test problem3.py. For each problem, put all your function definitions in one file. Do not create a separate file for each function. The modules problem1.py, problem2.py, and problem3.py should contain all the function definitions for Problems #1, #2, and #3, respectively. Problem 1 [18 points ] Palindrome Integers. An integer is said to be a palindrome if it reads the same forward and backward. For example, the integers 171, 5445, 99, and 5 are all palindromes. Given an integer that is not a palindrome, it is possible to generate a palindrome from it by repeatedly performing the following steps: Reverse the integer, and add the result to the original integer. This is illustrated in the following example. Given the integer 192, which is not a palindrome, we obtain a palindrome by applying the above steps four times: 2 1. Add 192 and its reverse, 291, to get 483. 2. Add 483 and its reverse, 384, to get 867. 3. Add 867 and its reverse, 768, to get 1635. 4. Add 1635 and its reverse, 5361, to get 6996, which is a palindrome! Write a program, containing the following three functions, to generate palindromes using the above procedure. The str and int built-in functions, which convert back and forth between integers and strings, will come in handy when implementing these functions. • A function called palindrome that has a single parameter N (a positive integer) and returns True if N is a palindrome and False otherwise. • A function called reverse int that has a single parameter N (a positive integer) and returns its reverse. • A function called palindrome generator without any parameters which reads in a positive integer from the user and informs him if the integer is a palindrome or not. If it is not, apply the procedure described above to obtain a palindrome. This should be done by making appropriate calls to the functions palindrome and reverse int. The function should print out every intermediate integer generated by the procedure until the final palindrome. It should also state the number of iterations that were required to go from the original integer to the palindrome. You should allow the user to repeatedly enter integers. See the sample runs for some examples. The palindrome generator -----------------------Enter a positive integer: 192 192 is not a palindrome. Generating a palindrome.... 483 867 1635 6996 4 iterations were needed to get to a palindrome. Do it again? [y/n] y Enter a positive integer: 1206 1206 is not a palindrome. Generating a palindrome.... 7227 1 iterations were needed to get to a palindrome. Do it again? [y/n] y Enter a positive integer: 13425652431 3 13425652431 is a palindrome. Do it again? [y/n] n Goodbye! Problem 2 [16 points ] Ubbi Dubbi. Ubbi Dubbi is a form of coded English often used for amusement (there is a short Wikipedia entry on Ubbi Dubbi, along with a link to a translator, if you want to be amused). The basic idea behind this coded language is to add the letters “ub” before the vowels in every syllable of the English word. However, writing code to identify syllables correctly would be somewhat involved. So instead, we use the following simple algorithm to translate an English word into Ubbi Dubbi: • Every consecutive sequence of vowels in the English word is replaced (in Ubbi Dubbi) by “ub” followed by that same sequence of vowels. We’ll treat ’a’, ’e’, ’i’, ’o’, ’u’, and ’y’ as vowels. The only exception to this vowel rule is when the letter ’e’ appears by itself at the very end of the word. Note that such an ’e’ is not pronounced (the words “take”, “mole”, and “mule” are some examples). In this case, the ’e’ is not preceded by “ub” in Ubbi Dubbi. • Every consonant letter in the English word stays as it is in Ubbi Dubbi. Write a program that translates English language sentences into Ubbi Dubbi. We will assume that the sentence is made up of words separated by blanks. A word is a string of letters without blanks. Any punctuation marks and blanks stay as they are in the Ubbi Dubbi translation. Implement the following functions in your program. • Write a function called ubbidubbi word with a single parameter eword. Assume that eword is a string of lower case characters without any spaces in it. The function returns a string that is the Ubbi Dubbi translation of eword. • Write a function called ubbidubbi sentence with a single string parameter esentence, which is an English language sentence. You may assume that esentence is all lower case (use the tolower() method to ensure this). The function returns a string which is the Ubbi Dubbi translation of esentence. Hint: Construct the Ubbi Dubbi translation by breaking up esentence into words, each of which can be translated using ubbidubbi word. • Write a function called ubbidubbi translator without any parameters. The function repeatedly reads in an English sentence from the user and then uses the function ubbidubbi sentence to print out the Ubbi Dubbi translation of that sentence. Here is a sample run: --------------------------Ubbi Dubbi Translator --------------------------Enter the English sentence: look! his tie is ugly. Translation: lubook! hubis tubie ubis ubugluby. 4 Do another? [y/n] y Enter the English sentence: that tub is a curious shape! Translation: thubat tubub ubis uba cuburubious shubape! Do another? [y/n] y Enter the English sentence: are you speaking ubbi dubbi? Translation: ubare yubou spubeakubing ububbubi dububbubi? Do another? [y/n] n gubo ubin pubeace! guboodbubye! Problem 3 [16 points ] Evaluating essays. You are asked to write a program that evaluates the level of an essay by counting how many “short”, “medium”, and “long” words it has. A word is said to be short if it has one to three characters, it is said to be medium if it has four to six characters, and it is said to be long if it has seven or more characters. In this program, you are asked to read in a text file and count the number of short, medium, and long words in the file. Then, print a message informing the user whether the essay is at an elementary school level, middle school level, high school level, or college level, determined as follows (yes, this is a rather superficial way to evaluate an essay, but we’re trying to keep things simple!): • If half or more of the words are long, then the essay is said to be at a “college level”. • Otherwise, if the number of long words is the greatest, the essay is said to be at a “high school level”. • If the number of medium words is the greatest, the essay is said to be at a “middle school level”. • If the number of short words is the greatest, the essay is said to be at an “elementary school level”. For example, the essay See Bob run He runs fast would be classified as an elementary school level essay. The following essay Observe Bob perambulate He perambulates speedily would be classified as a college level essay. For our purposes, a word is any consecutive sequence of non-whitespace characters. Hence, in the sentence 5 See Jane run. we have one three-letter word and two four-letter words (the word run. has four consecutive non-whitespace characters). On the other hand, the sentence See Jane run . has two three-letter words, one four-letter word, and one one-letter word. In order to carry out the above task, you are asked to implement one function, described below: • Implement a function called evaluate essay with a single parameter called essayfilename. The function should open the file essayfilename (containing the essay) for reading, count the number of short, medium, and long words in the essay, and then print a message informing the user of the level of the essay. Remember to close the file when done. Two sample test files called essay1.txt and essay2.txt are provided on Sakai. You can use these to test your implementation of evaluate essay. You should create your own test files as well (I will also use other test files to test your program). Submission Guidelines Implement the first problem in a file called problem1.py, the second one in a file called problem2.py, and the third in a file called problem3.py. Your name and RUID should appear as a comment at the very top of each file. Test each of your programs thoroughly before submitting your homework. When you are ready to submit, upload your files on Sakai as follows: 1. Use your web browser to go to the website https:sakai.rutgers.edu. 2. Log in by using your Rutgers login id and password, and click on the OBJECT-ORIENTED PROG S17 tab. 3. Click on the ’Assignments’ link on the left and go to ’Homework Assignment #1’ to find the homework file (hw1.pdf). 4. Use this same link to upload your homework files (problem1.py, problem2.py, and problem3.py) when you are ready to submit. You must submit your assignment at or before 11:59PM on February 6, 2017.
© Copyright 2026 Paperzz