0% found this document useful (0 votes)
2 views

Python

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Python

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 8

23-04-2024

tuesday
=========

1.array=1 2 1 2 1 3 2(pairs of each digit)

def navya(a):
pairs=0
my_set=set(a)
for i in my_set:
count=a.count(i)
x=count//2
pairs=pairs+x
return pairs
arr=list(map(int,input().split(' ')))
print(navya(arr))

output:
1 2 1 2 1 3 2
2
=========================================
2.write a python program to fing the the sum of given number

n=int(input('enter a number'))
s=0
for i in range(n+1):
s=s+i
print(s)

output:
enter a number5
15

time complexity=O(n)
(or)

n=int(input('enter a number'))
s=n*(n+1)//2
print(s)

time complexity=O(1)
===========================================
3.count of a given number

n=int(input('enter a number:'))
c=0
while n>0:
c=c+1
n=n//10
print(c)

output:
enter a number:632
3
=============================================
4.sum of digits in a given number

n=int(input('enter a number:'))
s=0
while n>0:
r=n%10
s=s+r
n=n//10
print(s)

output:
enter a number:7864
25
============================================
5.product of digits in a given number

n=int(input('enter a number:'))
p=1
while n>0:
r=n%10
p=p*r
n=n//10
print(p)

output:
enter a number:34
12
==================================================
6.print the even numbers in the given digit

n=int(input('enter a number:'))
while n>0:
r=n%10
if n%2==0:
print(r)
n=n//10

output:
enter a number:632
2
6
======================================================
7.print the odd numbers in the given digit

n=int(input('enter a number:'))
while n>0:
r=n%10
if n%2!=0:
print(r)
n=n//10

output:
enter a number:632
3
===========================================================
8.prime digits in a given number

def is_prime(num):
for i in range(2,num):
if num%i==0:
return False
return True
n=int(input('enter a number'))
while n>0:
r=n%10
if is_prime(r):
print(r)
n=n//10

output:
enter a number8537
7
3
5
==========================================================
9.given number is a trendy number or not(in a given number middle digit is
divisible by 3)

def is_valid(num):
if n>99&n<=999:
return True
return False
def is_trendy(num):
r=(num//10)%10
if r%3==0:
return True
return False
n=int(input('enter a number:'))
if is_valid(n):
if is_trendy(n):
print('it is a trendy number')
else:
print('not a trendy number')
else:
print('invalid num')

output:

enter a number:832
it is a trendy number
=============================================================
10.add first and last digit in a given list:
def is_valid(n):
if n>999 and n<=9999:
return True
return false
def key(n):
fn=n//1000
ln=n%10
print('the key is ',fn+ln)
num=int(input('enter a number'))
if is_valid(num):
key(num)

output:
enter a number 1001
the key is 2
================================================================
11.given number is divisible by all the digits or not:
def findDigits(n):
temp=n
count=0
while n>0:
ld=temp%10
if n%ld==0 and ld!=0:
count=count+1
return count
output:

=================================================================
12.find the maximum element in the given array:
l=[1,10,14,17,20,50,3,4]
maxy=0
for i in range(len(l)):
if l[i]>maxy:
maxy=l[i]
print(maxy)

output:
50
================================================================
13.find the second maximum element in the given array:
l=[2,3,10,1,5,9,4]
maxy=0
maxy2=0
for i in range(len(l)):
if l[i]>maxy:
maxy2=maxy
maxy=l[i]
elif l[i]>maxy2 and l[i]!=maxy:
maxy2=l[i]
print(maxy2)

output:
9
=================================================================
24-04-2024
wednesday

14.multiply a largest two values in a given array

arr=[4,2,1,3]
# 0 1 2 3
maxp=0
n=len(arr)
for i in range(n-1):
for j in range(i+1,n):
if arr[i]*arr[j]>maxp:
maxp=arr[i]*arr[j]
print(maxp)

output:
12

time complexity:O(n^2)

(or)

arr=[4,2,1,3]
# 0 1 2 3
arr.sort()
print(arr[-1]*arr[-2])

time complexity:O(nlogn)

(or)

arr=[4,2,1,3]
# 0 1 2 3
max1=0
max2=0
for i in range(len(arr)):
if arr[i]>max1:
max2=max1
max1=arr[i]
elif arr[i]>max2 and arr[i]!=max1:
max2=arr[i]
print(max1*max2)

time complexity:O(n)
(or)

arr=[4,2,1,3]
# 0 1 2 3
r1=max(arr)
arr.remove(r1)
r2=max(arr)
print(r1*r2)

time complexity:O(n)
=================================================
15.check array is sorted or not

def check_me(a):
for i in range(1,len(a)):
if a[i]<a[i-1]:
return False
return True
arr=list(map(int,input().split(' ')))
if check_me(arr):
print('sorted')
else:
print('not sorted')

output:
2 3 4 5
sorted
=====================================================
16.swapping of two elements by using temporary variable

def swap(a,x,y):
temp=a[x]
a[x]=a[y]
a[y]=temp
arr=[2,5,7,1,9]
# 0 1 2 3 4
print(arr)
swap(arr,0,4)
print(arr)

output:
[2, 5, 7, 1, 9]
[9, 5, 7, 1, 2]
========================================================
17.only prime numbers are printed in a given array

def is_prime(x):
if(x<=1):
return False
for i in range(2,x):
if x%i==0:
return False
return True
def check_prime(a):
for i in range(len(a)):
if is_prime(a[i]):
print(a[i])
l=list(map(int,input().split(',')))
check_prime(l)

output:
2,3,4,5,6,7,8,9,11
2
3
5
7
11
============================================================
18.find the sum of even numbers and odd numbers

def sum_of_even_odd(n):
even=0
odd=0
for i in range(len(n)):
if(n[i]%2==0):
even=even+n[i]
else:
odd=odd+n[i]
print('sum of even:',even,'sum of odd:',odd)
l=list(map(int,input().split(' ')))
sum_of_even_odd(l)

output:
2 3 4 5 6 7 8
sum of even: 20 sum of odd: 15
=======================================================
19.difference between maximum element and minimum element

def diff(n):
max=0
min=999
for i in range(len(n)):
if(n[i]>max):
max=n[i]
if(n[i]<min):
min=n[i]
print(max-min)
l=list(map(int,input().split(' ')))
diff(l)

output:
1 3 5 7 9
8
===========================================================
20.count

def cnt(n,t):
c=0
for i in range(len(n)):
if(n[i]==t):
c=c+1
print(c)
l=list(map(int,input().split()))
r=int(input())
cnt(l,r)

output:
1 2 13 1 4 1
1
3
===============================================================
21.unique occurances

def oc(arr):
d={}
for element in arr:
if element in d:
d[element]+=1
else:
d[element]=1
ur=set(d.values())
return len(d)==len(ur)
a=[1,1,1,1,2,2,2,3,3]
print(oc(a))

output:
True
==============================================================
22.

You might also like