Play with python-The Doormat Problem
Yesterday I was solving some problem in "HackerRank" as i was coding in python. I came across a problem that i initially thought was boring and time consuming. But after some moments i realised that it was quite interesting. It was a problem regarding designing a Doormat. Though it is a beginner level problem, but we can solve a lot of similar type problems if we understand how its done. So i am sharing this problem and its solution:
Problem:
Mr. Vincent works in a door mat manufacturing company. One day, he designed a new door mat with the following specifications:
Solution:
Problem:
Mr. Vincent works in a door mat manufacturing company. One day, he designed a new door mat with the following specifications:
- Mat size must be X. ( is an odd natural number, and is times .)
- The design should have 'WELCOME' written in the center.
- The design pattern should only use |, . and - characters.
Sample Designs:
Input Format:
A single line containing the space separated values of and .
Explanation:
Line 1: It takes the dimension of the doormat as space separated input, like "9 27"or"10 30" etc. This input is taken as string. But it is useless unless we seperate this two numbers and convert them into integer. So used split(), which splits the number. and these numbers are stored in "a" as list in string format.
Line 2: The first element of "a" is stored in "m" in integer format.
Line 3: The second element of "a" is stored in "n" in integer format.
Line 4: In c we stored the index of the mid line of the length. This is where we will print "Welcome". So we saved the position beforehand for later use.
Line 5: We stored ".|." in a variable as we have to use it many times later.
Line 6: It is a loop for printing ".|." in definite pattern in an increasing order before printing welcome.
Line 7,8: Here is the formula stating how many times ".|." will be printed in each line. In first line ".|." will be printed once(as i=0,2*i+1=1), in second line ".|." will be printed thrice (i=1,2*i+1=3) etc.
Line 9: Prints "Welcome in the middle line of its length.
Line 10-12: Prints ".|." in the same pattern as line 7,8 in descending order.
Output:
So, this is how I solved this problem. We can also solve it in more easier ways. Try to find different ways of solving problems like this. With similar types of logic you can also print the shape of a diamond, plus or any other simple logos. Try to find out and have fun.
Comments
Post a Comment