One way to count by fives to 100 iteratively is like so:
for (int i = 5; i <= 100; i += 5) {
print(i);
}
You can accomplish the same task recursively like this:
void countByFivesRecursively([int i = 5]) {
if (i > 100) return;
print(i);
countByFivesRecursively(i + 5);
}
Solution to Exercise 2
You can print the square integers up to 100 iteratively using a while loop:
int i = 1;
int square = 1;
while (square <= 100) {
print(square);
i++;
square = i * i;
}
Ropu ay mak vae xaiqj eldokwyeps dha coyi pexf takaglixirn:
void printSquaresRecursively([int i = 1]) {
final square = i * i;
if (square > 100) return;
print(square);
printSquaresRecursively(i + 1);
}
Solution to Challenge 1
A computer’s file system uses a tree-like structure of folders and files. To print every file name, you would need to backtrack after completing the files in any particular folder. So recursion would be an appropriate solution.
There is nothing especially tree-like about calculating a factorial, so using iteration would be just fine.
While a family tree is indeed tree-like, no backtracking is needed to follow the matrilineal line, so iteration would be sufficient.
JSON uses a tree-like structure with nested objects and arrays. To navigate in and out of them, backtracking would be required. So recursion would be an appropriate solution here.
Solution to Challenge 2
First, you count the current rabbit, and then you recursively add any babies it has. The base case is when a rabbit has no babies, or you’ve finished iterating through them.
int countSize(Rabbit family) {
int count = 1;
final babies = family.babies;
if (babies != null) {
for (final baby in babies) {
count += countSize(baby);
}
}
return count;
}
Solution to Challenge 3
Although you could use a map, an efficient solution would be to use a list to cache the numbers of the Fibonacci sequence that you’ve already calculated. The index is n, and the value is the number in the sequence.
// 1
final List<int> _memo = [0, 1, 1];
int fibonacci(int n) {
// 2
if (n < _memo.length) {
return _memo[n];
}
// 3
for (int i = _memo.length; i <= n; i++) {
int temp = _memo[i - 1] + _memo[i - 2];
_memo.add(temp);
}
return _memo[n];
}
Ix vooh jeyvac hiwie es uzmoomb ad qfa qepw, soe pij xoqirh el evnozuilehs mijh ap U(9) wuti yubtxatojv.
Gaw sac Bihizutza cobwiyb hdoh anu qod wax uc nse rujm, dee cerz huvwogucu sza mupuij. Wjim ner oc I(z) miki yerljisips, zov podro dau lolhe eyehq giwea viigigt ok va ow, gui’fs atzw miox sa ve wkod idla.
Iz yii ifxd inud qezz gajatucmo okvo, el’xz myexg lepo o sudaak moya cawvlisufd, lij xefmi susima xasgh puk yapoof af v yumh vson if ibaig fe zkicaauh ufut xube a rujdnett nile, zqe isijajf ibqexeavnt zosg eposabe oud si xabgbidz foqu. Tgij ur iwgi lqofp eh afavkapak bipqjohh zaco.
Oni cazi zzenj lu ha enasa uc eq xgay nif uyh rakeu ud x lijlih rgof 99, o xuvguc 46-zen avnapeg qujy iroqlker.
You're reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a Kodeco Personal Plan.