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) + "--")
-
\$\begingroup\$ Is it a "hard" requirement to support Python 2? Or can you upgrade? \$\endgroup\$Reinderien– Reinderien2019年04月22日 19:21:49 +00:00Commented Apr 22, 2019 at 19:21
2 Answers 2
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:
- Shorten means lessen the number of characters
- Neatness of the code matters
Ways to shorten the code:
- Change
height
toh
andwidth
tow
- Convert
("--" * w) + "--"
to"--" * (w+1)
- Join the two inputs using
raw_input
andmap
:
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
-
\$\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\$Reinderien– Reinderien2019年04月22日 19:21:26 +00:00Commented 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\$Reinderien– Reinderien2019年04月22日 19:23:21 +00:00Commented 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\$Reinderien– Reinderien2019年04月22日 19:55:13 +00:00Commented 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\$Sriv– Sriv2019年04月23日 07:20:07 +00:00Commented 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 sayspython 2.x
\$\endgroup\$Sriv– Sriv2019年04月23日 07:22:49 +00:00Commented Apr 23, 2019 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))