May 11, 2011. What we’ll do today Practice writing recursive specifications and functions Given a recursive problem definition Determine a proper specification.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Procedural Programming in C# Chapters Objectives You will be able to: Describe the most important data types available in C#. Read numeric values.
Dec 7, What we’ll do today Practice writing recursive specifications and functions Given a recursive problem definition Determine a proper specification.
Building Java Programs
1 Binary Arithmetic, Subtraction The rules for binary arithmetic are: = 0, carry = = 1, carry = = 1, carry = = 0, carry =
1 Recitation 7. Developing loops Introduction. This recitation concerns developing loops using their invariants and bound functions. Your recitation instructor.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Integer Data representation Addition and Multiplication.
Recursion & Collections API Recursion Revisited Programming Assignments using the Collections API.
Building Java Programs Chapter 12 Lecture 12-2: recursive programming reading:
1 “Not all recursive solutions are better than iterative solutions…” “… recursion, however, can provide elegantly simple solutions to problems of great.
Recursion A method is recursive if it makes a call to itself. A method is recursive if it makes a call to itself. For example: For example: public void.
Analyzing Complexity of Lists OperationSorted Array Sorted Linked List Unsorted Array Unsorted Linked List Search( L, x ) O(logn) O( n ) O( n ) Insert(
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
1 Chapter 3. Recursion Lecture 6. In functions and data structures.
BR 8/99 Binary Numbers Again Recall than N binary digits (N bits) can represent unsigned integers from 0 to 2 N bits = 0 to 15 8 bits = 0 to 255.
Section 2.6 Representation of numbers. Decimal representation (base 10) Given a positive integer X, the decimal representation of X is a string of digits.
CSE 501N Fall ‘09 12: Recursion and Recursive Algorithms 8 October 2009 Nick Leidenfrost.
Week 6.  Lab 1 and 2 results  Common mistakes in Style  Lab 1 common mistakes in Design  Lab 2 common mistakes in Design  Tips on PE preparation.
Variables and Data Types.  Variable: Portion of memory for storing a determined value.  Could be numerical, could be character or sequence of characters.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Recursion Pepper. Another way to loop Call yourself repeatedly until you say to stop. Example: add up 10 numbers using addUp. -- Input – number to count.
Digital Representations ME 4611 Binary Representation Only two states (0 and 1) Easy to implement electronically %0= (0) 10 %1= (1) 10 %10= (2) 10 %11=
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
Homework #4: Operator Overloading and Strings By J. H. Wang Apr. 17, 2009.
RECURSION, CONTINUED Lecture 8 CS2110 – Fall 2015.
Print Me Who’s Data? First Executed Name Me My Mistake Q $100 Q $200 Q $300 Q $400 Q $500 Q $100 Q $200 Q $300 Q $400 Q $500 Final Jeopardy.
Dec Important Steps 1. Precise Specification What does the method do? What are the preconditions? 2. Write the base case What is the most basic.
A: A: double “4” A: “34” 4.
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
1 CS1110 Lecture 16, 26 Oct 2010 While-loops Reading for next time: Ch (arrays) Prelim 2: Tu Nov 9 th, 7:30-9pm. Last name A-Lewis: Olin 155 Last.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 5.1 – 5.2.
Midterm 2 Review Notes on the CS 5 midterm Take-home exam due by 5:00 pm Sunday evening (11/14) Hand in your solutions under the door of my office, Olin.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2014.
CMSC 202 Lesson 26 Miscellaneous Topics. Warmup Decide which of the following are legal statements: int a = 7; const int b = 6; int * const p1 = & a;
8/2/00SEM107- © Kamin and ReddyClass 5 - Lists - 1 Class 5 - Lists r The list data type r Recursive methods on lists.
Prof. I. J. Chung Data Structure #1 Professor I. J. Chung.
Linked Data Structures
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Chapter 4 – Fundamental Data Types
2.5 Another Java Application: Adding Integers
Multiple variables can be created in one declaration
Rational Numbers SWBAT define the set of rational numbers; identify subsets of rational numbers; write rational numbers in equivalent forms.
Recursion (Continued)
Computer Programming Methodology Input and While Loop
University of Gujrat Department of Computer Science
Data Structures Mohammed Thajeel To the second year students
Java Software Structures: John Lewis & Joseph Chase
Recursion (Continued)
Review Operation Bingo
Set includes rational numbers and irrational numbers.
Building Java Programs
CSE 143 Lecture 11 Recursive Programming slides created by Marty Stepp
Recursion (Continued)
CPS120: Introduction to Computer Science
Recursion Problems.
Programming Languages
Building Java Programs
Recursion (Continued)
Building Java Programs
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Data Types and Maths Programming Guides.
Lecture 22: Number Systems
CSE 142, Spring 2013 Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 5.1 – 5.2.
Building Java Programs
Boolean in C++ CSCE 121.
Recursion Review Spring 2019 CS 1110
Building Java Programs
Presentation transcript:

May 11, 2011

What we’ll do today Practice writing recursive specifications and functions Given a recursive problem definition Determine a proper specification (note preconditions) Given a problem description and specification: Write the recursive base case Write the recursive call Verify that it is correct Questions?

Important Steps 1. Precise Specification What does the method do? What are the preconditions? 2. Write the base case What is the most basic case? What causes termination of the recursive method? 3. Write the recursive case How do we make progress toward termination? Is your computation correct?

Writing Specifications Write a specification for a Method that: 1. Computes the complement of a positive integer. ie. The complement of is /** = the complement of n, formed by replacing each decimal digit of n by 10-n. ie. the result for the integer is Precondition: n > 0 and no digit of n is 0 */ 2. Reduce the positive input integer to a single digit. ie > 47+2 = 49 -> 4+9 = 13 -> 1+3 = 4 /** = n reduced to a single digit (by repeatedly summing its digits). Precondition: n > 0 */

Writing Specifications Write a specification for a Method that: 3. Compresses a String such that duplicate letters are replaced with counts. ie. aaabbbbbbccd -> a3b6c2d1 /** = s compressed such that duplicates are replaced with the count of how many occurrences that character has in a row.*/ 4. Converts an input integer to a string representation with commas. ie is converted to 5,923,821. /** = String representation of integer with commas added*/

Complement of an Integer /** = the complement of n, formed by replacing each decimal digit of n by 10-n. ie. the result for the integer is Precondition: n > 0 and no digit of n is 0 */ public static int complement(int n) { // Base Case if (n < 10) return 10 - n; // Recursive Case return complement(n/10) * 10 + (10 - n%10); }

Spring 2008 Prelim 3 /** = n reduced to a single digit (by repeatedly summing its digits). Precondition: n > 0 */ public static int addUp (int n) { // Base case if (n < 10) return n; // Recursive Case int x = n/10 + n%10; return addUp(x); } How do we know this works? return “x reduced to a single digit (by repeatedly summing its digits)”

Spring 2010 Final Exam /** = s compressed such that duplicates are replaced with the count of how many occurrences that character has in a row. ie. "aaaaabbbbbccccccaaax" is compressed to "a5b5c6a3x1“ */ public static String compress(String s) { // Base case if (s.equals(“”)) return “”; // Recursive Case int x = eqChar(s) return "" + s.charAt(0) + x + compress(s.substring(x)); } /**= the number of times the first character in s occurs in a row at the start of the String s. */ public static int eqChar(String s) {…}

/** = String representation of integer with commas added*/ public static String addCommas(int n) { // Base case if (n < 1000) return “” + n; // Recursive Case String number = "" + n; return addCommas (n/1000) + ",“ + number.substring(number.length()-3); } Problem: Properly add commas to an integer and return the string representation. ie is converted to 5,923,821. Is something wrong?

/** = String representation of integer with commas added*/ public static String addCommas(int n) { if (n < 0) return "-" + addCommasHelper(-n); else return addCommasHelper(n); } /** = String representation of a positive integer with commas added. Precondition: n >= 0*/ private static String addCommasHelper(int n) { // Base case if (n < 1000) return “” + n; // Recursive Case String number = "" + n; return addCommasHelper(n/1000) + ",“ + number.substring(number.length()-3); } Problem: Properly add commas to an integer and return the string representation. ie is converted to 5,923,821.

An extra problem… Given: Class FacebookProfile public String getName(); public Vector getFriends(); We want to answer the question: Is this FacebookProfile at most 6 degrees away from Kevin Bacon? Specification: /** = “this FacebookProfile is at most 6 degrees away from Kevin Bacon” */

6-Degrees of Kevin Bacon /** = “this FacebookProfile is at most 6 degrees away from Kevin Bacon” */ public boolean sixDegreesOfKevinBacon() { return sixDegreesHelper(6); } /** = “this FacebookProfile is at most n degrees away from Kevin Bacon” */ private boolean sixDegreesHelper(int n) { // Base case if (getName().equals(“Kevin Bacon”)) return true; if (n == 0) return false; // Recursive Case Vector friends = getFriends(); for (int i=0; i<friends.size(); i++) { if (friends.get(i).sixDegreesHelper(n-1)) return true; } return false; }

Questions?

Good Luck! Don’t Stress! Take your time! Have a great Summer!