我希望使用所有可能的组合生成0和1的大字符串(最多1000个),而不使用itertools。我的代码适用于长度为20的字符串,但我有更大的数字问题。
exponent=input("String length:")
n= int(exponent) #Convert to integer the input
#Define a function to get the calc
def combinations(n):
# If the length of the string is 0 just write "empty string".
if n < 1:
print("empty string")
else:
for i in range(2**n):
# 2 raised to n power gives the total combinations
s = bin(i)[2:]
# Bin converts to binary the number
s = "0" * (n-len(s))+s
print(s)
print(combinations(n))
当我尝试像50这样的大数字时,我收到以下消息:
for i in range(2**n):
OverflowError: range() result has too many items
你知道如何改进我的代码吗?我如何花更少的记忆并尝试更大的数字呢?