Skip to content

Commit 3b03ade

Browse files
committed
add Recursion problem set
1 parent 5eaac6c commit 3b03ade

File tree

4 files changed

+111
-0
lines changed

4 files changed

+111
-0
lines changed

Diff for: recursion_problem_set/example1_reverse.dart

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/// Write a recursive function called reverse which accepts a string and returns a new string in reverse.
2+
///
3+
/// reverse('awesome') // 'emosewa'
4+
/// reverse('rithmschool') // 'loohcsmhtir'
5+
6+
void main() {
7+
print(reverse('awesome'));
8+
}
9+
10+
// Input: String
11+
// Output: The reverse of the input string
12+
reverse(String str) {
13+
// If the str is empty, then return (the base case).
14+
if (str.isEmpty) return str;
15+
16+
// return last char of str + reverse() of str excluding last char.
17+
final lastIndex = str.length - 1;
18+
return str[lastIndex] + reverse(str.substring(0, lastIndex));
19+
}

Diff for: recursion_problem_set/example2_is_palindrome.dart

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/// Write a recursive function called isPalindrome which returns true if the string
2+
/// passed to it is a palindrome (reads the same forward and backward). Otherwise it returns false.
3+
///
4+
/// isPalindrome('awesome') // false
5+
/// isPalindrome('foobar') // false
6+
/// isPalindrome('tacocat') // true
7+
/// isPalindrome('amanaplanacanalpanama') // true
8+
/// isPalindrome('amanaplanacanalpandemonium') // false
9+
10+
void main() {
11+
print(isPalindrome('tacocat'));
12+
}
13+
14+
// Input: String
15+
// Output: bool, if the passed string is palindrome
16+
bool isPalindrome(String str) {
17+
// If str is empty or single char return true (the base case).
18+
if (str.length <= 1) return true;
19+
20+
// compare first and last chars:
21+
// if they're the same return isPalindrome of str excluding first and last chars
22+
// or else return false
23+
final lastIndex = str.length - 1;
24+
if (str[0] == str[lastIndex]) {
25+
return isPalindrome(str.substring(1, lastIndex));
26+
}
27+
return false;
28+
}

Diff for: recursion_problem_set/example3_some_recursive.dart

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/// Write a recursive function called someRecursive which accepts an array and a callback.
2+
/// The function returns true if a single value in the array returns true when passed to the callback.
3+
/// Otherwise it returns false.
4+
///
5+
/// someRecursive([1,2,3,4], isOdd) // true
6+
/// someRecursive([4,6,8,9], isOdd) // true
7+
/// someRecursive([4,6,8], isOdd) // false
8+
/// someRecursive([4,6,8], val => val > 10); // false
9+
10+
void main() {
11+
print(someRecursive([4, 6, 8], isOdd));
12+
print(someRecursive(['a', 'b', 'c'], (String val) => val == 'c'));
13+
}
14+
15+
bool isOdd(int val) => val % 2 != 0;
16+
17+
// Input: array and a callback
18+
// Output: bool, if a single value in the array returns true when passed to the callback
19+
bool someRecursive<T>(List<T> arr, bool Function(T val) callBack) {
20+
// If list is empty return false (the base case)
21+
if (arr.isEmpty) return false;
22+
23+
// Test last value of arr at the callBack:
24+
// If it returns true then someRecursive should return true
25+
// Or else return someRecursive of arr excluding last value
26+
if (callBack(arr.last)) return true;
27+
return someRecursive(arr..removeLast(), callBack);
28+
}

Diff for: recursion_problem_set/example4_flatten.dart

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/// Write a recursive function called flatten which accepts an array of array
2+
/// and returns a new array with all values flattened.
3+
///
4+
/// flatten([1, 2, 3, [4, 5] ]) // [1, 2, 3, 4, 5]
5+
/// flatten([1, [2, [3, 4], [[5]]]]) // [1, 2, 3, 4, 5]
6+
/// flatten([[1],[2],[3]]) // [1,2,3]
7+
/// flatten([[[[1], [[[2]]], [[[[[[[3]]]]]]]]]]) // [1,2,3]
8+
9+
void main() {
10+
print(flatten([
11+
1,
12+
[
13+
2,
14+
[3, 4],
15+
[
16+
[5]
17+
]
18+
]
19+
]));
20+
}
21+
22+
// Input: an array of array
23+
// Output: new array with all values flattened
24+
List<dynamic> flatten(List<dynamic> arr) {
25+
// If arr is empty return it (the base case)
26+
if (arr.isEmpty) return arr;
27+
28+
// Start from last item:
29+
// if it's a list: return flatten of arr excluding last item + flatten of last item (as it's a list)
30+
// or else, return flatten of arr excluding last item + last item
31+
final newArray = arr.sublist(0, arr.length - 1);
32+
if (arr.last is List) {
33+
return [...flatten(newArray), ...flatten(arr.last)];
34+
}
35+
return [...flatten(newArray), arr.last];
36+
}

0 commit comments

Comments
 (0)