Conditionals !

Similar documents
Conditionals & Loops /

Functions & Variables /

COP 4516: Math for Programming Contest Notes

Euclid's Algorithm. MA/CSSE 473 Day 06. Student Questions Odd Pie Fight Euclid's algorithm (if there is time) extended Euclid's algorithm

1 Elementary number theory

Standard Version of Starting Out with C++, 4th Edition. Chapter 19 Recursion. Copyright 2003 Scott/Jones Publishing

UCT Algorithm Circle: Number Theory

Names and Functions. Chapter 2

Math Introduction to Advanced Mathematics

COMPSCI 230 Discrete Math Prime Numbers January 24, / 15

Solutions to the Second Midterm Exam

ELEMENTARY NUMBER THEORY AND METHODS OF PROOF

CS/COE 1501 cs.pitt.edu/~bill/1501/ More Math

Add Binary Numbers What is the largest decimal number you can represent using 3 bits?

Reading 8 : Recursion

Mathematics. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

Lecture Notes, CSE 232, Fall 2014 Semester

Loops / Repetition Statements

Chapter 4. Number Theory. 4.1 Factors and multiples

Intro to Computational Programming in C Engineering For Kids!

Programming & Data Structure Laboratory. Day 2, July 24, 2014

More on Strings & Arrays

Chapter 1 Programming: A General Overview

CS302: Self Check Quiz 2

SSEA Computer Science: Track A. Dr. Cynthia Lee Lecturer in Computer Science Stanford

Lecture Notes on Contracts

Integers and Mathematical Induction

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

Assertions & Verification & Example Loop Invariants Example Exam Questions

Lecture 10: Recursion vs Iteration

CS 3410 Ch 7 Recursion

CS 310 Advanced Data Structures and Algorithms

Assertions & Verification Example Exam Questions

Information Science 1

Problem Solving & C M. Tech. (CS) 1st year, 2014

Chapter 1 An Introduction to Computer Science. INVITATION TO Computer Science 1

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

Definition MATH Benjamin V.C. Collins, James A. Swenson MATH 2730

Two Approaches to Algorithms An Example (1) Iteration (2) Recursion

Chapter Summary. Mathematical Induction Recursive Definitions Structural Induction Recursive Algorithms

UEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter

Lecture 7 Number Theory Euiseong Seo

CSc 372 Comparative Programming Languages

CSc 372. Comparative Programming Languages. 8 : Haskell Function Examples. Department of Computer Science University of Arizona

BEng (Hons) Telecommunications. Examinations for / Semester 2

CSCE 110 Dr. Amr Goneid Exercise Sheet (7): Exercises on Recursion (Solutions)

Case by Case. Chapter 3

Number System. Introduction. Natural Numbers (N) Whole Numbers (W) Integers (Z) Prime Numbers (P) Face Value. Place Value

Lecture 5: Variables and Functions

Iteration. Chapter 7. Prof. Mauro Gaspari: Mauro Gaspari - University of Bologna -

CSC148, Lab #4. General rules. Overview. Tracing recursion. Greatest Common Denominator GCD

Math 171 Proficiency Packet on Integers

Genome Sciences 373 Genome Informa1cs. Quiz Sec1on #1 March 31, 2015

UNIT-II NUMBER THEORY

Exponentiation. Evaluation of Polynomial. A Little Smarter. Horner s Rule. Chapter 5 Recursive Algorithms 2/19/2016 A 2 M = (A M ) 2 A M+N = A M A N

bool bool - either true or false

Issue with Implementing PrimeSieve() in Go

CSC 222: Object-Oriented Programming. Spring 2012

1 Elementary number theory

Combinational Circuits Digital Logic (Materials taken primarily from:

Technical Section. Lab 4 while loops and for loops. A. while Loops or for loops

Recursion. Chapter 5

Is the statement sufficient? If both x and y are odd, is xy odd? 1) xy 2 < 0. Odds & Evens. Positives & Negatives. Answer: Yes, xy is odd

APCS Semester #1 Final Exam Practice Problems

1 / 43. Today. Finish Euclid. Bijection/CRT/Isomorphism. Fermat s Little Theorem. Review for Midterm.

CS 97SI: INTRODUCTION TO PROGRAMMING CONTESTS. Jaehyun Park

Creating a new data type

MAT 685: C++ for Mathematicians

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

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Odd-Numbered Answers to Exercise Set 1.1: Numbers

Recursive Functions. Biostatistics 615 Lecture 5

Flow of Control Branching 2. Cheng, Wei COMP May 19, Title

CSci 1113, Fall 2015 Lab Exercise 4 (Week 5): Write Your Own Functions. User Defined Functions

CSCE 110: Programming I

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Chapter 3. Set Theory. 3.1 What is a Set?

ASSIGNMENT 3 Methods, Arrays, and the Java Standard Class Library

QUIZ: What value is stored in a after this

Information Science 1

Today. Finish Euclid. Bijection/CRT/Isomorphism. Review for Midterm.

Programming Logic & Pseudocode. Python Bootcamp, Day 1 Anna Rosen

CS 221 Lecture. Tuesday, 11 October 2011

Chapter 4: Making Decisions. Copyright 2012 Pearson Education, Inc. Sunday, September 7, 14

Recursion(int day){return Recursion(day += 1);} Comp Sci 1575 Data Structures. Recursive design. Convert loops to recursion

Recursion CS GMU

Announcements. Homework 0: using cin with 10/3 is NOT the same as (directly)

Structured programming

Lecture 2. CS118 Term planner. Refinement. Recall our first Java program. Program skeleton GCD. For your first seminar. For your second seminar

Introduction to Scientific Computing

Math 55 - Spring 04 - Lecture notes # 1 - Jan 20 (Tuesday)

4. Functions. March 10, 2010

1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?

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

CPSC 121: Models of Computation Assignment #4, due Thursday, March 16 th,2017at16:00

Data Dependences and Parallelization

Know the Well-ordering principle: Any set of positive integers which has at least one element contains a smallest element.

ECE 2574: Data Structures and Algorithms - Recursion Part I. C. L. Wyatt

Loops / Repetition Statements

WYSE Academic Challenge Computer Science Test (State) 2012 Solution Set

Transcription:

Conditionals 02-201!

Computing GCD GCD Problem: Compute the greatest common divisor of two integers. Input: Two integers a and b. Output: The greatest common divisor of a and b. Exercise: Design an algorithm in pseudocode solving this problem.

The World s First Nontrivial Algorithm (~300 BC) Theorem: If a > b, then gcd(a, b) = gcd(a b, b). Example gcd(378, 273) = gcd(105, 273) = gcd(105, 168) = gcd(105, 63) = gcd(42, 63) = gcd(42, 21) = gcd(21, 21) = 21 Euclid! Exercise: Implement Euclid s algorithm in pseudocode.

Recall: Recursive Gauss RecursiveGauss(n) if n = 0 return 0 else return n + RecursiveGauss(n-1)

Recursive Euclid in Go Comments describing! the function! Function Name! Parameters! // Gcd(a,b) computes the greatest common // divisor of a and b func Gcd(a int, b int) int { Return type! comes after the () and before the {! if a == b { return a else if a > b { return Gcd(a - b, b) else { return Gcd(a, b - a) Return statement: stops! executing the function! and gives its value! Function call! (this one is recursive because it calls the function it s in)!

Recursive Factorial Exercise: Write a recursive Go function that takes an integer n and returns n! = (n)(n-1) (2)(1).

Recursive Fibonacci Exercise: Write a recursive Go function that takes an integer n and returns the n-th Fibonacci number.

The Problem with Recursive Fibonacci http://www.introprogramming.info!

Can You Spot the Error? func Gcd(a int, b int) int { if a == b { return a else if a > b { var c int c = a - b return Gcd(c, b) else { c = b - a return Gcd(a, c)

Scope: How Long Variables Last Variables persist from when they are created until the end of the innermost { block that they are in. func Gcd(a int, b int) int { var c int if a == b { return a else if a > b { c = a - b return Gcd(c, b) else { c = b - a return Gcd(a, c) variable c created variable c destroyed

Scope: How Long Variables Last Variables persist from when they are created until the end of the innermost { block that they are in. func Gcd(a int, b int) int { if a == b { return a else if a > b { var c int c = a - b return Gcd(c, b) else { var c int c = b - a return Gcd(a, c) c created c destroyed c resurrected c destroyed

Can You Spot the Error? Variables persist from when they are created until the end of the innermost { block that they are in. func Gcd(a int, b int) int { var c int if a == b { return a else if a > b { var c int c = a - b return Gcd(c, b) else { c = b - a return Gcd(a, c)

Scope of Parameters func Gcd(a int, b int) int { if a == b { return a else if a > b { return Gcd(a - b, b) else { return Gcd(a, b - a) variables a and b created variables a and b destroyed

Scope of Parameters func Gcd(a int, b int) int { if a == b { return a else if a > b { return Gcd(a - b, b) else { return Gcd(a, b - a) variables a and b created variables a and b destroyed var x, y int = 378, 273 var d int = Gcd(x, y) fmt.println( x is, x) fmt.println( y is, y) Think: What is printed?

Additional Conditions Boolean Operator Meaning e1 > e2 e1 is greater than e2! e1 < e2 e1 is less than e2! e1 >= e2 e1 is greater than or equal to e2! e1 <= e2 e1 is less than or equal to e2! e1 == e2 e1 is equal to e2! e1!= e2 e1 is not equal to e2!!e1 true if and only if e1 is false! (e1 and e2 can be complicated expressions) Example conditions: a > 10 * b + c 10 == 10 square(10) < 101-1 + 2!(x*y < 33) Boolean expressions: evaluate to true or false

Boolean Operators: AND and OR Boolean Operator Meaning pipe character Often above \ on your keyboard e1 && e2 e1 e2 true if e1 is true AND e2 is true! true if e1 is true OR e2 is true! Truth Table e 1 e 2 e 1 && e 2 e 1 e 2 true! true! true! true! true! false! false! true! false! true! false! true! false! false! false! false!

Order of Operator Precedence Precedence Operator 5! * / % 4! + - 3! ==!= < <= > >= 2! && 1! Example: 2*a + b!= 3 && a*c 3/z < -5 c + a/2 >= 15

Order of Operator Precedence Precedence Operator 5! * / % 4! + - 3! ==!= < <= > >= 2! && 1! Example: 2*a + b!= 3 && a*c 3/z < -5 c + a/2 >= 15

Order of Operator Precedence Precedence Operator 5! * / % 4! + - 3! ==!= < <= > >= 2! && 1! Example: 2*a + b!= 3 && a*c 3/z < -5 c + a/2 >= 15

Order of Operator Precedence Precedence Operator 5! * / % 4! + - 3! ==!= < <= > >= 2! && 1! Example: 2*a + b!= 3 && a*c 3/z < -5 c + a/2 >= 15

Order of Operator Precedence Precedence Operator 5! * / % 4! + - 3! ==!= < <= > >= 2! && 1! Example: 2*a + b!= 3 && a*c 3/z < -5 c + a/2 >= 15

Order of Operator Precedence Precedence Operator 5! * / % 4! + - 3! ==!= < <= > >= 2! && 1! Example: 2*a + b!= 3 && a*c 3/z < -5 c + a/2 >= 15

Order of Operator Precedence Precedence Operator 5! * / % 4! + - 3! ==!= < <= > >= 2! && 1! Example: 2*a + b!= 3 && (a*c 3/z < -5 c + a/2 >= 15) Like in math, we can use parentheses to force a different order.!

True/False Quiz a = 10 b = 50 a > 10 && b > 20 b==50 a == 10 && b >= 100 a==10 && b < 100 && a*b > 1000 a>5 && b<20 a==0 && b==0 a>20 b < 51 b-a*b > 0 a>5 b>20 && a==0 b==0 a=10 && b=50 a>5 (b>20 && a==0) b==0 a==10 && b >= 100 b == 50

Another If/Else Example // returns the smallest even number // among 2 ints; returns 0 if both are odd func SmallestEven(a, b int) int { if a % 2 == 0 && b % 2 == 0 { // both a and b are even, so return smaller one if a < b { return a else { return b else if a % 2 == 0 { // a is even but b is not return a else if b % 2 == 0 { // only b is even return b else { // both a and b are odd return 0 Why is this line OK? What would happen if we swapped this with the first case? % is the modulus operator: x % y is the remainder when integer x is divided by integer y.

Switch Statement switch statements let you express mutually exclusive tests compactly. // Returns the smallest even number // among 2 ints; returns 0 if both are odd func SmallestEven(a, b int) int { switch { case a % 2 == 0 && b % 2 == 0: if a < b { return a else { return b case a % 2 == 0: return a case b % 2 == 0: return b default: return 0 Each case part contains a condition, followed by a : and then a sequence of statements. The statements associated with the first true case will be executed. The optional default case is executed if none of the others are.

wikipedia user EMDX! Switch Is a Railroad Analogy

Switch Statement with Doomsday // KeyDay takes the month as input (between 1 and 12) // It returns the key doomsday of the month func KeyDay(month int) int { switch month { case 1: return 31 case 2: return 28 case 3: return 0 case 4: return 4 case 5: return 9 case 6: return 6 case 7: return 11 case 8: return 8 case 9: return 5 case 10: return 10 case 11: return 7 case 12: return 12

Switch Statement with the Skew Array 2 skew 1 0 1 2 0 5 10 15 20 C ATGGGCATCGGCCATACGCC // SkewValue computes the contribution of an individual // nucleotide to the computation of skew func SkewValue(symbol string) int { switch symbol { case A : return 0 case C : return -1 case G : return 1 case T : return 0

Guidelines for Programming 1. Get running programs first, then focus on formatting. 2. Comment your code more often than you think you should. 3. Don t write a single line of code before you plan (think about writing a short story). English first, then pseudocode, then Go. 4. Code the pieces that you know how to code first. 5. Code a little bit every day rather than a lot the night before an assignment is due. 6. Don t get bogged down on a single piece for more than 15-20 minutes. 7. Compile your code frequently. 8. Negative space is important you should be able to read code easily. 9. One way of doing something well > ten ways of doing it poorly.