Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am not passing any of the tests. This is my question. For this next exercise, you'll need to complete the function gen_pattern, which, when

I am not passing any of the tests. This is my question.

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:

def gen_pattern(chars): # YOUR CODE HERE

raise NotImplementedError()

This is the first test

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

This is the 2nd test

import unittest tc = unittest.TestCase() tc.assertEqual(gen_pattern('ABC'), '''....C.... ..C.B.C.. C.B.A.B.C ..C.B.C.. ....C....''')

tc.assertEqual(gen_pattern('#####'), '''........#........ ......#.#.#...... ....#.#.#.#.#.... ..#.#.#.#.#.#.#.. #.#.#.#.#.#.#.#.# ..#.#.#.#.#.#.#.. ....#.#.#.#.#.... ......#.#.#...... ........#........''')

This is the 2nd test

# (2 points) import unittest tc = unittest.TestCase() tc.assertEqual(gen_pattern('abcdefghijklmnop'), '''..............................p.............................. ............................p.o.p............................ ..........................p.o.n.o.p.......................... ........................p.o.n.m.n.o.p........................ ......................p.o.n.m.l.m.n.o.p...................... ....................p.o.n.m.l.k.l.m.n.o.p.................... ..................p.o.n.m.l.k.j.k.l.m.n.o.p.................. ................p.o.n.m.l.k.j.i.j.k.l.m.n.o.p................ ..............p.o.n.m.l.k.j.i.h.i.j.k.l.m.n.o.p.............. ............p.o.n.m.l.k.j.i.h.g.h.i.j.k.l.m.n.o.p............ ..........p.o.n.m.l.k.j.i.h.g.f.g.h.i.j.k.l.m.n.o.p.......... ........p.o.n.m.l.k.j.i.h.g.f.e.f.g.h.i.j.k.l.m.n.o.p........ ......p.o.n.m.l.k.j.i.h.g.f.e.d.e.f.g.h.i.j.k.l.m.n.o.p...... ....p.o.n.m.l.k.j.i.h.g.f.e.d.c.d.e.f.g.h.i.j.k.l.m.n.o.p.... ..p.o.n.m.l.k.j.i.h.g.f.e.d.c.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.. p.o.n.m.l.k.j.i.h.g.f.e.d.c.b.a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p ..p.o.n.m.l.k.j.i.h.g.f.e.d.c.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.. ....p.o.n.m.l.k.j.i.h.g.f.e.d.c.d.e.f.g.h.i.j.k.l.m.n.o.p.... ......p.o.n.m.l.k.j.i.h.g.f.e.d.e.f.g.h.i.j.k.l.m.n.o.p...... ........p.o.n.m.l.k.j.i.h.g.f.e.f.g.h.i.j.k.l.m.n.o.p........ ..........p.o.n.m.l.k.j.i.h.g.f.g.h.i.j.k.l.m.n.o.p.......... ............p.o.n.m.l.k.j.i.h.g.h.i.j.k.l.m.n.o.p............ ..............p.o.n.m.l.k.j.i.h.i.j.k.l.m.n.o.p.............. ................p.o.n.m.l.k.j.i.j.k.l.m.n.o.p................ ..................p.o.n.m.l.k.j.k.l.m.n.o.p.................. ....................p.o.n.m.l.k.l.m.n.o.p.................... ......................p.o.n.m.l.m.n.o.p...................... ........................p.o.n.m.n.o.p........................ ..........................p.o.n.o.p.......................... ............................p.o.p............................ ..............................p..............................''')

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeffrey A. Hoffer Fred R. McFadden

4th Edition

0805360476, 978-0805360479

More Books

Students also viewed these Databases questions

Question

What do you understand by Mendeleev's periodic table

Answered: 1 week ago