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

Problem Set2(Digit in a factor)

Problem Set2

Digits in a factor

Q. Given a number ‘n’, design an algorithm and write the Python program to  print the digits of ‘n’ that  divides ‘n’. Print the digits in reverse order of their appearance in the number ‘n’.  For example, if n is 122 then print 2, 2, 1. Use only conditional and iterative statements to write the code. If none of the digits divide the number, then print ‘No factors’

Input Format
A number, n 

Output Format
Digits in the number ‘n’ that divides ‘n’ in reverse order

Input:
Get the input number and store it in num1

Processing:
let temp = num1
let temp1 = 0
let count = 0
while temp!=0
  temp1 = temp%10
  if num1%temp1 == 0
   print(temp1)
   count+=1

Output:
Print the factors of the number in the reverse order if any else display no factors.

Algorithm:
Step1. Get the number and store it in variable n.
Step2. Assign count as 0 and let copy as n.
Step3. Repeat till copy is not equal to zero
Step3.1 Let temp as copy%10
Step3.2 if  the remainder obtained when n is divided by temp is zero
Step3.2.1 if true display temp and increment count by 1 
Step4. if count is equal to zero then display no factor
Step5. End

Flowchart:


To edit this click on this

Program:
n=int(input())
temp = n
count = 0
length = len(str(n))
for i in range(length):
 factor=temp%10
 if(n%factor==0):
  print(factor)
  count+=1
 temp=temp//10
if(count==0):
    print('No factors')

Problem Set2(Coprimes)

  1. Given two numbers ‘m’ and ‘n’, design an algorithm and write the Python code to check if they are relatively prime. Two integers ‘a’ and ‘b’ are said to be relatively prime, mutually prime, or coprime, if the only positive integer that divides both of them is 1. For example, 14 and 15 are coprime since the factors of 14 are 1, 2, 7, & 14 and factors of 15 are 1, 3, 5, & 15 and there is no common factor other than 1. (Use only conditional and iterational statements for designing the algorithm and implementation).



Input

Get the numbers m,n



Processing Involved

initialize factor as 1

initialize i as 1

if m is less than n then let min = m

else min = m

while factor is equal to 1 and i is less than or equal to min

if m % i is equal to 0 and n%i is equal to 0 then

factor is equal to i

outside if

i = i +1



Output

if factor is not equal to 1 then display that the two given numbers are not co-prime

else if factor is 1 then display the given numbers are coprime

Algorithm:

Step1. Get the numbers m,n

Step2. initialize factor as 1 and i as 1

Step3. find if m is smaller than n if yes then let min as m else assign min as n

Step4. Repeat while factor is equal to 1 and i is less than or equal to min

Step4.1 check if m % i is equal to 0 and n%i is equal to 0 then factor is equal to i

Step4.2 i = i +1

Step5. if factor is 1 then display the two numbers are coprimes else display that the two numbers are not coprimes

Step6. End

Program:

Program:
m = eval(input())
n = eval(input())
i = 1
factor = 1
if m >= n:
min = n
else:
min = m
while i <= min and factor == 1:
if m%i==0 and n%i == 0:
factor = i
i += 1
if factor == 1:
print("Coprime")
else:
print("Not coprime")

Flowchart

CoprimeDupl.png

To edit the flowchart click on this

Practice Problems 2(Water in a dam)

Water in a dam



  1. A city has a dam of capacity ‘x’ litres, water comes to the dam from ‘n’ places.  Given the value of ‘n’ and the quantity of water (in litres and millilitres) that comes from ‘n’ places, write an algorithm and the corresponding Python code to determine the total amount of water in the dam. Assume that the total quantity of water in the dam, will be always less than the capacity of the dam. For example, if there are three places from which water comes to the dam and the water from place 1 is 2 litres 500 ml, water from second place is 3 litres 400 ml and water from third place is 1 litre 700 ml then the total quantity of water in dam will be 7 litres 600 ml.



Input:

Get number of places from where water comes and store in variable n

get number of litres from place - i(litre)

get number of mililitres from place - i (mili)

where i goes from 1 to n



Processing:

let total_litre=0

let total_mili = 0

total_litre = total_litre + litre

total_mili = total_mili + mili

if total_mili > 1000 then

total_litre = total_litre + int(total_mili/1000)

total_mili = total_mili%1000



Output:

Display The total litres and the total millilitre



Algorithm:

Step1. Get the number of places from the user and store it in variable n.

Step2. Initialize i as 1 and total_litre and total_mili both as 0

Step3. Repeat until i is less than or equal to n

Step3.1 Get the number of litres of water from place i and store it in variable litre

Step3.2 Get the number of mililitres of water from place i  and store it variable mili_litre

Step3.3 total_litre is equal to the sum of itself and litre

Step3.4 total_mili is equal to the sum itself and mili_litre

Step3.5 increment i by 1

Step4. if total_mili is greater than or equal to 1000

Step4.1 total_litre is the sum of itself and integer division(total_mili/1000)

Step4.2 total_mili is equal to the remainder obtained when total_mili is divided by 1000

Step5. display total litres and total mililitres in dam

Step6. End

Program:

total_litre,total_mili = 0,0
number = eval(input())
for i in range(1,number+1):
    litre = eval(input())
    mili_litre = eval(input())
    total_litre += litre
    total_mili += mili_litre
if total_mili >= 1000:
    total_litre += int(total_mili/1000)
    total_mili = total_mili%1000

print(total_litre)
print(total_mili)

Flowchart:

damdupl.png

To edit this flowchart click on this link

Problem Set2(Pattern)

Pattern



  1. Given ‘n’, the number of rows, design an algorithm and write a Python code to draw a pattern. If n is ‘5’, the pattern looks as shown below:

**

****

******

********

**********



Input:

Get The Number of Rows as the input and store it in n variable



Processing:

i = 1

while i <=n

print ** i times in a line

i  += 1

Output:

Display The pattern according to the input of n



Algorithm:

Step1. Get the number of rows from the user and store it in n variable.

Step2. initialize i as 1

Step3. Repeat until i less than or equal to n

Step3.1 Print ** i times

Step3.2 increment the value of i by one

Step 4. End

Program:
n = eval(input())
for i in range(1,n+1):
  print('**'*i,end='\n')

Flowchart:



Practice Problems 2(Housefly Age)

Q.Houseflies have an approximate life of four weeks. Given the number of days a housefly lived, design an algorithm and write the Python code to determine its approximate age in seconds. Check for boundary conditions and print ‘Invalid input’ if condition is not satisfied. For example, if a housefly lived for 21 days then its approximate age in seconds is 21*24*60*60 is 1814400.



Input:

Get the age of housefly in days and store it in age variable



Processing:

age_seconds = age*24*60*60



Output:

Display the age in seconds



Algorithm:

Step1. Get the age of housefly in days and store it in age variable

Step2. Check if the age is greater than 0 and less than 28 days if yes then proceed to next step if no print ‘invalid input’ and ask the user to enter a valid input

Step3. age in seconds is equal to age * 24 * 60 * 60

Step4. Display the age in seconds

Step5. End

Program:
age = int(input())
if age<0 or age>=28:
    print("Invalid input")
else:  
    age_seconds = age * 24 * 60 *60

    print(age_seconds)


Flowchart:

AgeDupl.png

To edit this flowchart click on this link

Inlab2 SkillRack(Diagonal Length)

Q.Given two adjacent vertices of a square, write an algorithm and the subsequent python code to determine the length of the diagonal. Pythagorean theorem states that the square of the hypotenuse  is equal to the sum of the square of the other two sides.

Input Format:

X - co-ordinate of the first vertex of the square

Y - co-ordinate of the first vertex of the square

X - co-ordinate of the second vertex of the square

Y - co-ordinate of the second vertex of the square

Output Format:

The other two vertices of the square as a tuple,one in each line

Diagonal of the square with two decimal places only.

Algorithm:

Step1. Get the input of x,y coordinates of the of the adjacent two vertices and store it in x1,y1,x2,y2 variables

Step2. Check if x1 == x2 if true proceed to step2.1 else proceed to step3

Step2.1 length of side(distance) is equal to y2 - y1

Step2.2 length of diagonal(diag) is square root of 2 multiplied by distance

Step2.3 let x3 is equal to the sum of x2 and distance

Step2.4 let x4 is equal to x3

Step2.5 let y3 be equal to y2 and y4 be equal to y1

Step3.check if y1==y2 if true proceed to step3.1

Step3.1 distance is equal to the difference of x2 and x1

Step3.2 length of diagonal(diag) is square root of 2 multiplied by distance

Step3.3 let y3 is equal to the sum of y2 and distance

Step3.4 let y4 is equal to y3

Step3.5 let x3 be equal to x2 and x4 be equal to x1

Step4. Display (x3,y3) and (x4,y4)

Step5. Display the length of the diagonal (stored in variable diag)

Step6. End



Program:

https://gist.github.com/kausthub13/e6c72601b0c2693fb95b1fb088e0e9c8



Output:

Screenshot (1).png

SkillCopy (To enable copy paste functions in SkillRack)

SkillCopy is an extension available in google chrome which can be download

Problem Set2(Roman Numeral)

Q.Given a letter in Roman numeral, develop an algorithm and write the Python code to print the value of it. If some other letter other than Roman numeral is given as input then print ‘Enter a roman numeral’ and terminate.

Algorithm:

Step1.Get A Roman numeral from the user and store it as roman variable.

Step2.Check if roman is equal to 'I' then display 1

Step3. Else if roman is equal to 'V' then display 5

Step4. Else if roman is equal to 'X' then display 10

Step5. Else if roman is equal to 'L' then display 50

Step6. Else if roman is equal to 'C' then display 100

Step7. Else if roman is equal to 'D' then display 500

Step8. Else roman is equal to 'M' then display 1000

Step9. Else display Enter a Roman Numeral

Step10. End

Program:
roman = input()
if roman == 'I':
    print(1)
elif roman == 'V':
    print(5)
elif roman == 'X':
    print(10)
elif roman == 'L':
    print(50)
elif roman == 'C':
    print(100)
elif roman == 'D':
    print(500)
elif roman == 'M':
    print(1000)
else:
    print("Enter a roman numeral")


Flowchart:

to edit this flowchart click on this


Skillcopy(to enable copy paste in skillrack)

Skillcopy is the extension available in google chrome to enable skillcopy in skillrack.

To enable it go to Google chrome

Click on this link

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...