Skip to content

Commit cbe1059

Browse files
authored
Merge pull request #1 from Window23/done-with-chapter-4
adding some of the chapter4 exercices
2 parents 3db711c + 53d7ce0 commit cbe1059

13 files changed

Lines changed: 209 additions & 0 deletions
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
""" Calculate the square of number. """
2+
3+
number = float(input("Enter a number: "))
4+
number_expo = float(input("Enter a number as exponent: "))
5+
6+
def raising(x, y):
7+
return x ** y
8+
9+
print(f'{raising(number, number_expo)} is the result from {number} raised by {number_expo}.')
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
""" Let’s define a maximum function that determines and returns the
2+
largest of three values—the following session calls the function three times with integers,
3+
floating-point numbers and strings, respectively. """
4+
5+
value1 = float(input("Enter the first value: "))
6+
value2 = float(input("Enter the second value: "))
7+
value3 = float(input("Enter the third value: "))
8+
9+
10+
def largest_three(value1, value2, value3):
11+
max_value = value1
12+
if value2 > max_value:
13+
max_value = value2
14+
if value3 > max_value:
15+
max_value = value3
16+
return max_value
17+
18+
print(f'largest value of {value1}, {value2}, and {value3} is {largest_three(value1, value2, value3)}')
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
""" Let’s produce 6 mil random integers in the range 1–6 to simulate rolling a six-sided die: """
2+
3+
import random
4+
5+
def roll_normal_dice():
6+
roll_die_list = []
7+
frequency_1 = 0
8+
frequency_2 = 0
9+
frequency_3 = 0
10+
frequency_4 = 0
11+
frequency_5 = 0
12+
frequency_6 = 0
13+
for roll in range(60_000_000):
14+
n = random.randrange(1, 7)
15+
roll_die_list.append(n)
16+
if n == 1:
17+
frequency_1 += 1
18+
elif n == 2:
19+
frequency_2 += 1
20+
elif n == 3:
21+
frequency_3 += 1
22+
elif n == 4:
23+
frequency_4 += 1
24+
elif n == 5:
25+
frequency_5 += 1
26+
elif n == 6:
27+
frequency_6 += 1
28+
print(f'Face{"Frequency":>13}')
29+
print(f'{1:>4}{frequency_1:>13}')
30+
print(f'{2:>4}{frequency_2:>13}')
31+
print(f'{3:>4}{frequency_3:>13}')
32+
print(f'{4:>4}{frequency_4:>13}')
33+
print(f'{5:>4}{frequency_5:>13}')
34+
print(f'{6:>4}{frequency_6:>13}')
35+
36+
return
37+
38+
roll_normal_dice()
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
""" Requirements statement:
2+
Use a for statement, randrange and a conditional expression (introduced in the preceding chapter)
3+
to simulate 20 coin flips, displaying H for heads and T for tails all on the same line, each separated by a space. """
4+
5+
import random
6+
7+
def heads_tails():
8+
result = []
9+
for _ in range(20):
10+
roll = random.randrange(0, 2)
11+
if roll == 0:
12+
result.append("H")
13+
elif roll == 1:
14+
result.append("T")
15+
return print(result)
16+
17+
def heads_tails_2():
18+
for _ in range(20):
19+
print('H' if random.randrange(2) == 0 else 'T', end=' ')
20+
return print()
21+
22+
heads_tails()
23+
heads_tails_2()
24+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
""" Create a function named calculate_product that receives an arbitrary argument list
2+
and returns the product of all the arguments. Call the function with the
3+
arguments 10, 20 and 30, then with the sequence of integers produced by range(1, 6, 2). """
4+
5+
import math
6+
7+
8+
9+
def calculate_product(*prod):
10+
""" calculate the product of all the arguments. """
11+
return math.prod(prod)
12+
13+
def calculate_product_2(*args):
14+
""" calculate the product of all the arguments. """
15+
product_2 = 1
16+
for arg in args:
17+
product_2 *= arg
18+
return product_2
19+
20+
def calculate_product_3(*args):
21+
""" calculate the product of all the arguments. """
22+
product_3 = 1
23+
for arg in range(1, 6, 2):
24+
product_3 *= arg
25+
return product_3
26+
27+
def calculate_product_4(*args):
28+
""" calculate the product of all the arguments. """
29+
return math.prod(args)
30+
31+
print(calculate_product(10, 20, 30))
32+
print(calculate_product_2(10, 20, 30))
33+
print(calculate_product_3())
34+
print(calculate_product_4(*range(1, 6, 2)))
35+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
""" Import the decimal module with the shorthand name dec, then create a Decimal object with the value 2.5 and square its value. """
2+
3+
import decimal as dec
4+
5+
Decimal = dec.Decimal(2.5)
6+
print(Decimal ** 2)
7+
8+
9+
10+
11+
12+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
""" 4.1 (Discussion: else Clause) In the script of Fig4.1. , we did not include an else clause in the if…elif statement.
2+
What are the possible consequences of this choice?
3+
Answer: the ELSE was included in all the elif statements
4+
- Use if-else statements when the alternatives are mutually exclusive. This means that if one alternative is true, the other alternatives must be false. """
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
""" 4.2 (Discussion: Function-Call Stack) What happens if you keep pushing onto a stack, without enough popping?
2+
Answer: Stack overflow; memory consumption, right """
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
""" (What’s Wrong with This Code?) What is wrong with the following cube function’s definition?
2+
3+
4+
def cube(x):
5+
Calculate the cube of x.
6+
x ** 3
7+
print('The cube of 2 is', cube(2)) """
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
""" Replace the ***s in the seconds_since_midnight function so that it returns the number of seconds since midnight.
2+
The function should receive three integers representing the current time of day.
3+
Assume that the hour is a value from 0 (midnight) through 23 (11 PM) and that the minute and second are values from 0 to 59.
4+
Test your function with actual times.
5+
For example, if you call the function for 1:30:45 PM by passing 13, 30 and 45, the function should return 48645. """
6+
7+
def seconds_since_midnight(*args):
8+
hour_in_seconds = args[0] * 3600
9+
minute_in_seconds = args[1] * 60
10+
if args[3] == 'PM':
11+
hour_in_seconds = 12 * 3600 + (args[0] * 3600)
12+
else:
13+
hour_in_seconds = args[0] * 3600
14+
return print(hour_in_seconds + minute_in_seconds + args[2])
15+
16+
seconds_since_midnight(1, 30, 45, 'PM')

0 commit comments

Comments
 (0)