dsa lab task 1
dsa lab task 1
Reg: sp23-bcs-154
Lab assignment: 01
Subject: DSA
Recursion: 01
Question: 01
factorial(1) → 1
factorial(2) → 2
factorial(3) → 6
Code:
public int factorial(int n) {
if (n == 1) {
return 1;
Screenshot:
Question: 02
We have a number of bunnies and each bunny has two big floppy ears. We want to compute the
total number of ears across all the bunnies recursively (without loops or multiplication).
bunnyEars(0) → 0
bunnyEars(1) → 2
bunnyEars(2) → 4
Code:
if (bunnies == 0) {
return 0;
Screenshot:
Question: 03
fibonacci(0) → 0
fibonacci(1) → 1
fibonacci(2) → 1
Code:
if (n == 0) {
return 0;
if (n == 1) {
return 1;
Screenshot:
Question: 04
We have bunnies standing in a line, numbered 1, 2, ... The odd bunnies (1, 3,
..) have the normal 2 ears. The even bunnies (2, 4, ..) we'll say have 3 ears,
because they each have a raised foot. Recursively return the number of
"ears" in the bunny line 1, 2, ... n (without loops or multiplication).
bunnyEars2(0) → 0
bunnyEars2(1) → 2
bunnyEars2(2) → 5
Code:
if (bunnies == 0) {
return 0;
if (bunnies % 2 == 1) {
} else {
Screenshot:
Question: 05
We have triangle made of blocks. The topmost row has 1 block, the next row down has 2 blocks,
the next row has 3 blocks, and so on. Compute recursively (no loops or multiplication) the total
number of blocks in such a triangle with the given number of rows.
triangle(0) → 0
triangle(1) → 1
triangle(2) → 3
Code:
if (rows == 0) {
return 0;
Screenshot:
Question: 06
Given a non-negative int n, return the sum of its digits recursively (no loops).
Note that mod (%) by 10 yields the rightmost digit (126 % 10 is 6), while
divide (/) by 10 removes the rightmost digit (126 / 10 is 12).
sumDigits(126) → 9
sumDigits(49) → 13
sumDigits(12) → 3
Code:
if (n == 0) {
return 0;
Screenshot:
Question: 07
count7(717) → 2
count7(7) → 1
count7(123) → 0
Code:
if (n == 0) {
return 0;
Screenshot:
Question: 08
Given a non-negative int n, compute recursively (no loops) the count of the
occurrences of 8 as a digit, except that an 8 with another 8 immediately to
its left counts double, so 8818 yields 4. Note that mod (%) by 10 yields the
rightmost digit (126 % 10 is 6), while divide (/) by 10 removes the rightmost
digit (126 / 10 is 12).
count8(8) → 1
count8(818) → 2
count8(8818) → 4
Code:
if (n == 0) {
return 0;
if (n % 10 == 8) {
if ((n / 10) % 10 == 8) {
} else {
Screenshot:
Question: 09
Given base and n that are both 1 or more, compute recursively (no loops)
the value of base to the n power, so powerN(3, 2) is 9 (3 squared).
powerN(3, 1) → 3
powerN(3, 2) → 9
powerN(3, 3) → 27
Code:
if (n == 0) {
return 1;
Screenshot:
Question: 10
Given a string, compute recursively (no loops) the number of lowercase 'x'
chars in the string.
countX("xxhixx") → 4
countX("xhixhix") → 3
countX("hi") → 0
Code:
if (str.isEmpty()) {
return 0;
Screenshot:
Question: 11
countHi("xxhixx") → 1
countHi("xhixhix") → 2
countHi("hi") → 1
Code:
return 0;
if (str.substring(0, 2).equals("hi")) {
return 1 + countHi(str.substring(2));
return countHi(str.substring(1));
Screenshot:
Question: 12
Given a string, compute recursively (no loops) a new string where all
appearances of "pi" have been replaced by "3.14".
changePi("xpix") → "x3.14x"
changePi("pipi") → "3.143.14"
changePi("pip") → "3.14p"
Code:
if (str.length() < 2) {
return str;
if (str.substring(0, 2).equals("pi")) {
Screenshot:
Question: 13
Given a string, compute recursively a new string where all the 'x' chars have
been removed.
noX("xaxb") → "ab"
noX("abc") → "abc"
noX("xx") → ""
Code:
public String noX(String str) {
if (str.length() == 0) {
return "";
if (str.charAt(0) == 'x') {
return noX(str.substring(1));
Screenshot:
Question: 14
Code:
return false;
if (nums[index] == 6) {
return true;
Screenshot:
Question: 15
Given an array of ints, compute recursively the number of times that the
value 11 appears in the array. We'll use the convention of considering only
the part of the array that begins at the given 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.
array11([1, 2, 11], 0) → 1
array11([11, 11], 0) → 2
array11([1, 2, 3, 4], 0) → 0
Code:
return 0;
Screenshot:
Question: 16
Code:
return false;
return true;
Screenshot:
Question: 17
Given a string, compute recursively a new string where all the adjacent chars
are now separated by a "*".
allStar("hello") → "h*e*l*l*o"
allStar("abc") → "a*b*c"
allStar("ab") → "a*b"
Code:
if (str.length() <= 1) {
return str;
Screenshot:
Question: 18
Given a string, compute recursively a new string where identical chars that
are adjacent in the original string are separated from each other by a "*".
pairStar("hello") → "hel*lo"
pairStar("xxyy") → "x*xy*y"
pairStar("aaaa") → "a*a*a*a"
Code:
public String pairStar(String str) {
if (str.length() <= 1) {
return str;
if (str.charAt(0) == str.charAt(1)) {
Screenshot:
Question: 19
Given a string, compute recursively a new string where all the lowercase 'x'
chars have been moved to the end of the string.
endX("xxre") → "rexx"
endX("xxhixx") → "hixxxx"
endX("xhixhix") → "hihixxx"
Code:
if (str.length() == 0) {
return "";
if (first == 'x') {
Screenshot:
Question: 20
countPairs("axa") → 1
countPairs("axax") → 2
countPairs("axbx") → 1
Code:
if (str.length() < 3) {
return 0;
Screenshot:
Question: 21
Count recursively the total number of "abc" and "aba" substrings that appear
in the given string.
countAbc("abc") → 1
countAbc("abcxxabc") → 2
countAbc("abaxxaba") → 2
Code:
if (str.length() < 3) {
return 0;
Screenshot:
Question: 22
Given a string, compute recursively (no loops) the number of "11" substrings
in the string. The "11" substrings should not overlap.
count11("11abc11") → 2
count11("abc11x11x11") → 3
count11("111") → 1
Code:
if (str.length() < 2) {
return 0;
if (str.substring(0, 2).equals("11")) {
return 1 + count11(str.substring(2));
return count11(str.substring(1));
Screenshot:
Question: 23
stringClean("yyzzza") → "yza"
stringClean("abbbcdd") → "abcd"
stringClean("Hello") → "Helo"
Code:
if (str.length() <= 1) {
return str;
if (str.charAt(0) == str.charAt(1)) {
return stringClean(str.substring(1));
Graph:
Question: 24
countHi2("ahixhi") → 1
countHi2("ahibhi") → 2
countHi2("xhixhi") → 0
Solution:
if (str.length() < 2) {
return 0;
if (str.startsWith("hi")) {
return 1 + countHi2(str.substring(2));
return countHi2(str.substring(3));
return countHi2(str.substring(1));
Screenshot:
Question: 25
Code:
if (str.isEmpty()) {
return "";
if (str.charAt(0) == '(') {
if (str.charAt(str.length() - 1) == ')') {
return str;
return parenBit(str.substring(1));
Screenshot:
Question: 26Given a string, return true if it is a nesting of zero or more pairs
of parenthesis, like "(())" or "((()))". Suggestion: check the first and last chars,
and then recur on what's inside them.
nestParen("(())") → true
nestParen("((()))") → true
nestParen("(((x))") → false
Code:
if (str.isEmpty()) {
return true;
return false;
Screenshot:
Question: 27
Given a string and a non-empty substring sub, compute recursively the
number of times that sub appears in the string, without the sub strings
overlapping.
strCount("catcowcat", "cat") → 2
strCount("catcowcat", "cow") → 1
strCount("catcowcat", "dog") → 0
Code:
return 0;
if (str.startsWith(sub)) {
Screenshot:
Question: 28
Code:
if (n <= 0) {
return true;
return false;
if (str.startsWith(sub)) {
Screenshot:
Question: 29
strDist("catcowcat", "cat") → 9
strDist("catcowcat", "cow") → 3
strDist("cccatcowcatxx", "cat") → 9
Code:
return 0;
return str.length();
if (!str.startsWith(sub)) {
Screenshot:
Question: 30
Given a string, compute recursively (no loops) a new string where all the
lowercase 'x' chars have been changed to 'y' chars.
changeXY("codex") → "codey"
changeXY("xxhixx") → "yyhiyy"
changeXY("xhixhix") → "yhiyhiy"
Code:
if (str.isEmpty()) {
return "";
Screenshot: