Description
-
Open a browser and log into Brightspace
-
On the left hand side under Labs tab, find lab5 material contained in lab5-students.zip file
-
Download that file to the Desktop and unzip it.
Before starting, always make sure you are running Python 3
This slide is applicable to all labs, exercises, assignments … etc
ALWAYS MAKE SURE FIRST that you are running Python 3.4 (3.5 or 3.6 is fine too)
That is, when you click on IDLE (or start Python any other way) look at the first line that the Python shell displays. It should say Python 3.4 or 3.5 or 3.6 (and then some extra digits)
If you do not know how to do this, read the material provided with Lab 1. It explains it step by step
Do all the exercises labeled as Task in your head i.e. on a paper
Later on if you wish, you can type them into a computer (or copy/paste from the solutions once I poste them)
Study the following four fundamental algorithms:
Task 4 and Programming Exercise 1
-
After studying the previous four fundamental functions, how would you modify them to only sum odd numbers (or odd list elements)? Try yourself and then see next step for solutions.
-
Open the file called four_functions.py Copy/paste, one by one, Example 1 to 4 into Python visualizer. Run through each example and understand how the solutions work and how the variables change in the loops. As always, you can find python visualizer here (make sure you choose Python 3) http://www.pythontutor.com/visualize.html#mode=edit
-
Programming exercise: Write a function called ah(l,x,y) that given a list l, and integers x and y such that x <=y, returns two numbers. The first is the number of elements of l that are between x and y (including x and y). The second number is the minimum element of l that is between x and y (including x
and y). Example test:
-
t=[5, 1, -2.5, 10, 13, 8]
-
ah(t, 2,11)
(3, 5)
Recall that you can return two numbers referred by variables a and b by just returning a tuple (a,b)
How many stars does the following program print?
a) 0 b) 15 c) 45 d) 48 e) 68
Intermission: print function revisited
Built-in function print, when its completes printing, enters a new line. For example:
print(“This is”)
print(“Lab 5”)
Prints:
This is
Lab 5
As mentioned in the last lab, this default behavior of the print function can be changed by specifying what we want print function to end with. For example:
Introduction to Computing Using Python by Lj. Perkovic
-
What does this print?
-
-
-
What does this print?
-
-
-
-
What does this print?
-
What does the following program print?
Experiment with Perfect Numbers
-
A positive integer is called a perfect number if it is equal to the sum of all of its positive divisors, excluding itself. For example, 6 is perfect number since 6=1+2+3. The next is 28=1+2+4+7+12. There are four perfect numbers less than 10,000. Write a program that prints all these four numbers.
-
Your program should have a function called is_perfect that takes as input a positive integers and returns True if it is perfect and False otherwise.
Arithmetic progression
Recall that a sequence of numbers forms an arithmetic progression if the difference between every pair of consecutive numbers is the same. For example: -5, -1, 3, 7, 11 forms an arithmetic progression since the difference between every pair of consecutive numbers is 4. On the contrary 5, 10, 15, 24, 29 is not an arithmetic progression since the difference between some consecutive pairs is 5 and some 4.
A sequence that has exactly one number is considered arithmetic, too.
-
Write a function called arithmetic that takes as input a list of numbers and returns True if the numbers of the list form arithmetic
progression. And False otherwise
Testing:
-
arithmetic( [-5, -1, 3, 7, 11] ) True
-
arithmetic([0, -1, 3, 7, 11]) False
-
a = [5, 10, 15, 24, 29]
-
arithmetic(a)
-
arithmetic(a[:3]) True
and now … is it sorted?
Now modify your method arithmetic slightly so that instead it tests if the numbers in the give lists are ordered for smallest to largest. Call the new function is_sorted
Testing:
-
is_sorted([1, 1, 1, 7, 7]) True
-
is_sorted([-10, -1, 3, 7, 100]) True
-
is_sorted([0, 3, 1, 7, 11]) False
-
a = [5, -10, 15, 24, 29]
-
is_sorted(a)
-
is_sorted(a[1:4]) True
The the following two multiple choice questions:
https://runestone.academy/runestone/static/thinkcspy/Functions/FlowofExecutionSummary.html