-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmisc2
More file actions
109 lines (87 loc) · 3.11 KB
/
Copy pathmisc2
File metadata and controls
109 lines (87 loc) · 3.11 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#Fill in the blanks to make the print_prime_factors function print all the prime factors of a number.
#A prime factor is a number that is prime and divides another without a remainder.
def print_prime_factors(number):
# Start with two, which is the first prime
factor = 2
# Keep going until the factor is larger than the number
while factor <= number:
# Check if factor is a divisor of number
if number % factor == 0:
# If it is, print it and divide the original number
print(factor)
number = number / factor
else:
# If it's not, increment the factor by one
factor += 1
return "Done"
print_prime_factors(100)
# Should print 2,2, 5,5
#The multiplication_table function prints the results of a number passed to it multiplied by 1 through 5.
#An additional requirement is that the result is not to exceed 25, which is done with the break statement.
def multiplication_table(number):
# Initialize the starting point of the multiplication table
multiplier = 1
# Only want to loop through 5
while multiplier <= 5:
result = number * multiplier
# What is the additional condition to exit out of the loop?
if result > 25 :
break
print(str(number) + "x" + str(multiplier) + "=" + str(result))
# Increment the variable for the loop
multiplier += 1
multiplication_table(3)
# Should print: 3x1=3 3x2=6 3x3=9 3x4=12 3x5=15
multiplication_table(5)
# Should print: 5x1=5 5x2=10 5x3=15 5x4=20 5x5=25
multiplication_table(8)
# Should print: 8x1=8 8x2=16 8x3=24
def is_power_of_two(n):
# Check if the number can be divided by two without a remainder
while n % 2 == 0 and n != 0:
n = n / 2
# If after dividing by two the number is 1, it's a power of two
if n == 1:
return True
return False
print(is_power_of_two(0)) # Should be False
print(is_power_of_two(1)) # Should be True
print(is_power_of_two(8)) # Should be True
print(is_power_of_two(9)) # Should be False
#Fill in the empty function so that it returns the sum of all the divisors of a number, without including it. A divisor is a number that divides into another without a remainder.
def sum_divisors(n):
return sum([i
for i in range(1, n)
if n % i == 0
])
print(sum_divisors(0))
# 0
print(sum_divisors(3)) # Should sum of 1
# 1
print(sum_divisors(36)) # Should sum of 1+2+3+4+6+9+12+18
# 55
print(sum_divisors(102)) # Should be sum of 2+3+6+17+34+51
# 114
#sum_squares function
def square(n):
return n*n
def sum_squares(x):
sum = 0
for n in range(x):
sum += square(n)
return sum
print(sum_squares(10)) # Should be 285
#The retry function tries to execute an operation that might fail, it retries the operation for a number of attempts.
def retry(operation, attempts):
for n in range(attempts):
if operation():
print("Attempt " + str(n) + " succeeded")
if n == 1:
break
else:
print("Attempt " + str(n) + " failed")
retry(create_user, 3)
retry(stop_service, 5)
#Write a script that prints the multiples of 7 between 0 and 100. Print one multiple per line and avoid printing any numbers that aren't multiples of 7.
for x in range(0, 100, 7):
print(x)