Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

don't get why my code doesn't work...?

The assignment: Write a recursive function index_of_max(nums) that returns the index of the maximum value in the input list of numbers, leaving the original list unchanged.

Here's my code:

def index_of_max(nums):

if nums == []:

return "empty list" #base case 1

if len(nums) == 1:

return 0 #base case 2

else:

x = nums[0]

lst = nums[1:]

if x > index_of_max(lst):

return nums.index(x)

else:

return index_of_max(lst)

1 Answer

Relevance
  • roger
    Lv 7
    5 years ago

    try this

    #include <stdio.h>

    int getMax(int * n,int imx,int i){

    if(!i) return imx;

    if(n[i-1]> n[imx])

    imx= i-1 ;

    return getMax(n,imx,i-1);

    }

    int main(void){

    int mx,imx;

    int nelements;

    int j[]={ 200,4,5,6,13,3 ,6,10,2,201};

    nelements= sizeof j /sizeof *j;

    imx=getMax(j,0,nelements);

    printf(" %d %d\n", imx,j[imx]);

    return 0;

    }

Still have questions? Get your answers by asking now.