Skip to content

Commit a29f739

Browse files
committed
feat: examples and exercises from chapter 10
1 parent 7e7a63e commit a29f739

6 files changed

Lines changed: 120 additions & 0 deletions

File tree

chapter-10/01_countdown.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function countdown(n: number): void {
2+
for (let i = n; i >= 0; i--) {
3+
console.log(i);
4+
}
5+
console.log("Liftoff!");
6+
}
7+
8+
function countdownRecursive(n: number): void {
9+
console.log(n);
10+
if (n === 0) {
11+
console.log("Liftoff!");
12+
return;
13+
}
14+
countdownRecursive(n - 1);
15+
}

chapter-10/02_factorial.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def factorial(number)
2+
if number == 1
3+
return 1
4+
else
5+
return number * factorial(number - 1)
6+
end
7+
end

chapter-10/03_find_directories.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
4+
function findDirectories(directory) {
5+
const entries = fs.readdirSync(directory);
6+
7+
for (const entry of entries) {
8+
const fullPath = path.join(directory, entry);
9+
if (fs.statSync(fullPath).isDirectory() && entry !== '.' && entry !== '..') {
10+
console.log(fullPath);
11+
12+
const innerEntries = fs.readdirSync(fullPath);
13+
for (const innerEntry of innerEntries) {
14+
const innerPath = path.join(fullPath, innerEntry);
15+
if (fs.statSync(innerPath).isDirectory() && innerEntry !== '.' && innerEntry !== '..') {
16+
console.log(innerPath);
17+
}
18+
}
19+
}
20+
}
21+
}
22+
23+
function findDirectoriesRecursive(directory) {
24+
const entries = fs.readdirSync(directory);
25+
26+
for (const entry of entries) {
27+
const fullPath = path.join(directory, entry);
28+
if (fs.statSync(fullPath).isDirectory() && entry !== '.' && entry !== '..') {
29+
console.log(fullPath);
30+
findDirectoriesRecursive(fullPath);
31+
}
32+
}
33+
}

chapter-10/03_find_directories.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
def find_directories(directory) # only works for two levels deep
2+
Dir.foreach(directory) do |filename|
3+
if File.directory?("#{directory}/#{filename}") && filename != '.' && filename != '..'
4+
puts "#{directory}/#{filename}"
5+
Dir.foreach("#{directory}/#{filename}") do |inner_filename|
6+
if File.directory?("#{directory}/#{filename}/#{inner_filename}") && inner_filename != '.' && inner_filename != '..'
7+
puts "#{directory}/#{filename}/#{inner_filename}"
8+
end
9+
end
10+
end
11+
end
12+
end
13+
14+
def find_directories_recursive(directory) # works for any depth
15+
Dir.foreach(directory) do |filename|
16+
if File.directory?("#{directory}/#{filename}") && filename != '.' && filename != '..'
17+
puts "#{directory}/#{filename}"
18+
find_directories_recursive("#{directory}/#{filename}")
19+
end
20+
end
21+
end

chapter-10/ex3.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def sum(low, high)
2+
if low == high
3+
return low
4+
else
5+
return high + sum(low, high - 1)
6+
end
7+
end

chapter-10/ex4.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const array = [
2+
1,
3+
2,
4+
3,
5+
[4, 5, 6],
6+
7,
7+
[
8+
8,
9+
[
10+
9, 10, 11,
11+
[12, 13, 14]
12+
]
13+
],
14+
[
15+
15, 16, 17, 18, 19,
16+
[
17+
20, 21, 22,
18+
[
19+
23, 24, 25,
20+
[26, 27, 29]
21+
],
22+
30, 31
23+
],
24+
32
25+
],
26+
33
27+
]
28+
29+
function printArray(arr) {
30+
arr.forEach(item => {
31+
if (!Array.isArray(item)) {
32+
console.log(item);
33+
} else {
34+
printArray(item);
35+
}
36+
})
37+
}

0 commit comments

Comments
 (0)