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

3 comments:

  1. Thank you so much for posting these. You're a savior and more importantly the code passes as soon as I type them.

    ReplyDelete
    Replies
    1. You are welcome...I hope you keep coming back....I also hope share my website...Thank You for visiting :)

      Delete
  2. do i get all the answers for questions given by vit university

    ReplyDelete

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