-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweek_9.txt
79 lines (78 loc) · 2.17 KB
/
week_9.txt
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
resource url?
QUESTION ----------------
A number is considered to be ugly if its only prime factors are 2, 3 or 5.
------------------ANSWER
def checkUgly(n):
if n <= 0: return "not ugly"
while n % 2 == 0:
n //= 2
while n % 3 == 0:
n //= 3
while n % 5 == 0:
n //= 5
if n == 1:
return "ugly"
else: return "not ugly"
------------------------------
QUESTION ----------------
complete function to implement coin change making problem i.e. finding the minimum
------------------ANSWER
def coinChange(n):
c=0
while n>0:
if n>=4:
n-=4
elif n==3:
n-=3
elif n==2:
n-=2
elif n==1:
n-=1
c+=1
return c
------------------------------
QUESTION ----------------
An e-commerce company plans to give their customers a special discount for Christmas.
------------------ANSWER
def christmasDiscount(n):
d=0
for I in range(len(str(n))):
t=n%10
flag=0
for j in range(2,(t//2)+1):
if t%j==0:
flag=1
break
if flag==0:
d+=t
n//=10
return d
------------------------------
QUESTION ----------------
An automorphic number is a number whose square ends with the number itself.
------------------ANSWER
def automorphic (n):
if n <= 0:
return "Invalid input"
square = n*n
if str(square).endswith (str (n) ) :
return "Automorphic"
else:
return "Not Automorphic"
------------------------------
QUESTION ----------------
Given a number with maximum of 100 digits as input, find the difference between the sum
------------------ANSWER
def differenceSum (number):
number_str = str (number)
sum_even = 0
sum_odd = 0
for i, digit in enumerate (number_str):
if (i + 1) % 2 == 0:
sum_even += int (digit)
else:
sum_odd += int (digit)
return abs(sum_even - sum_odd)
number = 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
difference = differenceSum (number)
------------------------------