Generate an Hour Glass pattern












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
@@@@@@@
@@@@@
@@@
@
@@@
@@@@@
@@@@@@@









share|improve this question











$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










  • $begingroup$
    thank you so much for the correction
    $endgroup$
    – Adji
    Jan 22 at 12:44
















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
@@@@@@@
@@@@@
@@@
@
@@@
@@@@@
@@@@@@@









share|improve this question











$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










  • $begingroup$
    thank you so much for the correction
    $endgroup$
    – Adji
    Jan 22 at 12:44














9












9








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
@@@@@@@
@@@@@
@@@
@
@@@
@@@@@
@@@@@@@









share|improve this question











$endgroup$




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
@@@@@@@
@@@@@
@@@
@
@@@
@@@@@
@@@@@@@






python ascii-art






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 22 at 12:41









Toby Speight

24.4k740114




24.4k740114










asked Jan 22 at 12:11









AdjiAdji

462




462








  • 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










  • $begingroup$
    thank you so much for the correction
    $endgroup$
    – Adji
    Jan 22 at 12:44














  • 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










  • $begingroup$
    thank you so much for the correction
    $endgroup$
    – Adji
    Jan 22 at 12:44








2




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




$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




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




$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












$begingroup$
thank you so much for the correction
$endgroup$
– Adji
Jan 22 at 12:44




$begingroup$
thank you so much for the correction
$endgroup$
– Adji
Jan 22 at 12:44










1 Answer
1






active

oldest

votes


















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.






share|improve this answer











$endgroup$













    Your Answer





    StackExchange.ifUsing("editor", function () {
    return StackExchange.using("mathjaxEditing", function () {
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    });
    });
    }, "mathjax-editing");

    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "196"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f211992%2fgenerate-an-hour-glass-pattern%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    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.






    share|improve this answer











    $endgroup$


















      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.






      share|improve this answer











      $endgroup$
















        16












        16








        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.






        share|improve this answer











        $endgroup$



        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.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 22 at 14:33

























        answered Jan 22 at 14:05









        Mathias EttingerMathias Ettinger

        24.5k33184




        24.5k33184






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Code Review Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            Use MathJax to format equations. MathJax reference.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f211992%2fgenerate-an-hour-glass-pattern%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Probability when a professor distributes a quiz and homework assignment to a class of n students.

            Aardman Animations

            Are they similar matrix