print("Hello World")
name = input('What is your name? ')
print("Hello ", name)
name = input('What is your name?')
if name.lower() in ['sam','bob']:
print("Hello " +name)
n = int(input("Enter a number: "))
print("You have entered: ", n)
result = 0
for i in range(n+1):
#print(i)
result = result + i
print("The sum of numbers from 1..", n," is: " ,result)
n = int(input("Enter a number: "))
print("You have entered:", n)
result = 0
for i in range(n+1):
if (i%3 == 0) or (i%5==0):
#print(i)
result = result + i
print("Your answer is:" ,result)
n = input("Enter a Number: ")
n = int(n)
if (n%2 == 0):
print("You have entered an Even Number")
else:
print("You have entered an Odd Number")
n = input("Enter a number: ")
n = int(n)
print("***Multiplation table***")
for i in range(1,11):
print(n, 'x', i, '=', n*i)
def find_kth_smallest_element(arr, k):
arr = sorted(arr)
print("Sorted Array: ", arr)
return(arr[k-1])
arr = [3,5,78,2,89,5,101,0,87]
find_kth_smallest_element(arr,8)
# taking input from user
number = int(input("Enter any number: "))
# prime number is always greater than 1
if number > 1:
for i in range(2, number):
if (number % i) == 0:
print(number, "is not a prime number")
break
else:
print(number, "is a prime number")
# if the entered number is less than or equal to 1
# then it is not prime number
else:
print(number, "is not a prime number")
# taking input from user
number = input("Enter any number: ")
res = [int(x) for x in number]
print(res)
n = int(input("Enter a Number: "))
choice = input("Do you want to get a Sum or Product of 1..n?")
choice = choice.lower()
sum_value = 0
prod_value = 1
if choice == "sum":
for i in range(1, n+1):
sum_value = sum_value + i
print("Your answer is:" ,sum_value)
elif choice == "product":
for i in range(1,n+1):
#print(i)
prod_value = (prod_value * i)
print("Your answer is:" , prod_value)
else:
print("Please enter your choice correctly")
def isPalindrome(s):
return s == s[::-1]
a = str(input("Enter a string: "))
result = isPalindrome(a)
if result:
print("You have entered a palindrome")
else:
print("Your entered string is not a palindrome")
a = str(input("Enter a string: "))
b = input("Which character you need a count for? ")
count = 0
for i in a:
if i.lower() == b.lower():
count = count + 1
print("The Count is:" , count)
def FindMissingNum(A):
try:
B = [x for x in A if x > 0]
B = sorted(B)
for i in range(len(B)):
if (B[i+1] - B[i] > 1):
return(B[i] + 1)
elif (B[i+1] - B[i] == 0) or (B[i+1] - B[i] == 1):
pass
else:
return(B[-1] + 1)
except e:
print(e)
A = [1, 3, 6, 4, 1, 2]
FindMissingNum(A)
import random
print("*** Number Guessing Game***")
num = random.randint(1,99)
guess = int(input("Guess the number between 1 and 100: "))
chance = 0
while chance < 5:
if guess == num:
print("Congrats! You have guessed the number correct")
break
elif guess > num:
print("Your guess is too high. Try again!")
guess = int(input("Guess the number between 1 and 100 and something less than before: "))
#chance = chance + 1
else:
print("Your guess is too less.Try again")
guess = int(input("Guess the number between 1 and 100 and something more than before: "))
chance = chance + 1
if not chance < 5:
print("Sorry! YOU LOSE!!! The number is", num)
def linear_search(arr,x):
'''check if num is present in given array arr '''
print("The input array is: ", arr)
print("The target number is: ", x )
for i in range(len(arr)):
if arr[i] == x:
print("The target number is present in the array at: ",i)
return(i)
print("The target number is Not present in the array")
return(-1)
index = linear_search([6,12,4,56,78,85,1,2,89,100],3)
def binary_search(arr, x):
'''check if num is present in given array arr '''
print("The input array is: ", arr)
print("The target number is: ",x )
arr = sorted(arr)
print("The sorted array is: ", arr)
low = 0
high = len(arr) - 1
mid = 0
while low <= high:
mid = (high + low) // 2
# Check if x is present at mid
if arr[mid] < x:
low = mid + 1
# If x is greater, ignore left half
elif arr[mid] > x:
high = mid - 1
# If x is smaller, ignore right half
else:
print("The targer element is present in the given array")
return mid
# If we reach here, then the element was not present
print("The targer element is NOT present in the given array")
return -1
index = binary_search([6,12,4,56,78,85,1,2,89,100],89)
def matrix_search(mat,x):
'''check if num is present in given matrix mat '''
print("The input matrix is: ", mat)
print("The target number is: ", x )
rows = len(mat)
cols = len(mat[1])
print("Rows: ",rows , "Columns: ",cols)
for i in range(rows):
for j in range(cols):
if mat[i][j] == x:
print("The target number is present in the matrix at: ",(i,j))
return(i,j)
print("The target number is NOT present in the matrix. ")
return(-1)
mat = [[4, 5, 6,7],
[10, 2, 13, 0],
[1, 11, 18, 45]
]
matrix_search(mat,18)
def matrix_search_any(mat,x):
# using any() + list comprehension
result = any(x in sub for sub in mat)
return(result)
mat = [[4, 5, 6,7],
[10, 2, 13, 0],
[1, 11, 18, 45]
]
matrix_search_any(mat, 5)
def find_largest_num(mat):
rows = len(mat)
cols = len(mat[0])
n = mat[0][0]
for i in range(rows):
for j in range(cols):
if n < mat[i][j]:
n = mat[i][j]
return(n)
mat = [[4, 5, 6,7],
[87, 2, 13, 0],
[1, 11, 18, 45]
]
find_largest_num(mat)
def capitalize_input(s):
a = s.split()
result = ''
for i in range(len(a)):
result = result + " " +a[i].capitalize()
return(result)
capitalize_input('oindrila sen')