Some Common Issues: Common Algorithms

Slides:



Advertisements
Similar presentations
The Important Thing About By. The Important Thing About ******** The important thing about ***** is *****. It is true s/he can *****, *****, and *****.
Advertisements

Sorting algorithms Sieve of Eratosthenes
Logic Gates.
Playing computer with logic problems Please use speaker notes for additional information!
AB 11 22 33 44 55 66 77 88 99 10  20  19  18  17  16  15  14  13  12  11  21  22  23  24  25  26  27  28.
Tutorial #6 PseudoCode (reprise)
Computer Science Department FTSM Control Structure: Selection (Part 1) Knowledge: Understand various concepts of selection control structure Skill: Be.
Flow Charting, Structured English and PseudoCode
Lecture 14 Go over midterm results Algorithms Efficiency More on prime numbers.
Unit 171 Algorithms and Problem Solving  Introduction  Algorithm Design  Algorithm Properties  Algorithm Control Flow  Examples  Comparing Algorithms.
Programming Training Main Points: - Working with Functions in Python
PYTHON PROGRAMMING Week 10 – Wednesday. TERMS – CHAPTER 1 Write down definitions for these terms:  Computation  Computability  Computing  Artificial.
Python: Modularisation Damian Gordon. Modularisation Remember the prime checker program:
Selection: IF Statement Damian Gordon. adsdfsdsdsfsdfs dsdlkmfsdfmsdl kfsdmkfsldfmsk dddfsdsdfsd.
More on Functions (Part 2) Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Fibonacci Numbers Damian Gordon. Fibonacci Numbers.
Iteration: WHILE Loop Damian Gordon. WHILE Loop Consider the problem of searching for an entry in a phone book with only SELECTION:
Searching Damian Gordon. Google PageRank Damian Gordon.
Programming Training Main Points: - Problems with repetitions. - Discuss some important algorithms.
1 What We Will Learn  Introduction  Passing input parameters  Producing output  Scope of variables  Storage Class of variables  Function usage example.
ANALYSIS AND ALGORITHM DESIGN - IV. Repeat statements/looping/counting/iterations It is often necessary to repeat certain parts of a program a number.
Sorting: Optimising Bubblesort Damian Gordon. Sorting: Bubble Sort If we look at the bubble sort algorithm again:
Data Structures: Arrays Damian Gordon. Arrays Imagine we had to record the age of everyone in the class, we could do it declaring a variable for each.
Python: Arrays Damian Gordon. Arrays In Python arrays are sometimes called “lists” or “tuple” but we’ll stick to the more commonly used term “array”.
Modularisation Damian Gordon. Modularisation Let’s imagine we had code as follows:
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
Introduction to Python Damian Gordon. The Python Programming Language Python was developed by Guido van Rossum in the Netherlands in Van Rossum.
Python: Sorting - Bubblesort Damian Gordon. Sorting: Bubblesort The simplest algorithm for sort an array is called BUBBLE SORT. It works as follows for.
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.
Python: Selection Damian Gordon. Python: Selection We’ll consider two ways to do selection: The IF statement The CASE statement.
L AB 4 July 5th. F OR LOOP Exercise 1: Find two ways of “Summing all 1, 2, and 3 digit prime numbers.” Use for loop Hint: isprime, primes.
Python: Structured Programming Damian Gordon. Structured Programming Remember the modularised version of the prime number checking program:
Recursion Damian Gordon. Recursion Recursion: Factorial Recursion is a feature of modularisation, when a module can call itself to complete a solution.
Python: Iteration Damian Gordon. Python: Iteration We’ll consider four ways to do iteration: – The WHILE loop – The FOR loop – The DO loop – The LOOP.
Pseudocode. Algorithm A procedure for solving a problem in terms of the actions to be executed and the order in which those actions are to be executed.
Recursion Damian Gordon. Recursion Factorial Fibonacci Decimal to Binary conversion Travelling Salesman Problem Knight’s Tour.
More on Functions (Part 2) Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Introduction to Python Damian Gordon e:
Iteration: FOR, DO, LOOP Loop Damian Gordon. FOR Loop The FOR loop does the same thing as a WHILE loop but is easier if you are using the loop to do a.
Selection: CASE Statement Damian Gordon. Selection As well as the IF Statement, another form of SELECTION is the CASE statement.
Introduction to Python
More on Functions (Part 2)
Recall: d is a divisor of n  n % d == 0
Repetition Control Structure in C++ Program
Pseudocode (Revision)
Python: Advanced Sorting Algorithms
Logic Gates.
Linked Lists Damian Gordon.
Computer Programming Fundamentals
Programming – syllabus knowledge
Factors, multiple, primes: Factors from prime factors
Boolean Logic Damian Gordon.
Queues: Implemented using Arrays
Factors, multiple, primes: Prime factors
Logic Gates.
Patterns to KNOW.
Python: Algorithms Damian Gordon.
Programming Training Main Points: - Working with Functions in Python
Programming Training Main Points: - Problems with repetitions.
IF Statements.
More on Functions (Part 2)
More on Functions (Part 2)
Some Common Issues: Iteration
We are starting JavaScript. Here are a set of examples
Final Code Generation and Code Optimization
Structured Programming
Circular Queues: Implemented using Arrays
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
Queues: Implemented using Linked Lists
Factors, multiple, primes: Multiples
Presentation transcript:

Some Common Issues: Common Algorithms Damian Gordon

Prime Numbers

# PROGRAM CheckPrime: a = int(input("Please input value:")) b = a - 1 IsPrime = True while b != 1: # DO if a % b == 0: # THEN IsPrime = False # ENDIF; b = b - 1 # ENDWHILE; if IsPrime: print(a, "is a prime number") else: print(a, "is not a prime number") # END.

7 A # PROGRAM CheckPrime: a = int(input("Please input value:")) b = a - 1 IsPrime = True while b != 1: # DO if a % b == 0: # THEN IsPrime = False # ENDIF; b = b - 1 # ENDWHILE; if IsPrime: print(a, "is a prime number") else: print(a, "is not a prime number") # END.

7 A 7/6 A/B 7/5 A/B 7/4 A/B 7/3 A/B 7/2 A/B # PROGRAM CheckPrime: a = int(input("Please input value:")) b = a - 1 IsPrime = True while b != 1: # DO if a % b == 0: # THEN IsPrime = False # ENDIF; b = b - 1 # ENDWHILE; if IsPrime: print(a, "is a prime number") else: print(a, "is not a prime number") # END. 7/6 A/B 7/5 A/B 7/4 A/B 7/3 A/B 7/2 A/B

# PROGRAM CheckPrime: a = int(input("Please input value:")) b = a - 1 IsPrime = True while b != 1: # DO if a % b == 0: # THEN IsPrime = False # ENDIF; b = b - 1 # ENDWHILE; if IsPrime: print(a, "is a prime number") else: print(a, "is not a prime number") # END. Sentinel value

# PROGRAM CheckPrime: a = int(input("Please input value:")) b = a - 1 IsPrime = True while b != 1: # DO if a % b == 0: # THEN IsPrime = False # ENDIF; b = b - 1 # ENDWHILE; if IsPrime: print(a, "is a prime number") else: print(a, "is not a prime number") # END. Sentinel value

Fibonacci Numbers

# PROGRAM FibonacciNumbers: a = int(input("Please input value:")) FirstNum = 1 SecondNum = 1 while a != 1: # DO total = SecondNum + FirstNum FirstNum = SecondNum SecondNum = total a = a - 1 # ENDWHILE; print(total) # END.

+ FirstNum SecondNum Total # PROGRAM FibonacciNumbers: a = int(input("Please input value:")) FirstNum = 1 SecondNum = 1 while a != 1: # DO total = SecondNum + FirstNum FirstNum = SecondNum SecondNum = total a = a - 1 # ENDWHILE; print(total) # END. FirstNum + SecondNum Total

+ FirstNum SecondNum Total # PROGRAM FibonacciNumbers: a = int(input("Please input value:")) FirstNum = 1 SecondNum = 1 while a != 1: # DO total = SecondNum + FirstNum FirstNum = SecondNum SecondNum = total a = a - 1 # ENDWHILE; print(total) # END. FirstNum + SecondNum Total