forked from dabeaz-course/practical-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.py
More file actions
29 lines (21 loc) · 818 Bytes
/
functions.py
File metadata and controls
29 lines (21 loc) · 818 Bytes
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
def sumcount_using_range(n):
"""Returns the sum of all integers from 1 to n and the count of those integers."""
if n < 1:
return 0, 0
total_sum = sum(range(1, n + 1))
count = n
return total_sum, count
# print(sumcount_using_range(100)) # Output: (5050, 100)
def sumcount_using_formula(n):
"""Returns the sum of all integers from 1 to n and the count of those integers using formulas."""
if n < 1:
return 0, 0
total_sum = n * (n + 1) // 2 # floor division to ensure an integer result
count = n
return total_sum, count
# print(sumcount_using_formula(100)) # Output: (5050, 100)
def greetings(name):
"""Returns a greeting message for the given name."""
return f"Hello, {name}!"
print(greetings("Alice")) # Output: "Hello, Alice!"
help(greetings)