Tutorial 9 Iteration. Reminder Assignment 8 is due Wednesday.

Slides:



Advertisements
Similar presentations
CS 116 Tutorial 2 Functional Abstraction. Reminders Assignment 2 is due this Wednesday at Noon.
Advertisements

String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
CS0004: Introduction to Programming Repetition – Do Loops.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Repeating Actions While and For Loops
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Chapter 4 Repetitive Execution. 2 Types of Repetition There are two basic types of repetition: 1) Repetition controlled by a counter; The body of the.
Loops – While, Do, For Repetition Statements Introduction to Arrays
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
1 Infinite Loops  The body of a while loop eventually must make the condition false  If not, it is an infinite loop, which will execute until the user.
Week 7 - Programming II Today – more features: – Loop control – Extending if/else – Nesting of loops Debugging tools Textbook chapter 7, pages
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Intro to Robots Conditionals and Recursion. Intro to Robots Modulus Two integer division operators - / and %. When dividing an integer by an integer we.
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
Java Programming: From the Ground Up
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.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
CS 100 Introduction to Computing Seminar October 7, 2015.
Iterative Statements: while, for, do-while Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006.
Xiaojuan Cai Computational Thinking 1 Lecture 8 Loop Structure Xiaojuan Cai (蔡小娟) Fall, 2015.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 5 Repetition.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
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 10 Loops: while and for CSC1310 Fall 2009.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
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.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
Loops (While and For) CSE 1310 – Introduction to Computers and Programming 1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Programming in Java (COP 2250) Lecture 12 & 13 Chengyong Yang Fall, 2005.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
CSE123 - Lecture 4 Structured Programming- Loops.
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
String and Lists Dr. José M. Reyes Álamo.
Lesson #5 Repetition and Loops.
CprE 185: Intro to Problem Solving (using C)
while Repetition Structure
Chapter 3: Decisions and Loops
Topics Introduction to Repetition Structures
Lesson #5 Repetition and Loops.
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
Topics Introduction to Repetition Structures
Lecture 07 More Repetition Richard Gesick.
Week 8 - Programming II Today – more features: Loop control
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Control Structure Senior Lecturer
Outline Altering flow of control Boolean expressions
Lesson #5 Repetition and Loops.
CISC101 Reminders Quiz 1 grading underway Next Quiz, next week.
Module 4 Loops.
String and Lists Dr. José M. Reyes Álamo.
3.5- The while Statement The while statement has the following syntax:
Alternate Version of STARTING OUT WITH C++ 4th Edition
Console.WriteLine(“Good luck!”);
Topics Introduction to Repetition Structures
Lesson #5 Repetition and Loops.
REPETITION Why Repetition?
COMPUTING.
Presentation transcript:

Tutorial 9 Iteration

Reminder Assignment 8 is due Wednesday

Review Loops! ◦ while ◦ for ◦ nested

Review – While loops ***initialize variables*** while condition: ***body of while*** ***update variables *** It will continue to execute the body of the while loop until condition == False Variables MUST be updated, otherwise there might be an infinite loop! This part will continuously be executed until condition is False

Review – For loops for item in collection: *** body of for *** It will execute the body of the for loop len(collection) times, once for every element in collection Similar to map ; goes through every element in the collection Be careful of mutating your collection inside the loop!

while loop version of a for loop i = 0 while i < len(collection): item = collection[i] ***body of for*** i = i + 1 for item in collection: ***body of for***

Review – Nested loops for i in collection1: *** body of outer for *** for j in collection2: *** body of inner for *** For each i in collection1, the inner for loop will be executed Similar to nested lambda in Scheme Body of outer for loop The inner for loop will be executed len(collection2) times

Question 1 - all_same_type Write a function all_same_type that consumes a list and produce true if all members of that list are of the same type, else false. Note that Python's built-in type function does not distinguish between types of lists: ◦ type([1,2]) == type(['a','b']) => True

Question 3 – valid_input Write a function called valid_input that consumes a string to be used as the prompt, a list of strings of value inputs, and produces the value entered that is equal to one of the elements from the list. The function should continuously prompt the user for input until the user enters a value in the list of valid input values. You may assume that the user enters input that is the correct type.

Question 6 Write a Python function divisible_by_3 that consumes a Nat (called n), and produces True if n is divisible by 3, and False if it is not divisible by 3. You must use the following algorithm: ◦ The only numbers less than 10 that are divisible by 3 are: 0,3,6,9 ◦ A number is divisible by 3 if the sum of its digits is also divisible by 3. If the sum of the digits of the number is greater than 10, calculate the sum of the digits of the sum, and repeat until you get a number less than 10.

Question 4 – max_even_sum Write a Python function max_even_sum that consumes a nonempty list of lists of positive integers. It computes the sum of the even integers in each sublist, and produces the largest such sum. If a sublist contains no even integers, its sum is zero.

Question 2 – selection_sort Write a function selection_sort that consumes a list of numbers. The function will mutate the consumed list to be sorted in increasing order.

Question 5: RL_encode Write a Python function RL_encode that consumes a string made up of the four letters {A, C, G, T}, and returns a run- length encoding of the string.

Question 5: RL_decode Write a Python function RL_decode that consumes a run-length encoding of a string E made up of only {A,C,G,T} and returns the string D for which E is the run-length encoding.