From 8fb3596668d73582a9b07d2ec8443c837c73c5fd Mon Sep 17 00:00:00 2001 From: Emilianouz <135679131+Emilianouz@users.noreply.github.com> Date: Tue, 11 Nov 2025 11:56:34 +0000 Subject: [PATCH 1/3] Numeric Systems: answers --- number-systems/README.md | 41 ++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/number-systems/README.md b/number-systems/README.md index 77a3bde9..322d053c 100644 --- a/number-systems/README.md +++ b/number-systems/README.md @@ -5,61 +5,62 @@ Do not convert any binary numbers to decimal when solving a question unless the The goal of these exercises is for you to gain an intuition for binary numbers. Using tools to solve the problems defeats the point. Convert the decimal number 14 to binary. -Answer: +Answer: 1110 Convert the binary number 101101 to decimal: -Answer: +Answer: 45 Which is larger: 1000 or 0111? -Answer: +Answer: 1000 Which is larger: 00100 or 01011? -Answer: +Answer: 01011 What is 10101 + 01010? -Answer: +Answer: 11111 What is 10001 + 10001? -Answer: +Answer:100010 What's the largest number you can store with 4 bits, if you want to be able to represent the number 0? -Answer: +Answer: 1111 = 15 (largest representable number) + Number of possible values including 0 = 16 How many bits would you need in order to store the numbers between 0 and 255 inclusive? -Answer: +Answer: 8 bits How many bits would you need in order to store the numbers between 0 and 3 inclusive? -Answer: +Answer: 2 bits => 00, 01, 10, 11 How many bits would you need in order to store the numbers between 0 and 1000 inclusive? -Answer: +Answer: 10 bits How can you test if a binary number is a power of two (e.g. 1, 2, 4, 8, 16, ...)? -Answer: +Answer: Been a binary it would be 1 followed by 0s => 100, 10000, 10 Convert the decimal number 14 to hex. -Answer: +Answer:0xE Convert the decimal number 386 to hex. -Answer: +Answer: 0x182 Convert the hex number 386 to decimal. -Answer: +Answer: 902 Convert the hex number B to decimal. -Answer: +Answer:11 If reading the byte 0x21 as a number, what decimal number would it mean? -Answer: +Answer:33 If reading the byte 0x21 as an ASCII character, what character would it mean? -Answer: +Answer:! If reading the byte 0x21 as a greyscale colour, as described in "Approaches for Representing Colors and Images", what colour would it mean? -Answer: +Answer: Dark Grey If reading the bytes 0xAA00FF as an RGB colour, as described in "Approaches for Representing Colors and Images", what colour would it mean? -Answer: +Answer:Purple (magenta) If reading the bytes 0xAA00FF as a sequence of three one-byte decimal numbers, what decimal numbers would they be? -Answer: +Answer: 170 0 255 From a5dec6c962c560cde196b8f2353787ed848dc580 Mon Sep 17 00:00:00 2001 From: Emilianouz <135679131+Emilianouz@users.noreply.github.com> Date: Tue, 25 Nov 2025 16:22:27 +0000 Subject: [PATCH 2/3] Implementing shell tools so that they behave like the real versions on a typical system: cat, ls, and wc. --- implement-shell-tools/cat/cat-commander.js | 56 ++++++++++++++++++++ implement-shell-tools/ls/ls-commander.js | 36 +++++++++++++ implement-shell-tools/wc/wc-commander.js | 61 ++++++++++++++++++++++ 3 files changed, 153 insertions(+) create mode 100644 implement-shell-tools/cat/cat-commander.js create mode 100644 implement-shell-tools/ls/ls-commander.js create mode 100644 implement-shell-tools/wc/wc-commander.js diff --git a/implement-shell-tools/cat/cat-commander.js b/implement-shell-tools/cat/cat-commander.js new file mode 100644 index 00000000..5cc5ffb2 --- /dev/null +++ b/implement-shell-tools/cat/cat-commander.js @@ -0,0 +1,56 @@ +import { program } from "commander"; +import { promises as fs } from "node:fs"; +import process from "node:process"; + +program + .name("read-files-content") + .description("Read the content of a file (simulating cat command)") + .option("-n, --number","Number of lines") + .option("-b","Number of no-blanks lines") + .argument("","The file path to process"); //file + +program.parse(); + +const argv = program.args; + +if (argv.length === 0) { + console.error(`Expected file path(s),passed but got ${argv.length}.`); + process.exit(1); +} + +//const path = argv[0]; +for (const path of argv){ + const option = program.opts(); + const content = await fs.readFile(path, "utf-8"); + if (option.number){ + // split content + let lines = content.split(/\n/); + // removes last line + if (lines[lines.length - 1] === ""){ + lines = lines.slice(0,-1); + } + lines + .map((line,index) => `${(index + 1).toString().padStart(6)}\t${line}`) + .forEach(line => console.log(line)) + + } else if (option.b){ + let lines = content.split("\n"); + let linesCounter = 0; + if (lines[lines.length - 1] === ""){ + lines = lines.slice(0,-1); + } + lines + .map((line) => { + if (line.trim().length > 0) { + linesCounter++; + return `${linesCounter.toString().padStart(6)}\t${line}` + }else{ + return `${line}` + }} + ) + .forEach(line => console.log(line)) + }else { + const output = content.endsWith("\n") ? content.slice(0,-1) : content; + console.log(output); + } +} diff --git a/implement-shell-tools/ls/ls-commander.js b/implement-shell-tools/ls/ls-commander.js new file mode 100644 index 00000000..9ca8c442 --- /dev/null +++ b/implement-shell-tools/ls/ls-commander.js @@ -0,0 +1,36 @@ +import { program } from "commander"; +import { promises as fs } from "node:fs"; +import process from "node:process"; + +program + .name("list-files") + .description("List files in a directory") + .option("-1", "Show line by line") + .option("-a, --all", "Show hidden files") + .argument("[paths...]", "The directory paths",["."]); + +program.parse(); + +const argv = program.args; + +if (argv.length != 1){ + console.error(`Expected exactly 1 argument (a path) to be passed but got ${argv.length}`); + process.exit(1); +} +const dirPath = argv[0]; +const options = program.opts(); + +const entries = await fs.readdir(dirPath) + +const listFiles = entries.map((entry) => `${entry.padStart(6)}` ) +const noHidden = listFiles.filter(name => !name.startsWith('.')); + +if (options.all && options[1]){ + console.log(listFiles.join('\n')); +} else if (options.all) { + console.log(listFiles.join(' ')); +} else if (options[1]){ + console.log(noHidden.join('\n')); +} else { + console.log(noHidden.join(' ')); +} diff --git a/implement-shell-tools/wc/wc-commander.js b/implement-shell-tools/wc/wc-commander.js new file mode 100644 index 00000000..b1c28b2c --- /dev/null +++ b/implement-shell-tools/wc/wc-commander.js @@ -0,0 +1,61 @@ +import {program} from "commander"; +import { promises as fs } from "node:fs"; +import process from "node:process"; +import { glob } from "glob"; + +program + .name("content counter") + .description("Counts lines, words, or characters in a file or files of a directory.") + .option("-l,--lines","Options for l:Lines.") + .option("-w,--words","Options for w:words.") + .option("-c","Options for c:characters.") + .argument("","The file or directory to process"); + +program.parse(); + +const argv = program.args; + +const options = program.opts(); + +const result = []; +let totalLines = 0; +let totalWords = 0; +let totalChars = 0; + +for (const file of argv){ + + //1. Read each file + + const content = await fs.readFile(file,"utf-8"); + // 2. calculate values + const lines = content.split(/\n/g).length - 1 ; + const words = content.trim().split(/\s+/).length; + const chars = content.split("").length; + // variable to pile up final line results according options + const thisLine = []; + if (options.lines){ thisLine.push(lines.toString().padStart(8)) }; + if (options.words){ thisLine.push(words.toString().padStart(8)) }; + if (options.c){ thisLine.push(chars.toString().padStart(8)) }; + if (!options.lines && !options.words && !options.c){ thisLine.push(lines.toString().padStart(8),words.toString().padStart(8),chars.toString().padStart(8)) }; + result.push(`${thisLine.join("")} ${file}`) + // 3. add results + // variables to accumulate the total + totalLines += lines; + totalWords += words; + totalChars += chars; +// 4. add total results if several files +} +if (argv.length>1){ + const finalLine = []; + finalLine.push(totalLines.toString().padStart(8)), + finalLine.push(totalWords.toString().padStart(8)), + finalLine.push(totalChars.toString().padStart(8)); + result.push(`${finalLine.join("")} total`) +} +// 5. show results +const output = result.join('\n'); +console.log(output) + + + + From 555dd443012cca3497db785d186888760e9440df Mon Sep 17 00:00:00 2001 From: Emiliano Uruena Date: Tue, 25 Nov 2025 17:07:35 +0000 Subject: [PATCH 3/3] Delete number-systems/README.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit File doesn’t belong to this branch. --- number-systems/README.md | 66 ---------------------------------------- 1 file changed, 66 deletions(-) delete mode 100644 number-systems/README.md diff --git a/number-systems/README.md b/number-systems/README.md deleted file mode 100644 index 322d053c..00000000 --- a/number-systems/README.md +++ /dev/null @@ -1,66 +0,0 @@ -Do not use any tools or programming to solve these problems. Work it out yourself by hand, and fill in the answers. - -Do not convert any binary numbers to decimal when solving a question unless the question explicitly tells you to. - -The goal of these exercises is for you to gain an intuition for binary numbers. Using tools to solve the problems defeats the point. - -Convert the decimal number 14 to binary. -Answer: 1110 - -Convert the binary number 101101 to decimal: -Answer: 45 - -Which is larger: 1000 or 0111? -Answer: 1000 - -Which is larger: 00100 or 01011? -Answer: 01011 - -What is 10101 + 01010? -Answer: 11111 - -What is 10001 + 10001? -Answer:100010 - -What's the largest number you can store with 4 bits, if you want to be able to represent the number 0? -Answer: 1111 = 15 (largest representable number) - Number of possible values including 0 = 16 - -How many bits would you need in order to store the numbers between 0 and 255 inclusive? -Answer: 8 bits - -How many bits would you need in order to store the numbers between 0 and 3 inclusive? -Answer: 2 bits => 00, 01, 10, 11 - -How many bits would you need in order to store the numbers between 0 and 1000 inclusive? -Answer: 10 bits - -How can you test if a binary number is a power of two (e.g. 1, 2, 4, 8, 16, ...)? -Answer: Been a binary it would be 1 followed by 0s => 100, 10000, 10 - -Convert the decimal number 14 to hex. -Answer:0xE - -Convert the decimal number 386 to hex. -Answer: 0x182 - -Convert the hex number 386 to decimal. -Answer: 902 - -Convert the hex number B to decimal. -Answer:11 - -If reading the byte 0x21 as a number, what decimal number would it mean? -Answer:33 - -If reading the byte 0x21 as an ASCII character, what character would it mean? -Answer:! - -If reading the byte 0x21 as a greyscale colour, as described in "Approaches for Representing Colors and Images", what colour would it mean? -Answer: Dark Grey - -If reading the bytes 0xAA00FF as an RGB colour, as described in "Approaches for Representing Colors and Images", what colour would it mean? -Answer:Purple (magenta) - -If reading the bytes 0xAA00FF as a sequence of three one-byte decimal numbers, what decimal numbers would they be? -Answer: 170 0 255