3
\$\begingroup\$

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) + "--")
asked Apr 21, 2019 at 23:32
\$\endgroup\$
1
  • \$\begingroup\$ Is it a "hard" requirement to support Python 2? Or can you upgrade? \$\endgroup\$ Commented Apr 22, 2019 at 19:21

2 Answers 2

3
\$\begingroup\$

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

answered Apr 22, 2019 at 9:10
\$\endgroup\$
5
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Apr 22, 2019 at 19:21
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Apr 22, 2019 at 19:23
  • \$\begingroup\$ Furthermore, your input isn't equivalent. You're asking the user to input a tuple, instead of inputting the height and width individually. \$\endgroup\$ Commented Apr 22, 2019 at 19:55
  • \$\begingroup\$ @David White wanted to make the code as small as possible. And of course, I added Customize the code as you wish! :D \$\endgroup\$ Commented Apr 23, 2019 at 7:20
  • \$\begingroup\$ 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 \$\endgroup\$ Commented Apr 23, 2019 at 7:22
5
\$\begingroup\$

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))
answered Apr 22, 2019 at 19:53
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.