1.
reverse a number
def re(a):
left = 0
right = len(a) - 1
while left < right:
temp = a[left]
a[left] = a[right]
a[right] = temp
left += 1
right -= 1
return a
n = int(input("Enter the no of number: "))
print("Enter the number:")
a = []
for i in range(n):
[Link](int(input()))
print(re(a))
[Link]
def palin(a):
left = 0
right = len(a) - 1
while left < right:
if a[left] != a[right]:
return False
left += 1
right -= 1
return True
n = int(input("Enter the no of number: "))
print("Enter the number:")
a = []
for i in range(n):
[Link](int(input()))
res = palin(a)
if res == True:
print("Its a palindrome")
else:
print("Its not a palindrome")
[Link] number
def prime(a):
if a <= 1:
return False
for i in range(2, a):
if a % i == 0:
return False
return True
[Link]
a = int(input("Enter the number: "))
res = prime(a)
if res == True:
print("Its a prime number")
else:
print("Its not a prime number")
def fib(a):
if a == 0:
return 0
if a == 1:
return 1
return fib(a-1) + fib(a-2)
a = int(input("Enter the number: "))
for i in range(a):
print(fib(i))
[Link] number
num = int(input("Enter number: "))
original = num
digits = len(str(num))
sum = 0
while num > 0:
digit = num % 10
sum += digit ** digits
num = num // 10
if sum == original:
print("Armstrong number")
else:
print("Not Armstrong")
[Link] of digits of a number
def sum_digits(a):
temp = a
total = 0
while temp > 0:
digit = temp % 10
total = total + digit
temp = temp // 10
return total
a = int(input("Enter the number: "))
print(sum_digits(a))
[Link] in a digits of a number
def largest(n):
max_digit = 0
while n > 0:
digit = n % 10
if digit > max_digit:
max_digit = digit
n = n // 10
return max_digit
n = int(input("Enter the number: "))
print("Largest digit =", largest(n))
[Link] number
def perfect(a):
sum = 0
for i in range(1, a):
if a % i == 0:
sum += i
return sum
a = int(input("Enter the number: "))
res = perfect(a)
if res == a:
print("Its a perfect number")
else:
print("Its not a perfect number")
[Link] GCD and LCM
def gcd(a, b):
if b == 0:
return a
return gcd(b, a % b)
def lcm(a, b, gcd_value):
return (a * b) // gcd_value
a = int(input("Enter the number a: "))
b = int(input("Enter the number b: "))
gcd_value = gcd(a, b)
lcm_value = lcm(a, b, gcd_value)
print("GCD of", a, "and", b, "is:", gcd_value)
print("LCM of", a, "and", b, "is:", lcm_value)
[Link] a String
def reverse_string(a):
b = list(a)
left = 0
right = len(b) - 1
while left < right:
temp = b[left]
b[left] = b[right]
b[right] = temp
left += 1
right -= 1
return "".join(b)
a = input("Enter the string: ")
print(reverse_string(a))
[Link] a string with same order
def reverse_word(word):
arr = list(word)
left = 0
right = len(arr) - 1
while left < right:
arr[left], arr[right] = arr[right], arr[left]
left += 1
right -= 1
return "".join(arr)
sentence = input("Enter the sentence: ")
words = [Link]()
for w in words:
print(reverse_word(w), end=" ")
[Link] in string
def palindrome(a):
left = 0
right = len(a) - 1
while left < right:
if a[left] != a[right]:
return False
left += 1
right -= 1
return True
a = input("Enter the word: ")
if palindrome(a):
print("Its a palindrome")
else:
print("Its not a palindrome")
[Link] and constants
a = input("Enter the string: ")
a = [Link]()
vowels = 0
consonants = 0
for ch in a:
if ch == 'a' or ch == 'e' or ch == 'i' or ch == 'o' or ch == 'u':
vowels += 1
elif ch >= 'a' and ch <= 'z':
consonants += 1
print("Vowels:", vowels)
print("Consonants:", consonants)
[Link] characters
a = input("Enter the string: ")
count = 0
for ch in a:
count += 1
print("Number of characters:", count)
[Link] sentence
sentence = input("Enter the sentence: ")
words = [Link]()
largest = words[0]
for word in words:
if len(word) > len(largest):
largest = word
print("Largest word:", largest)
[Link] formation *******
def triangleformation(N,A):
[Link]()
count = 0
for largest in range (N-1,1,-1):
left = 0
right = largest - 1
while left < right:
if A[left] + A[right] > A[largest]:
count += right - left
right -= 1
else:
left += 1
return count
N = int(input("no of elements: "))
print("Enter the elements: ")
A = []
for i in range(N):
[Link](int(input()))
print(triangleformation(N,A))
[Link] sub array – min subarray
def minimumFunc(N, A):
[Link]()
max_val = A[0]
min_val = A[0]
result = 0
for i in range(N):
max_val = max(max_val, A[i])
min_val = min(min_val, A[i])
result += (max_val - min_val)
return result
N = int(input())
A = list(map(int, input().split()))
print(minimumFunc(N, A))