-
Notifications
You must be signed in to change notification settings - Fork 0
/
7-python-exercises.py
135 lines (120 loc) · 3.08 KB
/
7-python-exercises.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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
"""
Daniel Grounin
Dori Fourer
"""
#-----------Mission 1-------------#
def geometric_progression(a, q, n):
"""
Mission 1:
Function prints geometric sequence,
start with 'a' in range of 'n' with jumps of 'q'.
"""
if ((a or n) == 0):
return 0
else:
for i in range(n):
print(a*q**i)
#-----------Mission 2-------------#
def reverse(x):
"""
Mission 2:
Function reverse a number
"""
rev=0
if(x==0):
return 0
else:
while x>0:
remider=x%10
rev=(rev*10)+remider
'''Floor division'''
x=x//10
return (rev)
#-----------Mission 3-------------#
def removeDigit(Number,n):
"""
Mission 3:
Function that removes the 'n' digit from the 'Number' var
"""
newNumber,count,rev=0,0,0
if (Number or n)< 0:
return 0
else:
while (Number>0):
lastdigit = Number % 10
if (lastdigit==n):
Number=Number//10
else:
rev=(rev*10)+lastdigit
Number=Number//10
newNumber=newNumber+(lastdigit*10**count)
count=count+1
return newNumber
#-----------Mission 4-------------#
def figure(n):
"""
Mission 4:
Function that prints reverse triangle contains from numbers
"""
i = 1
j = n*2-2
for i in range(0,n):
print(' '*j, end=" ")
j = j - 2
for k in range(i+1):
print(k+1, end=" ")
k = k + 1
i = i + 1
print(" ")
#-----------Mission 5-------------#
def printEvenDigits(Number):
"""
Mission 5:
Recursive func prints only even digits
"""
newNumber, count, rev = 0, 0, 0
while (Number>0):
lastdigit = Number % 10
if (lastdigit %2 ==0):
rev = (rev * 10) + lastdigit
newNumber = newNumber + (lastdigit * 10 ** count)
count = count + 1
print(newNumber % 10, end='')
return printEvenDigits(Number//10)
#-----------Mission 6-------------#
def mulPrimeNumbers(Number):
"""
Mission 6:
Function identify prime numbers and return sum of them.
"""
sum = 1
while (Number>0):
lastdigit = Number % 10
if (lastdigit ==2 or lastdigit ==3 or lastdigit == 5 or lastdigit == 7 ):
sum=sum*lastdigit
Number=Number//10
else:
Number=Number//10
return sum
#-----------Mission 7-------------#
def luckyNumber(num):
"""
Mission 7:
boolean function that defines lucky numbers , we used two for loops
to and compare two different sums.
"""
sum1,sum2=0,1
if (num <0):
return 0
else:
for i in range(3):
lastdigit=num%10
sum1=sum1+lastdigit
num=num//10
for j in range(3):
lastdigit2=num%10
sum2=sum2*lastdigit2
num=num//10
if(sum1==sum2):
return True
return False