C51 11 Programm no × facweb1.redlands.edu.facultyfolder/p 111fassignS.htm rible
ID: 3600403 • Letter: C
Question
C51 11 Programm no × facweb1.redlands.edu.facultyfolder/p 111fassignS.htm rible Cite This For ve 123Movies | Free by Lunar I Redlands 1. Complelethe array05 metod below to compute recursively the number otimes that the value 5 appears in a given array Tip: Coisider uly lhe part of the aray hat begins at the give index In this way, a recursive call can pass index+1 to move down the array. The initial call will pass in index as 0 array(15([1, 2, 5], 0 returns l array05([5, 51. 0) returns 2 arrayo5([1, 2, 3,4,0) retuso public int arrayes(int1 nuns, int index) 2. Complete the countPi) method below to reeursively compute the number of times lowerease "hi" appears in a given string. countPixxpixx") returns l countPi("xpixpix") relurms 2 countPii) returns 1 public static int countPi(String str)f 3. Complete the sumPairs recursive methad below to count the number of pairs found in a given string. Assume that a "pair" in a string is defined as two instances of a char separated by a char. For example, the string "AxA" contains two A's separated by an x'. The A's make a pair. 3.37 PMExplanation / Answer
Hi, I have implemented first two functions. Please repost others in separate post.
public class Test {
public static int array05(int[] nums, int index) {
if(index >= nums.length)
return 0;
if(nums[index] == 5)
return 1 + array05(nums, index+1);
else
return array05(nums, index+1);
}
public static int countPi(String str) {
if(str == null || str.isEmpty())
return 0;
int index = str.indexOf("pi");
if(index == -1)
return 0;
return 1 + countPi(str.substring(index + 2));
}
public static void main(String[] args) {
System.out.println(countPi("xxpixx"));
System.out.println(countPi("xpixpix"));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.