Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jerinisready/779164c998440977c32856e6508c8d4b to your computer and use it in GitHub Desktop.
Save jerinisready/779164c998440977c32856e6508c8d4b to your computer and use it in GitHub Desktop.
Pattern Printing Breakdown lookaside triangles.

PATTERN PRINTING

C                     G
C O                 N G
C O D             I N G
C O D I         D I N G
C O D I N     O D I N G
C O D I N G C O D I N G
C O D I N     O D I N G
C O D I         D I N G
C O D             I N G
C O                 N G
C                     G


Consider it as Boxes
C _ _ _ _ _| _ _ _ _ _ G
C O _ _ _ _| _ _ _ _ N G
C O D _ _ _| _ _ _ I N G
C O D I _ _| _ _ D I N G
C O D I N _| _ O D I N G
C O D I N G| C O D I N G
-----------|------------
C O D I N _| _ O D I N G
C O D I _ _| _ _ D I N G
C O D _ _ _| _ _ _ I N G
C O _ _ _ _| _ _ _ _ N G
C _ _ _ _ _| _ _ _ _ _ G

SOLUTION Part 1.

Consider Upper Segment First.

C                     G
C O                 N G
C O D             I N G
C O D I         D I N G
C O D I N     O D I N G
C O D I N G C O D I N G
SOLUTION Part 2.

Consider Upper Left Triangle First.

C     
C O    
C O D   
C O D I  
C O D I N 
C O D I N G
SOLUTION Part 3.

Consider Put enough spaces with upper left Triangle to make it a square.

C _ _ _ _ _
C O _ _ _ _  
C O D _ _ _ 
C O D I _ _ 
C O D I N _
C O D I N G
SOLUTION Part 4.

Then repeat the same spaces to get it for upper right box.

C _ _ _ _ _ _ _ _ _ _ 
C O _ _ _ _ _ _ _ _ 
C O D _ _ _ _ _ _ 
C O D I _ _ _ _ 
C O D I N _ _ 
C O D I N G 

SOLUTION Part 5.

Then add the corresponding letters.

C _ _ _ _ _ _ _ _ _ _ G
C O _ _ _ _ _ _ _ _ N G
C O D _ _ _ _ _ _ I N G
C O D I _ _ _ _ D I N G
C O D I N _ _ O D I N G
C O D I N G C O D I N G

SOLUTION Part 6.

Let's try that for bottom portion.

C O D I N _ _ O D I N G
C O D I _ _ _ _ D I N G
C O D _ _ _ _ _ _ I N G
C O _ _ _ _ _ _ _ _ N G
C _ _ _ _ _ _ _ _ _ _ G

bhy

__author__ = "@jerinisready"
__doc__ = """Pattern Printing Breakdown."""
def main():
word = "CODING"
length = len(word)
# Top Portion
for i in range(1, length+1):
# line - part one - character
for j in range(0, i):
char = word[j]
print(char, end=' ')
no_of_spaces = length - i
# line - part two - spaces
print("_ " * no_of_spaces, end="")
# line - part three - spaces
print("_ " * no_of_spaces, end="")
# line - part four - character reverse.
# range(start, stop, step)
# range(12, 6, -1) # => [12, 11, 10, 9, 8 ,7]
# i
for j in range(i, 0, -1):
char = word[-j]
# a= 10; => -a => -10
print(char, end=" ")
print()
# Bottom Portion
for i in range(length-1, 0):
# line - part one - character
for j in range(0, i):
char = word[j]
print(char, end=' ')
no_of_spaces = length - i
# line - part two - spaces
print("_ " * no_of_spaces, end="")
# line - part three - spaces
print("_ " * no_of_spaces, end="")
# line - part four - character reverse.
# range(start, stop, step)
# range(12, 6, -1) # => [12, 11, 10, 9, 8 ,7]
# i
for j in range(i, 0, -1):
char = word[-j]
# a= 10; => -a => -10
print(char, end=" ")
print()
if __name__ == "__main__":
"Run Main Program"
main()
__author__ = "@jerinisready"
__doc__ = """Pattern Printing Breakdown."""
def print_line(i, word, length):
# line - part one - character
for j in range(0, i):
char = word[j]
print(char, end=' ')
no_of_spaces = length - i
# line - part two - spaces
print("_ " * no_of_spaces, end="")
# line - part three - spaces
print("_ " * no_of_spaces, end="")
# line - part four - character reverse.
# range(start, stop, step)
# range(12, 6, -1) # => [12, 11, 10, 9, 8 ,7]
# i
for j in range(i, 0, -1):
char = word[-j]
# a= 10; => -a => -10
print(char, end=" ")
print()
def main():
word = "CODING"
length = len(word)
# Top Portion
for i in range(1, length+1):
print_line(i, word, length)
for i in range(length-1, 0, -1):
print_line(i, word, length)
# DRY => Do not Repeat Yourself.
if __name__ == "__main__":
"Run Main Program"
main()
__author__ = "@jerinisready"
__doc__ = """Pattern Printing Breakdown."""
def main():
word = "CODING"
length = len(word) # 6
# Top Portion
for i in (1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1):
# line - part one - character
for j in range(0, i):
char = word[j]
print(char, end=' ')
no_of_spaces = length - i
# line - part two - spaces
print("_ " * no_of_spaces, end="")
# line - part three - spaces
print("_ " * no_of_spaces, end="")
# line - part four - character reverse.
# range(start, stop, step)
# range(12, 6, -1) # => [12, 11, 10, 9, 8 ,7]
# i
for j in range(i, 0, -1):
char = word[-j]
# a= 10; => -a => -10
print(char, end=" ")
print()
if __name__ == "__main__":
"Run Main Program"
main()
__author__ = "@jerinisready"
__doc__ = """Pattern Printing Breakdown."""
def main():
word = "CODING"
length = len(word) # 6
arr = []
for i in range(1, length+1):
arr.append(i)
for i in range(length-1, 1):
arr.append(i)
# Top Portion
for i in arr:
# line - part one - character
for j in range(0, i):
char = word[j]
print(char, end=' ')
no_of_spaces = length - i
# line - part two - spaces
print("_ " * no_of_spaces, end="")
# line - part three - spaces
print("_ " * no_of_spaces, end="")
# line - part four - character reverse.
# range(start, stop, step)
# range(12, 6, -1) # => [12, 11, 10, 9, 8 ,7]
# i
for j in range(i, 0, -1):
char = word[-j]
# a= 10; => -a => -10
print(char, end=" ")
print()
if __name__ == "__main__":
"Run Main Program"
main()
__author__ = "@jerinisready"
__doc__ = """Pattern Printing Breakdown."""
def main():
word = "CODING"
length = len(word) # 6
arr = [i for i in range(1, length+1)] + [i for i in range(length-1, 0, -1)]
# Top Portion
for i in arr:
# line - part one - character
for j in range(0, i):
char = word[j]
print(char, end=' ')
no_of_spaces = length - i
# line - part two - spaces
print("_ " * no_of_spaces, end="")
# line - part three - spaces
print("_ " * no_of_spaces, end="")
# line - part four - character reverse.
# range(start, stop, step)
# range(12, 6, -1) # => [12, 11, 10, 9, 8 ,7]
# i
for j in range(i, 0, -1):
char = word[-j]
# a= 10; => -a => -10
print(char, end=" ")
print()
if __name__ == "__main__":
"Run Main Program"
main()
__author__ = "@jerinisready"
__doc__ = """Pattern Printing Breakdown."""
"""
Just For fun, I have triued to solve the mystery in minumum lines of code. using joins, splices etc.
instead of using loops!
#NB * in list is a python spread operator to spread range objects into a list and return the list.
.
"""
def main():
word = "CODING"
length = len(word)
for i in [*range(1, length+1), *range(length-1, 0, -1)]:
no_of_spaces = length - i
_left_string = word[:i]
left_string = " ".join(_left_string)
spaces = " " * 2 * 2 * no_of_spaces
_right_string = word[-i:]
right_string = " ".join(_right_string)
print(left_string, spaces, right_string)
if __name__ == "__main__":
"Run Main Program"
main()
Part 01.

for i th line, print '0'th to 'i'th characters one by one from variable word

  for j in range(0, i):
      char = word[j]
      print(char, end=' ')
Part 02.

for length - i spaces.

  print("_ " * no_of_spaces, end="")
Part 03.

for length - i spaces.

  print("_ " * no_of_spaces, end="")
Part 04.

for i th line, print '-i'th to '-1'st characters one by one from variable word

  for j in range(i, 0, -1):
      char = word[-j]
      print(char, end=" ")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment