repl.it
@m1s5ed/

Sem_1_Exam-1 V2

Python

No description

fork
loading
Files
  • main.py
main.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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
import random

def hailstone(n): 
  hail_list = [n]
  seed = n
  while seed > 1:
    if seed % 2 == 0:
      seed = seed // 2
      hail_list += [seed]
    elif seed % 2 != 0:
      seed = seed * 3 + 1
      hail_list += [seed]
  return hail_list

def gcd(a, b): 
  if a < b:
    return gcd(a, b - a) 
  elif a == b:
    return a 
  elif (a > b):  
    return gcd(a - b, b)
  return 0  
  
 
def coprime(a, b): 
  if gcd(a,b) == 1: 
    return True
  else:
    return False    

def index_first(s,c):
  i = -1
  for n in s:
    if n != c:
      i += 1
    else:
      i += 1
      return i
  return -1

def index_last(s,c):
  total = 0
  for n in s:
    total += 1
  for i in range(total-1,0,-1):
    b = s[i]
    if b == c:
      return i
  return -1

def leap_year(y):
  if y % 100 == 0 and y % 400 != 0:
    return False
  elif y % 100 == 0 and y % 400 == 0:
    return True
  elif y % 4 == 0:
    return True
  else:
    return False

def fact(n):
  for num in range(1,n):
    n *= num
  return n

def reverse(s):
  r = ''
  for c in s:
    r = c + r
  return r

def is_prime(n):
  if n == 0 or n == 1:
    return False
  for i in range(2, n):
    if n % i == 0:
      return False
  return True

#Tests if a given n is composite
def is_composite(n):
  for i in range(2, n):
    if n % i == 0:
      return True
  if n == 1:
    return False
  else:
    return False

#returns the area of a triangle given all sides
def heron(a,b,c):
  A = (a**2 + b **2 + c **2) ** 2 - 2 * (a ** 4 + b ** 4 + c ** 4)
  A = (A ** (1/2)) * (1/4)
  return A 

#tests if the given measurements can make a triangle
def is_tri(a, b, c):
  if a > 0 and b > 0 and c > 0:
    if a + b > c and b + c > a and c + a > b:
      return True
    else:
      return False
  else:
    return False

def is_perf_sq(n):
  if n == 0:
    return False
  elif n == 1:
    return True
  for i in range(int(n**1/2)+1):
    if i * i == n:
      return True
  return False

def is_perf_cube(n):
  if n == 0:
    return False
  cube = n**(1./3.)
  if round(cube) ** 3 == n:
      return True
  else:
      return False

#returns a list of n perfect squares
def perf_list(n):
  sq_lst = []
  w = 0
  t = 0
  while w != n:
    t += 1
    if is_perf_sq(t):
      w += 1
      sq_lst.append(t)
  return sq_lst

def is_coprime(p,n):
  if p == n:
    return True
  

#finds the sum of the square of the first n integers
def e1(n):
  sum = 0
  for a in range(n+1):
    sum += a**2
  return sum

#finds the square of a sum of the first n integers
def e2(n):
  sum = 0
  for a in range(n+1):
    sum += a
  return sum**2

def e_3(n):
  return e2(n) - e1(n)

#finds the product of the first n evens
def e4(n):
  prod = 1
  for i in range(1, n*2 + 1):
    if i % 2 == 0:
      prod *= i
  return prod

#finds the product of the first n odds
def e5(n):
  prod = 1
  for i in range(1, n*2 + 1):
    if i % 2 != 0:
      prod *= i
  return prod

#finds the sum of the first n primes
def e6(n):
  i = 0
  sum = 0
  prime_count = 0
  while prime_count != n:
    if is_prime(i):
      sum += i
      i += 1
      prime_count += 1
    else:
      i += 1
  return sum

#finds the sum of the first n composites
def e7(n):
  i = 0
  sum = 0
  comp_count = 0
  while comp_count != n:
    if is_composite(i):
      sum += i
      i += 1
      comp_count += 1
    else:
      i += 1
  return sum

def e8(n):
  prime_count = 0
  for i in range(n):
    if is_prime(i):
      prime_count += 1
  return prime_count

def e9(n):
  comp_count = 0
  for i in range(n):
    if is_composite(i):
      comp_count += 1
  return comp_count

def e10(n):
  for i in range(n*2):
    if i % 2 != 0:
      pat_num = i
  pat = pat_num
  for c in range(n-1):
    pat += pat_num
  return pat

def e11(n):
  sum = 0
  for i in range(1,n+1):
    sum += i ** (i+1)
  return sum

def e12(n):
  even_count = 0
  a = 1
  b = 1
  fin_sum = 0
  while even_count != n:
    sum = a + b
    a = b
    b = sum
    if b % 2 == 0:
      print(fin_sum)
      even_count += 1
      fin_sum += b
  return fin_sum

def e13(n):
  score = 0
  for char in n:
    if char >= 'a':
      score += ord(char) - 96
  return score

def e14(n):
  score = 0
  char_list = ['!', '@', '#', '$', '%', '^', '&', '*', '(', ' )']
  for char in n:
    if char >= 'a' and char <= 'z':
      score += 1
    elif char >= 'A' and char <= 'Z':
      score += 2
    elif char >= '0' and char <= '9':
      score += int(char)
    elif char in char_list:
      score += 5
  return score

def e15(n):
  if is_perf_sq(n):
    return n
  for num in range(n+1):
    if is_perf_sq(n+num):
      return n + num
    elif is_perf_sq(n-num):
      return n - num


def e16(n):
  if is_perf_cube(n):
    return n
  for num in range(n+1):
    if is_perf_cube(n+num):
      return n + num
      break
    elif is_perf_cube(n-num):
      return n - num
      break

def e17(n):
  fin_s = ''
  for char in n:
    for m in range(0, 26):
      if char == chr(97+m):
        fin_s += chr(97 + (25 - m))
        break
  return fin_s

def e18(s):
  dec = 0
  s = reverse(s)
  for i in range(0,len(s)):
    for n in range(3):
      if int(s[i]) == n:
        dec += n * 3 ** i
  return dec

def e19(s):
  dec = 0
  s = reverse(s)
  for i in range(0,len(s)):
    for n in range(0,26):
      if s[i] == chr(97+n):
        dec += n * 26 **i
  return dec

def m1(n):
  total = 0
  start = 0
  x = n
  for num in range(n+1):
    start += x
    x -= 1
  for i in range(n):
    total += start
    start -= 1
  return total

def m2(n):
  fin_num = 0
  for num in range(n+1):
    fin_num += num * num
  return fin_num

def m3(n):
  count = 0
  for co in range(n):
    if coprime(co, n):
      count += 1
  return count

def m4(n):
  s = 1
  s1 = n
  while True:
    s1 = 1.001 ** s
    if s1 <= n:
      s += 1
    else:
      break
  return s

def m5(n):
  return m4(n) - 1

def m6(n):
  s = n
  n = fact(n)
  while 2 ** s < n:
    s += 1
  return s

def m7(n):
  g = n
  n = fact(n)
  while 2 ** g < n:
    g += 1
  while True:
    if 2 ** g > n:
      g -= 1
    else:
      return g

def m8(n):
  while n > 3:
    n -= 4
  if n == 3:
    return '-i'
  elif n == 2:
    return -1
  elif n == 1:
    return 'i'
  elif n == 0:
    return 1

def m9(n):
  leap_count = 1
  while leap_year(n) == False:
    n += 1
  for y in range(n+1, 2020):
    if leap_year(y) == True:
      leap_count += 1
  return leap_count

def m10(n):
  alm_prm = 0
  total = 0
  for num in range(1, n+1):
    for div in range(2, num):
      if num % div == 0:
        if is_prime(div) == True:
          alm_prm += 1
    if alm_prm == 2:
        total += num
        alm_prm = 0
    else:
      alm_prm = 0
  return total


def m11(n):
  count = 0
  for a in range(1, n):
    for b in range(a, n):
      for c in range(b, n):
        if a**2 + b**2 == c ** 2:
          count += 1
  return count

def m12(n):
  test1 = hailstone(n-1)
  for n1 in range(n-1):
    test = hailstone(n1)
    if len(test) > len(test1):
      test1 = test
  return len(test1)

def m13(n):
  score = 0
  bonus = False
  bonus_valid = False
  bonus_let = 0
  let = 0
  dig = 0
  sym = 0
  sym_lst = ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')']
  for i in range(len(n)):
    if n[i] in sym_lst:
      if sym != 3:
        score += 5
        sym += 1
        if not bonus:
          if let >= 1:
            bonus_valid = True
            bonus_let = let
            bonus = True
    elif (n[i] >= 'a' and n[i] <= 'z') or (n[i] >= 'A' and n[i] <= 'Z'):
      if let != 5:
        score += 1
        let += 1
        if bonus_valid:
          if let > bonus_let:
            bonus_valid = False
            score += 5
    elif n[i] >= '0' and n[i] <= '9':
      if dig != 3:
        score += 2
        dig += 1
    if sym == 3 and let == 5 and dig == 3:
      break
  if dig == 0:
    score -= 5
  if sym == 0:
    score -= 10
  return score

def h1(n):
  pass

def h2(n):
  sum = 0
  for num in range(1,n+1):
    sum += num ** 6
  return sum

def h5(n):
  p1 = 0
  p2 = 0
  p1_g = 0
  for a in range(n):
    for b in range(3):
      roll = random.randint(1,4)
      p1 += roll
    for c in range(6):
      roll = random.randint(1,4)
      p1 += roll
      roll = random.randint(1,6)
      p2 += roll
    if p1 > p2:
      p1_g += 1
    p1 = 0
    p2 = 0
  return str(p1_g) +':' + str(n)

def h6(n):
  zeros = 0
  i = 1
  fact_n = str(fact(fact(n)))
  while True:
    if fact_n[-i] == '0':
      zeros += 1
      i += 1
    else:
      return zeros 

def h8(n):
  N = ''
  for num in range(n+1):
    N += str(num)
    if len(N) == n + 1:
      return N[n]

def h10(n):
  i = 1
  a = 0
  b = 1
  if n == 0 or n == 1:
    return i
  while True:
    sum = a + b
    a = b
    b = sum
    i += 1
    if len(str(b)) >= n:
      return i
  
def h11(n):
  s = '0.1'
  a = 1
  b = 1
  for i in range(n):
    sum = a + b
    a = b
    b = sum
    s += str(a)
  s = s[:n+3]
  return float(s)

import math 
#Function to find the next perfect square 
  
def nextPerfectSquare(N): 
  
    nextN = math.floor(math.sqrt(N)) + 1
  
    return nextN * nextN 
  
def proper_fractions(n):
    def gcd(x,y):
        while y:
            (x, y) = (y, x % y)
        return x
    if n <= 1:
        return 0
    else:
        count = 0
        for num in range(1,n):
            denom = gcd(n,num)
            if denom == 1:
                count += 1
        return count
Fetching token
?