Pattern in Python












1















I want to draw a diamond pattern (using 1's) in the zeros matrix 80x80. The first half is going really well but I get nothing but 0s in the second half.



img = np.zeros((80,80))

def draw_pic(image):
for i in range(len(image)):
for j in range(len(image[i])):
print(int(image[i][j]), end = '')
print()

def gen_diamond(image):
ret = np.copy(image)
for i in range(len(image)):
for j in range(len(image[i])):
if (i < len(image)/2 and j >= len(image[i])/2 - (i + 1) and j <= len(image[i])/2 + i):
ret[i][j] = 1
if (i > len(image)/2 and j >= len(image[i])/2 - (i + 1)and j <= len(image[i])/2 - i):
ret[i][j] = 1


return ret

draw_pic(gen_diamond(img))









share|improve this question





























    1















    I want to draw a diamond pattern (using 1's) in the zeros matrix 80x80. The first half is going really well but I get nothing but 0s in the second half.



    img = np.zeros((80,80))

    def draw_pic(image):
    for i in range(len(image)):
    for j in range(len(image[i])):
    print(int(image[i][j]), end = '')
    print()

    def gen_diamond(image):
    ret = np.copy(image)
    for i in range(len(image)):
    for j in range(len(image[i])):
    if (i < len(image)/2 and j >= len(image[i])/2 - (i + 1) and j <= len(image[i])/2 + i):
    ret[i][j] = 1
    if (i > len(image)/2 and j >= len(image[i])/2 - (i + 1)and j <= len(image[i])/2 - i):
    ret[i][j] = 1


    return ret

    draw_pic(gen_diamond(img))









    share|improve this question



























      1












      1








      1








      I want to draw a diamond pattern (using 1's) in the zeros matrix 80x80. The first half is going really well but I get nothing but 0s in the second half.



      img = np.zeros((80,80))

      def draw_pic(image):
      for i in range(len(image)):
      for j in range(len(image[i])):
      print(int(image[i][j]), end = '')
      print()

      def gen_diamond(image):
      ret = np.copy(image)
      for i in range(len(image)):
      for j in range(len(image[i])):
      if (i < len(image)/2 and j >= len(image[i])/2 - (i + 1) and j <= len(image[i])/2 + i):
      ret[i][j] = 1
      if (i > len(image)/2 and j >= len(image[i])/2 - (i + 1)and j <= len(image[i])/2 - i):
      ret[i][j] = 1


      return ret

      draw_pic(gen_diamond(img))









      share|improve this question
















      I want to draw a diamond pattern (using 1's) in the zeros matrix 80x80. The first half is going really well but I get nothing but 0s in the second half.



      img = np.zeros((80,80))

      def draw_pic(image):
      for i in range(len(image)):
      for j in range(len(image[i])):
      print(int(image[i][j]), end = '')
      print()

      def gen_diamond(image):
      ret = np.copy(image)
      for i in range(len(image)):
      for j in range(len(image[i])):
      if (i < len(image)/2 and j >= len(image[i])/2 - (i + 1) and j <= len(image[i])/2 + i):
      ret[i][j] = 1
      if (i > len(image)/2 and j >= len(image[i])/2 - (i + 1)and j <= len(image[i])/2 - i):
      ret[i][j] = 1


      return ret

      draw_pic(gen_diamond(img))






      python nested-loops






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 16 '18 at 4:39







      Frankie

















      asked Nov 16 '18 at 0:42









      FrankieFrankie

      164




      164
























          2 Answers
          2






          active

          oldest

          votes


















          0














          Your error is in the range checks in the lower half. Let's look at the arithmetic for row 42 ...



                  if (i > len(image)/2 and
          j >= len(image[i])/2 - (i + 1) and
          j <= len(image[i])/2 - i):


          Substituting the proper values, we have:



                  if (42 > 40 and
          j >= 40 - (42 + 1) and
          j <= 40 - 42):


          That last condition cannot happen: you need to subtract the row number from the midpoint and take the absolute value. Simpler yet, just set your loop values directly to the ranges you need:



          row_mid = len(image) // 2
          col_mid = len(image[0]) // 2

          for row in range(row_mid):
          for col in range(col_mid-row, col_mid+row):
          print(row, col)
          ret[row, col] = 1

          for tmp in range(row_mid):
          row = len(image) - tmp # Work from the bottom up
          for col in range(col_mid-tmp, col_mid+tmp):
          ret[row, col] = 1


          Output for a 10x10 array:



          0000000000
          0000110000
          0001111000
          0011111100
          0111111110
          0000000000
          0111111110
          0011111100
          0001111000
          0000110000


          I'll trust you to adjust the boundary conditions. :-)






          share|improve this answer































            0














            Here is a solution. It works well on even by even and odd x odd image grids.



            img = np.zeros((12,12))


            def draw_pic(image):
            for i in range(len(image)):
            for j in range(len(image[i])):
            print(int(image[i][j]), end = '')
            print()

            def gen_diamond(image):
            ret = np.copy(image)
            for i in range(len(image)):
            for j in range(len(image[i])):
            if (i < len(image)/2 and j >= len(image[i])/2 - (i + 1) and j <= len(image[i])/2 + i):
            ret[i][j] = 1
            if (i >= len(image)/2 and j >= i-len(image[i])/2 and j <= (3/2*len(image[i])-i)-1):
            ret[i][j] = 1


            return ret

            draw_pic(gen_diamond(img))


            Output looks like this for 12x12



            000001100000
            000011110000
            000111111000
            001111111100
            011111111110
            111111111111
            111111111111
            011111111110
            001111111100
            000111111000
            000011110000
            000001100000


            and this for 11 x 11



            00000100000
            00001110000
            00011111000
            00111111100
            01111111110
            11111111111
            01111111110
            00111111100
            00011111000
            00001110000
            00000100000





            share|improve this answer























              Your Answer






              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: "1"
              };
              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: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              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%2fstackoverflow.com%2fquestions%2f53329841%2fpattern-in-python%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              Your error is in the range checks in the lower half. Let's look at the arithmetic for row 42 ...



                      if (i > len(image)/2 and
              j >= len(image[i])/2 - (i + 1) and
              j <= len(image[i])/2 - i):


              Substituting the proper values, we have:



                      if (42 > 40 and
              j >= 40 - (42 + 1) and
              j <= 40 - 42):


              That last condition cannot happen: you need to subtract the row number from the midpoint and take the absolute value. Simpler yet, just set your loop values directly to the ranges you need:



              row_mid = len(image) // 2
              col_mid = len(image[0]) // 2

              for row in range(row_mid):
              for col in range(col_mid-row, col_mid+row):
              print(row, col)
              ret[row, col] = 1

              for tmp in range(row_mid):
              row = len(image) - tmp # Work from the bottom up
              for col in range(col_mid-tmp, col_mid+tmp):
              ret[row, col] = 1


              Output for a 10x10 array:



              0000000000
              0000110000
              0001111000
              0011111100
              0111111110
              0000000000
              0111111110
              0011111100
              0001111000
              0000110000


              I'll trust you to adjust the boundary conditions. :-)






              share|improve this answer




























                0














                Your error is in the range checks in the lower half. Let's look at the arithmetic for row 42 ...



                        if (i > len(image)/2 and
                j >= len(image[i])/2 - (i + 1) and
                j <= len(image[i])/2 - i):


                Substituting the proper values, we have:



                        if (42 > 40 and
                j >= 40 - (42 + 1) and
                j <= 40 - 42):


                That last condition cannot happen: you need to subtract the row number from the midpoint and take the absolute value. Simpler yet, just set your loop values directly to the ranges you need:



                row_mid = len(image) // 2
                col_mid = len(image[0]) // 2

                for row in range(row_mid):
                for col in range(col_mid-row, col_mid+row):
                print(row, col)
                ret[row, col] = 1

                for tmp in range(row_mid):
                row = len(image) - tmp # Work from the bottom up
                for col in range(col_mid-tmp, col_mid+tmp):
                ret[row, col] = 1


                Output for a 10x10 array:



                0000000000
                0000110000
                0001111000
                0011111100
                0111111110
                0000000000
                0111111110
                0011111100
                0001111000
                0000110000


                I'll trust you to adjust the boundary conditions. :-)






                share|improve this answer


























                  0












                  0








                  0







                  Your error is in the range checks in the lower half. Let's look at the arithmetic for row 42 ...



                          if (i > len(image)/2 and
                  j >= len(image[i])/2 - (i + 1) and
                  j <= len(image[i])/2 - i):


                  Substituting the proper values, we have:



                          if (42 > 40 and
                  j >= 40 - (42 + 1) and
                  j <= 40 - 42):


                  That last condition cannot happen: you need to subtract the row number from the midpoint and take the absolute value. Simpler yet, just set your loop values directly to the ranges you need:



                  row_mid = len(image) // 2
                  col_mid = len(image[0]) // 2

                  for row in range(row_mid):
                  for col in range(col_mid-row, col_mid+row):
                  print(row, col)
                  ret[row, col] = 1

                  for tmp in range(row_mid):
                  row = len(image) - tmp # Work from the bottom up
                  for col in range(col_mid-tmp, col_mid+tmp):
                  ret[row, col] = 1


                  Output for a 10x10 array:



                  0000000000
                  0000110000
                  0001111000
                  0011111100
                  0111111110
                  0000000000
                  0111111110
                  0011111100
                  0001111000
                  0000110000


                  I'll trust you to adjust the boundary conditions. :-)






                  share|improve this answer













                  Your error is in the range checks in the lower half. Let's look at the arithmetic for row 42 ...



                          if (i > len(image)/2 and
                  j >= len(image[i])/2 - (i + 1) and
                  j <= len(image[i])/2 - i):


                  Substituting the proper values, we have:



                          if (42 > 40 and
                  j >= 40 - (42 + 1) and
                  j <= 40 - 42):


                  That last condition cannot happen: you need to subtract the row number from the midpoint and take the absolute value. Simpler yet, just set your loop values directly to the ranges you need:



                  row_mid = len(image) // 2
                  col_mid = len(image[0]) // 2

                  for row in range(row_mid):
                  for col in range(col_mid-row, col_mid+row):
                  print(row, col)
                  ret[row, col] = 1

                  for tmp in range(row_mid):
                  row = len(image) - tmp # Work from the bottom up
                  for col in range(col_mid-tmp, col_mid+tmp):
                  ret[row, col] = 1


                  Output for a 10x10 array:



                  0000000000
                  0000110000
                  0001111000
                  0011111100
                  0111111110
                  0000000000
                  0111111110
                  0011111100
                  0001111000
                  0000110000


                  I'll trust you to adjust the boundary conditions. :-)







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 16 '18 at 1:04









                  PrunePrune

                  45.4k143559




                  45.4k143559

























                      0














                      Here is a solution. It works well on even by even and odd x odd image grids.



                      img = np.zeros((12,12))


                      def draw_pic(image):
                      for i in range(len(image)):
                      for j in range(len(image[i])):
                      print(int(image[i][j]), end = '')
                      print()

                      def gen_diamond(image):
                      ret = np.copy(image)
                      for i in range(len(image)):
                      for j in range(len(image[i])):
                      if (i < len(image)/2 and j >= len(image[i])/2 - (i + 1) and j <= len(image[i])/2 + i):
                      ret[i][j] = 1
                      if (i >= len(image)/2 and j >= i-len(image[i])/2 and j <= (3/2*len(image[i])-i)-1):
                      ret[i][j] = 1


                      return ret

                      draw_pic(gen_diamond(img))


                      Output looks like this for 12x12



                      000001100000
                      000011110000
                      000111111000
                      001111111100
                      011111111110
                      111111111111
                      111111111111
                      011111111110
                      001111111100
                      000111111000
                      000011110000
                      000001100000


                      and this for 11 x 11



                      00000100000
                      00001110000
                      00011111000
                      00111111100
                      01111111110
                      11111111111
                      01111111110
                      00111111100
                      00011111000
                      00001110000
                      00000100000





                      share|improve this answer




























                        0














                        Here is a solution. It works well on even by even and odd x odd image grids.



                        img = np.zeros((12,12))


                        def draw_pic(image):
                        for i in range(len(image)):
                        for j in range(len(image[i])):
                        print(int(image[i][j]), end = '')
                        print()

                        def gen_diamond(image):
                        ret = np.copy(image)
                        for i in range(len(image)):
                        for j in range(len(image[i])):
                        if (i < len(image)/2 and j >= len(image[i])/2 - (i + 1) and j <= len(image[i])/2 + i):
                        ret[i][j] = 1
                        if (i >= len(image)/2 and j >= i-len(image[i])/2 and j <= (3/2*len(image[i])-i)-1):
                        ret[i][j] = 1


                        return ret

                        draw_pic(gen_diamond(img))


                        Output looks like this for 12x12



                        000001100000
                        000011110000
                        000111111000
                        001111111100
                        011111111110
                        111111111111
                        111111111111
                        011111111110
                        001111111100
                        000111111000
                        000011110000
                        000001100000


                        and this for 11 x 11



                        00000100000
                        00001110000
                        00011111000
                        00111111100
                        01111111110
                        11111111111
                        01111111110
                        00111111100
                        00011111000
                        00001110000
                        00000100000





                        share|improve this answer


























                          0












                          0








                          0







                          Here is a solution. It works well on even by even and odd x odd image grids.



                          img = np.zeros((12,12))


                          def draw_pic(image):
                          for i in range(len(image)):
                          for j in range(len(image[i])):
                          print(int(image[i][j]), end = '')
                          print()

                          def gen_diamond(image):
                          ret = np.copy(image)
                          for i in range(len(image)):
                          for j in range(len(image[i])):
                          if (i < len(image)/2 and j >= len(image[i])/2 - (i + 1) and j <= len(image[i])/2 + i):
                          ret[i][j] = 1
                          if (i >= len(image)/2 and j >= i-len(image[i])/2 and j <= (3/2*len(image[i])-i)-1):
                          ret[i][j] = 1


                          return ret

                          draw_pic(gen_diamond(img))


                          Output looks like this for 12x12



                          000001100000
                          000011110000
                          000111111000
                          001111111100
                          011111111110
                          111111111111
                          111111111111
                          011111111110
                          001111111100
                          000111111000
                          000011110000
                          000001100000


                          and this for 11 x 11



                          00000100000
                          00001110000
                          00011111000
                          00111111100
                          01111111110
                          11111111111
                          01111111110
                          00111111100
                          00011111000
                          00001110000
                          00000100000





                          share|improve this answer













                          Here is a solution. It works well on even by even and odd x odd image grids.



                          img = np.zeros((12,12))


                          def draw_pic(image):
                          for i in range(len(image)):
                          for j in range(len(image[i])):
                          print(int(image[i][j]), end = '')
                          print()

                          def gen_diamond(image):
                          ret = np.copy(image)
                          for i in range(len(image)):
                          for j in range(len(image[i])):
                          if (i < len(image)/2 and j >= len(image[i])/2 - (i + 1) and j <= len(image[i])/2 + i):
                          ret[i][j] = 1
                          if (i >= len(image)/2 and j >= i-len(image[i])/2 and j <= (3/2*len(image[i])-i)-1):
                          ret[i][j] = 1


                          return ret

                          draw_pic(gen_diamond(img))


                          Output looks like this for 12x12



                          000001100000
                          000011110000
                          000111111000
                          001111111100
                          011111111110
                          111111111111
                          111111111111
                          011111111110
                          001111111100
                          000111111000
                          000011110000
                          000001100000


                          and this for 11 x 11



                          00000100000
                          00001110000
                          00011111000
                          00111111100
                          01111111110
                          11111111111
                          01111111110
                          00111111100
                          00011111000
                          00001110000
                          00000100000






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 16 '18 at 1:26









                          James FultonJames Fulton

                          1825




                          1825






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • 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.


                              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%2fstackoverflow.com%2fquestions%2f53329841%2fpattern-in-python%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

                              Bressuire

                              Vorschmack

                              Quarantine