Tuesday 8 August 2017

PPS 3


Practice Problems 3 Fall 17

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

Code:
word=input()
word=list(word)
numbers = sum(c.isdigit() for c in word) #number of digits
words   = sum(c.isalpha() for c in word) #number of words
spaces  = sum(c.isspace() for c in word) #number of spaces
others  = len(word) - numbers - words - spaces #number of special characters
flag=0 #assume
if(len(word)>=8):
if(word[0].isalpha()):
if(numbers>0):
if(others>0):
flag=1
if(flag==1):
print('Valid')
else:
print('Invalid');

******************************************************************************************


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

Code:
word=input().rstrip().lower()
d=int(input())
l=list(word)
for i in range(len(l)):
temp=ord(l[i])+d
if(temp<123):
l[i]=chr(ord(l[i])+d)
else:
l[i]=chr(ord(l[i])+d-122+96)
print(''.join(map(str,l)))


******************************************************************************************

3) 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.
Code:
word=input().lower()
l=list(word)
count=0
for i in range(len(l)):
count=count+l.count(l[i])
if(count==len(l)):
print('Good')
else:
print('Bad')

******************************************************************************************

4) 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.
Code:
from datetime import datetime
t=input().lower()
print(datetime.strptime(t, "%I:%M:%S%p").strftime("%H:%M:%S"))

******************************************************************************************
# strptime => convert string to datetime object
# strftime => create formatted string for given time/date/datetime object according to the specified object
******************************************************************************************

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

Code:
word=input().lower()
l=list(word)
first_row='qwertyuiop'
second_row='asdfghjkl'
third_row='zxcvbnm'
if(all(letter in first_row for letter in word) or all(letter in second_row for letter in word) or all(letter in third_row for letter in word)):
print('Yes')
else:
print('No')


******************************************************************************************
I hope, You guys are smart enough by now to write the Input, Output and Pseudocode by yourself.
THANK YOU FOR VISITING...
Best of luck. 👍
******************************************************************************************

20 comments:

  1. the program was not run succesfully showing that some private hidden cases are failed.plz give reply for my question.

    ReplyDelete
  2. 2nd ques nt working..error in 10th line
    'int' object is not iterable

    ReplyDelete
    Replies
    1. Please send the screenshot, so that the error could be identified.

      Delete
    2. Post the screenshot on https://www.facebook.com/groups/1237814436348104/

      Delete
  3. Can u plss Elaborate the 4th question solution i didnt unerdstand it

    ReplyDelete
    Replies
    1. Inbuilt function has been used

      datetime.date.today().strftime("%Y")
      - Returns the current year (Eg: '2017')
      - strftime() actually pulls out the date from the date_string as a string.


      For more details, visit:
      https://www.guru99.com/date-time-and-datetime-classes-in-python.html

      Delete
  4. Hey,
    U can do those things by now.

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. Please update us with the pseudo code of these problems. It would be a great help.

    ReplyDelete
  7. The skill rack guys are not accepting codes in liu of amundation error.please help.

    ReplyDelete
  8. The code for question number 3 is not working.

    ReplyDelete
    Replies
    1. Please check the code properly. It is working.

      Delete
  9. sometimes skillrack shows "private hidden error" what does it mean?

    ReplyDelete
  10. Can u explain the last sentence of problem 2
    and what does ord() function do

    ReplyDelete
    Replies
    1. In Python, we can use ord('a') and chr(97) to transform a letter to number or transform a number to a letter.

      ord(a) gives the ascii value of 'a'

      For example, in python

      >>>ord("a")

      97

      >>>ord("A")

      65

      >>>chr(97)

      'a'

      >>>chr(90)

      'Z'

      Delete
  11. Thank you for letting me know. I will try to improve if there is really an issue with the codes. Else only God may help you.

    ReplyDelete
  12. bro atleaset post pseudocodes

    ReplyDelete
  13. bro can u pls tell me what's wrong in the tenth line of 2 nd qn

    ReplyDelete