From 350fa2ffaef42eba80dffab9766870c6983d3b15 Mon Sep 17 00:00:00 2001 From: Daniel Wagner-Hall Date: Wed, 26 Feb 2025 12:31:32 +0000 Subject: [PATCH 1/5] Sample solutions --- implement-cowsay/cow.py | 12 ++++ implement-cowsay/requirements.txt | 1 + implement-shell-tools/cat/cat.js | 56 +++++++++++++++++++ implement-shell-tools/cat/cat.py | 45 +++++++++++++++ implement-shell-tools/cat/package-lock.json | 21 +++++++ implement-shell-tools/cat/package.json | 6 ++ implement-shell-tools/ls/ls.js | 38 +++++++++++++ implement-shell-tools/ls/ls.py | 31 ++++++++++ implement-shell-tools/ls/package-lock.json | 21 +++++++ implement-shell-tools/ls/package.json | 6 ++ implement-shell-tools/wc/package-lock.json | 21 +++++++ implement-shell-tools/wc/package.json | 6 ++ implement-shell-tools/wc/wc.js | 50 +++++++++++++++++ implement-shell-tools/wc/wc.py | 45 +++++++++++++++ individual-shell-tools/awk/script-01.sh | 1 + individual-shell-tools/awk/script-02.sh | 3 + individual-shell-tools/awk/script-03.sh | 1 + individual-shell-tools/awk/script-04.sh | 1 + individual-shell-tools/awk/script-05.sh | 1 + .../awk/script-06-stretch.sh | 1 + .../awk/script-07-stretch.sh | 1 + individual-shell-tools/cat/script-01.sh | 1 + individual-shell-tools/cat/script-02.sh | 1 + individual-shell-tools/cat/script-03.sh | 1 + .../cat/script-04-stretch.sh | 1 + individual-shell-tools/grep/script-01.sh | 1 + individual-shell-tools/grep/script-02.sh | 1 + individual-shell-tools/grep/script-03.sh | 1 + individual-shell-tools/grep/script-04.sh | 1 + individual-shell-tools/grep/script-05.sh | 1 + individual-shell-tools/grep/script-06.sh | 1 + individual-shell-tools/grep/script-07.sh | 1 + individual-shell-tools/ls/script-01.sh | 1 + individual-shell-tools/ls/script-02.sh | 1 + individual-shell-tools/ls/script-03.sh | 1 + individual-shell-tools/ls/script-04.sh | 3 +- individual-shell-tools/sed/script-01.sh | 1 + individual-shell-tools/sed/script-02.sh | 1 + individual-shell-tools/sed/script-03.sh | 1 + individual-shell-tools/sed/script-04.sh | 1 + individual-shell-tools/sed/script-05.sh | 1 + individual-shell-tools/sed/script-06.sh | 1 + individual-shell-tools/wc/script-01.sh | 1 + individual-shell-tools/wc/script-02.sh | 1 + individual-shell-tools/wc/script-03.sh | 1 + jq/script-01.sh | 1 + jq/script-02.sh | 1 + jq/script-03.sh | 3 + jq/script-04.sh | 1 + jq/script-05.sh | 1 + jq/script-06.sh | 1 + jq/script-07.sh | 1 + jq/script-08.sh | 1 + jq/script-09.sh | 1 + jq/script-10.sh | 1 + jq/script-11.sh | 1 + number-systems/README.md | 40 ++++++------- shell-pipelines/ls-grep/script-01.sh | 1 + shell-pipelines/ls-grep/script-02.sh | 1 + shell-pipelines/ls-grep/script-03.sh | 1 + shell-pipelines/ls-grep/script-04.sh | 1 + .../sort-uniq-head-tail/script-01.sh | 1 + .../sort-uniq-head-tail/script-02.sh | 1 + .../sort-uniq-head-tail/script-03.sh | 1 + .../sort-uniq-head-tail/script-04.sh | 1 + .../sort-uniq-head-tail/script-05.sh | 1 + .../sort-uniq-head-tail/script-06.sh | 1 + .../sort-uniq-head-tail/script-07.sh | 1 + shell-pipelines/tr/script-01.sh | 1 + shell-pipelines/tr/script-02.sh | 1 + 70 files changed, 439 insertions(+), 21 deletions(-) create mode 100644 implement-cowsay/cow.py create mode 100644 implement-cowsay/requirements.txt create mode 100644 implement-shell-tools/cat/cat.js create mode 100644 implement-shell-tools/cat/cat.py create mode 100644 implement-shell-tools/cat/package-lock.json create mode 100644 implement-shell-tools/cat/package.json create mode 100644 implement-shell-tools/ls/ls.js create mode 100644 implement-shell-tools/ls/ls.py create mode 100644 implement-shell-tools/ls/package-lock.json create mode 100644 implement-shell-tools/ls/package.json create mode 100644 implement-shell-tools/wc/package-lock.json create mode 100644 implement-shell-tools/wc/package.json create mode 100644 implement-shell-tools/wc/wc.js create mode 100644 implement-shell-tools/wc/wc.py diff --git a/implement-cowsay/cow.py b/implement-cowsay/cow.py new file mode 100644 index 000000000..a303d4997 --- /dev/null +++ b/implement-cowsay/cow.py @@ -0,0 +1,12 @@ +import argparse +import cowsay + +parser = argparse.ArgumentParser( + prog="cowsay", + description="Make animals say things", +) +parser.add_argument("--animal", default="cow", help="The animal to be saying things.", choices=cowsay.char_names) +parser.add_argument("message", nargs="+", help="The message to say.") +args = parser.parse_args() + +cowsay.char_funcs[args.animal](" ".join(args.message)) diff --git a/implement-cowsay/requirements.txt b/implement-cowsay/requirements.txt new file mode 100644 index 000000000..c6b9ffd0e --- /dev/null +++ b/implement-cowsay/requirements.txt @@ -0,0 +1 @@ +cowsay diff --git a/implement-shell-tools/cat/cat.js b/implement-shell-tools/cat/cat.js new file mode 100644 index 000000000..3866556cc --- /dev/null +++ b/implement-shell-tools/cat/cat.js @@ -0,0 +1,56 @@ +import { program } from "commander"; +import { promises as fs } from "node:fs"; + +program + .name("cat") + .description("Concatenate and print files") + .option("-n", "Number lines") + .option("-b", "Number non-blank lines"); + +program.parse(); + +function parseNumberMode(options) { + if (options.b) { + return "non-blank"; + } else if (options.n) { + return "all"; + } else { + return "none"; + } +} + +function calculatePrefix(numberMode, nonBlankLineNumber, lineNumberIncludingBlanks, thisLineIsBlank) { + if (numberMode === "none") { + return ""; + } + let lineNumber; + if (numberMode === "all") { + lineNumber = lineNumberIncludingBlanks; + } else { + if (thisLineIsBlank) { + return ""; + } else { + lineNumber = nonBlankLineNumber; + } + } + return lineNumber.toString().padStart(6, " ") + " "; +} + +const numberMode = parseNumberMode(program.opts()); + +for (const path of program.args) { + const content = await fs.readFile(path, "utf-8"); + const lines = content.split("\n"); + let nonBlankLineNumber = 1; + for (let i = 0; i < lines.length; i++) { + const line = lines[i]; + if (i === lines.length - 1 && line === "") { + break; + } + const prefix = calculatePrefix(numberMode, nonBlankLineNumber, i + 1, line === ""); + console.log(`${prefix}${line}`); + if (line !== "") { + nonBlankLineNumber += 1; + } + } +} diff --git a/implement-shell-tools/cat/cat.py b/implement-shell-tools/cat/cat.py new file mode 100644 index 000000000..21f3c5441 --- /dev/null +++ b/implement-shell-tools/cat/cat.py @@ -0,0 +1,45 @@ +import argparse + +parser = argparse.ArgumentParser( + prog="cat", + description="Concatenate and print files", +) +parser.add_argument("-n", action="store_true", help="Number lines") +parser.add_argument("-b", action="store_true", help="Number non-blank lines") +parser.add_argument("paths", nargs="*", help="Paths to concatenate and print") +args = parser.parse_args() + +def parseNumberMode(): + if args.b: + return "non-blank" + elif args.n: + return "all" + else: + return "none" + + +def calculatePrefix(numberMode, nonBlankLineNumber, lineNumberIncludingBlanks, thisLineIsBlank): + if numberMode == "none": + return "" + if numberMode == "all": + lineNumber = lineNumberIncludingBlanks + elif thisLineIsBlank: + return "" + else: + lineNumber = nonBlankLineNumber + return f"{lineNumber}".rjust(6, " ") + " " + +numberMode = parseNumberMode() + +for path in args.paths: + with open(path, "r") as f: + content = f.read() + lines = content.split("\n"); + nonBlankLineNumber = 1; + for i, line in enumerate(lines): + if i == len(lines) - 1 and line == "": + break + prefix = calculatePrefix(numberMode, nonBlankLineNumber, i + 1, line == "") + print(f"{prefix}{line}"); + if line != "": + nonBlankLineNumber += 1 diff --git a/implement-shell-tools/cat/package-lock.json b/implement-shell-tools/cat/package-lock.json new file mode 100644 index 000000000..10cb6a0d2 --- /dev/null +++ b/implement-shell-tools/cat/package-lock.json @@ -0,0 +1,21 @@ +{ + "name": "cat", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "dependencies": { + "commander": "^12.1.0" + } + }, + "node_modules/commander": { + "version": "12.1.0", + "resolved": "https://registry.npmjs.org/commander/-/commander-12.1.0.tgz", + "integrity": "sha512-Vw8qHK3bZM9y/P10u3Vib8o/DdkvA2OtPtZvD871QKjy74Wj1WSKFILMPRPSdUSx5RFK1arlJzEtA4PkFgnbuA==", + "license": "MIT", + "engines": { + "node": ">=18" + } + } + } +} diff --git a/implement-shell-tools/cat/package.json b/implement-shell-tools/cat/package.json new file mode 100644 index 000000000..bb215295b --- /dev/null +++ b/implement-shell-tools/cat/package.json @@ -0,0 +1,6 @@ +{ + "type": "module", + "dependencies": { + "commander": "^12.1.0" + } +} diff --git a/implement-shell-tools/ls/ls.js b/implement-shell-tools/ls/ls.js new file mode 100644 index 000000000..76d4cd7b0 --- /dev/null +++ b/implement-shell-tools/ls/ls.js @@ -0,0 +1,38 @@ +import { program } from "commander"; +import { promises as fs } from "node:fs"; + +program + .name("ls") + .description("list directory contents") + .option("-1", "Force output to be one entry per line.") + .option("-a", "Include directory entries whose names begin with a dot (`.`)."); + +program.parse(); +const paths = program.args.length === 0 ? ["."] : program.args; +for (let i = 0; i < paths.length; i++) { + const path = paths[i]; + const readChildren = await fs.readdir(path); + const children = []; + let longest = 0; + if (program.opts().a) { + children.push(".", ".."); + } + for (const child of readChildren) { + if (program.opts().a || !child.startsWith(".")) { + children.push(child); + longest = Math.max(longest, child.length); + } + } + const padTo = longest + (longest % 8 === 0 ? 0 : 8 - (longest % 8)); + if (i > 0) { + console.log(); + } + if (paths.length > 1) { + console.log(`${path}:`); + } + if (program.opts()["1"]) { + console.log(children.join("\n")); + } else { + console.log(children.map((child) => child.padEnd(padTo)).join("").trimEnd()); + } +} diff --git a/implement-shell-tools/ls/ls.py b/implement-shell-tools/ls/ls.py new file mode 100644 index 000000000..844f86eb4 --- /dev/null +++ b/implement-shell-tools/ls/ls.py @@ -0,0 +1,31 @@ +import argparse +import os + +parser = argparse.ArgumentParser( + prog="ls", + description="List directory contents", +) +parser.add_argument("-1", action="store_true", help="Force output to be one entry per line.") +parser.add_argument("-a", action="store_true", help="Include directory entries whose names begin with a dot (`.`).") +parser.add_argument("paths", nargs="*", default=["."], help="Paths to list") +args = parser.parse_args() + +for i, path in enumerate(args.paths): + read_children = os.listdir(path) + children = [] + longest = 0 + if args.a: + children.extend([".", ".."]) + for child in sorted(read_children): + if args.a or not child.startswith("."): + children.append(child) + longest = max(longest, len(child)) + padTo = longest + (0 if longest % 8 == 0 else 8 - (longest % 8)) + if i > 0: + print() + if len(args.paths) > 1: + print(f"{path}:") + if getattr(args, "1"): + print("\n".join(children)) + else: + print("".join(map(lambda child: child.ljust(padTo), children)).rstrip()) diff --git a/implement-shell-tools/ls/package-lock.json b/implement-shell-tools/ls/package-lock.json new file mode 100644 index 000000000..f916c39ef --- /dev/null +++ b/implement-shell-tools/ls/package-lock.json @@ -0,0 +1,21 @@ +{ + "name": "ls", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "dependencies": { + "commander": "^12.1.0" + } + }, + "node_modules/commander": { + "version": "12.1.0", + "resolved": "https://registry.npmjs.org/commander/-/commander-12.1.0.tgz", + "integrity": "sha512-Vw8qHK3bZM9y/P10u3Vib8o/DdkvA2OtPtZvD871QKjy74Wj1WSKFILMPRPSdUSx5RFK1arlJzEtA4PkFgnbuA==", + "license": "MIT", + "engines": { + "node": ">=18" + } + } + } +} diff --git a/implement-shell-tools/ls/package.json b/implement-shell-tools/ls/package.json new file mode 100644 index 000000000..bb215295b --- /dev/null +++ b/implement-shell-tools/ls/package.json @@ -0,0 +1,6 @@ +{ + "type": "module", + "dependencies": { + "commander": "^12.1.0" + } +} diff --git a/implement-shell-tools/wc/package-lock.json b/implement-shell-tools/wc/package-lock.json new file mode 100644 index 000000000..12ffe6868 --- /dev/null +++ b/implement-shell-tools/wc/package-lock.json @@ -0,0 +1,21 @@ +{ + "name": "wc", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "dependencies": { + "commander": "^12.1.0" + } + }, + "node_modules/commander": { + "version": "12.1.0", + "resolved": "https://registry.npmjs.org/commander/-/commander-12.1.0.tgz", + "integrity": "sha512-Vw8qHK3bZM9y/P10u3Vib8o/DdkvA2OtPtZvD871QKjy74Wj1WSKFILMPRPSdUSx5RFK1arlJzEtA4PkFgnbuA==", + "license": "MIT", + "engines": { + "node": ">=18" + } + } + } +} diff --git a/implement-shell-tools/wc/package.json b/implement-shell-tools/wc/package.json new file mode 100644 index 000000000..bb215295b --- /dev/null +++ b/implement-shell-tools/wc/package.json @@ -0,0 +1,6 @@ +{ + "type": "module", + "dependencies": { + "commander": "^12.1.0" + } +} diff --git a/implement-shell-tools/wc/wc.js b/implement-shell-tools/wc/wc.js new file mode 100644 index 000000000..806943399 --- /dev/null +++ b/implement-shell-tools/wc/wc.js @@ -0,0 +1,50 @@ +import { program } from "commander"; +import { promises as fs } from "node:fs"; + +program + .name("wc") + .description("word, line, and byte count") + .option("-c", "Output the number of bytes") + .option("-w", "Output the number of words") + .option("-l", "Output the number of lines"); + +program.parse(); + +const opts = program.opts(); +const showAll = !(opts.c || opts.w || opts.l); +const showBytes = opts.c || showAll; +const showWords = opts.w || showAll; +const showLines = opts.l || showAll; + +function formatOutputLine(bytes, words, lines, path) { + let line = ""; + if (showLines) { + line += lines.toString().padStart(8); + } + if (showWords) { + line += words.toString().padStart(8); + } + if (showBytes) { + line += bytes.toString().padStart(8); + } + line += " " + path; + return line; +} + +let totalBytes = 0; +let totalWords = 0; +let totalLines = 0; +for (const path of program.args) { + const content = await fs.readFile(path, "utf-8"); + const bytes = content.length; + const words = content.split(/\s+/).filter((word) => word.length > 0).length; + const rawLines = content.split("\n").length; + const lines = (content.slice(-1) === "\n") ? rawLines - 1 : rawLines; + totalBytes += bytes; + totalWords += words; + totalLines += lines; + console.log(formatOutputLine(bytes, words, lines, path)); +} +if (program.args.length > 1) { + console.log(formatOutputLine(totalBytes, totalWords, totalLines, "total")); +} diff --git a/implement-shell-tools/wc/wc.py b/implement-shell-tools/wc/wc.py new file mode 100644 index 000000000..37c3a5770 --- /dev/null +++ b/implement-shell-tools/wc/wc.py @@ -0,0 +1,45 @@ +import argparse + +parser = argparse.ArgumentParser( + prog="wc", + description="word, line, and byte count", +) +parser.add_argument("-c", action="store_true", help="Output the number of bytes.") +parser.add_argument("-w", action="store_true", help="Output the number of words.") +parser.add_argument("-l", action="store_true", help="Output the number of lines.") +parser.add_argument("paths", nargs="+", help="Paths to list") +args = parser.parse_args() + +show_all = not (args.c or args.w or args.l) +show_bytes = args.c or show_all +show_words = args.w or show_all +show_lines = args.l or show_all + +def format_output_line(bytes, words, lines, path): + line = "" + if show_lines: + line += f"{lines}".rjust(8) + if show_words: + line += f"{words}".rjust(8) + if show_bytes: + line += f"{bytes}".rjust(8) + line += " " + path + return line + +total_bytes = 0 +total_words = 0 +total_lines = 0 +for path in args.paths: + with open(path, "r") as f: + content = f.read() + bytes = len(content) + words = len(content.split()) + raw_lines = len(content.split("\n")) + lines = raw_lines - 1 if content[-1] == "\n" else raw_lines + total_bytes += bytes + total_words += words + total_lines += lines + print(format_output_line(bytes, words, lines, path)) + +if len(args.paths) > 1: + print(format_output_line(total_bytes, total_words, total_lines, "total")) diff --git a/individual-shell-tools/awk/script-01.sh b/individual-shell-tools/awk/script-01.sh index 8db4390af..90e2397d6 100755 --- a/individual-shell-tools/awk/script-01.sh +++ b/individual-shell-tools/awk/script-01.sh @@ -4,3 +4,4 @@ set -euo pipefail # TODO: Write a command to output just the names of each player in `scores-table.txt`. # Your output should contain 6 lines, each with just one word on it. +awk '{print $1}' scores-table.txt diff --git a/individual-shell-tools/awk/script-02.sh b/individual-shell-tools/awk/script-02.sh index 5956be9bd..a9e6a11c6 100755 --- a/individual-shell-tools/awk/script-02.sh +++ b/individual-shell-tools/awk/script-02.sh @@ -4,3 +4,6 @@ set -euo pipefail # TODO: Write a command to output the names of each player, as well as their city. # Your output should contain 6 lines, each with two words on it, separated by a space. +awk '{print $1, $2}' scores-table.txt +# Or alternatively, if we need more control over the formatting: +awk '{printf "%s %s\n", $1, $2}' scores-table.txt diff --git a/individual-shell-tools/awk/script-03.sh b/individual-shell-tools/awk/script-03.sh index af7c6e8b9..9c2945bff 100755 --- a/individual-shell-tools/awk/script-03.sh +++ b/individual-shell-tools/awk/script-03.sh @@ -5,3 +5,4 @@ set -euo pipefail # TODO: Write a command to output just the names of each player along with the score from their first attempt. # Your output should contain 6 lines, each with one word and one number on it. # The first line should be "Ahmed 1". +awk '{printf "%s %s\n", $1, $3}' scores-table.txt diff --git a/individual-shell-tools/awk/script-04.sh b/individual-shell-tools/awk/script-04.sh index bf15703c7..e774cf9bb 100755 --- a/individual-shell-tools/awk/script-04.sh +++ b/individual-shell-tools/awk/script-04.sh @@ -5,3 +5,4 @@ set -euo pipefail # TODO: Write a command to output just the names of each player in London along with the score from their last attempt. # Your output should contain 3 lines, each with one word and one number on it. # The first line should be "Ahmed 4". +awk '{printf "%s %s\n", $1, $NF}' scores-table.txt diff --git a/individual-shell-tools/awk/script-05.sh b/individual-shell-tools/awk/script-05.sh index d1680cb02..53aafa226 100755 --- a/individual-shell-tools/awk/script-05.sh +++ b/individual-shell-tools/awk/script-05.sh @@ -5,3 +5,4 @@ set -euo pipefail # TODO: Write a command to output just the names of each player along with the number of times they've played the game. # Your output should contain 6 lines, each with one word and one number on it. # The first line should be "Ahmed 3". +awk '{printf "%s %d\n", $1, NF-2}' scores-table.txt diff --git a/individual-shell-tools/awk/script-06-stretch.sh b/individual-shell-tools/awk/script-06-stretch.sh index 0201e6378..841f60196 100755 --- a/individual-shell-tools/awk/script-06-stretch.sh +++ b/individual-shell-tools/awk/script-06-stretch.sh @@ -6,3 +6,4 @@ set -euo pipefail # TODO: Write a command to output the total of adding together all players' first scores. # Your output should be exactly the number 54. +awk '{sum += $3} END {print sum}' scores-table.txt diff --git a/individual-shell-tools/awk/script-07-stretch.sh b/individual-shell-tools/awk/script-07-stretch.sh index 3f7155880..fd95b80a1 100755 --- a/individual-shell-tools/awk/script-07-stretch.sh +++ b/individual-shell-tools/awk/script-07-stretch.sh @@ -7,3 +7,4 @@ set -euo pipefail # TODO: Write a command to output just the names of each player along with the total of adding all of that player's scores. # Your output should contain 6 lines, each with one word and one number on it. # The first line should be "Ahmed 15". The second line should be "Basia 37" +awk '{total=0 ; for (i=2; i<=NF; i++) {total += $i} ; print $1, total}' scores-table.txt diff --git a/individual-shell-tools/cat/script-01.sh b/individual-shell-tools/cat/script-01.sh index c85053e0f..edcc54fb4 100755 --- a/individual-shell-tools/cat/script-01.sh +++ b/individual-shell-tools/cat/script-01.sh @@ -4,3 +4,4 @@ set -euo pipefail # TODO: Write a command to output the contents of the helper-1.txt file inside the helper-files directory to the terminal. # The output of this command should be "Once upon a time...". +cat ../helper-files/helper-1.txt diff --git a/individual-shell-tools/cat/script-02.sh b/individual-shell-tools/cat/script-02.sh index 01bbd5eab..d8187ff42 100755 --- a/individual-shell-tools/cat/script-02.sh +++ b/individual-shell-tools/cat/script-02.sh @@ -11,3 +11,4 @@ set -euo pipefail # It looked delicious. # I was tempted to take a bite of it. # But this seemed like a bad idea... +cat ../helper-files/* diff --git a/individual-shell-tools/cat/script-03.sh b/individual-shell-tools/cat/script-03.sh index 37573b0c1..dd6b1d8b1 100755 --- a/individual-shell-tools/cat/script-03.sh +++ b/individual-shell-tools/cat/script-03.sh @@ -9,3 +9,4 @@ set -euo pipefail # 1 It looked delicious. # 2 I was tempted to take a bite of it. # 3 But this seemed like a bad idea... +cat -n ../helper-files/helper-3.txt diff --git a/individual-shell-tools/cat/script-04-stretch.sh b/individual-shell-tools/cat/script-04-stretch.sh index 00fe3c48b..70f8fdb68 100755 --- a/individual-shell-tools/cat/script-04-stretch.sh +++ b/individual-shell-tools/cat/script-04-stretch.sh @@ -13,3 +13,4 @@ set -euo pipefail # 3 It looked delicious. # 4 I was tempted to take a bite of it. # 5 But this seemed like a bad idea... +cat ../helper-files/* | cat -n diff --git a/individual-shell-tools/grep/script-01.sh b/individual-shell-tools/grep/script-01.sh index fb05f42f2..f374150c9 100755 --- a/individual-shell-tools/grep/script-01.sh +++ b/individual-shell-tools/grep/script-01.sh @@ -4,3 +4,4 @@ set -euo pipefail # TODO: Write a command to output every line in dialogue.txt said by the Doctor. # The output should contain 6 lines. +grep "^Doctor:" dialogue.txt diff --git a/individual-shell-tools/grep/script-02.sh b/individual-shell-tools/grep/script-02.sh index df6f85640..31d357030 100755 --- a/individual-shell-tools/grep/script-02.sh +++ b/individual-shell-tools/grep/script-02.sh @@ -4,3 +4,4 @@ set -euo pipefail # TODO: Write a command to output every line in dialogue.txt that contains the word Doctor (regardless of case). # The output should contain 9 lines. +grep -i "doctor" dialogue.txt diff --git a/individual-shell-tools/grep/script-03.sh b/individual-shell-tools/grep/script-03.sh index 5383fe578..f6f25b87c 100755 --- a/individual-shell-tools/grep/script-03.sh +++ b/individual-shell-tools/grep/script-03.sh @@ -4,3 +4,4 @@ set -euo pipefail # TODO: Write a command to output the number of lines in dialogue.txt that contain the word Doctor (regardless of case). # The output should be exactly the number 9. +grep -c -i "doctor" dialogue.txt diff --git a/individual-shell-tools/grep/script-04.sh b/individual-shell-tools/grep/script-04.sh index 80ee04776..886a42edd 100755 --- a/individual-shell-tools/grep/script-04.sh +++ b/individual-shell-tools/grep/script-04.sh @@ -4,3 +4,4 @@ set -euo pipefail # TODO: Write a command to output every line in dialogue.txt that does not contain the word "Hello" (regardless of case). # The output should contain 10 lines. +grep -v -i "hello" dialogue.txt diff --git a/individual-shell-tools/grep/script-05.sh b/individual-shell-tools/grep/script-05.sh index 1eb538185..8754273d3 100755 --- a/individual-shell-tools/grep/script-05.sh +++ b/individual-shell-tools/grep/script-05.sh @@ -4,3 +4,4 @@ set -euo pipefail # TODO: Write a command to output every line in dialogue.txt that contains the string "cure", as well as the line before that line. # The output should contain two pairs of two lines of text (with a separator between them). +grep -B1 "cure" dialogue.txt diff --git a/individual-shell-tools/grep/script-06.sh b/individual-shell-tools/grep/script-06.sh index 5670e3b6c..0e8244ff7 100755 --- a/individual-shell-tools/grep/script-06.sh +++ b/individual-shell-tools/grep/script-06.sh @@ -4,3 +4,4 @@ set -euo pipefail # TODO: Write a command to output the name of every `.txt` file in this directory which contains a line of dialogue said by the Doctor. # The output should contain two filenames. +grep -l "^Doctor:" *.txt diff --git a/individual-shell-tools/grep/script-07.sh b/individual-shell-tools/grep/script-07.sh index 9670ebad9..9fabc87f0 100755 --- a/individual-shell-tools/grep/script-07.sh +++ b/individual-shell-tools/grep/script-07.sh @@ -4,3 +4,4 @@ set -euo pipefail # TODO: Write a command to output, for each `.txt` file in this directory, how many lines of dialogue the Doctor has. # The output should show that dialogue.txt contains 6 lines, dialogue-2.txt contains 2, and dialogue-3.txt contains 0. +grep -c "^Doctor:" *.txt diff --git a/individual-shell-tools/ls/script-01.sh b/individual-shell-tools/ls/script-01.sh index 241b62f5e..c9cd189b5 100755 --- a/individual-shell-tools/ls/script-01.sh +++ b/individual-shell-tools/ls/script-01.sh @@ -13,3 +13,4 @@ fi # TODO: Write a command to list the files and folders in this directory. # The output should be a list of names including child-directory, script-01.sh, script-02.sh, and more. +ls diff --git a/individual-shell-tools/ls/script-02.sh b/individual-shell-tools/ls/script-02.sh index d0a5a10f4..d37175729 100755 --- a/individual-shell-tools/ls/script-02.sh +++ b/individual-shell-tools/ls/script-02.sh @@ -4,3 +4,4 @@ set -euo pipefail # TODO: Write a command which lists all of the files in the directory named child-directory. # The output should be a list of names: helper-1.txt, helper-2.txt, helper-3.txt. +ls child-directory diff --git a/individual-shell-tools/ls/script-03.sh b/individual-shell-tools/ls/script-03.sh index 781216d21..5161171ec 100755 --- a/individual-shell-tools/ls/script-03.sh +++ b/individual-shell-tools/ls/script-03.sh @@ -5,3 +5,4 @@ set -euo pipefail # TODO: Write a command which _recursively_ lists all of the files and folders in this directory _and_ all of the files inside those folders. # The output should be a list of names including: child-directory, script-01.sh, helper-1.txt (and more). # The formatting of the output doesn't matter. +ls -R diff --git a/individual-shell-tools/ls/script-04.sh b/individual-shell-tools/ls/script-04.sh index 72f3817b3..b4bdb1f57 100755 --- a/individual-shell-tools/ls/script-04.sh +++ b/individual-shell-tools/ls/script-04.sh @@ -15,9 +15,10 @@ echo "First exercise (sorted newest to oldest):" # TODO: Write a command which lists the files in the child-directory directory, one per line, sorted so that the most recently modified file is first. # The output should be a list of names in this order, one per line: helper-3.txt, helper-1.txt, helper-2.txt. - +ls -1 -t child-directory echo "Second exercise (sorted oldest to newest):" # TODO: Write a command which does the same as above, but sorted in the opposite order (oldest first). # The output should be a list of names in this order, one per line: helper-2.txt, helper-1.txt, helper-3.txt. +ls -1 -t -r child-directory diff --git a/individual-shell-tools/sed/script-01.sh b/individual-shell-tools/sed/script-01.sh index 3eba6fa4d..ba68ff315 100755 --- a/individual-shell-tools/sed/script-01.sh +++ b/individual-shell-tools/sed/script-01.sh @@ -5,3 +5,4 @@ set -euo pipefail # TODO: Write a command to output input.txt with all occurrences of the letter `i` replaced with `I`. # The output should contain 11 lines. # The first line of the output should be: "ThIs Is a sample fIle for experImentIng with sed.". +sed -e 's/i/I/g' input.txt diff --git a/individual-shell-tools/sed/script-02.sh b/individual-shell-tools/sed/script-02.sh index abdd64d06..be485a0af 100755 --- a/individual-shell-tools/sed/script-02.sh +++ b/individual-shell-tools/sed/script-02.sh @@ -5,3 +5,4 @@ set -euo pipefail # TODO: Write a command to output input.txt with numbers removed. # The output should contain 11 lines. # Line 6 of the output should be " Alisha". +sed 's/[0-9]//g' input.txt diff --git a/individual-shell-tools/sed/script-03.sh b/individual-shell-tools/sed/script-03.sh index dd284a296..cebc09125 100755 --- a/individual-shell-tools/sed/script-03.sh +++ b/individual-shell-tools/sed/script-03.sh @@ -4,3 +4,4 @@ set -euo pipefail # TODO: Write a command to output input.txt removing any line which contains a number. # The output should contain 6 lines. +sed -e '/[0-9]/d' input.txt diff --git a/individual-shell-tools/sed/script-04.sh b/individual-shell-tools/sed/script-04.sh index 0052ac6c4..3b6ed6f18 100755 --- a/individual-shell-tools/sed/script-04.sh +++ b/individual-shell-tools/sed/script-04.sh @@ -4,3 +4,4 @@ set -euo pipefail # TODO: Write a command to output input.txt replacing every occurrence of the string "We'll" with "We will". # The output should contain 11 lines. +sed -e "s/We\'ll/We will/g" input.txt diff --git a/individual-shell-tools/sed/script-05.sh b/individual-shell-tools/sed/script-05.sh index 2dcc91a0c..d5305aa96 100755 --- a/individual-shell-tools/sed/script-05.sh +++ b/individual-shell-tools/sed/script-05.sh @@ -6,3 +6,4 @@ set -euo pipefail # If a line starts with a number and a space, make the line instead end with a space and the number. # So line 6 which currently reads "37 Alisha" should instead read "Alisha 37". # The output should contain 11 lines. +sed -e 's/^\([0-9]*\) \(.*\)$/\2 \1/' input.txt diff --git a/individual-shell-tools/sed/script-06.sh b/individual-shell-tools/sed/script-06.sh index 0b9390170..9d685b3b7 100755 --- a/individual-shell-tools/sed/script-06.sh +++ b/individual-shell-tools/sed/script-06.sh @@ -8,3 +8,4 @@ set -euo pipefail # The output should contain 11 lines. # Line 3 should be "It contains many lines, and there are some things you may want to do with each of them.". # Line 11 should be "We also should remember, when we go shopping, to get 4 items: oranges, cheese, bread, olives.". +sed -e 's/,\([^ ]\)/, \1/g' input.txt diff --git a/individual-shell-tools/wc/script-01.sh b/individual-shell-tools/wc/script-01.sh index c9dd6e5df..c944eeafe 100755 --- a/individual-shell-tools/wc/script-01.sh +++ b/individual-shell-tools/wc/script-01.sh @@ -4,3 +4,4 @@ set -euo pipefail # TODO: Write a command to output the number of words in the file helper-files/helper-3.txt. # The output should include the number 19. The output should not include the number 92. +wc -w ../helper-files/helper-3.txt diff --git a/individual-shell-tools/wc/script-02.sh b/individual-shell-tools/wc/script-02.sh index 8feeb1a62..16e530918 100755 --- a/individual-shell-tools/wc/script-02.sh +++ b/individual-shell-tools/wc/script-02.sh @@ -4,3 +4,4 @@ set -euo pipefail # TODO: Write a command to output the number of lines in the file helper-files/helper-3.txt. # The output should include the number 3. The output should not include the number 19. +wc -l ../helper-files/helper-3.txt diff --git a/individual-shell-tools/wc/script-03.sh b/individual-shell-tools/wc/script-03.sh index 6b2e9d3d1..6ac2210dd 100755 --- a/individual-shell-tools/wc/script-03.sh +++ b/individual-shell-tools/wc/script-03.sh @@ -8,3 +8,4 @@ set -euo pipefail # 1 7 39 ../helper-files/helper-2.txt # 3 19 92 ../helper-files/helper-3.txt # 5 30 151 total +wc ../helper-files/* diff --git a/jq/script-01.sh b/jq/script-01.sh index 95827f688..0d69705d1 100755 --- a/jq/script-01.sh +++ b/jq/script-01.sh @@ -5,3 +5,4 @@ set -euo pipefail # The input for this script is the person.json file. # TODO: Write a command to output the name of the person. # Your output should be exactly the string "Selma", but should not contain any quote characters. +jq -r '.name' person.json diff --git a/jq/script-02.sh b/jq/script-02.sh index 21544d67b..74a3ded85 100755 --- a/jq/script-02.sh +++ b/jq/script-02.sh @@ -5,3 +5,4 @@ set -euo pipefail # The input for this script is the person.json file. # TODO: Write a command to output the address of the person, all on one line, with a comma between each line. # Your output should be exactly the string "35 Fashion Street, London, E1 6PX", but should not contain any quote characters. +jq -r '.address | join(", ")' person.json diff --git a/jq/script-03.sh b/jq/script-03.sh index 3566f03ba..6a2f566a4 100755 --- a/jq/script-03.sh +++ b/jq/script-03.sh @@ -5,3 +5,6 @@ set -euo pipefail # The input for this script is the person.json file. # TODO: Write a command to output the name of the person, then a comma, then their profession. # Your output should be exactly the string "Selma, Software Engineer", but should not contain any quote characters. +jq -r '[ .name, .profession ] | join(", ")' person.json +# Or alternatively, we can do two selects in a string interpolation: +jq -r '"\(.name), \(.profession)"' person.json diff --git a/jq/script-04.sh b/jq/script-04.sh index 015997e18..e50f742a4 100755 --- a/jq/script-04.sh +++ b/jq/script-04.sh @@ -6,3 +6,4 @@ set -euo pipefail # TODO: Write a command to output just the names of each player, one per line. # Your output should contain 6 lines, each with just one word on it. # Your output should not contain any quote characters. +jq -r '.[].name' scores.json diff --git a/jq/script-05.sh b/jq/script-05.sh index 993fc9ee3..ab78fbad4 100755 --- a/jq/script-05.sh +++ b/jq/script-05.sh @@ -5,3 +5,4 @@ set -euo pipefail # The input for this script is the scores.json file. # TODO: Write a command to output the names of each player, as well as their city. # Your output should contain 6 lines, each with two words on it. +jq -r '.[] | "\(.name) \(.city)"' scores.json diff --git a/jq/script-06.sh b/jq/script-06.sh index 8b6e74c52..47fee35b5 100755 --- a/jq/script-06.sh +++ b/jq/script-06.sh @@ -6,3 +6,4 @@ set -euo pipefail # TODO: Write a command to output just the names of each player along with the score from their first attempt. # Your output should contain 6 lines, each with one word and one number on it. # The first line should be "Ahmed 1" with no quotes. +jq -r '.[] | "\(.name) \(.scores[0])"' scores.json diff --git a/jq/script-07.sh b/jq/script-07.sh index d43f93d1b..f20ab3d7f 100755 --- a/jq/script-07.sh +++ b/jq/script-07.sh @@ -6,3 +6,4 @@ set -euo pipefail # TODO: Write a command to output just the names of each player along with the score from their last attempt. # Your output should contain 6 lines, each with one word and one number on it. # The first line should be "Ahmed 4" with no quotes. +jq -r '.[] | "\(.name) \(.scores[-1])"' scores.json diff --git a/jq/script-08.sh b/jq/script-08.sh index 6671fd1ba..165e67a4e 100755 --- a/jq/script-08.sh +++ b/jq/script-08.sh @@ -6,3 +6,4 @@ set -euo pipefail # TODO: Write a command to output just the names of each player along with the number of times they've played the game. # Your output should contain 6 lines, each with one word and one number on it. # The first line should be "Ahmed 3" with no quotes. +jq -r '.[] | "\(.name) \(.scores | length)"' scores.json diff --git a/jq/script-09.sh b/jq/script-09.sh index c2536a536..3ce4c2b71 100755 --- a/jq/script-09.sh +++ b/jq/script-09.sh @@ -6,3 +6,4 @@ set -euo pipefail # TODO: Write a command to output just the names of each player along with the total scores from all of their games added together. # Your output should contain 6 lines, each with one word and one number on it. # The first line should be "Ahmed 15" with no quotes. +jq -r '.[] | "\(.name) \(.scores | add)"' scores.json diff --git a/jq/script-10.sh b/jq/script-10.sh index 8e9d75f07..8b1243257 100755 --- a/jq/script-10.sh +++ b/jq/script-10.sh @@ -5,3 +5,4 @@ set -euo pipefail # The input for this script is the scores.json file. # TODO: Write a command to output the total of adding together all players' first scores. # Your output should be exactly the number 54. +jq -r '[.[].scores[0]] | add' scores.json diff --git a/jq/script-11.sh b/jq/script-11.sh index d2337a6b2..7a7dba0a0 100755 --- a/jq/script-11.sh +++ b/jq/script-11.sh @@ -5,3 +5,4 @@ set -euo pipefail # The input for this script is the scores.json file. # TODO: Write a command to output the total of adding together all scores from all games from all players. # Your output should be exactly the number 164. +jq -r '[.[].scores[0]] | add' scores.json diff --git a/number-systems/README.md b/number-systems/README.md index 77a3bde94..6753debd9 100644 --- a/number-systems/README.md +++ b/number-systems/README.md @@ -5,61 +5,61 @@ 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: 15 How many bits would you need in order to store the numbers between 0 and 255 inclusive? -Answer: +Answer: 8 How many bits would you need in order to store the numbers between 0 and 3 inclusive? -Answer: +Answer: 3 How many bits would you need in order to store the numbers between 0 and 1000 inclusive? -Answer: +Answer: 10, but you'd have 23 spare values, so you can actually store between 0 and 1023 with 10 bits. How can you test if a binary number is a power of two (e.g. 1, 2, 4, 8, 16, ...)? -Answer: +Answer: Test that it has exactly one 1. Convert the decimal number 14 to hex. -Answer: +Answer: E Convert the decimal number 386 to hex. -Answer: +Answer: 182 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. 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 diff --git a/shell-pipelines/ls-grep/script-01.sh b/shell-pipelines/ls-grep/script-01.sh index 8c7d968a2..b77993b26 100755 --- a/shell-pipelines/ls-grep/script-01.sh +++ b/shell-pipelines/ls-grep/script-01.sh @@ -4,3 +4,4 @@ set -euo pipefail # TODO: Write a command to output the names of the files in the sample-files directory whose name contains at least one upper case letter. # Your output should contain 11 files. +ls sample-files | grep '[A-Z]' diff --git a/shell-pipelines/ls-grep/script-02.sh b/shell-pipelines/ls-grep/script-02.sh index 16f5f71d9..4fa4206cd 100755 --- a/shell-pipelines/ls-grep/script-02.sh +++ b/shell-pipelines/ls-grep/script-02.sh @@ -4,3 +4,4 @@ set -euo pipefail # TODO: Write a command to output the names of the files in the sample-files directory whose name starts with an upper case letter. # Your output should contain 10 files. +ls sample-files | grep '^[A-Z]' diff --git a/shell-pipelines/ls-grep/script-03.sh b/shell-pipelines/ls-grep/script-03.sh index a302ab036..9b79f1c8c 100755 --- a/shell-pipelines/ls-grep/script-03.sh +++ b/shell-pipelines/ls-grep/script-03.sh @@ -4,3 +4,4 @@ set -euo pipefail # TODO: Write a command to output the names of the files in the sample-files directory whose name starts with an upper case letter and doesn't contain any other upper case letters. # Your output should contain 7 files. +ls sample-files | grep '^[A-Z][^A-Z]*$' diff --git a/shell-pipelines/ls-grep/script-04.sh b/shell-pipelines/ls-grep/script-04.sh index c000b7e3b..b9b1fef9e 100755 --- a/shell-pipelines/ls-grep/script-04.sh +++ b/shell-pipelines/ls-grep/script-04.sh @@ -4,3 +4,4 @@ set -euo pipefail # TODO: Write a command to count the number of files in the sample-files directory whose name starts with an upper case letter and doesn't contain any other upper case letters. # Your output should be the number 7. +ls sample-files | grep '^[A-Z][^A-Z]*$' | wc -l diff --git a/shell-pipelines/sort-uniq-head-tail/script-01.sh b/shell-pipelines/sort-uniq-head-tail/script-01.sh index 171e1f989..845ad89da 100755 --- a/shell-pipelines/sort-uniq-head-tail/script-01.sh +++ b/shell-pipelines/sort-uniq-head-tail/script-01.sh @@ -5,3 +5,4 @@ set -euo pipefail # The input for this script is the scores-table.txt file. # TODO: Write a command to output scores-table.txt, with lines sorted by the person's name. # The first line of your output should be "Ahmed London 1 10 4" (with no quotes). And the third line should be "Chandra Birmingham 12 6". +sort scores-table.txt diff --git a/shell-pipelines/sort-uniq-head-tail/script-02.sh b/shell-pipelines/sort-uniq-head-tail/script-02.sh index 29c3c2524..baa6b861a 100755 --- a/shell-pipelines/sort-uniq-head-tail/script-02.sh +++ b/shell-pipelines/sort-uniq-head-tail/script-02.sh @@ -5,3 +5,4 @@ set -euo pipefail # The input for this script is the scores-table.txt file. # TODO: Write a command to output scores-table.txt, with lines sorted by the person's first score, descending. # The first line of your output should be "Basia London 22 9 6" (with no quotes). +sort -n -k3 -r scores-table.txt diff --git a/shell-pipelines/sort-uniq-head-tail/script-03.sh b/shell-pipelines/sort-uniq-head-tail/script-03.sh index bcbaf3420..23eb67923 100755 --- a/shell-pipelines/sort-uniq-head-tail/script-03.sh +++ b/shell-pipelines/sort-uniq-head-tail/script-03.sh @@ -8,3 +8,4 @@ set -euo pipefail # Basia London 22 9 6 # Piotr Glasgow 15 2 25 11 8 # Chandra Birmingham 12 6 +sort -n -k3 -r scores-table.txt | head -n3 diff --git a/shell-pipelines/sort-uniq-head-tail/script-04.sh b/shell-pipelines/sort-uniq-head-tail/script-04.sh index 65a5cfba8..93c88aa96 100755 --- a/shell-pipelines/sort-uniq-head-tail/script-04.sh +++ b/shell-pipelines/sort-uniq-head-tail/script-04.sh @@ -5,3 +5,4 @@ set -euo pipefail # The input for this script is the scores-table.txt file. # TODO: Write a command to output scores-table.txt, with shows the line for the player whose first score was the second highest. # Your output should be: "Piotr Glasgow 15 2 25 11 8" (without quotes). +sort -n -k3 -r scores-table.txt | tail -n+2 | head -n1 diff --git a/shell-pipelines/sort-uniq-head-tail/script-05.sh b/shell-pipelines/sort-uniq-head-tail/script-05.sh index a93cd9f9d..8eba57e36 100755 --- a/shell-pipelines/sort-uniq-head-tail/script-05.sh +++ b/shell-pipelines/sort-uniq-head-tail/script-05.sh @@ -6,3 +6,4 @@ set -euo pipefail # TODO: Write a command to show a list of all events that have happened, without duplication. # The order they're displayed doesn't matter, but we never want to see the same event listed twice. # Your output should contain 6 lines. +sort events.txt | uniq diff --git a/shell-pipelines/sort-uniq-head-tail/script-06.sh b/shell-pipelines/sort-uniq-head-tail/script-06.sh index 715c7ae5c..e2fc1be4a 100755 --- a/shell-pipelines/sort-uniq-head-tail/script-06.sh +++ b/shell-pipelines/sort-uniq-head-tail/script-06.sh @@ -5,3 +5,4 @@ set -euo pipefail # The input for this script is the events.txt file. # TODO: Write a command to show how many times anyone has entered and exited. # It should be clear from your script's output that there have been 5 Entry events and 4 Exit events. +awk '{print $1}' events.txt | sort | uniq -c diff --git a/shell-pipelines/sort-uniq-head-tail/script-07.sh b/shell-pipelines/sort-uniq-head-tail/script-07.sh index 7fd07e1ff..ed7481d63 100755 --- a/shell-pipelines/sort-uniq-head-tail/script-07.sh +++ b/shell-pipelines/sort-uniq-head-tail/script-07.sh @@ -6,3 +6,4 @@ set -euo pipefail # TODO: Write a command to show how many times anyone has entered and exited. # It should be clear from your script's output that there have been 5 Entry events and 4 Exit events. # The word "Event" should not appear in your script's output. +tail -n+2 events-with-timestamps.txt | awk '{print $3}' | sort | uniq -c diff --git a/shell-pipelines/tr/script-01.sh b/shell-pipelines/tr/script-01.sh index 8bb0211e9..75c3ed25f 100755 --- a/shell-pipelines/tr/script-01.sh +++ b/shell-pipelines/tr/script-01.sh @@ -6,3 +6,4 @@ set -euo pipefail # The author got feedback that they're using too many exclamation marks (!). # # TODO: Write a command to output the contents of text.txt with every exclamation mark (!) replaced with a full-stop (.). +cat text.txt | tr '!' '.' diff --git a/shell-pipelines/tr/script-02.sh b/shell-pipelines/tr/script-02.sh index cf3a503a2..f8c4121cb 100755 --- a/shell-pipelines/tr/script-02.sh +++ b/shell-pipelines/tr/script-02.sh @@ -7,3 +7,4 @@ set -euo pipefail # so every Y should be a Z, and every Z should be a Y! # # TODO: Write a command to output the contents of text.txt with every Y and Z swapped (both upper and lower case). +cat text.txt | tr 'yzYZ' 'zyZY' From 6780bec327067e781bd13a9b8d9010be369a3f03 Mon Sep 17 00:00:00 2001 From: Daniel Wagner-Hall Date: Sat, 1 Mar 2025 09:58:06 +0000 Subject: [PATCH 2/5] Fix mistake in answer --- number-systems/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/number-systems/README.md b/number-systems/README.md index 6753debd9..da424df44 100644 --- a/number-systems/README.md +++ b/number-systems/README.md @@ -29,7 +29,7 @@ How many bits would you need in order to store the numbers between 0 and 255 inc Answer: 8 How many bits would you need in order to store the numbers between 0 and 3 inclusive? -Answer: 3 +Answer: 2 How many bits would you need in order to store the numbers between 0 and 1000 inclusive? Answer: 10, but you'd have 23 spare values, so you can actually store between 0 and 1023 with 10 bits. From c11fe1eb097d598e53ac81aebc007b130d931b68 Mon Sep 17 00:00:00 2001 From: Daniel Wagner-Hall Date: Mon, 3 Mar 2025 09:37:43 +0000 Subject: [PATCH 3/5] Filter for London --- individual-shell-tools/awk/script-04.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/individual-shell-tools/awk/script-04.sh b/individual-shell-tools/awk/script-04.sh index e774cf9bb..22f203a24 100755 --- a/individual-shell-tools/awk/script-04.sh +++ b/individual-shell-tools/awk/script-04.sh @@ -5,4 +5,4 @@ set -euo pipefail # TODO: Write a command to output just the names of each player in London along with the score from their last attempt. # Your output should contain 3 lines, each with one word and one number on it. # The first line should be "Ahmed 4". -awk '{printf "%s %s\n", $1, $NF}' scores-table.txt +awk '$2 == "London" {printf "%s %s\n", $1, $NF}' scores-table.txt From e5bbbad00321907abbd6073476358890507db7c2 Mon Sep 17 00:00:00 2001 From: Daniel Wagner-Hall Date: Mon, 3 Mar 2025 09:39:40 +0000 Subject: [PATCH 4/5] Add all the scores, not just the first one --- jq/script-11.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jq/script-11.sh b/jq/script-11.sh index 7a7dba0a0..da686da5d 100755 --- a/jq/script-11.sh +++ b/jq/script-11.sh @@ -5,4 +5,4 @@ set -euo pipefail # The input for this script is the scores.json file. # TODO: Write a command to output the total of adding together all scores from all games from all players. # Your output should be exactly the number 164. -jq -r '[.[].scores[0]] | add' scores.json +jq -r '[.[].scores | add] | add' scores.json From 7cab233d5da22a43431ff12828f39dc33cc8048d Mon Sep 17 00:00:00 2001 From: Borja Lorente Date: Wed, 28 May 2025 03:24:11 -0700 Subject: [PATCH 5/5] feat(shell-tools): Add check.sh scripts for a quick smoke test (#62) ## Changelist While reviewing PRs from students, I found it useful to have a small script that runs their solution along with the relevant unix utility, and compares the output. Here are those scripts. Usage examples: ```shell $ cd implement-shell-tools/wc $ ./check.sh #Will test "node wc.js" $ USER_COMMAND="python3 wc.py" ./check.sh ``` --- implement-shell-tools/cat/check.sh | 19 +++++++++++++++++++ implement-shell-tools/ls/check.sh | 18 ++++++++++++++++++ implement-shell-tools/wc/check.sh | 19 +++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100755 implement-shell-tools/cat/check.sh create mode 100755 implement-shell-tools/ls/check.sh create mode 100755 implement-shell-tools/wc/check.sh diff --git a/implement-shell-tools/cat/check.sh b/implement-shell-tools/cat/check.sh new file mode 100755 index 000000000..ec8b0a64a --- /dev/null +++ b/implement-shell-tools/cat/check.sh @@ -0,0 +1,19 @@ +#!/bin/bash + +user_command=(${USER_COMMAND:-"node cat.js"}) + +function test() { + args=${@} + echo "======" + echo "Testing cat ${args}" + echo "=======" + diff -y <(cat ${args[@]}) <(${user_command[@]} ${args[@]}) + echo "" + echo "" +} + +test 'sample-files/1.txt' +test '-n' 'sample-files/1.txt' +test 'sample-files/*.txt' +test '-n' 'sample-files/*.txt' +test '-b' 'sample-files/3.txt' diff --git a/implement-shell-tools/ls/check.sh b/implement-shell-tools/ls/check.sh new file mode 100755 index 000000000..34490cf21 --- /dev/null +++ b/implement-shell-tools/ls/check.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +user_command=(${USER_COMMAND:-"node ls.js"}) + +function test() { + args=${@} + echo "======" + echo "Testing ls ${args}" + echo "=======" + diff -y <(/bin/ls ${args[@]}) <(${user_command[@]} ${args[@]}) + echo "" + echo "" +} + +test +test '-1' +test '-1' 'sample-files' +test '-1' '-a' 'sample-files' diff --git a/implement-shell-tools/wc/check.sh b/implement-shell-tools/wc/check.sh new file mode 100755 index 000000000..46761bf17 --- /dev/null +++ b/implement-shell-tools/wc/check.sh @@ -0,0 +1,19 @@ +#!/bin/bash + +user_command=(${USER_COMMAND:-"node wc.js"}) + +function test() { + args=${@} + echo "======" + echo "Testing wc ${args}" + echo "=======" + diff -y <(wc ${args[@]}) <(${user_command[@]} ${args[@]}) + echo "" + echo "" +} + +test 'sample-files/*' +test '-l' 'sample-files/3.txt' +test '-w' 'sample-files/3.txt' +test '-c' 'sample-files/3.txt' +test '-l' 'sample-files/*'