用递归查找所有索引

时间:2012-11-13 07:22:30

标签: python recursion

我编写一个程序,找到list / srting中特定元素的所有索引号,我必须使用递归,而我的func必须只得到2个参数。

我的问题是我的程序只找到第一个索引并停止,我该如何处理呢?

我的代码:

def find_all(L, v):
    return 0 if L[0] == v else 1 + find_all(L[1:], v)

输入: 1. find_all( [1,2,3,4,2,4,5,2,1], 2) 2. find_all("hello wonderful world", "w")

输出: 1. [1,4,7] 2. [6,16]

5 个答案:

答案 0 :(得分:4)

你可以使用Pythons能力向后走过一个列表并抓住最后一个元素。然后将列表与+运算符放在一起。通过向后浏览列表,您可以在找到值时找到indice,而不是在从列表开头移动到结束时丢失它。

def find_all(L, v):
    if not L:
            return []

    result = []
    if L[-1] == v:
            result = [len(L)-1]

    return find_all(L[:-1], v) + result

答案 1 :(得分:1)

你必须以某种方式跟踪计数器。我们的想法是使用find_all(L, v)作为“真实”递归函数的接口:

def find_all(L, v):
    return _find_all(L, v, 0)

def _find_all(L, v, position):
    # Your algorithm here

考虑到这是家庭作业,我不会为你做这项工作,但你应该能够继续从这里开始。

答案 2 :(得分:0)

C ++中的代码

 int allIndexes(int input[], int size, int x, int output[]){

 if(size==0)
      return 0;
    int ans=allIndexes(input, size-1, x , output );
    if(input[size-1]==x)
    {
        output[ans]=size-1;
       return ans+1; 
    }
    return ans;
}  

答案 3 :(得分:0)

int AllIndexesRecursive(int input[], int size, 
                    int x, int output[]) 
{ 

    // If an empty array comes 
    // to the function, then 
    // return zero 
    if (size == 0) { 
        return 0; 
    } 

    // Getting the recursive answer 
    int smallAns = AllIndexesRecursive(input + 1, 
                                    size - 1, x, output); 

    // If the element at index 0 is equal 
    // to x then add 1 to the array values 
    // and shift them right by 1 step 
    if (input[0] == x) { 
        for (int i = smallAns - 1; i >= 0; i--) { 
            output[i + 1] = output[i] + 1; 
        } 

        // Put the start index in front 
        // of the array 
        output[0] = 0; 
        smallAns++; 
    } 
    else { 

        // If the element at index 0 is not equal 
        // to x then add 1 to the array values 
        for (int i = smallAns - 1; i >= 0; i--) { 
            output[i] = output[i] + 1; 
        } 
    } 
    return smallAns; 
} 

// Function to find all indices of a number 
void AllIndexes(int input[], int n, int x) 
{ 
    int output[n]; 
    int size = AllIndexesRecursive(input, n, 
                                x, output); 
    for (int i = 0; i < size; i++) { 
        cout << output[i] << " "; 
    } 
} 

答案 4 :(得分:0)

Java code:
/ Java program to find all 
// indices of a number 
public class GFG { 
  
    public int[] AllIndexesRecursive(int input[], 
                                int x, int start) 
    { 
        // If the start index reaches the 
        // length of the array, then 
        // return empty array 
        if (start == input.length) { 
            int[] ans = new int[0]; // empty array 
            return ans; 
        } 
  
        // Getting the recursive answer in 
        // smallIndex array 
        int[] smallIndex = AllIndexesRecursive(input, x, 
                                              start + 1); 
  
        // If the element at start index is equal 
        // to x then 
        // (which is the answer of recursion) and then 
        // (which came through recursion) 
        if (input[start] == x) { 
            int[] myAns = new int[smallIndex.length + 1]; 
  
            // Put the start index in front 
            // of the array 
            myAns[0] = start; 
            for (int i = 0; i < smallIndex.length; i++) { 
                  
                // Shift the elements of the array 
                // one step to the right 
                // and putting them in 
                // myAns array 
                myAns[i + 1] = smallIndex[i]; 
            } 
            return myAns; 
        } 
        else { 
              
            // If the element at start index is not 
            // equal to x then just simply return the 
            // answer which came from recursion. 
            return smallIndex; 
        } 
    }