This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
num = int(input("Escolha um número decimal para transformar em binario: ")) | |
result = [] | |
zero = '0' | |
while num > 0: | |
num, bit = divmod(num, 2) | |
result.append(bit) | |
zero = '' | |
# Imprimir em ordem inversa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def primes_up_to(N): | |
sieve = N*[True] | |
for i in range(2, N): | |
if not sieve[i]: continue | |
yield i | |
for j in range(i*i, N, i): | |
sieve[j] = False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def _le(left, right): | |
return left if left <= right else right | |
def merge(left, right, cmp=_le): | |
""" | |
Merges two ordered iterables (left and right) keeping result ordered according to cmp. | |
:param left: iterable | |
:param right: iterable | |
:param cmp: callable which receive one element from left and another from right and returns the on to be merged. |