Showing posts with label PPS3. Show all posts
Showing posts with label PPS3. Show all posts

Problem Set 3(Time Conversion)

Time Conversion:
Given a time in the 12-hour format with the suffix , either AM/PM, convert that  into a 24-hour format. 12-hour format is hours:minutes:seconds followed by AM or PM, where the hours range from 0 to 12, minutes range from 0 to 59, seconds range from 0 to 59.  24-hours format is hours:minutes and seconds , where hours range from 0 to 23, minutes range from 0 to 59, seconds range from 0 to 59. All the three components: hours, minutes, seconds are represented in the two digit format.
Note Midnight 12 o’clock is 12:00:00AM in the 12-hour format and it is 00:00:00 in 24- hour format. 12 Noon is 12:00:00PM in the 12-hour format and it is 12:00:00 in the 24- hour format.
For example, if input is 07:05:45PM then the output is 19:05:45 and if the input is 07:05:45AM then the output is 07:05:45.
Input format:
Time in 12-hour format with suffix, either  AM or PM
Output format:
Print time in 24-hour format
Boundary Conditions:
0 < hour, minute and seconds < 60
Meridium should be either “AM” or “PM”

Input:
Time in correct format from the user

Processing:
hrs = time[0:2]
minutes = time[3:5]
seconds = time[6:8]
AM_PM = time[8:10]
flag = True
if int(hrs) > 12:
    flag == False
if AM_PM == 'AM' and flag == True:
    if int(hrs) == 12:
        hrs = '0'
elif AM_PM == 'PM' and flag == True:
    if int(hrs) != 12:
        hrs = int(hrs) + 12
if int(minutes) > 60 or int(seconds) > 60:
    flag = False
if len(str(hrs)) != 2:
    hrs = '0' + str(hrs)
if len(str(minutes)) != 2:
    minutes = '0' + minutes
if len(str(seconds)) != 2:
    seconds = '0' + seconds

Output:
Display time in the 24 hour format

Algorithm:
Step1. Get the time in correct format from the user(time).
Step2. Split the time with respect to : and store each portion in hrs, minutes, seconds and AM_PM.
Step3. Check if hrs is greater than 12 if true then change flag to False else proceed to step4
Step4. Check if AM_PM is ‘AM’ and also check if flag is equal to True if both these are true then proceed to Step4.1 else proceed to step5
Step4.1 check if hrs is 12 if true then assign hrs as 0
Step5. Check if the element present in the third index of array is ‘PM’ and also check if flag is equal to True if both these are true then proceed to Step5.1 else proceed to step6
Step5.1 check if zeroth element of array is not equal to 12 if true then assign hrs as the sum of the integer conversion of hrs and 12
Step6. Check if the minutes and seconds are greater than 60 all the conditions are satisfied assign flag as false
Step7. If length of minutes string is less than two then concatenate it with a 0 at the front and do the same for seconds if length of the seconds string is less than 2
Step8. If flag is true then display the time 24 hr format else display invalid.
Step9. End
Program:
time = input()
hrs = time[0:2]
minutes = time[3:5]
seconds = time[6:8]
AM_PM = time[8:10]
flag = True
if int(hrs) > 12:
    flag == False
if AM_PM == 'AM' and flag == True:
    if int(hrs) == 12:
        hrs = '0'
elif AM_PM == 'PM' and flag == True:
    if int(hrs) != 12:
        hrs = int(hrs) + 12
if int(minutes) > 60 or int(seconds) > 60:
    flag = False
if len(str(hrs)) != 2:
    hrs = '0' + str(hrs)
if len(str(minutes)) != 2:
    minutes = '0' + minutes
if len(str(seconds)) != 2:
    seconds = '0' + seconds
if flag==True:
    print("%s:%s:%s"%(hrs,minutes,seconds))
else:

    print('Invalid')

Flowchart:

To edit this flowchart click on this

Problem Set3(Good or Bad)

GOOD OR BAD

Q. A word is called as a good word if all the letters of the word are distinct. That is, all the letters of the word are different from each other letter. Else, the word is called as a bad word.
Write an algorithm and the subsequent Python code to check if the given word is good or bad.: e.g. START, GOOD, BETTER are bad: WRONG is good! Make the comparison to be case insensitive.
Input format:
A word
Output format:
Print ‘Good’ if all letters of the word are distinct and print ‘bad’ otherwise
Input:
The Word (word)

Processing:
length = len(word)
count=0
for i in range(0,word):
    for j in range(0,length):
        if word[i]==word[j]:
            count+=1
Output:

Display whether the word is good or bad
Program:
word = input('')
word = word.lower()
length = len(word)
count=0
for i in range(0,length):
    for j in range(0,length):
        if word[i]==word[j]:
            count+=1
if count==len(word):
    print('Good')
else:
    print('Bad')

Algorithm:
Step1.Read the word.
Step2.Initialize i, j and count as 0
Step3.Repeat till i is less than length
Step3.1 Repeat till j is less than length
Step3.1.1 if the letter at the ith position of word is equal to that of jth position  of the word then increase count by 1
Step4. If count is equal to length display Good else display Bad
Step5. End

Flowchart:     
            
To edit this flowchart click on this

Problem Set3(Password Check)

Password Check
Q.Given a word, check if it is a valid password or not.  A password is said to be valid if it satisfies the following conditions:
i) Should begin with a letter
ii) Should contain atleast one digit and one special character
iii) Length of the password should be atleast 8
Print ‘Valid' if the given word satisfies the above three conditions  and print ‘Invalid’ otherwise.
Input format:
A word
Output format:
Print ‘Valid' if the given word satisfies the above three conditions  and print ‘Invalid’ otherwise.
Input:
The password(password)

Processing
length = len(password)
count_digit = 0
count_special = 0
for i in range(length):
    if i == 0:
        if password[0].isalpha() == False:
            break
    else:
        if password[i].isdigit() == True:
            count_digit+=1
            continue
        elif password[i].isalpha() == False:
            count_special+=1
            continue
Output:
Display if the password is Valid or Invalid.
Algorithm:
Step1. Get the password(password)
Step2. Initialize count_digit and count_special as zero
Step3. Let length variable be assigned as the length of the password.
Step4. Check if length is less than 8 if yes then diplay Invalid else proceed to step5
Step5. initialize i as 0 and repeat till i is less than length.
Step5.1 if i is equal to zero then check if ith character of the password variable is not an alphabet if true then break out of the loop.
Step5.2 if i is not equal to zero then proceed to step5.3
Step5.3 check if ith character of password is a digits list if true then increment count_digit variable by 1 and increment i by one and then move on to the loops starting point else move on to step 5.4
Step5.4 check if ith character of password is an alphabet if false then increment count_special variable by 1 and increment i by one and then move on to the start of the loop.
Step6. Check if both count_digit and count_special are not equal to zero if true display Valid else display Invalid.
Step7.End
Program:
password = input()
length = len(password)
count_digit = 0
count_special = 0
if length >= 8:
    for i in range(length):
        if i == 0:
            if password[0].isalpha() == False:
                break
        else:
            if password[i].isdigit() == True:
                count_digit+=1
                continue
            elif password[i].isalpha() == False:
                count_special+=1
                continue
if count_digit == 0 or count_special == 0:
    print('Invalid')
else:
    print('Valid')

Flowchart:
 To edit this flowchart click on this

Problem Set3(Caeser Cipher)

Q. In Caesar cipher, each letter is replaced by another letter which occurs at the  d-th position (when counted from the position of the original letter),  in the  English alphabet.  For identifying the position of a letter, we follow the usual order of the English alphabet, from a to z. Given a word and a positive integer d, use Caesar cipher to encrypt it. For example, if the word is 'ball' and the value of 'd' is 3 then the new encrypted word is 'edoo'. 'x' will be replaced by 'a', 'y' should be replaced by 'b' and 'z' should be replaced by 'c'. While the code is submitted for Online Judge (SkillRack), use rstrip(), to remove carriage return character in the input.
Input Format
Word
A positive integer 'd'
Output format:
Encrypted word
Input:
The word(word)
A positive number(number)

Processing:
word.rstrip()
number = int(input())
number = abs(number)
number = number%26
code = []
for i in range(len(word)-1):
    temp = ord(word[i])+ number
    if temp>122:
        temp = temp - 122
        temp = 96 + temp
        code.insert(i,chr(temp))
    else:
        code.insert(i,chr(temp))
Output:
Display the encrypted word
 

Algorithm:
Step1. Get the word from the user.
Step2. Remove the carriage from the word
Step3. Get a positive integer
Step4. Find the positive equivalent of the number if not positive
Step5. Change number as the remainder when number is divided by 26
Step6. Initialize code as an empty list and i as 0.
Step7. Repeat till i is less than length of the word
Step7.1 assign temp as ord(word[i]) + number ord() is used to covert the character in its decimal equivalent using ASCII or UNICODE conversions.
Step7.2 check if temp is greater than 122 if true proceed to 7.2.1 else move on to step 7.3
Step7.2.1 assign temp as the difference between temp and 122
Step7.2.2 assign temp as the sum of temp and 96
Step7.2.3 insert the character equivalent of temp in the ith index of code.
Step7.3 if temp is not greater than 122 then insert the character equivalent of temp at the ith index of code.
Step8. Display the encrypted code.

Program:
word = input()
word.rstrip()
number = int(input())
number = abs(number)
number = number%26
code = []
for i in range(len(word)):
    temp = ord(word[i])+ number
    if temp>122:
        temp = temp - 122
        temp = 96 + temp
        code.insert(i,chr(temp))
    else:
        code.insert(i,chr(temp))
for i in code:
    print(i,end='')

Issue:
If the expected output does not come then just change 'for i in range(len(word)):'
to for i in range(len(word)-1):

Flowchart:


To edit this flowchart click on this

Problem Set 3(Keyboard Line)

Problem Set 3


Q.Given an  English word,  write an algorithm and the subsequent Python code to check if the given word can be typed using just a single row of the keyboard. (e.g. POTTER, EQUITY). Print 'Yes' if the letters of the word are from a single row and print 'No' otherwise.
Input format:
A word
Output format:
Print ‘Yes’ if all letters of the word are from same row in a keyboard

Input
The word(word)

Processing
row = 0
for i in range(length):
    if word[i] in 'qwertyuiopQWERTYUIOP':
        if row in [2,3]:
            flag = False
            break
        row = 1
    elif word[i] in 'asdfghjklASDFGHJKL':
        if row in [1,3]:
            flag = False
            break
        row = 2
    elif word[i] in 'zxcvbnmZXCVBNM':
        if row in [1,2]:
            flag = False
            break
        row = 3
Output:
Display Yes if the all words are from the same line on the keyboard
Else display No
 

Algorithm:
Step1. Get the word from the user(word)
Step2. Find the length of the word and store it in variable ‘length’
Step3. Initialize flag as True and row as zero
Step4. Repeat till i is less than length of the word(word)
Step4.1 Check if the ith letter of the word is in the first line of the keyboard if true proceed to Step4.1.1 else proceed to Step4.2
Step4.1.1 check if value of row is equal to 2 or 3 if yes then initialize flag as false and proceed to step 5
Step4.2 Check if the ith letter of the word is in the second line of the keyboard if true proceed to Step4.2.1 else proceed to Step4.3
Step4.2.1 check if value of row is equal to 1 or 3 if yes then initialize flag as false and proceed to step 5
Step4.3 Check if the ith letter of the word is in the third line of the keyboard if true proceed to Step4.3.1
Step4.1.1 check if value of row is equal to 1 or 2 if yes then initialize flag as false and proceed to step 5
Step5. Check if flag is true if yes then display ‘Yes’ else display ‘No’

Step6. End 

Program:
word = input()
length = len(word)
flag = True
row = 0
for i in range(length):
    if word[i] in 'qwertyuiopQWERTYUIOP':
        if row in [2,3]:
            flag = False
            break
        row = 1
    elif word[i] in 'asdfghjklASDFGHJKL':
        if row in [1,3]:
            flag = False
            break
        row = 2
    elif word[i] in 'zxcvbnmZXCVBNM':
        if row in [1,2]:
            flag = False
            break
        row = 3
if flag == True:
    print("Yes")
else:
    print("No")

Flowchart:
To edit this flowchart click on this

About Problem Set 3 and the blog



Problem Set 3 is ought to be delayed. This was what the teacher told. Dont worry i'll upload the solutions in two days or so.
I want you guyz who read my blog to open this page and comment on how i could improve my blog for the upcoming problem sets as now onwards this would contain python programs and moreover plz inform when would the faculty start teaching you python so that i could upload the solutions as soon as possible. Plz comment to this post.

Now You Guyz dont have to register to comment so plz comment freely. Your feedback is really important for me imporve my blog

Bonus Practice Problems(GMT To IST)

IST (Indian Standard Time) is 5 hours 30 minutes ahead of GMT(Greenwich Mean Time). Develop an algorithm and write the Python code to find...