Programming Training Main Points: - Working with Functions in Python

Slides:



Advertisements
Similar presentations
Discrete Mathematics Lecture 3
Advertisements

Introduction to Computing Science and Programming I
Tutorial #6 PseudoCode (reprise)
Structured programming
11 and 6 3 and 9 2, 5, 10 and 4, 8 Divisibility Rules.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Algorithmics - Lecture 21 LECTURE 2: Algorithms description - examples -
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Looping Exercises Deciding Which Loop to Use At this.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Recursion Review.
Computer Programming Basics Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea.
Programming Training Main Points: - Lists / Arrays in Python. - Fundamental algorithms on Arrays.
Programming Training Main Points: - Python Statements - Problems with selections.
Python: Modularisation Damian Gordon. Modularisation Remember the prime checker program:
1.6 Loops academy.zariba.com 1. Lecture Content 1.While loops 2.Do-While loops 3.For loops 4.Foreach loops 5.Loop operators – break, continue 6.Nested.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
WIKIPEDIA HAS MANY MORE DIVISIBILITY RULES. EXAMPLE Since 52=13(4) is divisible by 4, is divisible by 4 Since 452=56(8)+4 is not divisible.
Programming Training Main Points: - Problems with repetitions. - Discuss some important algorithms.
Introduction to Programming (in C++) Loops Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Lecture 15: Control Flow (cont). 2 Lecture Contents: t Design and implementation of programs illustrating linear algorithms, branch algorithms and loop.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
More on Efficiency Issues. Greatest Common Divisor given two numbers n and m find their greatest common divisor possible approach –find the common primes.
5 While-Statements © 2010 David A Watt, University of Glasgow Accelerated Programming 2 Part I: Python Programming.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Chapter 2: Algorithm Analysis Application of Big-Oh to program analysis Logarithms in Running Time Lydia Sinapova, Simpson College Mark Allen Weiss: Data.
Prime Numbers Damian Gordon. Prime Numbers So let’s say we want to express the following algorithm: – Read in a number and check if it’s a prime number.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
Recitation 8 Programming for Engineers in Python.
Programming Training kiddo Main Points: - Python Statements - Problems with selections.
Application: Algorithms Lecture 19 Section 3.8 Tue, Feb 20, 2007.
CSCI 125 & 161 Lecture 12 Martin van Bommel. Prime Numbers Prime number is one whose only divisors are the number 1 and itself Therefore, number is prime.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
CPSC 233 Tutorial 5 February 2 th /3 th, Java Loop Statements A portion of a program that repeats a statement or a group of statements is called.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 3. Time Complexity Calculations.
Quiz1 Question  Add Binary Numbers a) b) c) d) e) none
Repetition Looping. Types for while do..while for The for loop allows you to iterate through a variable for a specific range of values. You must supply.
Programming Training Main Points: - More Fundamental algorithms on Arrays. - Reading / Writing from files - Problem Solving.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Computer C programming Chapter 3. CHAPTER 3 Program Looping –The for Statement –Nested for Loops –for Loop Variants –The while Statement –The do Statement.
Control Structures: Examples. for-loop example Q: If a=1, b=3, and x=7, what is the value of x when the loop terminates? A: x=1 for(k=a; k
Introduction to Algorithms
Main Points: - Python Statements - Problems with selections.
Introduction to C++ Programming Language
Recall: d is a divisor of n  n % d == 0
GC211Data Structure Lecture2 Sara Alhajjam.
Repetition-Counter control Loop
Ch 7: JavaScript Control Statements I.
Main Points: - More Fundamental Algorithms on Arrays.
2008/09/24: Lecture 6b CMSC 104, Section 0101 John Y. Park
Lecture 4B More Repetition Richard Gesick
Control Structure Senior Lecturer
Repetition and Loop Statements
Control Structure Senior Lecturer
Programming Training Main Points:
Python: Algorithms Damian Gordon.
Programming Training Main Points: - Working with Functions in Python
Programming Training Main Points: - Problems with repetitions.
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
The structure of programming
More Loops Topics Counter-Controlled (Definite) Repetition
Application: Algorithms
Application: Algorithms
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
REPETITION Why Repetition?
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

Programming Training Main Points: - Working with Functions in Python - Problems with Numbers. - Discuss some important algorithms - Primality - Digits - Greatest Common Divisor

Python Repetitions. while repetitions execute repeatedly a statement while a condition holds or for a number of times. while test : line1 line2 … # endwhile While loop repeats the block while test holds. If test is initially false the the block is not exeecuted. Block to repeat

Python Repetitions. for repetitions execute repeatedly a statement while a condition holds or for a number of times. for elem in iterator : line1 line2 … # endfor For loop repeats statements for all elems in iterator. iterator is a set of elements that can be traversed. Block to repeat

Functions in Python A Python program is a sequence of a functions which can be executed. Function = Routine = Procedure = Method = Solution for a sub-problem. A Python function is written once and used/called as many times as needed. def function_name(arg1, arg2,…): statements to find the output; return output; # end function 1. function_name is the name of the function 2. arg1, arg2,…are the function arguments (usually the inputs) 3. the function body must find the output and return it.

Steps to work with functions 1. Define these functions you want to work with - Start from the arguments as inputs. - Give the sequence of statements to construct the output res. - Return the output res. 2. Call a function at any time you need. - The call is function_name(a1,a2,…) and returns a value that can be used. - a1, a2,… are the actual/current arguments 3. Actual arguments are NOT change after the call even if they change in the function

Example 1. 1. Find the minimum and maximum of two integers a and b. Inputs: a, b – int Outputs: max, min – int How to do it? Compare a and b and then decide on min and max def maxMin(a, b): # compare a and b if a >= b : max = a min = b else : # endif return max, min # end maxMin How to find the max of 4 numbers a, b, c, d def max4Nrs(a, b, c, d): max1, min1 = maxMin(a,b) max2, min2 = maxMin(c,d) max3, min3 = maxMin(max1, max2) return max3 # end max4Nrs

Find pow so that d^pow | n. Any time when a divisor is found we remove it from the number. Counting problems use a counting counter cont: - initial value is 0. - count increases when the event occurs. while n is divisible by d - divide n by d - count that

Find pow so that d^pow | n. Inputs: n, d – long  The inputs are the function arguments Output: pow – long  The output must be calculated and returned. Remarks: 1) The call power(n,d) does not change n 2) The function can be applied in prime decomposition - traverse all possible divisors - find the power of one divisor - extract it from the number def power(n,d): count = 0 while n % d == 0 : # intiger division and count n = n // d count = count + 1 # endwhile return count # end power

Prime numbers If a number n is integer then find whether is prime or not. n is prime when n has only 2 divisors 1 and n. Example: - n =2, 3, 5, 7 are all prime - even numbers are not prime. - 12345678910987654321 is prime -1234567891010987654321 is prime too

Prime numbers (1) Some Remarks: n==2  n is prime. Even numbers: n%2==0  n is not prime Odd numbers have only odd divisors d=3,5,7,… Repeat for d=3,5,7,…sqrt(n) - if d is divisor then n is not prime.

Prime numbers (2) Function to test something are named isSomething() def isPrime(n): if n==2 or n == 3 : return 1 #endif if n % 2 == 0 : return 0 for d in range(3, math.sqrt(n)+1, 2) : if n % d == 0 : # endif # endfor # end isPrime Function to test something are named isSomething() and return either 0 or 1. Important algorithms with multiple applications Note: it only traverses the odd numbers up to sqrt(n)

Problems with Prime numbers Write a program to list all primes up to a threshold. Write a program to list all palyndrom primes. Write a program to list all primes whose mirror is prime too. Given an even number n then write a program to decompose n in a sum of 2 primes e.g. n = p1+p2 with p1 and p2 primes [Goldbach’s conjecture] Etc. def printPrimes(): n = int(input('input n: ')) # travers all numbers for i in range(2, n+1) : # test primality if isprime(i) : print(i) # endif # endfor # end printPrimes

Find the reverse / mirror of a long. If n is a long number then last=n % 10 is the last digit. n = n/10 is the new number from which we remove last. n = n*10 +digit is the new number to which digit is added. If we repeat removing the last digit then we can find: - all the digits of the number. - number of digits. - the mirror of a number We repeat while the number is not fully done.

def reverse(n): mirrorN = 0 while n != 0 : digit = n % 10 n = n // 10 mirrorN = mirrorN * 10 + digit #endwhile return mirrorN #end reverse def isPalindrome(n): mirror = reverse(n) if n == mirror : return 1 else : return 0 #endif #end isPalindrome

Greatest Common Divisor If a, b are integer then the greatest common divisor Example: - a=24, b=36  gcd=12 - a=25, b=33  gcd=1 How to solve it? - the Euclidian algorithm

The Euclidean Algorithm Sequence of divisions until the remainder is 0. Repeat the following: - divide the numbers - if remainder is 0 then break - change the numbers: first=second; second=remainder

def gcd(a,b): first, second = a, b while first % second != 0 : # int divide first to second remaider = first % second result = first //second # prepare the next division first = second second = remaider # endwhile return second # end gcd

To do List Read more about working with functions. Solve the HW problems.