-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeek 4.py
More file actions
316 lines (252 loc) · 5.52 KB
/
Copy pathWeek 4.py
File metadata and controls
316 lines (252 loc) · 5.52 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
4.1
Factors of a number
Determine the factors of a number (i.e., all positive integer values that evenly divide into a number).
For example:
Input Result
20 1 2 4 5 10 20
k=int(input())
l=[]
for i in range(1,k+1):
if(k%i==0):
l.append(i)
for j in l:
print(j,end=' ')
4.2
Non Repeated Digit Count
Write a program to find the count of non-repeated digits in a given number N. The number will be passed to the program as an input of type int.
Assumption: The input number will be a positive integer number >= 1 and <= 25000.
Some examples are as below.
If the given number is 292, the program should return 1 because there is only 1 non-repeated digit '9' in this number
If the given number is 1015, the program should return 2 because there are 2 non-repeated digits in this number, '0', and '5'.
If the given number is 108, the program should return 3 because there are 3 non-repeated digits in this number, '1', '0', and '8'.
If the given number is 22, the function should return 0 because there are NO non-repeated digits in this number.
For example:
Input Result
292 1
1015 2
108 3
22 0
n=int(input())
l=[]
k=[]
while n>0:
a=n%10
n=n//10
l.append(a)
for i in range(len(l)):
if l.count(l[i])==1:
k.append(l[i])
print(len(k))
4.3
Prime Checking
Write a program that finds whether the given number N is Prime or not. If the number is prime, the program should return 2 else it must return 1.
Assumption: 2 <= N <=5000, where N is the given number.
Example1: if the given number N is 7, the method must return 2
Example2: if the given number N is 10, the method must return 1
For example:
Input Result
7 2
10 1
a=int(input())
for i in range(2,a):
if(a%2==0):
flag=0
elif(a%i!=0):
flag=1
else:
flag=0
if(flag==1):
print("2")
elif(flag==0):
print("1")
4.4
Next Perfect Square
Given a number N, find the next perfect square greater than N.
Input Format:
Integer input from stdin.
Output Format:
Perfect square greater than N.
Example Input:
10
Output:
16
a=int(input())
c=[]
for i in range(0,a):
b=i**2
if(b>a):
c.append(b)
print(c[0])
4.
Nth Fibonacci
Write a program to return the nth number in the fibonacci series. The value of N will be passed to the program as input.
NOTE: Fibonacci series looks like –
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, . . . and so on.
i.e. Fibonacci series starts with 0 and 1, and continues generating the next number as the sum of the previous two numbers.
• first Fibonacci number is 0,
• second Fibonacci number is 1,
• third Fibonacci number is 1,
• fourth Fibonacci number is 2,
• fifth Fibonacci number is 3,
• sixth Fibonacci number is 5,
• seventh Fibonacci number is 8, and so on.
For example:
Input:
7
Output
8
a=[0,1]
for i in range(0,100):
a.append(a[-1]+a[-2])
q=int(input())
print(a[q-1])
4.6
Disarium Number
A Number is said to be Disarium number when the sum of its digit raised to the power of their respective positions becomes equal to the number itself. Write a program to print number is Disarium or not.
Input Format:
Single Integer Input from stdin.
Output Format:
Yes or No.
Example Input:
175
Output:
Yes
Explanation
1^1 + 7^2 +5^3 = 175
Example Input:
123
Output:
No
For example:
Input Result
175 Yes
123 No
import math
n=int(input())
a=len(str(n))
sum=0
x=n
while(x!=0):
r=x%10
sum=int(sum+math.pow(r,a))
a-=1
x=x//10
if(sum==n):
print("Yes")
else:
print("No")
4.7
Sum of Series
Write a program to find the sum of the series 1 +11 + 111 + 1111 + . . . + n terms (n will be given as input from the user and sum will be the output)
Sample Test Cases
Test Case 1
Input
4
Output
1234
Explanation:
as input is 4, have to take 4 terms.
1 + 11 + 111 + 1111
Test Case 2
Input
6
Output
123456
For example:
Input Result
3 123
n=int(input())
b=1
sum=0
for i in range(1,n+1):
sum+=b
b=(b*10)+1
print(sum)
Ex. No. : 4.8 Date: 10.04.2024
Register No.: 231001085 Name: Kavipriya M A
Unique Digit Count
Write a program to find the count of unique digits in a given number N. The number will be passed to the program as an input of type int.
Assumption: The input number will be a positive integer number >= 1 and <= 25000.
For e.g.
If the given number is 292, the program should return 2 because there are only 2 unique digits '2' and '9' in this number
If the given number is 1015, the program should return 3 because there are 3 unique digits in this number, '1', '0', and '5'.
For example:
Input Result
292 2
1015 3
a=int(input())
b=[]
while a>0:
c=a%10
a=a//10
b.append(c)
b=list(set(b))
print(len(b))
4.9
Product of single digit
Given a positive integer N, check whether it can be represented as a product of single digit numbers.
Input Format:
Single Integer input.
Output Format:
Output displays Yes if condition satisfies else prints No.
Example Input:
14
Output:
Yes
Example Input:
13
Output:
No
a=int(input())
flag=0
for i in range(10):
for j in range(10):
if(i*j==a):
flag=1
break
if(flag==1):
print("Yes")
else:
print("No")
4.10
Perfect Square After adding One
Given an integer N, check whether N the given number can be made a perfect square after adding 1 to it.
Input Format:
Single integer input.
Output Format:
Yes or No.
Example Input:
24
Output:
Yes
Example Input:
26
Output:
No
For example:
Input Result
24 Yes
import math
n=int(input())
a=n+1
sr=int(math.sqrt(a))
if(sr*sr==a):
print("Yes")
else:
print("No")