Question

[EC1-1] (patterns3.py) More fun with * patterns... Each of these is worth 0.25 points, except for...

[EC1-1] (patterns3.py) More fun with * patterns... Each of these is worth 0.25 points, except for (f) and (g), which are worth 1 point each.

Write a program that reads an int N >= 0, then prints out each of the following patterns of *. Here, you may use the * repetition operator, if you wish. Also, each pattern must be output via a single function call to the given named function with single parameter N. Examples for N==4 follow, with explanations of how the displayed diagram reflects this value of N:

(a) def solid_diamond(N) => 2*N+1 lines, first with 1 star, second with 3 stars, ..., N+1 line with 2*N+1 stars, then subsequent lines repeating the pattern, reversed; each line's stars are centered between first and Nth columns:

(b) def hollow_diamond(N) => same as above, but with no interior stars (no 2 stars adjacent on same line):


(c) def checkerboard(N) => "Checkerboard" of * and - of side N: first row starts with *, then alternates - and d. Second row starts with -, then alternates * and -. Thus, adjacent lines start with alternating symbols.

(d) def vee(N) => "V" with N rows:

(e) def x(N) => "X" with 2*N-1 rows:

(f) "Nested squares" with outer side length of 2*N+1 and each inner square spaced as shown. Note that any two *'s, each from a different square, are neither adjacent on the same line nor in the same column:

def nested_squares(N)=> N==4, 2*N+1 == 9: three nested squares

(g) "Nested right triangles" with outer legs each having 2*N+1 *'s and each inner triangle spaced as shown. As in the previous problem, note that any two *'s, each from a different triangle, are neither adjacent on the same line or in the same column on adjacent lines:

def nested_triangles(N)=> N==4, 2*N+1 == 9: two triangles

N==6, 2*N+1 == 13: three triangles

0 0
Add a comment Improve this question Transcribed image text
Answer #1

def solid_diamond(N):

for i in range(2*N+1):

for j in range(-N,N+1,1):

if abs(j) <= i and i <= N:

print('*', end="")

elif abs(j) <= N-i%(N+1)-1 and i > N:

print('*', end="")

else:

print(end=" ")

print()

def hollow_diamond(N):

for i in range(2*N+1):

for j in range(-N,N+1,1):

if abs(j) == i and i <= N:

print('*', end="")

elif abs(j) == N-i%(N+1)-1 and i > N:

print('*', end="")

else:

print(end=" ")

print()

def checkerboard(N):

for i in range(N):

for j in range(N):

if i%2 == 0:

if j%2 == 0:

print('*',end=' ')

else:

print('-',end=' ')

else:

if j%2 == 0:

print('-',end=' ')

else:

print('*',end=' ')

print()

def vee(N):

for i in range(N-1,-1,-1):

for j in range(N-1,-N,-1):

if i == abs(j):

print('*',end='')

else:

print(end=' ')

print()

def x(N):

for i in range(N-1,-N,-1):

for j in range(N-1,-N,-1):

if abs(i) == abs(j):

print('*',end='')

else:

print(end=' ')

print()

n=int(input('Enter N : '))

solid_diamond(n)

print()

hollow_diamond(n)

print()

checkerboard(n)

print()

vee(n)

print()

x(n)

print()

# "Nested squares" and "Nested right triangles" -> I dont understand the problem can you show me the diagram

Add a comment
Know the answer?
Add Answer to:
[EC1-1] (patterns3.py) More fun with * patterns... Each of these is worth 0.25 points, except for...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Nested squares in python programming (f) "Nested squares" with outer side length of 2*N+1 and each inner square spaced as shown. Note that any two s, each from a different square, are separat...

    Nested squares in python programming (f) "Nested squares" with outer side length of 2*N+1 and each inner square spaced as shown. Note that any two s, each from a different square, are separated by at least one blank. That is, each is neither a djacent to the other on the same line nor in the same column: def nested squares (N)-> N-4, 2*N+1-9: three nested squares 立 立ựựự立 立立立立立 立立 立 立立立立立 立 (f) "Nested squares" with outer side length...

  • In Python Trace "Stars.py" by drawing a table that shows the values of relevant variables (row...

    In Python Trace "Stars.py" by drawing a table that shows the values of relevant variables (row and star) and keeping track of the output. This program contains a nested for loop, so for each iteration of the outer loop, the inner loop executes in its entirety: row star 1 1 2 1 2 2 3 1 3 2 3 3 ... ... #******************************************************************** # stars.py # # Demonstrates the use of nested for loops. #******************************************************************** #----------------------------------------------------------------- # Prints a triangle...

  • E COMP 1224 Sec. 1 Spring 2017 Lab 1. I need the solution for question 2...

    E COMP 1224 Sec. 1 Spring 2017 Lab 1. I need the solution for question 2 only please. Thank you I need the solution for question 2 only please. Thank you COMP 1224 Sec. 1 Spring 2017 Lab 1 Your first lab will be similar to the examples discussed in class, covering programming hanction, and recursion. It includes the following tasks Using CdC write loop(s) to control and output a star pattern as follows: giving a integer N (use cin...

  • The program will need to accept two input arguments: the path of the input file and...

    The program will need to accept two input arguments: the path of the input file and the path of the output file. See etc/cpp/example.ifstream.cpp for the basis of how to do this. Once the input and output file paths have been received, the program will need to open the input and output files, read and process each input file line and create a corresponding output file line, close the input and output files, and then create and output some summary...

  • You will implement and test several short recursive methods below. With the proper use of recursi...

    this can be done in one class or two separate classes,in java thanks! You will implement and test several short recursive methods below. With the proper use of recursion, none of these methods should require more than a dozen lines of code. Test all these in the same class. Keep adding methods and testing until all are working 5. A Fractal Pattern Examine this pattern of stars and blanks, and write a recursive method that can generate patterns such as...

  • Can someone finish this class for me? There are comment instructions with what to do in...

    Can someone finish this class for me? There are comment instructions with what to do in the methods already. public class ArrayDemo   {                               public void demonstrateTask1(){                                System.out.println("Demonstrate   Task   1");                                                               int[]   numbers =   null;   //   note   that   numbers   references   nothing   (null)   initially                           ...

  • Question A matrix of dimensions m × n (an m-by-n matrix) is an ordered collection of m × n elemen...

    Question A matrix of dimensions m × n (an m-by-n matrix) is an ordered collection of m × n elements. which are called eernents (or components). The elements of an (m × n)-dimensional matrix A are denoted as a,, where 1im and1 S, symbolically, written as, A-a(1,1) S (i.j) S(m, ). Written in the familiar notation: 01,1 am Gm,n A3×3matrix The horizontal and vertical lines of entries in a matrix are called rows and columns, respectively A matrix with the...

  • This program is C++ You have just been offered your first big software contract. The scope...

    This program is C++ You have just been offered your first big software contract. The scope of work is to create a tool for grade school students to learn their times tables. In this program, you will create a 10 x 10 grid of times tables as a cheat sheet for the students to learn. This will display the values of the times tables all the way from 1x1 to 10x10. Make use of formatting tools like inserting tabs, setwidth(),...

  • Please answer all fill in the blanks, thanks! In the United States, tire tread depth is...

    Please answer all fill in the blanks, thanks! In the United States, tire tread depth is measured in 32nds of an inch. Car tires typically start out with 10/32 to 11/32 of an inch of tread depth. In most states, a tire is legally worn out when its tread depth reaches 2/32 of an inch. A random sample of four tires provides the following data on mileage and tread depth: Mileage (10,000 miles) Tread Depth (32nds of an inch) Tire...

  • In this lab you will convert lab5.py to use object oriented programming techniques. The createList and...

    In this lab you will convert lab5.py to use object oriented programming techniques. The createList and checkList functions in lab5.py become methods of the MagicList class, and the main function of lab6.py calls methods of the MagicList class to let the user play the guessing game.                              A. (4pts) Use IDLE to create a lab6.py. Change the 2 comment lines at the top of lab6.py file: First line: your full name Second line: a short description of what the program...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT