{"id":27431,"date":"2018-02-02T21:48:52","date_gmt":"2018-02-02T16:18:52","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27431"},"modified":"2018-10-31T15:51:47","modified_gmt":"2018-10-31T10:21:47","slug":"python-programming-interesting-methode-to-generate-binary-numbers-from-1-to-n","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/python-programming-interesting-methode-to-generate-binary-numbers-from-1-to-n\/","title":{"rendered":"Interesting Method to Generate Binary Numbers from 1 to n"},"content":{"rendered":"<p><span style=\"color: #333300;\"><strong>Method to Generate Binary Numbers<\/strong><\/span><\/p>\n<p>Given a number n, write a function that generates and prints all <a href=\"https:\/\/www.wikitechy.com\/tutorials\/c-programming\/c-program-to-convert-decimal-to-binary\" target=\"_blank\" rel=\"noopener\">binary numbers<\/a> with <a href=\"https:\/\/www.wikitechy.com\/tutorials\/javascript\/validate-decimal-numbers\" target=\"_blank\" rel=\"noopener\">decimal values<\/a> from 1 to n.<span id=\"more-130391\"><\/span><\/p>\n<h3 id=\"examples\"><span style=\"color: #003366;\">Examples:<\/span><\/h3>\n<pre>Input: n = 2\r\nOutput: 1, 10\r\n\r\nInput: n = 5\r\nOutput: 1, 10, 11, 100, 101\r\n<\/pre>\n<p>A simple method is to run a <a href=\"https:\/\/www.wikitechy.com\/tutorials\/python\/python-while-loop\" target=\"_blank\" rel=\"noopener\">loop<\/a> from 1 to n, call decimal to binary inside the loop.<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-31790\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2018\/02\/generate-binary-number.png\" alt=\"Generate binary number\" width=\"205\" height=\"361\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2018\/02\/generate-binary-number.png 205w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2018\/02\/generate-binary-number-170x300.png 170w\" sizes=\"(max-width: 205px) 100vw, 205px\" \/><\/p>\n<p>Following is an interesting method that uses queue data structure to print binary numbers.<\/p>\n<p>1) <strong>Create<\/strong> an empty queue of <a href=\"https:\/\/www.wikitechy.com\/tutorials\/python\/interesting-facts-about-strings-in-python-slicing\" target=\"_blank\" rel=\"noopener\">strings<\/a><br \/>\n2)<strong> Enqueue<\/strong> the first binary number \u201c1\u201d to <a href=\"https:\/\/www.wikitechy.com\/tutorials\/python\/python-data-structure-priority-queue-and-heapq\" target=\"_blank\" rel=\"noopener\">queue<\/a>.<br \/>\n3) Now<strong> run a loop<\/strong> for generating and printing n binary numbers.<br \/>\na) <strong>Dequeue<\/strong> and <strong>Print<\/strong> the front of queue.<br \/>\nb) Append \u201c0\u201d at the end of front item and <strong>enqueue<\/strong> it.<br \/>\nc) Append \u201c<strong>1<\/strong>\u201d at the end of front item and<strong> enqueue<\/strong> it.<\/p>\n[ad type=\u201dbanner\u201d]\n<h4 id=\"python-programming-method-to-generate-binary-numbers\"><span style=\"color: #008080;\"><strong>Python Programming Method to Generate Binary Numbers<\/strong><\/span><\/h4>\n[pastacode lang=\u201dpython\u201d manual=\u201d%23%20Python%20program%20to%20generate%20binary%20numbers%20from%20%0A%23%201%20to%20n%20%0A%20%0A%23%20This%20function%20uses%20queu%20data%20structure%20to%20print%20binary%20numbers%0Adef%20generatePrintBinary(n)%3A%0A%20%20%20%20%20%0A%20%20%20%20%23%20Create%20an%20empty%20queue%0A%20%20%20%20from%20Queue%20import%20Queue%0A%20%20%20%20q%20%3D%20Queue()%0A%20%20%20%20%20%0A%20%20%20%20%23%20Enqueu%20the%20first%20binary%20number%0A%20%20%20%20q.put(%221%22)%0A%20%0A%20%20%20%20%23%20This%20loop%20is%20like%20BFS%20of%20a%20tree%20with%201%20as%20root%0A%20%20%20%20%23%200%20as%20left%20child%20and%201%20as%20right%20child%20and%20so%20on%0A%20%20%20%20while(n%3E0)%3A%0A%20%20%20%20%20%20%20%20n-%3D%201%0A%20%20%20%20%20%20%20%20%23%20Print%20the%20front%20of%20queue%0A%20%20%20%20%20%20%20%20s1%20%3D%20q.get()%20%0A%20%20%20%20%20%20%20%20print%20s1%20%0A%20%20%20%20%20%0A%20%20%20%20%20%20%20%20s2%20%3D%20s1%20%23%20Store%20s1%20before%20changing%20it%0A%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%23%20Append%20%220%22%20to%20s1%20and%20enqueue%20it%0A%20%20%20%20%20%20%20%20q.put(s1%2B%220%22)%0A%20%0A%20%20%20%20%20%20%20%20%23%20Append%20%221%22%20to%20s2%20and%20enqueue%20it.%20Note%20that%20s2%0A%20%20%20%20%20%20%20%20%23%20contains%20the%20previous%20front%0A%20%20%20%20%20%20%20%20q.put(s2%2B%221%22)%0A%20%20%20%20%20%0A%20%0A%23%20Driver%20program%20to%20test%20above%20function%0An%20%3D%2010%0AgeneratePrintBinary(n)%0A%20%0A%0A\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h3 id=\"output\"><span style=\"color: #008000;\">Output:<\/span><\/h3>\n<pre>1\r\n10\r\n11\r\n100\r\n101\r\n110\r\n111\r\n1000\r\n1001\r\n1010<\/pre>\n[ad type=\u201dbanner\u201d]\n","protected":false},"excerpt":{"rendered":"<p>Interesting Method to Generate Binary Numbers from 1 to n Given a number n, write a function that generates and prints all binary numbers<\/p>\n","protected":false},"author":1,"featured_media":31259,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[73012,80124],"tags":[81620,81616,81615,81617,81618,81614,81619],"class_list":["post-27431","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-structures","category-queue","tag-c-program-to-find-binary-equivalent-of-a-number","tag-generate-all-binary-numbers-of-length-n-in-c","tag-generate-all-binary-strings-of-length-n-java","tag-generate-all-binary-strings-of-length-n-python","tag-generate-all-strings-of-n-bits-in-c","tag-generate-binary-numbers-from-1-to-n-python","tag-given-a-string-of-characters-numbers-and-symbols-reverse-only-the-characters-in-the-array"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27431","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=27431"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27431\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media\/31259"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27431"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27431"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27431"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}