The message packer reads a string
You need to implement a Message Packer. The message packer reads a string
Rules:
- adds two chars ZZ to mark the beginning of the message
- add number 0 to mark the end of the message.
- The input string is reversed inside the message.
- If the input string contains char Z then it is replaced by # in the message. The lower case character z is retained
- If the input string contains numeric character 0 then it is replaced by $.
- Only lower case letter ([a-z]), upper case letter ([A-Z]) and numeric characters ([0-9]) are accepted. If the string contains any other characters an error message Invalid String
- Maximum number of characters accepted in the string is 8. If the number exceeds 8 then Exceeded Limit is output
Sample:
- Input : You need to read a string from STDIN.
- Output : Message Packed as per rules above or Invalid String or Exceeded Limit is printed.
- Input: PQRS
- Output: ZZSRQP0
- Input: ABbcZdz
- Output: ZZzd#cbBA0
- Input: X1340yZ
- Output: ZZ#y$431X0
- Input: AB*D1$34
- Output: Invalid String
- Input: ABCD12345
- Output: Exceeded Limit
Program:
def main():
message = raw_input("Enter value :")
count = 0
reverse = ''.join(reversed(message)) # Reversed the string , must need join
lis = list(reverse) # Reverse the string
lengh = len(reverse)
if lengh > 8: # Length should be less than 8
print "Exceeded Limit"
return
count = 0
for char in lis:
if char == 'Z':
lis[count] = '#'
elif char == '0':
lis[count] = '$'
elif not (char.isupper() or char.islower() or char.isdigit()):
print "Invalid String"
return
count = count + 1
lis.append('0')
lis = ['Z'] + lis # Append the Z to the begging of the list
lis = ['Z'] + lis
result = ''.join(lis) # Convert list to the string
print result
main()
Output:
acp_python$ python messagePacker.py
Enter value :ABbcZdz
ZZzd#cbBA0
acp_python$ vi messagePacker.py
acp_python$ python messagePacker.py
Enter value :X1340yZ
ZZ#y$431X0
acp_python$ python messagePacker.py
Enter value :AB*D1$34
Invalid String
acp_python$ python messagePacker.py
Enter value :ABCD12345
Exceeded Limit
No comments:
Post a Comment