Task 1: What does thisprint?
Task 2: What does thisprint?
Task 3: What does thisprint?
Returns sum of allpositive
integers smaller thann
def summation(n):
‘’'(number)->number”’
odd_sum = 0
for num in range(n):
odd_sum = odd_sum + num
return odd_sum
Sum of numbers in a given list a.
A solution with loop overelements
def sum_list_v1(a):
”’ (list)->num ”’
list_sum = 0
for item in a:
list_sum = list_sum + item
return list_sum
return list_sum 8
Returns a product of allpositive
integers smaller thann
def product(n):
‘’'(number)->number’’’
prod = 1
for num in range(n):
prod = prod * num
return prod
Sum of numbers in a given list a.
Asolution with a loop over indices
def sum_list_v2(a):
”’ (list)->num ”’
list_sum = 0
i = 0
for i in range(len(a)):
list_sum = list_sum + a[i]
Study the following four fundamentalalgorithms:
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.
2. 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
3. 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)
Recallthat you can return two numbers referred by variables a and b by justreturning a tuple
(a,b)
9
Task 4 and Programming Exercise1
Task 5
How many stars does the following program print?
10
a) 0 b) 15 c)45 d) 48 e) 68
Intermission: print functionrevisited
In other words, unless specified otherwise, the default end forthe printfunction is end=’
’
This is***Lab 5
This isLab 6
This is Lab 7
Would
Print
=>
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:
print(“This is”, end=’***’)
print(“Lab 5.”)
print(“This is”, end=”)
print(“Lab 6”, end=’
’)
print(“This is”, end=’ ‘)
print(“Lab 7”)
>>> pets = [‘boa’, ‘cat’, ‘dog’]
>>> for pet in pets:
print(pet)
boa
cat
dog
>>>
Introduction to Computing Using Python by Lj. Perkovic
More examples of use ofprint function
Function print prints, by default,
a newline character after printing its arguments
The endargument allows for customized end characters
>>> pets = [‘boa’, ‘cat’, ‘dog’]
>>> for pet in pets:
print(pet)
boa
cat
dog
>>>
>>> pets = [‘boa’, ‘cat’, ‘dog’]
>>> for pet in pets:
print(pet)
boa
cat
dog
>>> for pet in pets:
print(pet, end=’, ‘)
boa, cat, dog,
>>>
>>> pets = [‘boa’, ‘cat’, ‘dog’]
>>> for pet in pets:
print(pet)
boa
cat
dog
>>> for pet in pets:
print(pet, end=’, ‘)
boa, cat, dog,
>>> for pet in pets:
print(pet, end=’!!! ‘)
boa!!! cat!!! dog!!!
>>>
Task 6
13
1. What does this print?
2. What does this print?
3. What does this print?
Task 7
What does the following program print?
14
Programming Exercise 2:
Experiment with PerfectNumbers • A positive integer is called a perfect number ifitis equalto
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 allthese four numbers.
• Your program should have a function called is_perfect that
takes as input a positive integers and returns True if itis perfect
and False otherwise.
• Once you are done. Modify your program so that it looks for all
perfect numbers smaller than 35 million. What do you notice?
Assuming that you computer can do a billion instructions in a sec, can
you figure out how long,roughly, will ittake your computerto find 5th
perfect number (it is 33,550,336). Is the answer roughly: couple of
minutes, couple of hours, couple of days, … weeks, months, years ?
What if you wanted to wait until it prints 6th perfect number, which is
8,589,869,056?
•
15
Programming Exercise3a:
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 ifthe numbers ofthe listform arithmetic
progression. And Falseotherwise
Testing:
>>> arithmetic( [-5, -1, 3, 7, 11] )
True
>>> arithmetic([0, -1, 3, 7, 11])
False
>>> a = [5, 10, 15, 24, 29]
>>> arithmetic(a)
False
>>> arithmetic(a[:3])
True
Programming Exercise3b:
and now … is itsorted?
Now modify your method arithmetic slightly so that instead ittests ifthe 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)
False
>>> is_sorted(a[1:4])
True
Task 6: Flow of execution
The the following two multiple choice questions:
https://runestone.academy/ns/books/published/py4e-int/functions/flowofexecution.html
1120, aliasing, Function, Lists, Loops, More, Range, solved, Variables, “pointer/reference”
[SOLVED] Iti 1120 lab # 5 lists , range function more for loops,“pointer/reference” variables, aliasing
$25
File Name: Iti_1120_lab___5_lists___range_function_more_for_loops____pointer_reference____variables__aliasing.zip
File Size: 923.16 KB
Only logged in customers who have purchased this product may leave a review.
Reviews
There are no reviews yet.