# Trellis Generator

I've been tasked with writing a simple trellis generator. The user is prompted with entering the height and width of the desired trellis, and gets a printed result of a trellis with the entered dimensions. I'm looking for code compact improvements, as I was told to use as little code as possible to generate the trellis.

script.py

height = input("Enter EVEN height of trellis: ")
width = input("Enter EVEN width of trellis: ")

print(("--" * width) + "--" )
for i in range(height):
print("|" + ("/\\" * width) + "|")
print("|" + ("\\/" * width) + "|")
print(("--" * width) + "--")

• Is it a "hard" requirement to support Python 2? Or can you upgrade? – Reinderien Apr 22 '19 at 19:21

I really don't get the "Enter EVEN height of trellis: " part.

Enter EVEN height of trellis: 3
Enter EVEN width of trellis: 3
--------
|/\/\/\|
|\/\/\/|
|/\/\/\|
|\/\/\/|
|/\/\/\|
|\/\/\/|
--------


But let's continue.

Assumptions:

1. Shorten means lessen the number of characters
2. Neatness of the code matters

Ways to shorten the code:

• Change height to h and width to w
• Convert ("--" * w) + "--" to "--" * (w+1)
• Join the two inputs using raw_input and map:
h, w = map(int, raw_input("Enter EVEN 'height width' of trellis: ").split())
• Join the two print statements in the for loop:
 print ("|" + "/\\" * w + "|") + "\n" + ("|" + "\\/" * w + "|")

If neatness doesn't matter, remove all spaces and newlines in the code.

My improved version:

h, w = map(int, raw_input("Enter EVEN 'height width' of trellis: ").split())

print "--" * (w+1)

for i in range(h):
print ("|" + "/\\" * w + "|") + "\n" + ("|" + "\\/" * w + "|")

print "--" * (w+1)


Customize the code as you wish!

That's all I can think about. Good luck!

Edit: If python 3.x is allowed:

h, w = map(int, input("Enter EVEN 'height width' of trellis: ").split())
print('--' * (w+1), *(("|" + "/\\" * w + "|") + '\n' + ("|" + "\\/" * w + "|") for _ in range(h)), '--' * (w+1), sep='\n')


I hope it works. I haven't checked it :D

• raw_input should be avoided since it isn't compatible with Python 3. You can do an equivalent operation that will be compatible with both. – Reinderien Apr 22 '19 at 19:21
• I also think that the gymnastics you've done with map are on the whole counterproductive; even if you want to "code golf" this thing there are saner ways. – Reinderien Apr 22 '19 at 19:23
• Furthermore, your input isn't equivalent. You're asking the user to input a tuple, instead of inputting the height and width individually. – Reinderien Apr 22 '19 at 19:55
• @David White wanted to make the code as small as possible. And of course, I added Customize the code as you wish! :D – Sriv Apr 23 '19 at 7:20
• raw_input should be avoided since it isn't compatible with Python 3. I thought about it first too, but the question tag says python 2.x – Sriv Apr 23 '19 at 7:22

Since we're playing code golf, this is passable - but please don't do it in production. It's too confusing. I've taken the liberty of using Python 3 despite the question being tagged for 2; if you really need it for 2 I can provide an alternative (that will be longer).

h, w = (int(input(f'Enter EVEN {dim} of trellis: ')) for dim in ('height', 'width'))
arrow = r'\/'
print('{horz}\n{rows}{horz}'.format(horz='--'*(w + 1),
rows=f'|{arrow[::-1] * w}|\n|{arrow * w}|\n' * h))