简体   繁体   English

在python中使用递归查找1和最大N位之间的整数

[英]Find integers between 1 and the biggest N-bits using recursion in python

I need to find the integers between 1 and the biggest N-bits integer,eg: when n = 3. I need to return 1...999.我需要找到介于 1 和最大 N 位整数之间的整数,例如:当 n = 3 时。我需要返回 1...999。 And using recursion.并使用递归。 The following is my code.The problem is that I don't know the exact data structure to represent the number.以下是我的代码。问题是我不知道表示数字的确切数据结构。 Accurate to return (n=2): [1,2,3,....99] , but I return [[0,0],[0,1],..[9,9]] .I use list to represent the number.准确返回 (n=2): [1,2,3,....99] ,但我返回[[0,0],[0,1],..[9,9]] 。我使用列表来表示数字。 Does anyone know the exact forn to represent the numbers?有谁知道代表数字的确切形式?

class Solution:
# @param n: An integer.
# return : A list of integer storing 1 to the largest number with n digits.
def setOnebyOne(self,numList,number,n,index):
    if index == n-1:
        print 'index = n-1',n-1,number
        numList.append(number)
        return numList

    print index,'setting',number

    for i in range(10):
        if i == 0:
            number.append(i)
        else:
            number[index+1] = i
        print number
        self.setOnebyOne(numList, number,n,index+1)  

def numbersByRecursion(self, n):
    # write your code here
    if n <1:
        return None
    numList = []
    for i in range(10):
        print i
        number =[]
        print number
        number.append(i)
        print 'number[0]= ',number
        self.setOnebyOne(numList, number,n,0)

This is one way to do it.这是一种方法。

class Solution():
    def __init__(self,inp):
        self.inp = inp
        self.val = pow(10,inp) - 1
        self.ans = []

    def solution(self):
        if self.val>0:
            self.ans.append(self.val)
            self.val-=1
            self.solution()


inp = input()
sol = Solution(inp)

sol.solution()
print sol.ans

Also you might wanna see this.你也可能想看这个。 Recursion in Python? Python中的递归? RuntimeError: maximum recursion depth exceeded while calling a Python object 运行时错误:调用 Python 对象时超出了最大递归深度

Python has a recursion depth limit . Python 有递归深度限制

Check it out by executing通过执行检查它

import sys
print sys.getrecursionlimit()

EDITED已编辑

def numbersByRecursion(n,largest,result):`
    def recursion(num,largest,result):
        if num <= largest:
            result.append(num)
            return recursion(num+1,largest,result) 
        else:
            return result
    return recursion(n,largest,result)


result = []
n = input()
largest = pow(10,n) - 1
ans = numbersByRecursion(1,largest,result)
print ans

If your question is how to beat a list like如果您的问题是如何击败像

list = [[0,0], [0,1], [0,2], ... , [9,9]]

into进入

result_list = [0, 1, 2, 3, ..., 99]

then you could do:那么你可以这样做:

def format_list(list):
    result_list = []
    for item in list:
        result = 0
        power = len(item)*10
        for digit in item:
            result += digit ** power
            power /= 10
        result_list.append(result)
    return result_list

Disclaimer: Didn't test this免责声明:没有测试这个

Try this尝试这个

def myfn(n):
    def myfn2(i):
        if i==int(n*'9'):
            return [int(n*'9')]
        return [i]+myfn2(i+1)
    return myfn2(1)

This gives这给

>>>myfn(2)
[1,2,.....,98,99]

Hope this helps希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:如何将n位转换为64位? - Python: How do you convert n-bits to base 64? 使用python查找整数中数字总和的递归函数 - Recursion function to find sum of digits in integers using python 如何使用递归而不是在python中使用运算符找到两个整数中的较大者 - How to find larger of two integers using recursion and not using operators in python python在列表列表中找到最大的零序列(递归) - python find biggest sequence of zeros in list of lists (recursion) Python递归,打印两个整数之间的值 - Python recursion, printing values between two integers Python:如何使用递归反转整数 - Python: How to reverse integers using recursion python如何在整数列表和位字符串之间传递? - python how to transfer between a list of integers and string of bits? 你如何在 python 中为阶乘创建一个函数,该阶乘是使用递归的所有小于或等于 n 的正偶数整数的乘积 - How do you make a function in python for a factorial that is the product of all the positive EVEN integers less than or equal to n using recursion 如何使用python查找矩阵中平方的最大和 - How to find the biggest sum of a square in a matrix using python 如何在Python中使用OpenCV查找图片上的最大矩形? - How to find the biggest rectangle on picture using OpenCV in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM