{"id":446,"date":"2007-01-22T22:48:08","date_gmt":"2007-01-23T05:48:08","guid":{"rendered":"http:\/\/www.gubatron.com\/blog\/2007\/01\/22\/como-es-que-convierto-de-decimal-a-binario\/"},"modified":"2007-01-22T22:48:08","modified_gmt":"2007-01-23T05:48:08","slug":"como-es-que-convierto-de-decimal-a-binario","status":"publish","type":"post","link":"https:\/\/www.gubatron.com\/blog\/como-es-que-convierto-de-decimal-a-binario\/","title":{"rendered":"Como es que convierto de decimal a binario?"},"content":{"rendered":"<p>Recuerdo que esto fue uno de los primeros ejercicios de programacion que me pusieron a hacer en Haskell, convertir de decimal a binario. Aqui una simple implementacion propia en python mientras estaba practicando pal google code jam (que la hice por gusto pq python tiene modulos para convertir de cualquier base a otra)<\/p>\n<p>Juguemos a Python golf, y mandenme implementaciones con menos lineas de codigo, excelente ejercicio para los que estan aprendiendo python, como veran, un lenguaje sin pelos en la lengua, todo full sencillo.<\/p>\n<pre>\ndef dec2bin(num):\n    if num<0:\n        return 0\n\n    if num<=1:\n        return num\n\n    coef = num\/2\n    rem = num%2\n\n    result = str(rem)\n\n    while coef > 0:\n        rem = coef%2\n        coef = coef\/2\n\n        result = str(rem) + result\n<\/pre>\n<p>Envien implementaciones mas cortas, no digo que esta sea la mejor o la mas eficiente, pero me salio de lo que me acorde, parece que funciona fino para numeros positivos:<\/p>\n<pre>\ngubatron@aria:~\/tmp$ time python badbinary.py\n10000000000000 en binario es 1110100011010100101001010001000000000000\n\nreal    0m0.017s\nuser    0m0.012s\nsys     0m0.004s\n\ngubatron@aria:~\/tmp$ cat \/proc\/cpuinfo\nprocessor       : 0\nvendor_id       : AuthenticAMD\ncpu family      : 15\nmodel           : 47\nmodel name      : AMD Athlon(tm) 64 Processor 3200+\nstepping        : 0\ncpu MHz         : 2010.510\ncache size      : 512 KB\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Recuerdo que esto fue uno de los primeros ejercicios de programacion que me pusieron a hacer en Haskell, convertir de decimal a binario. Aqui una simple implementacion propia en python mientras estaba practicando pal google code jam (que la hice por gusto pq python tiene modulos para convertir de cualquier base a otra) Juguemos a [&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,30,43],"tags":[],"class_list":["post-446","post","type-post","status-publish","format-standard","hentry","category-code","category-geeklife","category-linux"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5Unzf-7c","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":446,"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":3942,"url":"https:\/\/www.gubatron.com\/blog\/pascal-triangle-generator-in-python-and-then-in-haskell-the-gubatron-method\/","url_meta":{"origin":446,"position":1},"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":1114,"url":"https:\/\/www.gubatron.com\/blog\/using-a-linear-array-as-a-bidimensional-matrix\/","url_meta":{"origin":446,"position":2},"title":"Using a linear array as a bidimensional matrix","author":"gubatron","date":"January 27, 2009","format":false,"excerpt":"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.\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":446,"position":3},"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":[]},{"id":272,"url":"https:\/\/www.gubatron.com\/blog\/project-snowrss\/","url_meta":{"origin":446,"position":4},"title":"Project SnowRSS","author":"gubatron","date":"March 18, 2006","format":false,"excerpt":"SnowRSS is a GPL RSS Aggregator engine I wrote in python. 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 only download the code\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":[]},{"id":510,"url":"https:\/\/www.gubatron.com\/blog\/fuck-the-python-borg-i-like-singleton-better\/","url_meta":{"origin":446,"position":5},"title":"Fuck the Python Borg, I like Singleton Better","author":"gubatron","date":"April 25, 2007","format":false,"excerpt":"I've read in parts of the web (and on the Martinelli's Python Cookbok) that it's better to do the Borg pattern over singletons, they say something alongs the lines of: \"who cares about identity, care about shared state\" Coming from the Java world, I just can't understand that, why waste\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":[]}],"_links":{"self":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/446","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=446"}],"version-history":[{"count":0,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/446\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/media?parent=446"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/categories?post=446"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/tags?post=446"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}