Skip to main content

Section 1.5 HW3

Exercises Exercises

1. Print List: 1 through m.

Write a program to do the following:
- read a positive integer mfrom standard input
- use the listfunction and the rangefunction to print a list of all the positive integers from 1 through m.
Notice the form of the output is a list, which displays as a comma-separated sequence of values enclosed by square brackets.
For example:
Input Result
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5, 6, 7]
[1, 2, 3]

2. Countdown! Print List: k down to 0.

Write a program which will read a positive integer k from standard input. Use the listfunction and the rangefunction to print a list of integers from the given integer k down to 0.
Notice the form of the output is a list.
Hint: When we give the range function 3 arguments, the third argument is the "step size". A negative step size allows us to count down. For example, range(10, 1, -2) is the sequence 10, 8, 6, 4, 2.
For example:
Input Result
[5, 4, 3, 2, 1, 0]
[0]
[3, 2, 1, 0]

3. Print List: k down to j.

Write a program which will read two integers, j and k, from standard input. Use the listfunction and the rangefunction to print a list of integers from k down to j (inclusive).
Notice the form of the output is a list.
You may assume that j is less than or equal to k.
For example:
Input Result
2
5
[5, 4, 3, 2]
0
0
[0]
-3
3
[3, 2, 1, 0, -1, -2, -3]

4. Print List of Evens from -10 to k.

Write a program which will read an integer k from standard input. Use the listfunction and the rangefunction to print a list of the even integers from -10 up to k (inclusive).
Notice the form of the output is a list.
NOTE: If k is less than -10, your range command should automatically produce an empty sequence, resulting in an empty list being printed.Β  See the provided examples.
For example:
Input Result
[-10, -8, -6, -4, -2, 0, 2, 4]
-11
[-10, -8, -6, -4, -2, 0, 2, 4, 6, 8, 10]
[-10, -8, -6, -4, -2, 0, 2]

5. Print Multiples of m.

Define a function print_multiples(m) that takes a positive integer parameter, m, and prints all the multiplesof m which areΒ  between 1 and 100, inclusive.
Use a for loop to print one number per line of output.
Hint: The first multiple of m is m. Then m + m, then m + m + m, etc.
For example:
Test Result
m = 25
print_multiples(m)
25
50
75
100
m = 45
print_multiples(m)
45
90
m = 32
print_multiples(m)
32
64
96

6. Print List of Integers from 1 through maximum, with a Condition.

Read two positive integers, kand maximum, from standard input.
Use the rangefunction to print a list of all the integers x between 1 and maximum inclusive such that x % k is 1.
Notice the form of the output is a list.
Hint:The first number is 1. The next number (if there is a next number) is 1 + k. Then 1 + k + k, etc. What is the step size for this use of the range function?
Note: DO NOT use an "if" statement, just print the list of a range.
For example:
Input Result
5
18
[1, 6, 11, 16]
2
3
[1, 3]
7
25
[1, 8, 15, 22]

7. Count from j to k.

Write the code needed to get integers j and k from standard input, then use a for loop to print out all the integers from j through k. You can assume that k will be more than j.
Each number should be printed on a separate line.Β See the examples for the expected format of the output.
Hint: You need to use a for loop to iterate over a specific sequence of integers. Start by determining a correct call to the range function to create that sequence.
For example:
Input Result
2
4
2
3
4
-3
1
-3
-2
-1
0
1
5
6
5
6

8. For Loop: Count by Twos.

Write the code needed to get two integers, j and k, from standard input. Using a for loop and the range function, "count" by twos from j to k (inclusive), one integer per line. You may assume that j is less than or equal to k.
For example:
Input Result
4
11
4
6
8
10
1
5
1
3
5
-3
3
-3
-1
1
3
8
16
8
10
12
14
16

9. Summit.

The function summit takes two integer parameters, start and stop, and returns the sum of all integers from start to stop, inclusive.Β For example, summit(-2, 3) returnsthe value of -2 + -1 + 0 + 1 + 2 + 3, which is 3. You may assume that start <= stop.
Write a definition for summit.
Use the accumulate-a-sumpattern to solve this problem:
- Initialize the accumulator variable.
- Start a for loop to iterate over the required sequence of integers (make a call to the range function); for num in range(....):
- Inside the for loop, update the accumulator by adding numto the accumulator.
- After the for loop, return the value of the accumulator variable.
For example:
Test Result
print(summit(1, 3))
print(summit(4, 9))
print(summit(25, 25))
print(summit(-10, 23))
221

10. Sum Odds from 1 to n (accumulate a sum).

The function summation takes a positive integer n and returnsthe sum of all the odd integers from 1 through n.Β  For example, summation(5) returns 9 since 1 + 3 + 5 equals 9.
For example:
Test Result
print(summation(5))
print(summation(1))
print(summation(1001))
251001

11. Sum of Squares (accumulate a sum).

The function sum_of_squares takes a parameter n and returns the sum of squaresΒ \(1^2+2^2+3^2+ \cdots + n^2\)
For example:
Test Result
print(sum_of_squares(3))
print(sum_of_squares(2))

12. Accumulate the nth partial sum of an infinite series.

Write a function partial_sum(n) which accumulates and returns the sum of the first n terms of the infinite sequence
\begin{gather*} \frac{1}{2} + \frac{1}{4} + \frac{1}{8} + \frac{1}{16} + \frac{1}{32} + \cdots \end{gather*}
Note: n will be a positive integer. For example, when n is 3, the return value is 1/2 + 1/4 + 1/8 (the result from adding the first three terms), which equals 0.875.
Hint: Each denominator is twice as large as the previous denominator. The nth denominator is \(2^n\text{.}\) You might solve this problem using two accumulators: acccould be the accumulated sum so far, and denomcould be the value for the denominator of the next term to be added.
For example:
Test Result
ans = partial_sum(3)
print(ans)
0.875
ans = partial_sum(6)
print(ans)
0.984375
ans = partial_sum(9)
print(ans)
0.998046875

13. Factorial Function (accumulate a product).

The function factorial takes a non-negative integer parameter, n, and returnsthe value of n factorial (n!). Note:
  • When n is positive, n! = 1 * 2 * 3 * ... * n.Β  For example, 5! = 1 * 2 * 3 * 4 * 5 = 120.
  • When n is zero, n! is defined to be 1. (Function returns 1.)
Note: DO NOT use an "if" statement to check whether n is zero. Your accumulator should automatically find the correct answer in this case (0! = 1).
For example:
Test Result
print(factorial(3))
print(factorial(4))
print(factorial(0))
You have attempted of activities on this page.