CSC300 / CSC402: Does this work? [13/14] Previous pageContentsNext page

01
02
03
04
05
06
07
08
  public static int numFives (double[] a) {
    if (a.length == 0) 
      return 0;
    int result = numFives (Arrays.copyOfRange (a, 1, a.length));
    if (a[0] == 5.0)
      result += 1;
    return result;  
  }

This corresponds to the following code in python:

01
02
03
04
05
06
07
def numFives (a):
  if len(a) == 0: 
    return 0
  result = numFives (a[1:])
  if (a[0] == 5.0):
    result += 1
  return result

Previous pageContentsNext page