{"id":1114,"date":"2009-01-27T13:18:12","date_gmt":"2009-01-27T20:18:12","guid":{"rendered":"http:\/\/www.gubatron.com\/blog\/?p=1114"},"modified":"2009-01-27T13:18:12","modified_gmt":"2009-01-27T20:18:12","slug":"using-a-linear-array-as-a-bidimensional-matrix","status":"publish","type":"post","link":"https:\/\/www.gubatron.com\/blog\/using-a-linear-array-as-a-bidimensional-matrix\/","title":{"rendered":"Using a linear array as a bidimensional matrix"},"content":{"rendered":"<p>Often times I find the need to use a list or linear array as if it was a table.<\/p>\n<p>Everytime I need to do so, I always end up coding functions to convert a (x,y) coordinate to the real index n in the array.<\/p>\n<p>Let me illustrate, with an example. You have a string that defines the elements of a game board, and you want to work using (x,y) coordinates.<\/p>\n<p>[python]<br \/>\ns=&quot;xxxxx@xx@xxx@@xx&quot;<br \/>\n[\/python]<\/p>\n<p>If you were to look at it as a matrix (width=4), it&#8217;d be something like this<br \/>\n[python]<br \/>\ns=&quot;xxxx<br \/>\n   x@xx<br \/>\n   @xxx<br \/>\n   @@xx&quot;<br \/>\n[\/python]<\/p>\n<p>However, I can&#8217;t do<br \/>\ns[x,y], since it&#8217;s a linear array, it&#8217;s a string.<\/p>\n<p>You need to convert from (x,y) to a number that represents an index in the array.<\/p>\n<p>This is very simple:<br \/>\n[python]<br \/>\nwidth=4<br \/>\ndef getNforXY(x,y):<br \/>\n  return x + width*y<br \/>\n[\/python]<\/p>\n<p>What if you want to do it backwards. What if you need to know what&#8217;s the X and Y for a given index N in the string?<\/p>\n<p>[python]<br \/>\ndef getXYforN(n):<br \/>\n  y = int(n\/width)<br \/>\n  x = n &#8211; width\/y<br \/>\n  return (x,y)<br \/>\n[\/python]<\/p>\n<p>Cheers<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Often times I find the need to use a list or linear array as if it was a table. Everytime I need to do so, I always end up coding functions to convert a (x,y) coordinate to the real index n in the array. Let me illustrate, with an example. You have a string that [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[15,65],"tags":[133,154,1437,654,1458,999,1054],"class_list":["post-1114","post","type-post","status-publish","format-standard","hentry","category-code","category-python","tag-algorithms","tag-arrays","tag-code","tag-matrix","tag-python","tag-tip","tag-utils"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5Unzf-hY","jetpack-related-posts":[{"id":764,"url":"https:\/\/www.gubatron.com\/blog\/python-ip2num-num2ip-store-an-ip-string-as-a-4-byte-int\/","url_meta":{"origin":1114,"position":0},"title":"[Python] ip2num \/ num2ip &#8211; Store an IP string as a 4 byte int.","author":"gubatron","date":"March 27, 2008","format":false,"excerpt":"This is probably everywhere, maybe python also comes with it, but I wanted to have my own implementation, and I'll leave it here for future reference. Basically, sometimes you don't want to store IPs in Strings cause they take too much space, instead you want to be a good programmer\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3922,"url":"https:\/\/www.gubatron.com\/blog\/python-in-functional-style-how-to-add-2-lists-of-integers-without-using-loops\/","url_meta":{"origin":1114,"position":1},"title":"Python in Functional Style: How to add 2 lists of integers without using loops","author":"gubatron","date":"January 27, 2021","format":false,"excerpt":"Usually you'd add a list of integers this way: [pastacode lang=\"python\" manual=\"a%20%3D%20%5B2%2C%202%2C%202%2C%202%5D%0Ab%20%3D%20%5B2%2C%202%2C%202%2C%202%5D%0Ac%20%3D%20%5B%5D%0Afor%20i%20in%20range(len(a))%3A%0A%20c.append(a%5Bi%5D%20%2B%20b%5Bi%5D)\" message=\"\" highlight=\"\" provider=\"manual\"\/] You can do it functionally without any loops in different ways: Using map and a lambda that adds them up [pastacode lang=\"python\" manual=\"c%20%3D%20list(map(lambda%20x%2Cy%3A%20x%2By%2C%20a%2C%20b))\" message=\"\" highlight=\"\" provider=\"manual\"\/] or you can import the add operator as a\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"Close End 10cm 80cm 5# 10pcs white Metal Zipper for Sewing ...","src":"https:\/\/external-content.duckduckgo.com\/iu\/?u=https%3A%2F%2Fae01.alicdn.com%2Fkf%2FHTB1x3GhNFXXXXaGXFXXq6xXFXXXu%2FClose-End-10cm-80cm-5-10pcs-white-Metal-Zipper-for-Sewing-zip-Garment-Accessories-Jeans-Zippers.jpg&f=1&nofb=1","width":350,"height":200},"classes":[]},{"id":2767,"url":"https:\/\/www.gubatron.com\/blog\/ubuntu-packages-for-a-kick-ass-web-server\/","url_meta":{"origin":1114,"position":2},"title":"ubuntu packages for a kick ass web server","author":"gubatron","date":"September 7, 2012","format":false,"excerpt":"Copy and paste the following list on a file, say \"packages.txt\". To install all just do: sudo apt-get install $(cat packages.txt) accountsservice acpid adduser ant ant-optional apache2-utils apparmor apport apport-symptoms apt apt-transport-https apt-utils apt-xapian-index aptitude at base-files base-passwd bash bash-completion bc bind9-host bsdmainutils bsdutils busybox-initramfs busybox-static byobu bzip2 ca-certificates ca-certificates-java\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":520,"url":"https:\/\/www.gubatron.com\/blog\/recordando-algoritmos-quicksort\/","url_meta":{"origin":1114,"position":3},"title":"Recordando algoritmos, Quicksort","author":"gubatron","date":"May 17, 2007","format":false,"excerpt":"Leyendo no se que en internet, lei mencionar algo sobre preguntas de entrevistas de trabajo, y mencionaron que siempre es bueno saber como funciona Quicksort. Por algun motivo, el algoritmo de ordenamiento que siempre recuerdo es Bubble sort, pero todos sabemos que no es el mejor. So decidi repasar y\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3942,"url":"https:\/\/www.gubatron.com\/blog\/pascal-triangle-generator-in-python-and-then-in-haskell-the-gubatron-method\/","url_meta":{"origin":1114,"position":4},"title":"Pascal Triangle Generator in Python, and then in Haskell &#8211; The Gubatron Method","author":"gubatron","date":"May 6, 2021","format":false,"excerpt":"Here's in python, imperatively, and then in functional style without the need for loops. https:\/\/gist.github.com\/gubatron\/ed966ea4e614d6733715376ad5cfb85f Here's in Haskell, I call it the gubatron's method, explained in the comments. Saw it by looking at a pattern while trying to solve it in paper, it just clicked. Not sure if this is\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":273,"url":"https:\/\/www.gubatron.com\/blog\/snowrss\/","url_meta":{"origin":1114,"position":5},"title":"SnowRSS","author":"gubatron","date":"March 18, 2006","format":false,"excerpt":"SnowRSS SnowRSS is an RSS Aggregator engine I wrote in python (Licensed under the GPL). Currently it's been under use in wedoit4you.com and its stable. It can read RSS and ATOM feeds. It uses the feedparser python module, and the MySQLdb python module to do the job. DOWNLOAD You can\u2026","rel":"","context":"In &quot;Gubatron&quot;","block_context":{"text":"Gubatron","link":"https:\/\/www.gubatron.com\/blog\/category\/gubatron\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/1114","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/comments?post=1114"}],"version-history":[{"count":0,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/1114\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/media?parent=1114"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/categories?post=1114"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/tags?post=1114"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}