Kraegan Epsilon
Kraegan Epsilon

Reputation: 377

Mirroring something to another side in Python

I'm trying to write a christmas tree in Python, and I'm trying to make it mirror to another side, so the text is mirrored to itself on the left so instead of having one side of the tree i have both sides

tree = "I"


for i in range(12):
    print(tree.ljust(2-i) * i)

Upvotes: 2

Views: 375

Answers (5)

enrico.bacis
enrico.bacis

Reputation: 31524

There are better ways to do this, in fact you don't really need to mirror, you could just adapt the padding on the left, but let's assume that you want a real mirroring, so that each line has the same number of characters.

You should first multiply the string and then justify it. Then you can use a slice operator to reverse the halves ([::-1]).

size = 12
for i in range(1, size):
    half = (tree * i).rjust(size - 1)
    print half + half[::-1]

output

          II
         IIII
        IIIIII
       IIIIIIII
      IIIIIIIIII
     IIIIIIIIIIII
    IIIIIIIIIIIIII
   IIIIIIIIIIIIIIII
  IIIIIIIIIIIIIIIIII
 IIIIIIIIIIIIIIIIIIII
IIIIIIIIIIIIIIIIIIIIII

And remember..

Every time you draw a tree with a pc, you make Don Knuth happy

Merry Christmas Don!

Upvotes: 3

mhawke
mhawke

Reputation: 87134

I prefer pointy trees, and (just to be different) using str.center() over str.rjust():

def tree(height, symbol='I'):
    width = height*2 - 1
    for i in range(height):
        print((symbol * ((i*2)+1)).center(width))

>>> tree(12)
           I           
          III          
         IIIII         
        IIIIIII        
       IIIIIIIII       
      IIIIIIIIIII      
     IIIIIIIIIIIII     
    IIIIIIIIIIIIIII    
   IIIIIIIIIIIIIIIII   
  IIIIIIIIIIIIIIIIIII  
 IIIIIIIIIIIIIIIIIIIII 
IIIIIIIIIIIIIIIIIIIIIII

Upvotes: 0

Henrik
Henrik

Reputation: 2229

it's almost christmas, adding some ornaments?

import random

tree = "I"
for i in range(12):
    row = list(tree*(2*i))
    if( i > 2):
        index = int(random.random() * len(row))
        if( index < len(row) - 1):
            row[index] = "["
            row[index + 1] = "]"

        index2 = int(random.random() * len(row))
        if( index2 != index and index2 != (index + 1)):
            row[index2] = "O"

    print (("".join(row)).rjust(12 + i))

tada:

           II
          IIII
         I[]III
        IIII[]IO
       IIIOIIII[]
      IIIIIIIIO[]I
     III[]IIIIIOIII
    IIIIIOIIIIIIII[]
   IIIIIIIIOIIII[]III
  IIIIOIIIIIIIIIIIIIII
 II[]IIIIIIIIIIOIIIIIII

Upvotes: 1

simonzack
simonzack

Reputation: 20948

You should use rjust instead of ljust since the spaces should be padded on the left. Also you need to double the count since 3 chars don't really align with 2 properly.

tree = "I"
for i in range(12):
    print((tree*(2*i)).rjust(12 + i))

output:

          II
         IIII
        IIIIII
       IIIIIIII
      IIIIIIIIII
     IIIIIIIIIIII
    IIIIIIIIIIIIII
   IIIIIIIIIIIIIIII
  IIIIIIIIIIIIIIIIII
 IIIIIIIIIIIIIIIIIIII
IIIIIIIIIIIIIIIIIIIIII

Upvotes: 2

Sergey Melekhin
Sergey Melekhin

Reputation: 109

Here's my take on it:

l = 13
for i in range(l):
    print(' ' * int((l - i)/2) + ('#' * i))

Upvotes: 0

Related Questions