Question: I need some PYTHON help! For this next exercise, you'll need to complete the function gen_pattern, which, when called with a string of length 1,

I need some PYTHON help!

For this next exercise, you'll need to complete the function gen_pattern, which, when called with a string of length 1, will generate an ASCII art pattern of concentric diamonds using those characters. The following are examples of patterns returned by the function:

> print(gen_pattern('X')) X > print(gen_pattern('XY')) ..Y.. Y.X.Y ..Y.. > print(gen_pattern('WXYZ')) ......Z...... ....Z.Y.Z.... ..Z.Y.X.Y.Z.. Z.Y.X.W.X.Y.Z ..Z.Y.X.Y.Z.. ....Z.Y.Z.... ......Z...... 

Note that the function will return the pattern as a string (as opposed to printing it out), and so each line of the pattern should be separated by a newline, written as ' ' in Python. The second pattern above, as returned by gen_pattern, would be ''..Y.. Y.X.Y ..Y..'.

You ought to find the string join and center methods helpful in your implementation. They are demonstrated here:

> '*'.join(['one', 'two', 'three']) 'one*two*three' > '*'.join('abcde') 'a*b*c*d*e' > 'hello'.center(11, '*') '***hello***' 

Complete the gen_pattern function, below:

import unittest tc = unittest.TestCase() tc.assertEqual(gen_pattern('@'), '@') tc.assertEqual(gen_pattern('@%'), '''..%.. %.@.% ..%..''')

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!