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');
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')
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"))
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')
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. 👍
******************************************************************************************
the program was not run succesfully showing that some private hidden cases are failed.plz give reply for my question.
ReplyDeleteFor which question, is it so?
Delete2nd ques nt working..error in 10th line
ReplyDelete'int' object is not iterable
Please send the screenshot, so that the error could be identified.
DeletePost the screenshot on https://www.facebook.com/groups/1237814436348104/
DeleteCan u plss Elaborate the 4th question solution i didnt unerdstand it
ReplyDeleteInbuilt function has been used
Deletedatetime.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
Hey,
ReplyDeleteU can do those things by now.
This comment has been removed by the author.
ReplyDeletePlease update us with the pseudo code of these problems. It would be a great help.
ReplyDeleteKhud se likh
DeleteThe skill rack guys are not accepting codes in liu of amundation error.please help.
ReplyDeleteThe code for question number 3 is not working.
ReplyDeletePlease check the code properly. It is working.
Deletesometimes skillrack shows "private hidden error" what does it mean?
ReplyDeleteCan u explain the last sentence of problem 2
ReplyDeleteand what does ord() function do
In Python, we can use ord('a') and chr(97) to transform a letter to number or transform a number to a letter.
Deleteord(a) gives the ascii value of 'a'
For example, in python
>>>ord("a")
97
>>>ord("A")
65
>>>chr(97)
'a'
>>>chr(90)
'Z'
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.
ReplyDeletebro atleaset post pseudocodes
ReplyDeletebro can u pls tell me what's wrong in the tenth line of 2 nd qn
ReplyDelete