-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04-python-functions-lab.py
More file actions
59 lines (43 loc) · 1.46 KB
/
Copy path04-python-functions-lab.py
File metadata and controls
59 lines (43 loc) · 1.46 KB
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
50
51
52
53
54
55
56
57
58
59
# ## Challenges
# 1. Write a function named `sum_to` that accepts a single integer, `n`, and returns the sum of the integers from 1 to `n`.
# For example:
# ```python
# sum_to(6) # returns 21
# sum_to(10) # returns 55
# ```
def sum_to(nums):
sum = 0
for n in range(1, nums + 1):
sum += n
return sum
# 2. Write a function named `largest` that takes a list of numbers as an argument and returns the largest number in that list.
# For example:
# ```python
# largest([1, 2, 3, 4, 0]) # returns 4
# largest([10, 4, 2, 231, 91, 54]) # returns 231
# ```
def largest(n):
n.sort()
return n[-1]
# 3. Write a function named `occurances` that takes two string arguments as input and counts the number of occurances of the second string inside the first string.
# For example:
# ```python
# occurances('fleep floop', 'e') # returns 2
# occurances('fleep floop', 'p') # returns 2
# occurances('fleep floop', 'ee') # returns 1
# occurances('fleep floop', 'fe') # returns 0
# ```
def occurances(string, occur):
return string.count(occur)
# 4. Write a function named `product` that takes an *arbitrary* number of numbers, multiplies them all together, and returns the product.<br>(HINT: Review your notes on `*args`).
# For example:
# ```python
# product(-1, 4) # returns -4
# product(2, 5, 5) # returns 50
# product(4, 0.5, 5) # returns 10.0
# ```
def product(*args):
product = 1
for arg in args:
product *= arg
return product