CMPT 120 Lecture 17 Practice Exercise 3 SOLUTIONS

  • Slides: 11
Download presentation
CMPT 120 Lecture 17 – Practice Exercise 3 SOLUTIONS

CMPT 120 Lecture 17 – Practice Exercise 3 SOLUTIONS

Goals for Today! 1. Practise designing a solution (algorithm) to a problem 2. Practise

Goals for Today! 1. Practise designing a solution (algorithm) to a problem 2. Practise implementing a program (solution) described by an algorithm 2

Question 1 Homework – from a few lectures’ ago! • Problem Statement: • Given

Question 1 Homework – from a few lectures’ ago! • Problem Statement: • Given 3 numbers, find the largest one. • How to proceed: • Write an algorithm, i. e. , steps, in pseudocode (not in Python) on the supplied piece of paper • Write your name • Submit at the end of the lecture Possible solution: https: //repl. it/repls/Teeming. Micro. Handwritingrecognition 3

Question 1 – Handout – Page 1 Homework – from a few lectures’ ago!

Question 1 – Handout – Page 1 Homework – from a few lectures’ ago! • Problem Statement: • Given 3 numbers, find the largest one. 4

Question 2 • Problem Statement: • Given 2 words, figure out if they are

Question 2 • Problem Statement: • Given 2 words, figure out if they are palindromes. • How to proceed: • Write an algorithm, i. e. , steps, in pseudocode (not in Python) on the supplied piece of paper • Write your name • Submit at the end of the lecture Possible solution : https: //repl. it/repls/Honeydew. Grouchy. Metrics 5

Question 2 – Handout – Page 2 • Problem Statement: • Given 2 words,

Question 2 – Handout – Page 2 • Problem Statement: • Given 2 words, figure out if they are palindromes. 6

Question 3 – Homework – from a few lectures’ ago! • Problem Statement: •

Question 3 – Homework – from a few lectures’ ago! • Problem Statement: • Let’s create a function that returns the largest number out of 3 numbers • Requirements: • You cannot use the max( ) built-in function • You cannot use the word max to name your function • How to proceed: • Implement your algorithm in Python using Repl. It • No submission required Possible solution: https: //repl. it/repls/Teeming. Micro. Handwritingrecognition 7

Question 4 – Back to our Guessing Game • Remember our Guessing Game program

Question 4 – Back to our Guessing Game • Remember our Guessing Game program from Lecture 7 (May 22), have a look at its version 4 on our course web site 1. List an input for which this Guessing Game version 4 works just fine! 1. input: three wrong numbers 2. input: two numbers then the correct number 2. List an input for which this Guessing Game version 4 does not work well (its behaviour does not make sense) ! 1. input: the correct number first 2. input: a wrong number then the correct number 8

Question 4 – Back to our Guessing Game • Problem Statement: • Fix our

Question 4 – Back to our Guessing Game • Problem Statement: • Fix our Guessing Game version 4 (hence creating version 5) • This signifies that when you enter the input you listed in 2. on the previous slide, our Guessing Game version 5 will work just fine! Possible solution: https: //repl. it/repls/Heavenly. Busy. Net 9

Question 5 - Loops Let’s convert the following Python code fragment such that it

Question 5 - Loops Let’s convert the following Python code fragment such that it no longer uses a while loop but instead, it uses a for loop: fruit = ["banana", "apple", "plum"] index = 0 while index < len(fruit): print(fruit[index]) index = index + 1 Solution: index = 0 for index in range(len(fruit)) : print(fruit[index]) 10

Question 6 - Strange Calculator • Problem Statement: • Write a program that takes

Question 6 - Strange Calculator • Problem Statement: • Write a program that takes a string as an input, such as “ 24 + 16”, “ 30 – 5”, “ 10 * 4”, 36 / 2”, compute the equation found in the string and output the equation and its result such as 24 + 16 = 40 • Requirements: • Note that the output, such as 24 + 16 = 40, is no longer made of strings, but they are all integers • Your program cannot make use of functions such as eval( ), etc… Please, feel free to send your solution to this question to the instructor feedback. 11