Stefano
Stefano

Reputation: 118

Letters as star pattern

I've written these two functions to return a letters 'A' and 'H' as a stars pattern. Is there any way to print them next each other or should I rewrite the code to print them as one pattern?

rows = 8
cols = 8
sym = '*'
thick = 2

def A(rows, cols, sym, thick):
    tmp = ''
    for i in range(rows):
        for j in range(cols):
            if (i == 0 or i == 1) and j != 1 and j != 0:
                tmp += sym
            elif i > 1 and j == 0 or j == cols - 1:
                tmp += sym * thick
            elif i == 3 or i == 4:
                tmp += sym
            else:
                tmp += ' '
        tmp += '\n'
    return tmp

def H(rows, cols, sym, thick):
    tmp = ''
    for i in range(rows):
        for j in range(cols):
            if j == 0 or j == 7:
                tmp += sym * thick
            if i != 3 and (i != 4):
                tmp += ' '
            elif (i == 3 and j != 7) or (i == 4 and j != 7):
                tmp += sym
        tmp += '\n'
    return tmp


str = H(rows, cols, sym, thick)
str += A(rows, cols, sym, thick)
print(str)

The output is:

the output here

Upvotes: 1

Views: 1796

Answers (2)

KarelZe
KarelZe

Reputation: 1703

Here is alternative solution:

from operator import add

h = H(rows, cols, sym, thick)
a = A(rows, cols, sym, thick)

# horizontal join
res = '\n'.join(map(add, h.split('\n'), a.split('\n')))
print(res)

which yields:

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

The inconsistencies in spacing come from the fact, that you add no padding to the H bar.

Upvotes: 1

Lev M.
Lev M.

Reputation: 6269

There are 3 general solutions:

  1. Add logic to split the returned strings in to lines, than recombine them
  2. Add logic to print the returned strings line by line
  3. Use the pycurses module to create a separate window for each character and print them there.

I will demonstrate method #1 here as it is the simplest:

#you need this if you plan on letters of different height
from itertools import zip_longest

rows = 8
cols = 8
sym = '*'
thick = 2

def A(rows, cols, sym, thick):
    tmp = ''
    for i in range(rows):
        for j in range(cols):
            if (i == 0 or i == 1) and j != 1 and j != 0:
                tmp += sym
            elif i > 1 and j == 0 or j == cols - 1:
                tmp += sym * thick
            elif i == 3 or i == 4:
                tmp += sym
            else:
                tmp += ' '
        tmp += '\n'
    return tmp

def H(rows, cols, sym, thick):
    tmp = ''
    for i in range(rows):
        for j in range(cols):
            if j == 0 or j == 7:
                tmp += sym * thick
            if i != 3 and (i != 4):
                tmp += ' '
            elif (i == 3 and j != 7) or (i == 4 and j != 7):
                tmp += sym
        tmp += '\n'
    return tmp


h = H(rows, cols, sym, thick)
a = A(rows, cols, sym, thick)
s = ""

#if your letters are guaranteed to be same height, you can use regular zip
for line in zip_longest(h.split('\n'), a.split('\n'), fillvalue=''):
    s += line[0] + ' ' + line[1] + '\n'
print(s)

Notice I changed your str var to s because str is a built in function in Python and you should not use it for your own variable names!

Also, all lines in "H" except middle to have a space at the end.
This is a bug in your H function, but should be easy to fix for you.

Upvotes: 2

Related Questions