REGULAR EXPRESSION
PROBLEM -
Assume that you are entering a shopping mall with your car. You need to pay for parking vehicle. An operator was doing manual calculation for calculating the total hours spent. Help him do it automatically by writing a python code. Also mall opens by 8AM in the morning and clases by 12 in the night. No cars can be parked after 12.
Upto 3 hours Rs.100
for every additional hour :Rs.40
Input
Intime in the 24 hrs format
outtime in 24 hrs format
Output
Amount in Rs
Input :
10:00
22:20
Output :
Rs.500
Input :
10AM
10:20 PM
Output :
Invalid Input
CODE -
import re
a=input()
b=input()
if (re.match("[0-2][0-9]+\:[0-2][0-9]",a)) and (re.match("[0-2][0-9]+\:[0-2][0-9]",b)):
if int(b[3:5])!=0:
time=int(b[0:2])-int(a[0:2])+1
else:
time=int(b[0:2])-int(a[0:2])
if time>3:
f=(time-3)*40+100
elif time==3:
f=100
print("Rs.",f,sep='')
else:
print("Invalid input")
--------------------------------------------------------------------------------------------------------------------------------
PROBLEM -
MAC address is an id allocated to the computer or mobile devices at the time of manufacturing. It is used to uniquely identify a device within Local Area Network. Validate the following MAC.
A valid MAC has 6 bytes seperated by : and each byte is presented as 2 hexadecimal numbers
Input
AB:12:cD:12:AF:CE
Output
Yes
Input
12:w0:23
Output
No
CODE -
import re
a=input()
if (re.match("[0-9A-Fa-f][0-9A-Fa-f]+\:[0-9A-Fa-f][0-9A-Fa-f]+\:[0-9A-Fa-f][0-9A-Fa-f]+\:[0-9A-Fa-f][0-9A-Fa-f]+\:[0-9A-Fa-f][0-9A-Fa-f]+\:[0-9A-Fa-f][0-9A-Fa-f]",a)):
print("Yes")
else :
print("No")
OUTPUT -
--------------------------------------------------------------------------------------------------------------------------------
PROBLEM -
Check whether the given identifier is a valid SID. A valid SID has 8, 4, 4, 4, 12 hexa decimal digits separated by '-'
Input
e02fd0e4-00fd-090A-ca30-0d00a0038ba0
Output
Yes
Input
e02gd0e4-00fd-090A-ca30-0d00a0038ba0
Output
No
CODE -
import re
a=input()
if (re.match("[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]+\-[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]+\-[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]+\-[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]+\-[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]",a)):
print("Yes")
else:
print("No")
OUTPUT -
--------------------------------------------------------------------------------------------------------------------------------
PROBLEM -
Given a dob, find out whether the person is retired. A person is retired if his age is >60 year or age is more than(60*365) days. If a person is retired display "Retired" , if not display "Not Retired".
Assume an year has 365 days.
Input
23-02-1956
Output
Retired
Input
20-12-1995
Output
Not Retired
CODE -
import re
a=input()
d='02-12-2021' #take today's date as d
if (re.match("^[0-9][0-9]+\-[0-1][0-9]+\-[0-2][0-9][0-9][0-9]$",a)):
dday=int(d[0:2])+int(d[3:5])*30+int(d[6:])*365
aday=int(a[0:2])+int(a[3:5])*30+int(a[6:])*365
days=dday-aday
if days>(60*365):
print("Retired")
else:
print("Not Retired")
a=input()
d='02-12-2021' #take today's date as d
if (re.match("^[0-9][0-9]+\-[0-1][0-9]+\-[0-2][0-9][0-9][0-9]$",a)):
dday=int(d[0:2])+int(d[3:5])*30+int(d[6:])*365
aday=int(a[0:2])+int(a[3:5])*30+int(a[6:])*365
days=dday-aday
if days>(60*365):
print("Retired")
else:
print("Not Retired")
OUTPUT -
--------------------------------------------------------------------------------------------------------------------------------
PROBLEM -
IP address is used to uniquely identive the device connected to the internet. Validate the classless IPv4 address An IP address is of the format
A.B.C.D/X
and
A, B, C , D and X must be integers
0 <= A <= 255
0 <= B <= 255
0 <= C <= 255
0 <= D <= 255
0 <= X <= 31
Input
103.041.147.102/31
Output
Valid
Input
307.041.147.102/30
Output
Invalid
CODE -
import re
a=input()
if re.match("^[0-2][0-9][0-9]+\.[0-2][0-9][0-9]+\.[0-2][0-9][0-9]+\.[0-2][0-9][0-9]+/[0-3][0-9]$",a):
print("Valid")
else:
print("Invalid")
OUTPUT -
--------------------------------------------------------------------------------------------------------------------------------
PROBLEM -
You have given sample input string and its corresponding output format. Just observe the pattern of the input output transformation and implement a python program to verify the pattern using regular expression concepts.
Input:
KDeoAL1Ok2lOO3HserfAJSIskdsf
Output :
HHeoHH1Hk2lHH3HserfHHHHskdsf
KDLLAL1OL2LOO3HLLLLAJSILLLLL
KDeoALNOkNlOONHserfAJSIskdsf
CODE -
import re
n=input()
print(re.sub('[A-Z]','H',n))
print(re.sub('[a-z]','L',n))
print(re.sub('[0-9]','N',n))
n=input()
print(re.sub('[A-Z]','H',n))
print(re.sub('[a-z]','L',n))
print(re.sub('[0-9]','N',n))
OUTPUT -
--------------------------------------------------------------------------------------------------------------------------------------------------------
Comments
Post a Comment