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

IAT-1 Workbook P3-Python

The document contains Python programs and examples for solving various problems. It includes programs to: 1. Find the average of two test scores. 2. Check if a number is a palindrome and count the occurrences of each digit. 3. Form an integer with the number of digits in the input number at the tens place and the last digit of the input number at the ones place. 4. Print all prime numbers in a given range. 5. Find words in a sentence longer than a given length. 6. Find duplicate characters in a string. 7. Convert between binary, decimal, and hexadecimal numbers. 8. Analyze a sentence to count words,

Uploaded by

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

IAT-1 Workbook P3-Python

The document contains Python programs and examples for solving various problems. It includes programs to: 1. Find the average of two test scores. 2. Check if a number is a palindrome and count the occurrences of each digit. 3. Form an integer with the number of digits in the input number at the tens place and the last digit of the input number at the ones place. 4. Print all prime numbers in a given range. 5. Find words in a sentence longer than a given length. 6. Find duplicate characters in a string. 7. Convert between binary, decimal, and hexadecimal numbers. 8. Analyze a sentence to count words,

Uploaded by

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

6/24/23, 10:44 AM IAT-1 Workbook P3-Python

1. Write a python program to find the better of two test average marks out of three
test’s marks accepted from the user.
Test case 1:
20
15
22
Output:
21.0

In [2]:

test1=int(input())
test2=int(input())
test3=int(input())
if(test1<test2 and test1<test3):
avg=(test2+test3)/2
elif(test2<test1 and test2<test3):
avg=(test1+test3)/2
else:
avg=(test1+test2)/2
print(avg)

30
30
30
30.0

2. Develop a Python program to check whether a given number is palindrome or not and
also count the number of occurrences of each digit in the input number.
Input: 12321
Output:Palindrome
      22122
Input: 12982221
Output:Not Palindrome
      24114442

In [6]:

num=input() #num="12321" is a string


revnum=num[-1::-1]
if(num==revnum):
print("Palindrome")
else:
print("Not Palindrome")
for digit in num:
print(num.count(digit),end="")

45232223254
Palindrome
22525552522

localhost:8888/notebooks/IAT-1 Workbook P3-Python.ipynb 1/16


6/24/23, 10:44 AM IAT-1 Workbook P3-Python

3. Write a Python Program to form an Integer that has the Number of Digits at Ten’s
Place and the Least Significant Digit of the Entered Integer at One’s Place
Test case 1:
enter number:129
Output:39
Test case 2:
enter number:24678
Output:58

In [7]:

num=input()
L=len(num)
LSB=num[-1]
print(L*10+int(LSB))

129
39

4. Write a Python program to print all Prime numbers in an Interval



Test case:
Input: 2 70
Output: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67

In [11]:

def is_prime(N):
for i in range(2,(N//2)+1):
if(N%i==0):
return False
return True
##################################
list1=input().split()
low=int(list1[0])
high=int(list1[1])
for num in range(low,high+1):
if(is_prime(num)):
print(num,end=", ")

2 70
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67,

localhost:8888/notebooks/IAT-1 Workbook P3-Python.ipynb 2/16


6/24/23, 10:44 AM IAT-1 Workbook P3-Python

In [ ]:

# Write a Python program to input the sentence of words


# and find the list of words that are longer than n from
# a given sentence. Take n and sentence as input.
Test case 1:
5
Hi Python is loosely typed Language
Output:
['Python', 'loosely', 'Language']
Test case 2:
4
Find the List of Words that are Longer than n from a given List
of Words

Output:
['Words', 'Longer', 'given', 'Words']

In [12]:

L=int(input())
list1=input().split()
list2=[]
for word in list1:
if(len(word)>L):
list2.append(word)
print(list2)

5
python loosely typed language
['python', 'loosely', 'language']

In [ ]:

# Write a Python Program to find all the duplicate characters


# in a given string
Test case 1:
Input: hellooo
Output: lo
Test case 2:
Input: institution
Output:int

localhost:8888/notebooks/IAT-1 Workbook P3-Python.ipynb 3/16


6/24/23, 10:44 AM IAT-1 Workbook P3-Python

In [14]:

st1=input()
st2=""
for ch in st1:
if(st1.count(ch)>1 and (ch not in st2)):
st2=st2+ch
print(st2)

hellooo
lo

In [19]:

# Develop a python program to convert binary to decimal


Input: 1111
Output:15
def bin_deci(num):
i=0
decimal=0
while(num>0):
d=num%10
decimal=decimal+d*(2**i)
i=i+1
num=num//10
return decimal

num=int(input())
print(bin_deci(num))

11111110
254

In [24]:

# Develop a python program to convert decimal to binary


Input: 1111
Output:15
def deci_bin(num):
i=0
binary=""
while(num>0):
bit=num%2
binary=str(bit)+binary
num=num//2
return binary

num=int(input())
print(deci_bin(num))

254
11111110

localhost:8888/notebooks/IAT-1 Workbook P3-Python.ipynb 4/16


6/24/23, 10:44 AM IAT-1 Workbook P3-Python

In [26]:

#Program to convert decimal to hexadecimal


num=int(input())
print(hex(num))

100
0x64

In [29]:

#Program to convert hexadecimal to decimal


num=input()
decnum=int(num,base=16)
print(decnum)

0xff
255

In [ ]:

8. a) Write a Python program that accepts a sentence


and find the number of words, digits, uppercase letters
and lowercase letters.

Input:India won at 56runs!!!!
Output:4 2 1 13

In [32]:

st=input()
words=0
digits=0
up=0
low=0
for ch in st:
if(ch==" "):
words+=1
elif(ch.isdigit()):
digits+=1
elif(ch.islower()):
low+=1
elif(ch.isupper()):
up+=1
print(words+1,digits,up,low)

India won by 45runs


4 2 1 13

localhost:8888/notebooks/IAT-1 Workbook P3-Python.ipynb 5/16


6/24/23, 10:44 AM IAT-1 Workbook P3-Python

In [ ]:

b) Write a Python program to find the string similarity


between two given strings
Sample Input:
Python Exercises
Python Exercises
Output:1.0

Sample Input:
Python Exercises
Python Exercise
Output
0.967741935483871

In [35]:

st1=input().lower()
st2=input().lower()
L1=len(st1)
L2=len(st2)
L=min(L1,L2)
similar=0
for i in range(L):
if(st1[i]==st2[i]):
similar+=1
print(similar*2/(L1+L2))

python exercises
python exercise
0.967741935483871

In [ ]:

9. Write a Python program to input the sentence.


Find out the word in the sentence which contains the highest number
of vowels in it.
Test Case 1:
Hi Aeiou How are you Finee
Output:
Aeiou

localhost:8888/notebooks/IAT-1 Workbook P3-Python.ipynb 6/16


6/24/23, 10:44 AM IAT-1 Workbook P3-Python

In [42]:

list1=input().split()
m=0
for word in list1:
cnt=0
for ch in word:
if(ch in "aeiouAEIOU"):
cnt+=1
if(cnt>m):
reqword=word
m=cnt
print(reqword)

Hi Aeiou How are you Finee


Aeiou

In [44]:

# Write a Python Program to print


# all odd factors of an input number n in range 1 to n.
n=int(input())
for i in range(1,n+1):
if(n%i==0 and i%2!=0):
print(i, end=" ")

21
1 3 7 21

In [ ]:

#Write a program to convert roman numbers in to


#integer values using dictionaries
Input="III" Output=3
Input="CDXLIII" Output=443

localhost:8888/notebooks/IAT-1 Workbook P3-Python.ipynb 7/16


6/24/23, 10:44 AM IAT-1 Workbook P3-Python

In [ ]:

13. Few prime numbers can be equal to the sum of other consecutive
prime numbers.
For example
5 = 2 + 3,
17 = 2 + 3 + 5 + 7,
41 = 2 + 3 + 5 + 7 + 11 + 13.
Find out how many prime numbers which satisfy this property
are present in the range 3 to N subject to a constraint that
summation should always start with number 2.
Write a python program to find out the prime numbers that
satisfy the above mentioned property in a given range.
Input:
15
Output:
5
Input:
20
Output:
5
17

In [ ]:

#Logic..N=100
1) Generate the list of prime numbers 2 to N
list1=[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31,
37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
2) start testing from second to last element. if second is sum of
first, third is sum of first+second and so on
3)if(true) print the prime element

localhost:8888/notebooks/IAT-1 Workbook P3-Python.ipynb 8/16


6/24/23, 10:44 AM IAT-1 Workbook P3-Python

In [73]:

def is_prime(N):
for i in range(2,(N//2)+1):
if(N%i==0):
return False
return True
##################################

N=int(input())
list1=[]
for num in range(2,N+1):
if(is_prime(num)):
list1.append(num)
print(list1)
for i in range(1,len(list1)):
num=list1[i]
sum1=0
for ele in list1[0:i]:
sum1=sum1+ele
if(sum1==num):
print(num)
break

100
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 7
1, 73, 79, 83, 89, 97]
5
17
41

In [ ]:

14. Write a Python program to check the validity of password


input by users.
Following are the criteria for checking the password:
1. At least 2 letter between [a-z]
2. At least 2 number between [0-9]
1. At least 1 letter between [A-Z]
3. At least 1 character from [$#@]
4. Minimum length of transaction password: 8
5. Maximum length of transaction password: 15
Your program should accept set of passwords and will check
them according to the above criteria.
Passwords that match the criteria are to be printed.
Input:
5
ABd12t34@1
a F1ttt#
2w3EEEgw3*
2We3345
ab34#ullo*12AakQ
output:
ABd12t34@1

localhost:8888/notebooks/IAT-1 Workbook P3-Python.ipynb 9/16


6/24/23, 10:44 AM IAT-1 Workbook P3-Python

In [76]:

def is_pwd(pwd):
L=len(pwd)
l=0
d=0
u=0
c=0

if(L>=8 and L<=15):


for ch in pwd:
if(ch.isdigit()):
d=d+1
elif(ch.islower()):
l=l+1
elif(ch.isupper()):
u=u+1
elif(ch in "$#@"):
c=c+1
total=c+u+l+d
if(l>=2 and d>=2 and u>=1 and c>=1 and total==L):
return True
else:
return False

##############################################

N=int(input())
list1=[]
for i in range(N):
passwd=input()
if(is_pwd(passwd)):
list1.append(passwd)
for pw in list1:
print(pw)

2
2w3EEEgw3*
a F1ttt#

localhost:8888/notebooks/IAT-1 Workbook P3-Python.ipynb 10/16


6/24/23, 10:44 AM IAT-1 Workbook P3-Python

In [ ]:

15. USN is University Seat Number which contains


1 digit region code, followed by 2 characters College code,
2 digit year and 2 character branch code and 3 digit roll number
Write a Python program to read n number of strings and
print all the strings which are USN. First input number of
strings and then the strings.
Input: region,college,year,branch,roll
4
askdf
1CR20IS003
1CR19EC112
2CE18EC110
Output:
1CR20IS003
1CR19EC112
2CE18EC110

In [78]:

def is_usn(w):
L=len(w)
if(L==10):
region=w[0].isdigit()
college=w[1:3].isalpha()
year=w[3:5].isdigit()
branch=w[5:7].isalpha()
roll=w[7:10].isdigit()
if(region and college and year and branch and roll):
return True
return False
#######################################
N=int(input())
list1=[]
for i in range(N):
usn=input()
if(is_usn(usn)):
list1.append(usn)
for u in list1:
print(u)

1
1CR1mEC112

In [ ]:

askdf
1CR20IS003
1CR19EC112
2CE18EC110

localhost:8888/notebooks/IAT-1 Workbook P3-Python.ipynb 11/16


6/24/23, 10:44 AM IAT-1 Workbook P3-Python

In [ ]:

16. Write a function called isphonenumber() to recognize


a pattern 415-555-4242.
Program:
Test case 1:415-555-4242
Output:True

Test case 2:415-555+4242
Output:False

In [79]:

def is_phonenumber(num):
list1=num.split("-")
if(len(list1)==3):
n1=list1[0]
n2=list1[1]
n3=list1[2]
cond1=len(n1)==3 and n1.isdigit()
cond2=len(n2)==3 and n2.isdigit()
cond3=len(n3)==4 and n3.isdigit()
if(cond1 and cond2 and cond3):
return True
return False
###################
ph=input()
if(is_phonenumber(ph)):
print(ph)

445-567-9876
445-567-9876

In [ ]:

17. Write a program that reads n strings as input.


Display all the strings that starts with the word ‘Hello’
and ends with the word of digits 0 - 9.
First input the number of strings and then the strings.

Test Case 1:
4
Hello How you 9
Helo How is
How are you99
Hello 999
Output:
Hello How you 9
Hello 999

localhost:8888/notebooks/IAT-1 Workbook P3-Python.ipynb 12/16


6/24/23, 10:44 AM IAT-1 Workbook P3-Python

In [81]:

N=int(input())
list1=[]
for i in range(N):
st=input()
list1.append(st)
for st in list1:
list2=st.split()
first=list2[0].lower()
last=list2[-1]
if(first=="hello" and last.isdigit()):
print(st)

4
Hello How you 9
Helo How is
How are you99
Hello 999
Hello How you 9
Hello 999

In [ ]:

18. Write a Python program to input email addresses


and print the user name and company names of the given
email addresses. Email addresses in the "[email protected]"
format. Both user names and company names are composed of letters
only.
Input:
[email protected] [email protected] [email protected]

Output:
peter yahoo
john google
xyz accenture

In [82]:

emails=input().split()
for email in emails:
ind1=email.index("@")
ind2=email.index(".")
name=email[0:ind1]
company=email[ind1+1:ind2]
print(name,company)

[email protected] [email protected] [email protected]


peter yahoo
john google
xyz accenture

localhost:8888/notebooks/IAT-1 Workbook P3-Python.ipynb 13/16


6/24/23, 10:44 AM IAT-1 Workbook P3-Python

In [ ]:

19. Write a Python program to take dates as string input and


convert a date of yyyy-mm-dd format to dd-mm-yyyy format.
If the date is in dd-mm-yyyy format do not change it.
Input:
2016-01-20 2020-05-25 12-10-2008 01-12-2007 1999-12-21
Output:
20-01-2016 25-05-2020 12-10-2008 01-12-2007 21-12-1999

In [84]:

dates=input().split()

for date in dates:
list1=date.split("-")
if(len(list1[0])==4):
list1.reverse()
st1="-".join(list1)
print(st1,end=" ")
else:
print(date,end=" ")

2016-01-20 2020-05-25 12-10-2008 01-12-2007 1999-12-21


20-01-2016 25-05-2020 12-10-2008 01-12-2007 21-12-1999

localhost:8888/notebooks/IAT-1 Workbook P3-Python.ipynb 14/16


6/24/23, 10:44 AM IAT-1 Workbook P3-Python

In [ ]:

20. URL is Uniform Resource Locator which consists of Scheme,


Sub domain, Second level domain and top domain followed by
sub directories and file name for example
http://blog.hubspot.com/marketing/Index.html.
In the above example
http - Scheme,
blog - Sub domain
com - top domain
marketing-Directory
index.html-file name
Scheme can also take the value ftp
Top domain can take value com or org or in
Take n URL strings as input, write a Python program to
display the proper URLS, and also display count of the URLs
that start with 'https' and ends with 'com'

Input:
6
https://www.yahoo.com
http://blog.hubspot.com/marketing/Index.html
http://www.cmrit.ac.in
https://www.karnatakacareers.in
ftp://internet.address.edu/file/path/file.txt
abc:/xxx.abc.vvv.in
Output:
https://www.yahoo.com
http://blog.hubspot.com/marketing/Index.html
http://www.cmrit.ac.in
https://www.karnatakacareers.in
ftp://internet.address.edu/file/path/file.txt
1

localhost:8888/notebooks/IAT-1 Workbook P3-Python.ipynb 15/16


6/24/23, 10:44 AM IAT-1 Workbook P3-Python

In [88]:

valid_Scheme=["https","http","ftp"]
domain=[".com",".in",".edu",".org"]
N=int(input())
url_list=[]
for i in range(N):
url=input()
url_list.append(url)
cnt=0
for url in url_list:
list1=url.split("://")
if(list1[0] in valid_Scheme):

for ele in domain:


if(ele in url):
print(url)
break
if(list1[0]=='https' and url[-4:]==".com"):
cnt+=1
print(cnt)

1
https://www.yahoo.com (https://www.yahoo.com)
https://www.yahoo.com (https://www.yahoo.com)
1

In [5]:

# Write a program to convert roman numbers in to


# integer values using dictionaries
roman = {'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000}
r=input()
deci=0
for i in range(len(r)):
prev=roman[r[i-1]]
current=roman[r[i]]
if current > prev and i>0:
deci += current - 2 * prev
else:
deci += current
print(deci)

CDXLIII
443

In [ ]:

localhost:8888/notebooks/IAT-1 Workbook P3-Python.ipynb 16/16

You might also like