9
\$\begingroup\$

Here is my code for making an Hour Glass pattern with odd or even input using Python. I think I could make it simpler.

Here's the output example:

Expected Output

And then, here is my code:

def evenGlassHour(target):
 jsp=1
 jtop=target
 jbot=2
 jbotspace=int(target/2)
 eventarget=int(target/2)
 temp=""
 for i in range(eventarget):
     for j in range(i):
         temp+=" "
     for jsp in range(jtop):
         temp+="@"
     jtop-=2
     temp+="\n"
 for i in range(eventarget-1):
     for j in range(jbotspace-2):
         temp+=" "
     for j in range(jbot+2):
         temp+="@"
     jbot+=2
     jbotspace-=1
     temp+="\n"

 print(temp)

def oddGlassHour(target):
 jsp=1
 jtop=target
 jbot=1
 jbotspace=int(target/2)
 oddtarget=int(target/2)
 temp=""
 for i in range(oddtarget):
     for j in range(i):
         temp+=" "
     for jsp in range(jtop):
         temp+="@"
     jtop-=2
     temp+="\n"
 for i in range(oddtarget+1):
     for j in range(jbotspace):
         temp+=" "
     for j in range(jbot):
         temp+="@"
     jbot+=2
     jbotspace-=1
     temp+="\n"

 print(temp)

target=int(input("Input : "))

if(target%2==0):
 evenGlassHour(target)
else:
 oddGlassHour(target)

And this is the result from my code:

 Input : 6
 @@@@@@
  @@@@
   @@
  @@@@
 @@@@@@

 Input : 7
 @@@@@@@
  @@@@@
   @@@
    @
   @@@
  @@@@@
 @@@@@@@
\$\endgroup\$
  • 2
    \$\begingroup\$ Welcome to Code Review! I changed the title so that it describes what the code does per site goals: "State what your code does in your title, not your main concerns about it.". Please check that I haven't misrepresented your code, and correct it if I have. \$\endgroup\$ – Toby Speight Jan 22 at 12:41
  • 3
    \$\begingroup\$ I've also edited to remove your request for Java code, as that's off-topic here; we simply review your code. We might make concrete suggestions, but we don't write code to order! \$\endgroup\$ – Toby Speight Jan 22 at 12:42
16
\$\begingroup\$

Read PEP8, it will give you directions on how to write Python code that look like Python code to other.

Other than that, the behaviour you're seeking is already implemented in range:

>>> a = range(7, 0, -2)
>>> list(a)
[7, 5, 3, 1]

You just need to reverse it to form the full hourglass:

>>> a = range(7, 0, -2)
>>> list(a) + list(reversed(a))
[7, 5, 3, 1, 1, 3, 5, 7]

And remove the repeated center:

>>> decreasing = range(7, 0, -2)
>>> increasing = reversed(decreasing)
>>> next(increasing)  # Remove duplicated center
>>> list(decreasing) + list(increasing)
[7, 5, 3, 1, 3, 5, 7]

Now you can make use of itertools.chain instead of list concatenation and turn your function into a generator to separate computation from printing.

import itertools


def hourglass(size):
    decreasing = range(size, 0, -2)
    increasing = reversed(decreasing)
    next(increasing, None)  # Remove duplicated center
    for length in itertools.chain(decreasing, increasing):
        yield '{:^{}}'.format('@' * length, size)


if __name__ == '__main__':
    for i in range(20):  # Test our function
        print('\n'.join(hourglass(i)), end='\n---\n')

Note that if you are using Python 3.6+ you can format the line using

yield f'{"@" * length:^{size}}'

instead which may be slightly more readable.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.