@Charanpreet/

Lab 3.02 Examples

Python

No description

fork
loading
Files
  • main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Returns the product of all integers
# from 1 to the given number. If the
# given number is less than 1, returns None.
# product_of_integers_from_1_to(1) -> 1
# product_of_integers_from_1_to(2) -> 2
# product_of_integers_from_1_to(3) -> 6
# product_of_integers_from_1_to(4) -> 24
# product_of_integers_from_1_to(5) -> 120
def product_of_integers_from_1_to(n):
  if n < 1:
    return None
  product = 1
  for num in range(1, n + 1):
    product = product * num
  return product


# Returns the product of all odd integers
# from 1 to the given number. If the
# given number is less than 1, returns None.
# product_of_odd_integers_from_1_to(0) -> None
# product_of_odd_integers_from_1_to(1) -> 1
# product_of_odd_integers_from_1_to(2) -> 1
# product_of_odd_integers_from_1_to(3) -> 3
# product_of_odd_integers_from_1_to(4) -> 3
# product_of_odd_integers_from_1_to(5) -> 15
def product_of_odd_integers_from_1_to(n):
  if n < 1:
    return None
  product = 1
  for num in range(1, n + 1):
    if num % 2 == 1:
      product = product * num
  return product


# Returns the count of integers that divide into the
# given number without a remainder.
# Assume the given number is a postive integer.
# count_divisors(1) -> 1
# count_divisors(10) -> 4
# count_divisors(36) -> 9
# count_divisors(80) -> 10
def count_divisors(n):
  count = 0
  for possible_divisor in range(1, n + 1):
    if n % possible_divisor == 0:
      count = count + 1
  return count