how to print/output overlapping diamonds in python3

  • Last Update :
  • Techknowledgy :

How about this?

n = 4

for i in list(range(1, n + 1)) + list(range(n - 1, 0, -1)):
   rowpattern = (' ' * (n - i) + '* ' * (i) + ' ' * (n - i)) * n
print(rowpattern)

EDIT: Following comment below, this is more accurate:

n = 4

for i in list(range(1, n)) + list(range(n, 0, -1)):
   rowpattern = ' ' * (n - i) + ('* ' * (i
         if i != n
         else i - 1) +
      ' ' * (2 * (n - i) - 2)) * n + '*' * (1
      if i == n
      else 0)
print(rowpattern)

Each loop creates a line as a list of spaces and places part of each of the n diamonds in it before printing the line in one go:

n = int(input()) #for number of diamonds per row

for row in range(n):
   expanse = [' '] * (n + 1) * (n - 1) * 2
spaces_before = n - row - 1
stars = '* ' * (row + 1)
for diamond in range(n):
   prefix = spaces_before + diamond * (n - 1) * 2
expanse[prefix: prefix + len(stars) - 1] = stars
print(''.join(expanse))

for row in range(n - 1):
   expanse = [' '] * (n + 1) * (n - 1) * 2
spaces_before = row + 1
stars = '* ' * (n - row - 1)
for diamond in range(n):
   prefix = spaces_before + diamond * (n - 1) * 2
expanse[prefix: prefix + len(stars) - 1] = stars
print(''.join(expanse))
def print_diamonds(n):

   from functools
import lru_cache

number_of_peaks = n
step_size = (n * 2) - 2

@lru_cache(maxsize = n)
def get_star_indecies(y):
   star_index_offset = 2 * y
begin = star_index_offset
end = (number_of_peaks * step_size) + begin

star_indecies = {
   * range(begin, end, step_size)
}
if y == 0:
   return star_indecies
else:
   return star_indecies | get_star_indecies(y - 1)

for iteration in range(n * 2 - 1):
   y = -abs(iteration - (n - 1)) + (n - 1)
star_indecies = get_star_indecies(y)
line_length = max(star_indecies) + 1
space_offset = n - y - 1
space_padding = " " * space_offset
line = space_padding
for index in range(line_length):
   line += [" ", "*"][index in star_indecies]
print(line)

def main():

   print_diamonds(5)

return 0

if __name__ == "__main__":
   import sys
sys.exit(main())

This is my answer to the proposed problem:

class Diamond(object):
   def __init__(self, size = 3):
   self.size = size
self.max_line_size = (size * 2) + 1
self.max_line = [self.one_line(size)]
self.lines_below = map(self.one_line, range(size - 1, 0, -1))
self.lines_above = self.lines_below[::-1]
self.lines = self.lines_above + self.max_line + self.lines_below

def __str__(self):
   return "\n".join(self.lines)

def one_line(self, num_of_diamonds):
   line_size = (num_of_diamonds * 2) + 1
line = list()
for i in range(1, line_size + 1):
   el = "*"
if (i % 2 == 0)
else " "
line.append(el)
str_line = "".join(line)
return (" " * ((self.max_line_size - line_size) / 2)) + str_line + (" " * ((self.max_line_size - line_size) / 2))

class Diamonds(object):
   def __init__(self, size = 3):
   self.size = size
self.diamond = Diamond(size)

def __str__(self):
   return "\n".join([i[: -1] + (i[2: -1] * (self.size - 1)) for i in self.diamond.lines])

print(Diamonds(3))
print(Diamonds(4))
print(Diamonds(5))

Suggestion : 2

How would you output n overlapping diamonds anycodings_python with each diamond having a height of 2n-1. anycodings_python Here are the required outputs for:,Each loop creates a line as a list of anycodings_python spaces and places part of each of the n anycodings_python diamonds in it before printing the line anycodings_python in one go:,Bash loop over files in directory outputting non existent files,This is my answer to the proposed anycodings_python problem:

n = 3

  * * *
  *
  * * * * *
  *
  * * * * * *
  *
  * * * * *
  *
  * *

n = 4

   * * * *
   *
   * * * * * * *
   *
   * * * * * * * * * * *
   *
   * * * * * * * * * * * *
   *
   * * * * * * * * * * *
   *
   * * * * * * *
   *
   * * *

n = 5

    * * * * *
    *
    * * * * * * * * *
    *
    * * * * * * * * * * * * * *
    *
    * * * * * * * * * * * * * * * * * * *
    *
    * * * * * * * * * * * * * * * * * * * *
    *
    * * * * * * * * * * * * * * * * * * *
    *
    * * * * * * * * * * * * * *
    *
    * * * * * * * * *
    *
    * * * *

This is the code I used:

n = int(input()) #for number of diamonds per row
height = 2 * n - 1

for j in range(1, n + 1): #for printing h no.of diamonds
#from row 1 to middle row
for row in range(1, (height + 1) //2 + 1):
      for spaces in range((height + 1) //2 - row): #print spaces per row
         print(" ", end = "") for stars in range((2 * row) - 1): #print stars per row
         if stars % 2 == 0:
         print("*", end = "")
         else:
            print(" ", end = "")
         print()

         #from middle row to last row
         for row in range((height + 1) //2 + 1, height + 1):
            for spaces in range(row - (height + 1) //2):
               print(" ", end = "") for stars in range((height + 1 - row) * 2 - 1):
               if stars % 2 == 0:
               print("*", end = "")
               else:
                  print(" ", end = "")
               print()

How about this?

n = 4

for i in list(range(1, n + 1)) + list(range(n - 1, 0, -1)):
   rowpattern = (' ' * (n - i) + '* ' * (i) + ' ' * (n - i)) * n
print(rowpattern)

EDIT: Following comment below, this is anycodings_python more accurate:

n = 4

for i in list(range(1, n)) + list(range(n, 0, -1)):
   rowpattern = ' ' * (n - i) + ('* ' * (i
         if i != n
         else i - 1) +
      ' ' * (2 * (n - i) - 2)) * n + '*' * (1
      if i == n
      else 0)
print(rowpattern)

Each loop creates a line as a list of anycodings_python spaces and places part of each of the n anycodings_python diamonds in it before printing the line anycodings_python in one go:

n = int(input()) #for number of diamonds per row

for row in range(n):
   expanse = [' '] * (n + 1) * (n - 1) * 2
spaces_before = n - row - 1
stars = '* ' * (row + 1)
for diamond in range(n):
   prefix = spaces_before + diamond * (n - 1) * 2
expanse[prefix: prefix + len(stars) - 1] = stars
print(''.join(expanse))

for row in range(n - 1):
   expanse = [' '] * (n + 1) * (n - 1) * 2
spaces_before = row + 1
stars = '* ' * (n - row - 1)
for diamond in range(n):
   prefix = spaces_before + diamond * (n - 1) * 2
expanse[prefix: prefix + len(stars) - 1] = stars
print(''.join(expanse))
def print_diamonds(n):

   from functools
import lru_cache

number_of_peaks = n
step_size = (n * 2) - 2

@lru_cache(maxsize = n)
def get_star_indecies(y):
   star_index_offset = 2 * y
begin = star_index_offset
end = (number_of_peaks * step_size) + begin

star_indecies = {
   * range(begin, end, step_size)
}
if y == 0:
   return star_indecies
else:
   return star_indecies | get_star_indecies(y - 1)

for iteration in range(n * 2 - 1):
   y = -abs(iteration - (n - 1)) + (n - 1)
star_indecies = get_star_indecies(y)
line_length = max(star_indecies) + 1
space_offset = n - y - 1
space_padding = " " * space_offset
line = space_padding
for index in range(line_length):
   line += [" ", "*"][index in star_indecies]
print(line)

def main():

   print_diamonds(5)

return 0

if __name__ == "__main__":
   import sys
sys.exit(main())

This is my answer to the proposed anycodings_python problem:

class Diamond(object):
   def __init__(self, size = 3):
   self.size = size
self.max_line_size = (size * 2) + 1
self.max_line = [self.one_line(size)]
self.lines_below = map(self.one_line, range(size - 1, 0, -1))
self.lines_above = self.lines_below[::-1]
self.lines = self.lines_above + self.max_line + self.lines_below

def __str__(self):
   return "\n".join(self.lines)

def one_line(self, num_of_diamonds):
   line_size = (num_of_diamonds * 2) + 1
line = list()
for i in range(1, line_size + 1):
   el = "*"
if (i % 2 == 0)
else " "
line.append(el)
str_line = "".join(line)
return (" " * ((self.max_line_size - line_size) / 2)) + str_line + (" " * ((self.max_line_size - line_size) / 2))

class Diamonds(object):
   def __init__(self, size = 3):
   self.size = size
self.diamond = Diamond(size)

def __str__(self):
   return "\n".join([i[: -1] + (i[2: -1] * (self.size - 1)) for i in self.diamond.lines])

print(Diamonds(3))
print(Diamonds(4))
print(Diamonds(5))

Suggestion : 3

Return the output speed of the terminal in bits per second. On software terminal emulators it will have a fixed high value. Included for historical reasons; in former times, it was used to write output loops for time delays and occasionally to change interfaces depending on the line speed.,Return True if the terminal has insert- and delete-character capabilities. This function is included for historical reasons only, as all modern software terminal emulators have such capabilities.,Return True if the terminal has insert- and delete-line capabilities, or can simulate them using scrolling regions. This function is included for historical reasons only, as all modern software terminal emulators have such capabilities.,Must be called if the programmer wants to use colors, and before any other color manipulation routine is called. It is good practice to call this routine right after initscr().

import locale
locale.setlocale(locale.LC_ALL, '')
code = locale.getpreferredencoding()

Suggestion : 4

Given the number of rows N, write a program to print the hallow diamond pattern similar to the pattern shown below., 4. Alphabetic SymbolWrite a program to print the right alphabetic triangle up to the given N rows.Input ,The output should be (2*N - 1) rows and (2*N - 1) columns containing the alphabet characters in the hollow diamond pattern.Explanation,For example, if the given number is 5, the pattern should contain 9 rows and 9 columns as shown below.

Given the number of rows N, write a program to print the hallow diamond pattern similar to the pattern shown below.

    A
    B B
    C C
    D D
    E E
    D D
    C C
    B B
    A

For example, if the given number is 5, the pattern should contain 9 rows and 9 columns as shown below.

    A
    B B
    C C
    D D
    E E
    D D
    C C
    B B
    A
# Python 3 program to print a hallow diamond pattern given N number of rows
alphabets = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
   'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
]
# Diamond array
diamond = []

rows = -1
# Prompt user to enter number of rows
while rows < 1 or rows > 26:
   rows = int(input("Enter number of rows N: "))
for i in range(0, rows):
   # Add letters to the diamond
diamond.append("")
for j in range(0, rows - i):
   diamond[i] += " "
diamond[i] += alphabets[i];
if alphabets[i] != 'A':
   # Put spaces between letters
for j in range(0, 2 * i - 1):
   diamond[i] += " "
diamond[i] += alphabets[i]
# Print the first part of the diamond
print(diamond[i])
# Print the second part of the diamond
for i in reversed(range(0, rows - 1)):
   print(diamond[i])