# PRINT N PRIME NUM:
n = int(input('Enter: '))
num = 2
count = 0
while count < n:
isPrime = True
for i in range(2, num):
if num % i == 0:
isPrime = False
if isPrime == True:
print(num)
count += 1
num += 1
----------------------------------------------
# PRINT N FIBO NUM:
n = int(input('Enter: '))
a = 1
b = 1
count = 0
while count < n:
print(a)
temp = a + b
a = b
b = temp
count += 1
-----------------------------------------------
# CHECK ASCENDING:
n = int(input('Enter length: '))
lst = []
for i in range(n):
num = int(input('Enter num: '))
lst.append(num)
isIncreasing = True
for j in range(len(alist) - 1):
if alist[j] > alist[j + 1]:
isIncreasing = False
if isIncreasing == True:
print('increasing')
else:
print('not increasing')
-----------------------------------------------
# PRINT PRODUCT OF DIGITS:
n = int(input('Enter: '))
product = 1
while n > 0:
rem = n % 10
product *= rem
n //= 10
print(product)
------------------------------------------------
# PRINT NTH PRIME NUMBER BASED ON USER'S INPUT: ?????
n = int(input('Enter: '))
pos = 1
num = 2
while pos < n:
isPrime = True
for i in range(2, num):
if num % i == 0:
isPrime = False
break
if isPrime == True:
pos += 1
num += 1
if pos == n:
print(num)
------------------------------------------------
# FIND THE 2nd LARGEST NUM:
n = int(input('Enter: '))
lst = []
for i in range(n):
num = int(input('Enter num: '))
lst.append(num)
print(lst)
for i in range(len(lst) - 1):
for j in range(i + 1, len(lst)):
if lst[j] < lst[i]:
lst[i], lst[j] = lst[j], lst[i]
lst.remove(lst[len(lst) - 1])
sec_small = lst[len(lst) - 1]
print(sec_small)
-----------------------------------------------
# FIND 3RD SMALLEST NUMBER:
n = int(input('Enter: '))
lst = []
for i in range(n):
num = int(input('Enter num: '))
lst.append(num)
print(lst)
for i in range(len(lst) - 1):
for j in range(i + 1, len(lst)):
if lst[i] < lst[j]:
lst[j], lst[i] = lst[i], lst[j]
lst.remove(lst[len(lst) - 1])
lst.remove(lst[len(lst) - 1])
thrid_small = lst[len(lst) - 1]
print(thrid_small)
-------------------------------------------------
logn => Binary search
n.logn => Merge sort
n => Linear/Sequential search
n^2 => Other types
------------------------------------------------
# REPLACE OLD CHAR W/ NEW ONES:
def replace(s, old, new):
s = s + ' '
res = ''
for i in range(len(s)):
if s[i] == old[0]:
if s[i : (i + len(old)) == old:
res += new
i += len(old)
else:
res += s[i]
i += 1
else:
res += s[i]
i += 1
-------------------------------------------------
def encrypt(s):
toMorse = ''
for i in range(len(s)):
if s[i] != ' ':
toMorse = toMorse + morse_code[s[i]] + ' '
else:
toMorse = toMorse + ' '
return toMorse
print(encrypt('TRY UR BEST'))
-------------------------------------------------
def split(s):
s = s + ' '
lst = []
word = ''
for i in range(len(s)):
if s[i] != ' ':
word += s[i]
elif word != '':
lst.append(word)
word = ''
return lst
print(split('Hello 1 2 3')
----------------------------------------------
# replace:
def replace(s, old, new):
res = ''
i = 0
while i < len(s):
if s[i] == old[0]:
if s[i : (i + len(old))] == old:
res = res + new
i += len(old)
else:
res = res + s[i]
i += 1
else:
res = res + s[i]
i += 1
return res
print(replace('to bbeabe or not to bbbeebb', 'be', 'work'))
-------------------------------------------------
# decrypt:
def decrypt(s):
toStr = ''
word = ''
s = s + ''
i = 0
while i < len(s):
if s[i] != ' ':
word += s[i]
else:
if word != '':
for akey in morse_code:
if morse_code[akey] == word:
toStr = toStr + key
word = ''
else:
toStr = toStr + ' '
return toStr
print(decrypt('... --- ...')