Skip to main content

Section 1.7 HW4

Exercises Exercises

1. Between 0 and 1.

Write a function called between_zero_and_onethat takes a parameter num; num will be a number at run time.  The function should return True if num falls strictly between 0 and 1; otherwise, it should return False.
NOTE: This function does NOT read anything from standard input.
NOTE: Your function should not print anything to standard output. Use a return statement, not a print statement, at the end of your function.
Hint: Do not return the strings "True", "False". Boolean values are not quoted.
For example:
Test Result
between_zero_and_one(0.5)
True
between_zero_and_one(1.0)
False
between_zero_and_one(-2.2)
False

2. Divides.

Define a function dividesthat takes integer parameters n and d.
-If n is a multiple of d, that is, if "d divides n" evenly, the function returns True and prints a message. For example, if n is 12 and d is 2, the message will say, "12 is divisible by 2"
- Otherwise, the function returns False and prints  a negative message, for example, "12 is not divisible by 11"
- Your function might  be around 7 lines long.
Hint 1:  Use the remainder operator and an if-else conditional.
Hint 2: Do not return the strings "True", "False". Return a Boolean constant, either True or False (no quotes)
For example:
Test Result
result = divides(12, 2)
print("Return value is", result)
12 is divisible by 2
Return value is True
result = divides(12, 5)
print("Return value is", result)
12 is not divisible by 5
Return value is False

3. Num Is Even.

The function isEventakes one parameter, n (an integer). isEven returns a boolean result.
- isEven returns Trueif n is even,
- otherwise isEven returns False.
Define the isEven function.
Remember: Do not return the strings "True", "False". Return a Boolean constant, either True or False (no quotes)
For example:
Test Result
print(isEven(39))
False
print(isEven(26))
True
print(isEven(11))
False

4. isPositive.

The function isPositivetakes one parameter, n (a number).
- If n is positive, the function prints a message stating that n is positive and then returns True
-  If n is zero, the function prints a message stating that n is zero and then returns False
- If n is negative, the function prints a message stating that n is negative and then returns False
Write a definition for isPositive.
Remember: Do not return "True", "False". Return the Boolean constants: True, False (no quotation marks).
For example:
Test Result
print(isPositive(35))
35 is positive
True
print(isPositive(0))
0 is zero
False
print(isPositive(-0.1))
-0.1 is negative
False

5. Divisible by 3 or 5.

This problem requires a short program, not a function definition.
Write the code needed to get an integer from standard input.  Then, write a simple decision statement that will output a message reporting whether the number is evenly divisible by 3, or by 5, or both, or neither.  You might need about 10 lines of code.
See the examples for the expected format of the output.
For example:
Input Result
-7
-7 is divisible by neither 3 nor 5
60
60 is divisible by both 3 and 5
54
54 is divisible by 3
125
125 is divisible by 5

6. Is In Unit Circle.

Write a function called is_in_unit_circle that takes two parameters, x and y (floats), the coordinates of a point in the x-y plane.
- The function returns Trueif (x, y) is less than one unit away from (0,0) [i.e., if the point (x, y) is inside the unit circle];
- Otherwise, the function returns False
Hint:Use the distance formula to calculate the distance (or the square of the distance) from (x, y) to (0, 0). Then compare that distance with 1.
Distance between two points = \(\sqrt{(x_2-x_1)^2 + (y_2-y_2)^2}\)
For example:
Test Result
is_in_unit_circle(2, .9)
False
is_in_unit_circle(.70, -.61)
True
is_in_unit_circle(.055, .81)
True

7. Sum over a list.

Define the function list_sum(L); it takes one parameter L, a list of numbers, and returns the sum of all the items in L.
Hint: Use the "accumulate a sum" pattern, and use L as the sequence expression in your for loop.
For example:
Test Result
L = [9, 6, 8, 5, 7]
list_sum(L)
35
L = [15, 7, -1, 11, 19, 3, -5]
list_sum(L)
49
L = []
list_sum(L)
0

8. List Product.

Define a function list_product(L) which takes a parameter L, a list of numbers, and returns the product of all the items in L.
Hint: Use the accumulate-a-product pattern as discussed in class. Remember, you should not initialize your accumulator variable to 0 when it represents a product.
For example:
Test Result
L = [3, 2, 7, -1, 4]
list_product(L)
-168
L = [55]
list_product(L)
55

9. Count Negatives in a list.

Define a function count_negatives(L), which takes a list of numbers, L, as its parameter. The function returns an integer count telling how many of the items in L are negative (less than zero).
Hint: Use the accumulator pattern.
- Initialize the accumulator to 0
- Use a for loop to iterate over the items in L
- Use an if statement to update the accumlator every time the current item is negative
- Return the accumulator
For example:
Test Result
L = [0, -10, 2, -8, -4, -6, -2, 6, 4]
count_negatives(L)               
5
L = [0, 6, 4]
count_negatives(L)
0
L = [-10, -4, -6, -2, 6, 4]
count_negatives(L)
4

10. Count SPAM in list.

Define the function count_SPAM(L), which takes a list of strings, L, as its parameter. The function returns a count of how many times the word "SPAM" occurs as an item in L.
See the examples for the expected format of the output.
For example:
Test Result
L  = ["SPAM", "banana", "apple", "SPAM", "tuna"]
count_SPAM(L)
2
L = ["a", "stitch", "in", "time"]
count_SPAM(L)
0
L = ["SPAM", "SPAM", "SPAM", "egg", "and", "SPAM"]
count_SPAM(L)
4

11. Count Divisors.

Write a short program, approximately 12 lines of code, to read a positive integer n from the keyboard, count the number of  positive integer divisors of n, and print a message reporting that count. In addition, if n is prime, also print, "That’s a prime number!".  HINT: A positive integer n is prime provided n has exactly two positive integer divisors, 1 and n.
For example, the divisors of 12 are 1, 2, 3, 4, 6, 12. There are 6 of them. If n = 12 is read from the keyboard, the program will print, "12 has 6 divisors."
Warning: Notice that when n = 1, there is only one divisor. So the message needs to say, "1 has 1 divisor" (not "1 has 1 divisors").
See the examples for the expected format of the output.
For example:
Input Result
1
1 has 1 divisor
3
3 has 2 divisors
That number is prime!
6
6 has 4 divisors

12. Accumulate Nth Fibonacci.

Write a function Fto compute the Nth Fibonnaci number, where N is an integer parameter.
By definition, F(1) and F(2) are both 1, while later terms are found  by adding the previous two terms to make the next. Algebraically, we define F(N) as follows:
  • F(1) = F(2) = 1
  • When N > 2, F(N) = F(N - 1) + F(N - 2)
So F(3) = F(2) + F(1) = 1  + 1  = 2, F(4) = F(3) +  F(2) = 2 + 1 = 3, F(5) =  5, F(6) =  8, etc.
When N is positive, F should return the Nth Fibonacci number, F(N); when N is not positive, F should return the Python object None.
Big Hint: Use two accumulator variables, previousand current, since we will always add the last two terms to make the next term.
  • Initialize previousto have the value of F(1), and initialize currentto have the value of F(2).
  • Think about how many additional terms the for loop needs to calculate to determine F(N) when N > 2. That number is the argument for the range function call in the first line of the loop.
  • In the body of the loop, update both accumulators: let nextbe the sum of previousand current, update previousto equal current, and update currentto equal next.
  • When the for loop ends, return current.
For example:
Test Result
F(10)
55
F(4)
3
F(3)
2
You have attempted of activities on this page.