From 06113837c2d9e181746e5a43717a66aec24a58e1 Mon Sep 17 00:00:00 2001 From: Fradoka Date: Tue, 15 Jul 2025 06:06:30 +0100 Subject: [PATCH 01/15] exercise ls --- individual-shell-tools/ls/script-01.sh | 3 +++ individual-shell-tools/ls/script-02.sh | 2 ++ individual-shell-tools/ls/script-03.sh | 1 + individual-shell-tools/ls/script-04.sh | 5 ++++- 4 files changed, 10 insertions(+), 1 deletion(-) diff --git a/individual-shell-tools/ls/script-01.sh b/individual-shell-tools/ls/script-01.sh index 241b62f5e..92631c6c0 100755 --- a/individual-shell-tools/ls/script-01.sh +++ b/individual-shell-tools/ls/script-01.sh @@ -13,3 +13,6 @@ 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. + +thisDir="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/ls" +ls "$thisDir" \ No newline at end of file diff --git a/individual-shell-tools/ls/script-02.sh b/individual-shell-tools/ls/script-02.sh index d0a5a10f4..2346ae304 100755 --- a/individual-shell-tools/ls/script-02.sh +++ b/individual-shell-tools/ls/script-02.sh @@ -4,3 +4,5 @@ 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. +thisDir="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/ls/child-directory" +ls "$thisDir" \ No newline at end of file diff --git a/individual-shell-tools/ls/script-03.sh b/individual-shell-tools/ls/script-03.sh index 781216d21..f43826708 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. +find . \ No newline at end of file diff --git a/individual-shell-tools/ls/script-04.sh b/individual-shell-tools/ls/script-04.sh index 72f3817b3..1d6bf63d1 100755 --- a/individual-shell-tools/ls/script-04.sh +++ b/individual-shell-tools/ls/script-04.sh @@ -15,9 +15,12 @@ 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. - +thisDir="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/ls/child-directory" +ls -1t "$thisDir" 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. +thisDir="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/ls/child-directory" +ls -1tr "$thisDir" \ No newline at end of file From 1f5b567c1ecf8fa8f797264c849a14cd410f862a Mon Sep 17 00:00:00 2001 From: Fradoka Date: Tue, 15 Jul 2025 06:30:44 +0100 Subject: [PATCH 02/15] exercise cat --- individual-shell-tools/cat/script-01.sh | 2 ++ individual-shell-tools/cat/script-02.sh | 2 ++ individual-shell-tools/cat/script-03.sh | 2 ++ individual-shell-tools/cat/script-04-stretch.sh | 2 ++ 4 files changed, 8 insertions(+) diff --git a/individual-shell-tools/cat/script-01.sh b/individual-shell-tools/cat/script-01.sh index c85053e0f..7d0f34ba4 100755 --- a/individual-shell-tools/cat/script-01.sh +++ b/individual-shell-tools/cat/script-01.sh @@ -4,3 +4,5 @@ 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...". +path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/helper-files/helper-1.txt" +cat "$path" \ No newline at end of file diff --git a/individual-shell-tools/cat/script-02.sh b/individual-shell-tools/cat/script-02.sh index 01bbd5eab..2cff2c632 100755 --- a/individual-shell-tools/cat/script-02.sh +++ b/individual-shell-tools/cat/script-02.sh @@ -11,3 +11,5 @@ set -euo pipefail # It looked delicious. # I was tempted to take a bite of it. # But this seemed like a bad idea... +path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/helper-files" +cat "$path"/*.txt \ No newline at end of file diff --git a/individual-shell-tools/cat/script-03.sh b/individual-shell-tools/cat/script-03.sh index 37573b0c1..c14b10371 100755 --- a/individual-shell-tools/cat/script-03.sh +++ b/individual-shell-tools/cat/script-03.sh @@ -9,3 +9,5 @@ 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... +path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/helper-files/helper-3.txt" +cat -n "$path" \ No newline at end of file diff --git a/individual-shell-tools/cat/script-04-stretch.sh b/individual-shell-tools/cat/script-04-stretch.sh index 00fe3c48b..73640a351 100755 --- a/individual-shell-tools/cat/script-04-stretch.sh +++ b/individual-shell-tools/cat/script-04-stretch.sh @@ -13,3 +13,5 @@ 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... +path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/helper-files" +cat "$path"/*.txt | cat -n \ No newline at end of file From 0a7108712503aaaafff386fd28e7834d50f15bad Mon Sep 17 00:00:00 2001 From: Fradoka Date: Tue, 15 Jul 2025 06:40:15 +0100 Subject: [PATCH 03/15] exercise wc --- individual-shell-tools/wc/script-01.sh | 2 ++ individual-shell-tools/wc/script-02.sh | 2 ++ individual-shell-tools/wc/script-03.sh | 2 ++ 3 files changed, 6 insertions(+) diff --git a/individual-shell-tools/wc/script-01.sh b/individual-shell-tools/wc/script-01.sh index c9dd6e5df..acc39b8d9 100755 --- a/individual-shell-tools/wc/script-01.sh +++ b/individual-shell-tools/wc/script-01.sh @@ -4,3 +4,5 @@ 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. +path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/helper-files/helper-3.txt" +wc -w "$path" \ No newline at end of file diff --git a/individual-shell-tools/wc/script-02.sh b/individual-shell-tools/wc/script-02.sh index 8feeb1a62..32a6cb5d9 100755 --- a/individual-shell-tools/wc/script-02.sh +++ b/individual-shell-tools/wc/script-02.sh @@ -4,3 +4,5 @@ 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. +path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/helper-files/helper-3.txt" +wc -l "$path" \ No newline at end of file diff --git a/individual-shell-tools/wc/script-03.sh b/individual-shell-tools/wc/script-03.sh index 6b2e9d3d1..cf4fc6b73 100755 --- a/individual-shell-tools/wc/script-03.sh +++ b/individual-shell-tools/wc/script-03.sh @@ -8,3 +8,5 @@ set -euo pipefail # 1 7 39 ../helper-files/helper-2.txt # 3 19 92 ../helper-files/helper-3.txt # 5 30 151 total +path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/helper-files" +wc "$path"/*.txt \ No newline at end of file From 72271cfc88e9466e5a696a6c6065aa9c7b21b8fc Mon Sep 17 00:00:00 2001 From: Fradoka Date: Tue, 15 Jul 2025 15:26:12 +0100 Subject: [PATCH 04/15] exercise grep --- individual-shell-tools/grep/script-01.sh | 2 ++ individual-shell-tools/grep/script-02.sh | 2 ++ individual-shell-tools/grep/script-03.sh | 2 ++ individual-shell-tools/grep/script-04.sh | 2 ++ individual-shell-tools/grep/script-05.sh | 2 ++ individual-shell-tools/grep/script-06.sh | 2 ++ individual-shell-tools/grep/script-07.sh | 2 ++ 7 files changed, 14 insertions(+) diff --git a/individual-shell-tools/grep/script-01.sh b/individual-shell-tools/grep/script-01.sh index fb05f42f2..54169a1c2 100755 --- a/individual-shell-tools/grep/script-01.sh +++ b/individual-shell-tools/grep/script-01.sh @@ -4,3 +4,5 @@ set -euo pipefail # TODO: Write a command to output every line in dialogue.txt said by the Doctor. # The output should contain 6 lines. +path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/grep/dialogue.txt" +grep '^Doctor:' "$path" \ No newline at end of file diff --git a/individual-shell-tools/grep/script-02.sh b/individual-shell-tools/grep/script-02.sh index df6f85640..2cde17d15 100755 --- a/individual-shell-tools/grep/script-02.sh +++ b/individual-shell-tools/grep/script-02.sh @@ -4,3 +4,5 @@ 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. +path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/grep/dialogue.txt" +grep -i 'Doctor' "$path" \ No newline at end of file diff --git a/individual-shell-tools/grep/script-03.sh b/individual-shell-tools/grep/script-03.sh index 5383fe578..38637aae9 100755 --- a/individual-shell-tools/grep/script-03.sh +++ b/individual-shell-tools/grep/script-03.sh @@ -4,3 +4,5 @@ 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. +path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/grep/dialogue.txt" +grep -i 'Doctor' "$path" | wc -l \ No newline at end of file diff --git a/individual-shell-tools/grep/script-04.sh b/individual-shell-tools/grep/script-04.sh index 80ee04776..ab2566f42 100755 --- a/individual-shell-tools/grep/script-04.sh +++ b/individual-shell-tools/grep/script-04.sh @@ -4,3 +4,5 @@ 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. +path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/grep/dialogue.txt" +grep -vi 'Hello' "$path" \ No newline at end of file diff --git a/individual-shell-tools/grep/script-05.sh b/individual-shell-tools/grep/script-05.sh index 1eb538185..eb0b06cd0 100755 --- a/individual-shell-tools/grep/script-05.sh +++ b/individual-shell-tools/grep/script-05.sh @@ -4,3 +4,5 @@ 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). +path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/grep/dialogue.txt" +grep -B 1 'cure' "$path" \ No newline at end of file diff --git a/individual-shell-tools/grep/script-06.sh b/individual-shell-tools/grep/script-06.sh index 5670e3b6c..2686fe8ca 100755 --- a/individual-shell-tools/grep/script-06.sh +++ b/individual-shell-tools/grep/script-06.sh @@ -4,3 +4,5 @@ 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. +path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/grep" +grep -l '^Doctor:' "$path"/*.txt \ No newline at end of file diff --git a/individual-shell-tools/grep/script-07.sh b/individual-shell-tools/grep/script-07.sh index 9670ebad9..a6ca13ff6 100755 --- a/individual-shell-tools/grep/script-07.sh +++ b/individual-shell-tools/grep/script-07.sh @@ -4,3 +4,5 @@ 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. +path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/grep/" +grep -c '^Doctor:' "$path"*.txt | sed "s|$path||" \ No newline at end of file From 0d3bc17ba208b3f0693667f84c2232fbf3dd364b Mon Sep 17 00:00:00 2001 From: Fradoka Date: Tue, 15 Jul 2025 16:29:01 +0100 Subject: [PATCH 05/15] exercise sed --- individual-shell-tools/sed/script-01.sh | 2 ++ individual-shell-tools/sed/script-02.sh | 2 ++ individual-shell-tools/sed/script-03.sh | 2 ++ individual-shell-tools/sed/script-04.sh | 4 ++++ individual-shell-tools/sed/script-05.sh | 2 ++ individual-shell-tools/sed/script-06.sh | 2 ++ 6 files changed, 14 insertions(+) diff --git a/individual-shell-tools/sed/script-01.sh b/individual-shell-tools/sed/script-01.sh index 3eba6fa4d..ed6ca0f06 100755 --- a/individual-shell-tools/sed/script-01.sh +++ b/individual-shell-tools/sed/script-01.sh @@ -5,3 +5,5 @@ 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.". +path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/sed/input.txt" +sed 's/i/I/g' $path \ No newline at end of file diff --git a/individual-shell-tools/sed/script-02.sh b/individual-shell-tools/sed/script-02.sh index abdd64d06..cdd2432c6 100755 --- a/individual-shell-tools/sed/script-02.sh +++ b/individual-shell-tools/sed/script-02.sh @@ -5,3 +5,5 @@ 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". +path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/sed/input.txt" +sed "s/[0-9]//g" $path \ No newline at end of file diff --git a/individual-shell-tools/sed/script-03.sh b/individual-shell-tools/sed/script-03.sh index dd284a296..11b1e827e 100755 --- a/individual-shell-tools/sed/script-03.sh +++ b/individual-shell-tools/sed/script-03.sh @@ -4,3 +4,5 @@ set -euo pipefail # TODO: Write a command to output input.txt removing any line which contains a number. # The output should contain 6 lines. +path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/sed/input.txt" +sed "/[0-9]/d" $path \ No newline at end of file diff --git a/individual-shell-tools/sed/script-04.sh b/individual-shell-tools/sed/script-04.sh index 0052ac6c4..cf3a23017 100755 --- a/individual-shell-tools/sed/script-04.sh +++ b/individual-shell-tools/sed/script-04.sh @@ -4,3 +4,7 @@ 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. +path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/sed/input.txt" +old="We'll" +new="We will" +sed "s/$old/$new/g" $path \ No newline at end of file diff --git a/individual-shell-tools/sed/script-05.sh b/individual-shell-tools/sed/script-05.sh index 2dcc91a0c..922d14b54 100755 --- a/individual-shell-tools/sed/script-05.sh +++ b/individual-shell-tools/sed/script-05.sh @@ -6,3 +6,5 @@ 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. +path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/sed/input.txt" +sed -E 's/^([0-9]+) (.*)/\2 \1/' $path \ No newline at end of file diff --git a/individual-shell-tools/sed/script-06.sh b/individual-shell-tools/sed/script-06.sh index 0b9390170..82de10996 100755 --- a/individual-shell-tools/sed/script-06.sh +++ b/individual-shell-tools/sed/script-06.sh @@ -8,3 +8,5 @@ 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.". +path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/sed/input.txt" +sed -E 's/, ?/, /g' $path \ No newline at end of file From 22a04bb029a8daddf25ddbbaa1e11bda8f81a374 Mon Sep 17 00:00:00 2001 From: Fradoka Date: Tue, 15 Jul 2025 17:15:59 +0100 Subject: [PATCH 06/15] exercise awk --- individual-shell-tools/awk/script-01.sh | 2 ++ individual-shell-tools/awk/script-02.sh | 2 ++ individual-shell-tools/awk/script-03.sh | 2 ++ individual-shell-tools/awk/script-04.sh | 2 ++ individual-shell-tools/awk/script-05.sh | 2 ++ individual-shell-tools/awk/script-06-stretch.sh | 2 ++ individual-shell-tools/awk/script-07-stretch.sh | 6 ++++++ 7 files changed, 18 insertions(+) diff --git a/individual-shell-tools/awk/script-01.sh b/individual-shell-tools/awk/script-01.sh index 8db4390af..336b3fd5a 100755 --- a/individual-shell-tools/awk/script-01.sh +++ b/individual-shell-tools/awk/script-01.sh @@ -4,3 +4,5 @@ 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. +path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/awk/scores-table.txt" +awk '{print $1}' $path \ No newline at end of file diff --git a/individual-shell-tools/awk/script-02.sh b/individual-shell-tools/awk/script-02.sh index 5956be9bd..828fabb0c 100755 --- a/individual-shell-tools/awk/script-02.sh +++ b/individual-shell-tools/awk/script-02.sh @@ -4,3 +4,5 @@ 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. +path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/awk/scores-table.txt" +awk '{print $1, $2}' $path \ No newline at end of file diff --git a/individual-shell-tools/awk/script-03.sh b/individual-shell-tools/awk/script-03.sh index af7c6e8b9..f8561ed57 100755 --- a/individual-shell-tools/awk/script-03.sh +++ b/individual-shell-tools/awk/script-03.sh @@ -5,3 +5,5 @@ 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". +path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/awk/scores-table.txt" +awk '{print $1, $3}' $path \ No newline at end of file diff --git a/individual-shell-tools/awk/script-04.sh b/individual-shell-tools/awk/script-04.sh index bf15703c7..15ca5e934 100755 --- a/individual-shell-tools/awk/script-04.sh +++ b/individual-shell-tools/awk/script-04.sh @@ -5,3 +5,5 @@ 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". +path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/awk/scores-table.txt" +awk '/London/ {print $1, $NF}' $path \ No newline at end of file diff --git a/individual-shell-tools/awk/script-05.sh b/individual-shell-tools/awk/script-05.sh index d1680cb02..29aa79ae6 100755 --- a/individual-shell-tools/awk/script-05.sh +++ b/individual-shell-tools/awk/script-05.sh @@ -5,3 +5,5 @@ 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". +path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/awk/scores-table.txt" +awk '{print $1, NF-2}' $path \ No newline at end of file diff --git a/individual-shell-tools/awk/script-06-stretch.sh b/individual-shell-tools/awk/script-06-stretch.sh index 0201e6378..bf9e60a46 100755 --- a/individual-shell-tools/awk/script-06-stretch.sh +++ b/individual-shell-tools/awk/script-06-stretch.sh @@ -6,3 +6,5 @@ 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. +path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/awk/scores-table.txt" +awk '{sum += $3} END {print sum}' $path \ No newline at end of file diff --git a/individual-shell-tools/awk/script-07-stretch.sh b/individual-shell-tools/awk/script-07-stretch.sh index 3f7155880..38eb8bedf 100755 --- a/individual-shell-tools/awk/script-07-stretch.sh +++ b/individual-shell-tools/awk/script-07-stretch.sh @@ -7,3 +7,9 @@ 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" +path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/awk/scores-table.txt" +awk '{ + sum = 0 + for (i = 3; i <= NF; i++) sum += $i} { + print $1, sum +}' $path \ No newline at end of file From 32a3a871984f9f7d4a83523ef64072c218ae4bd4 Mon Sep 17 00:00:00 2001 From: Fradoka Date: Tue, 15 Jul 2025 22:15:26 +0100 Subject: [PATCH 07/15] exercise number systems --- number-systems/README.md | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/number-systems/README.md b/number-systems/README.md index 77a3bde94..328312ba8 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 or 0b1111 How many bits would you need in order to store the numbers between 0 and 255 inclusive? -Answer: +Answer:8 bits (255 in base 2 is 0b11111111) How many bits would you need in order to store the numbers between 0 and 3 inclusive? -Answer: +Answer:2 bits (3 in base 2 is 0b11) How many bits would you need in order to store the numbers between 0 and 1000 inclusive? -Answer: +Answer:10 bits (1000 in base 2 is 0b1111101000) How can you test if a binary number is a power of two (e.g. 1, 2, 4, 8, 16, ...)? -Answer: +Answer:a binary number is a power of 2 if it has exactly one "1" bit in its representation. 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 shade of gray 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:(RGB 170, 0, 255) high red, no green, maximum blue. 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 4ef88e262b96a3c848e772c887e73e37d28bafa8 Mon Sep 17 00:00:00 2001 From: Fradoka Date: Thu, 31 Jul 2025 15:11:57 +0100 Subject: [PATCH 08/15] Revert mistaken commits from main branch --- individual-shell-tools/awk/script-01.sh | 2 - individual-shell-tools/awk/script-02.sh | 2 - individual-shell-tools/awk/script-03.sh | 2 - individual-shell-tools/awk/script-04.sh | 2 - individual-shell-tools/awk/script-05.sh | 2 - .../awk/script-06-stretch.sh | 2 - .../awk/script-07-stretch.sh | 6 --- individual-shell-tools/cat/script-01.sh | 2 - individual-shell-tools/cat/script-02.sh | 2 - individual-shell-tools/cat/script-03.sh | 2 - .../cat/script-04-stretch.sh | 2 - individual-shell-tools/grep/script-01.sh | 2 - individual-shell-tools/grep/script-02.sh | 2 - individual-shell-tools/grep/script-03.sh | 2 - individual-shell-tools/grep/script-04.sh | 2 - individual-shell-tools/grep/script-05.sh | 2 - individual-shell-tools/grep/script-06.sh | 2 - individual-shell-tools/grep/script-07.sh | 2 - individual-shell-tools/ls/script-01.sh | 3 -- individual-shell-tools/ls/script-02.sh | 2 - individual-shell-tools/ls/script-03.sh | 1 - individual-shell-tools/ls/script-04.sh | 5 +-- individual-shell-tools/sed/script-01.sh | 2 - individual-shell-tools/sed/script-02.sh | 2 - individual-shell-tools/sed/script-03.sh | 2 - individual-shell-tools/sed/script-04.sh | 4 -- individual-shell-tools/sed/script-05.sh | 2 - individual-shell-tools/sed/script-06.sh | 2 - individual-shell-tools/wc/script-01.sh | 2 - individual-shell-tools/wc/script-02.sh | 2 - individual-shell-tools/wc/script-03.sh | 2 - number-systems/README.md | 40 +++++++++---------- 32 files changed, 21 insertions(+), 90 deletions(-) diff --git a/individual-shell-tools/awk/script-01.sh b/individual-shell-tools/awk/script-01.sh index 336b3fd5a..8db4390af 100755 --- a/individual-shell-tools/awk/script-01.sh +++ b/individual-shell-tools/awk/script-01.sh @@ -4,5 +4,3 @@ 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. -path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/awk/scores-table.txt" -awk '{print $1}' $path \ No newline at end of file diff --git a/individual-shell-tools/awk/script-02.sh b/individual-shell-tools/awk/script-02.sh index 828fabb0c..5956be9bd 100755 --- a/individual-shell-tools/awk/script-02.sh +++ b/individual-shell-tools/awk/script-02.sh @@ -4,5 +4,3 @@ 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. -path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/awk/scores-table.txt" -awk '{print $1, $2}' $path \ No newline at end of file diff --git a/individual-shell-tools/awk/script-03.sh b/individual-shell-tools/awk/script-03.sh index f8561ed57..af7c6e8b9 100755 --- a/individual-shell-tools/awk/script-03.sh +++ b/individual-shell-tools/awk/script-03.sh @@ -5,5 +5,3 @@ 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". -path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/awk/scores-table.txt" -awk '{print $1, $3}' $path \ No newline at end of file diff --git a/individual-shell-tools/awk/script-04.sh b/individual-shell-tools/awk/script-04.sh index 15ca5e934..bf15703c7 100755 --- a/individual-shell-tools/awk/script-04.sh +++ b/individual-shell-tools/awk/script-04.sh @@ -5,5 +5,3 @@ 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". -path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/awk/scores-table.txt" -awk '/London/ {print $1, $NF}' $path \ No newline at end of file diff --git a/individual-shell-tools/awk/script-05.sh b/individual-shell-tools/awk/script-05.sh index 29aa79ae6..d1680cb02 100755 --- a/individual-shell-tools/awk/script-05.sh +++ b/individual-shell-tools/awk/script-05.sh @@ -5,5 +5,3 @@ 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". -path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/awk/scores-table.txt" -awk '{print $1, NF-2}' $path \ No newline at end of file diff --git a/individual-shell-tools/awk/script-06-stretch.sh b/individual-shell-tools/awk/script-06-stretch.sh index bf9e60a46..0201e6378 100755 --- a/individual-shell-tools/awk/script-06-stretch.sh +++ b/individual-shell-tools/awk/script-06-stretch.sh @@ -6,5 +6,3 @@ 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. -path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/awk/scores-table.txt" -awk '{sum += $3} END {print sum}' $path \ No newline at end of file diff --git a/individual-shell-tools/awk/script-07-stretch.sh b/individual-shell-tools/awk/script-07-stretch.sh index 38eb8bedf..3f7155880 100755 --- a/individual-shell-tools/awk/script-07-stretch.sh +++ b/individual-shell-tools/awk/script-07-stretch.sh @@ -7,9 +7,3 @@ 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" -path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/awk/scores-table.txt" -awk '{ - sum = 0 - for (i = 3; i <= NF; i++) sum += $i} { - print $1, sum -}' $path \ No newline at end of file diff --git a/individual-shell-tools/cat/script-01.sh b/individual-shell-tools/cat/script-01.sh index 7d0f34ba4..c85053e0f 100755 --- a/individual-shell-tools/cat/script-01.sh +++ b/individual-shell-tools/cat/script-01.sh @@ -4,5 +4,3 @@ 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...". -path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/helper-files/helper-1.txt" -cat "$path" \ No newline at end of file diff --git a/individual-shell-tools/cat/script-02.sh b/individual-shell-tools/cat/script-02.sh index 2cff2c632..01bbd5eab 100755 --- a/individual-shell-tools/cat/script-02.sh +++ b/individual-shell-tools/cat/script-02.sh @@ -11,5 +11,3 @@ set -euo pipefail # It looked delicious. # I was tempted to take a bite of it. # But this seemed like a bad idea... -path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/helper-files" -cat "$path"/*.txt \ No newline at end of file diff --git a/individual-shell-tools/cat/script-03.sh b/individual-shell-tools/cat/script-03.sh index c14b10371..37573b0c1 100755 --- a/individual-shell-tools/cat/script-03.sh +++ b/individual-shell-tools/cat/script-03.sh @@ -9,5 +9,3 @@ 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... -path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/helper-files/helper-3.txt" -cat -n "$path" \ No newline at end of file diff --git a/individual-shell-tools/cat/script-04-stretch.sh b/individual-shell-tools/cat/script-04-stretch.sh index 73640a351..00fe3c48b 100755 --- a/individual-shell-tools/cat/script-04-stretch.sh +++ b/individual-shell-tools/cat/script-04-stretch.sh @@ -13,5 +13,3 @@ 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... -path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/helper-files" -cat "$path"/*.txt | cat -n \ No newline at end of file diff --git a/individual-shell-tools/grep/script-01.sh b/individual-shell-tools/grep/script-01.sh index 54169a1c2..fb05f42f2 100755 --- a/individual-shell-tools/grep/script-01.sh +++ b/individual-shell-tools/grep/script-01.sh @@ -4,5 +4,3 @@ set -euo pipefail # TODO: Write a command to output every line in dialogue.txt said by the Doctor. # The output should contain 6 lines. -path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/grep/dialogue.txt" -grep '^Doctor:' "$path" \ No newline at end of file diff --git a/individual-shell-tools/grep/script-02.sh b/individual-shell-tools/grep/script-02.sh index 2cde17d15..df6f85640 100755 --- a/individual-shell-tools/grep/script-02.sh +++ b/individual-shell-tools/grep/script-02.sh @@ -4,5 +4,3 @@ 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. -path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/grep/dialogue.txt" -grep -i 'Doctor' "$path" \ No newline at end of file diff --git a/individual-shell-tools/grep/script-03.sh b/individual-shell-tools/grep/script-03.sh index 38637aae9..5383fe578 100755 --- a/individual-shell-tools/grep/script-03.sh +++ b/individual-shell-tools/grep/script-03.sh @@ -4,5 +4,3 @@ 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. -path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/grep/dialogue.txt" -grep -i 'Doctor' "$path" | wc -l \ No newline at end of file diff --git a/individual-shell-tools/grep/script-04.sh b/individual-shell-tools/grep/script-04.sh index ab2566f42..80ee04776 100755 --- a/individual-shell-tools/grep/script-04.sh +++ b/individual-shell-tools/grep/script-04.sh @@ -4,5 +4,3 @@ 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. -path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/grep/dialogue.txt" -grep -vi 'Hello' "$path" \ No newline at end of file diff --git a/individual-shell-tools/grep/script-05.sh b/individual-shell-tools/grep/script-05.sh index eb0b06cd0..1eb538185 100755 --- a/individual-shell-tools/grep/script-05.sh +++ b/individual-shell-tools/grep/script-05.sh @@ -4,5 +4,3 @@ 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). -path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/grep/dialogue.txt" -grep -B 1 'cure' "$path" \ No newline at end of file diff --git a/individual-shell-tools/grep/script-06.sh b/individual-shell-tools/grep/script-06.sh index 2686fe8ca..5670e3b6c 100755 --- a/individual-shell-tools/grep/script-06.sh +++ b/individual-shell-tools/grep/script-06.sh @@ -4,5 +4,3 @@ 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. -path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/grep" -grep -l '^Doctor:' "$path"/*.txt \ No newline at end of file diff --git a/individual-shell-tools/grep/script-07.sh b/individual-shell-tools/grep/script-07.sh index a6ca13ff6..9670ebad9 100755 --- a/individual-shell-tools/grep/script-07.sh +++ b/individual-shell-tools/grep/script-07.sh @@ -4,5 +4,3 @@ 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. -path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/grep/" -grep -c '^Doctor:' "$path"*.txt | sed "s|$path||" \ No newline at end of file diff --git a/individual-shell-tools/ls/script-01.sh b/individual-shell-tools/ls/script-01.sh index 92631c6c0..241b62f5e 100755 --- a/individual-shell-tools/ls/script-01.sh +++ b/individual-shell-tools/ls/script-01.sh @@ -13,6 +13,3 @@ 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. - -thisDir="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/ls" -ls "$thisDir" \ No newline at end of file diff --git a/individual-shell-tools/ls/script-02.sh b/individual-shell-tools/ls/script-02.sh index 2346ae304..d0a5a10f4 100755 --- a/individual-shell-tools/ls/script-02.sh +++ b/individual-shell-tools/ls/script-02.sh @@ -4,5 +4,3 @@ 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. -thisDir="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/ls/child-directory" -ls "$thisDir" \ No newline at end of file diff --git a/individual-shell-tools/ls/script-03.sh b/individual-shell-tools/ls/script-03.sh index f43826708..781216d21 100755 --- a/individual-shell-tools/ls/script-03.sh +++ b/individual-shell-tools/ls/script-03.sh @@ -5,4 +5,3 @@ 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. -find . \ No newline at end of file diff --git a/individual-shell-tools/ls/script-04.sh b/individual-shell-tools/ls/script-04.sh index 1d6bf63d1..72f3817b3 100755 --- a/individual-shell-tools/ls/script-04.sh +++ b/individual-shell-tools/ls/script-04.sh @@ -15,12 +15,9 @@ 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. -thisDir="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/ls/child-directory" -ls -1t "$thisDir" + 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. -thisDir="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/ls/child-directory" -ls -1tr "$thisDir" \ No newline at end of file diff --git a/individual-shell-tools/sed/script-01.sh b/individual-shell-tools/sed/script-01.sh index ed6ca0f06..3eba6fa4d 100755 --- a/individual-shell-tools/sed/script-01.sh +++ b/individual-shell-tools/sed/script-01.sh @@ -5,5 +5,3 @@ 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.". -path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/sed/input.txt" -sed 's/i/I/g' $path \ No newline at end of file diff --git a/individual-shell-tools/sed/script-02.sh b/individual-shell-tools/sed/script-02.sh index cdd2432c6..abdd64d06 100755 --- a/individual-shell-tools/sed/script-02.sh +++ b/individual-shell-tools/sed/script-02.sh @@ -5,5 +5,3 @@ 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". -path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/sed/input.txt" -sed "s/[0-9]//g" $path \ No newline at end of file diff --git a/individual-shell-tools/sed/script-03.sh b/individual-shell-tools/sed/script-03.sh index 11b1e827e..dd284a296 100755 --- a/individual-shell-tools/sed/script-03.sh +++ b/individual-shell-tools/sed/script-03.sh @@ -4,5 +4,3 @@ set -euo pipefail # TODO: Write a command to output input.txt removing any line which contains a number. # The output should contain 6 lines. -path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/sed/input.txt" -sed "/[0-9]/d" $path \ No newline at end of file diff --git a/individual-shell-tools/sed/script-04.sh b/individual-shell-tools/sed/script-04.sh index cf3a23017..0052ac6c4 100755 --- a/individual-shell-tools/sed/script-04.sh +++ b/individual-shell-tools/sed/script-04.sh @@ -4,7 +4,3 @@ 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. -path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/sed/input.txt" -old="We'll" -new="We will" -sed "s/$old/$new/g" $path \ No newline at end of file diff --git a/individual-shell-tools/sed/script-05.sh b/individual-shell-tools/sed/script-05.sh index 922d14b54..2dcc91a0c 100755 --- a/individual-shell-tools/sed/script-05.sh +++ b/individual-shell-tools/sed/script-05.sh @@ -6,5 +6,3 @@ 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. -path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/sed/input.txt" -sed -E 's/^([0-9]+) (.*)/\2 \1/' $path \ No newline at end of file diff --git a/individual-shell-tools/sed/script-06.sh b/individual-shell-tools/sed/script-06.sh index 82de10996..0b9390170 100755 --- a/individual-shell-tools/sed/script-06.sh +++ b/individual-shell-tools/sed/script-06.sh @@ -8,5 +8,3 @@ 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.". -path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/sed/input.txt" -sed -E 's/, ?/, /g' $path \ No newline at end of file diff --git a/individual-shell-tools/wc/script-01.sh b/individual-shell-tools/wc/script-01.sh index acc39b8d9..c9dd6e5df 100755 --- a/individual-shell-tools/wc/script-01.sh +++ b/individual-shell-tools/wc/script-01.sh @@ -4,5 +4,3 @@ 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. -path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/helper-files/helper-3.txt" -wc -w "$path" \ No newline at end of file diff --git a/individual-shell-tools/wc/script-02.sh b/individual-shell-tools/wc/script-02.sh index 32a6cb5d9..8feeb1a62 100755 --- a/individual-shell-tools/wc/script-02.sh +++ b/individual-shell-tools/wc/script-02.sh @@ -4,5 +4,3 @@ 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. -path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/helper-files/helper-3.txt" -wc -l "$path" \ No newline at end of file diff --git a/individual-shell-tools/wc/script-03.sh b/individual-shell-tools/wc/script-03.sh index cf4fc6b73..6b2e9d3d1 100755 --- a/individual-shell-tools/wc/script-03.sh +++ b/individual-shell-tools/wc/script-03.sh @@ -8,5 +8,3 @@ set -euo pipefail # 1 7 39 ../helper-files/helper-2.txt # 3 19 92 ../helper-files/helper-3.txt # 5 30 151 total -path="/Users/cyf/Documents/SDC/Module-Tools/individual-shell-tools/helper-files" -wc "$path"/*.txt \ No newline at end of file diff --git a/number-systems/README.md b/number-systems/README.md index 328312ba8..77a3bde94 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:1110 +Answer: Convert the binary number 101101 to decimal: -Answer:45 +Answer: Which is larger: 1000 or 0111? -Answer:1000 +Answer: Which is larger: 00100 or 01011? -Answer:01011 +Answer: What is 10101 + 01010? -Answer:11111 +Answer: What is 10001 + 10001? -Answer:100010 +Answer: What's the largest number you can store with 4 bits, if you want to be able to represent the number 0? -Answer:15 or 0b1111 +Answer: How many bits would you need in order to store the numbers between 0 and 255 inclusive? -Answer:8 bits (255 in base 2 is 0b11111111) +Answer: How many bits would you need in order to store the numbers between 0 and 3 inclusive? -Answer:2 bits (3 in base 2 is 0b11) +Answer: How many bits would you need in order to store the numbers between 0 and 1000 inclusive? -Answer:10 bits (1000 in base 2 is 0b1111101000) +Answer: How can you test if a binary number is a power of two (e.g. 1, 2, 4, 8, 16, ...)? -Answer:a binary number is a power of 2 if it has exactly one "1" bit in its representation. +Answer: Convert the decimal number 14 to hex. -Answer:E +Answer: Convert the decimal number 386 to hex. -Answer:182 +Answer: Convert the hex number 386 to decimal. -Answer:902 +Answer: Convert the hex number B to decimal. -Answer:11 +Answer: If reading the byte 0x21 as a number, what decimal number would it mean? -Answer:33 +Answer: 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:Dark shade of gray +Answer: If reading the bytes 0xAA00FF as an RGB colour, as described in "Approaches for Representing Colors and Images", what colour would it mean? -Answer:(RGB 170, 0, 255) high red, no green, maximum blue. +Answer: If reading the bytes 0xAA00FF as a sequence of three one-byte decimal numbers, what decimal numbers would they be? -Answer:170, 0, 255 +Answer: From 4e42980c686242c747a365d8a74b27c1b1e95602 Mon Sep 17 00:00:00 2001 From: Fradoka Date: Thu, 31 Jul 2025 17:05:03 +0100 Subject: [PATCH 09/15] implemented ls --- implement-shell-tools/ls/my_ls.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 implement-shell-tools/ls/my_ls.js diff --git a/implement-shell-tools/ls/my_ls.js b/implement-shell-tools/ls/my_ls.js new file mode 100644 index 000000000..03c0a9178 --- /dev/null +++ b/implement-shell-tools/ls/my_ls.js @@ -0,0 +1,29 @@ +import { readdirSync } from 'node:fs'; +import { argv } from 'node:process'; + +const args = argv.slice(2); // skip node and script name + +let showAll = false; +let dir = '.'; //set dir to current directory + +// Parse args to check weather or not to show hidden files +//and ignore -1 because our output is already 1 per line +args.forEach(arg => { + if (arg === '-a') { + showAll = true; + } else if (arg !== '-1') { + dir = arg; //assumes anything else is our target directory + } +}); + +try { + let files = readdirSync(dir); // Reads the contents of the dir synchronously (readdirSync) + + if (!showAll) { + files = files.filter(file => !file.startsWith('.')); + } + + files.forEach(file => console.log(file)); +} catch (err) { + console.error(`Cannot access '${dir}': ${err.message}`); +} \ No newline at end of file From 647f1716aa529a37823330db682f774a2a2b6816 Mon Sep 17 00:00:00 2001 From: Fradoka Date: Thu, 31 Jul 2025 18:33:04 +0100 Subject: [PATCH 10/15] implemented cat and wc --- implement-shell-tools/cat/my_cat.js | 41 +++++++++++++ implement-shell-tools/wc/my_wc.js | 89 +++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 implement-shell-tools/cat/my_cat.js create mode 100644 implement-shell-tools/wc/my_wc.js diff --git a/implement-shell-tools/cat/my_cat.js b/implement-shell-tools/cat/my_cat.js new file mode 100644 index 000000000..5125f9eda --- /dev/null +++ b/implement-shell-tools/cat/my_cat.js @@ -0,0 +1,41 @@ +import { promises as fs } from 'node:fs'; +import { argv } from 'node:process'; + +const args = argv.slice(2); // skip node and script name + +let showLineNumb = false; +let showNoBlankNumb = false +let file = ''; + + +args.forEach(arg => { + if (arg === '-n') { + showLineNumb = true; + } else if (arg === '-b') { + showLineNumb = false; + showNoBlankNumb = true; + }else { + file = arg; + } +}); + +try { + let content = await fs.readFile(file, 'utf-8'); + const lines = content.split('\n'); + + if (showLineNumb) { + // Number all lines + content = lines.map((line, i) => `${i + 1}\t${line}`).join('\n'); + }else if (showNoBlankNumb) { + // Number only non-blank lines + let counter = 1; + content = lines.map(line => { + if (line.trim() === '') return ''; + return `${counter++}\t${line}`; + }).join('\n'); + } + + console.log (content) +} catch (err) { + console.error(`Cannot access '${file}': ${err.message}`); +} \ No newline at end of file diff --git a/implement-shell-tools/wc/my_wc.js b/implement-shell-tools/wc/my_wc.js new file mode 100644 index 000000000..14b8ac5ef --- /dev/null +++ b/implement-shell-tools/wc/my_wc.js @@ -0,0 +1,89 @@ +import { promises as fs } from 'node:fs'; +import { argv } from 'node:process'; + +const args = argv.slice(2); + +if (args.length === 0) { + console.error('Usage: node my_wc.mjs [-l] [-w] [-c] [file2 ...]'); + process.exit(1); +} + +// Parse flags and files +const flags = { + l: false, + w: false, + c: false, +}; + +const files = []; + +for (const arg of args) { + if (arg.startsWith('-') && arg.length > 1) { + for (const ch of arg.slice(1)) { + if (flags.hasOwnProperty(ch)) { + flags[ch] = true; + } else { + console.error(`Unknown option: -${ch}`); + process.exit(1); + } + } + } else { + files.push(arg); + } +} + +if (files.length === 0) { + console.error('Error: no files specified.'); + process.exit(1); +} + +// If no flags specified, show all by default +const showLines = flags.l || (!flags.l && !flags.w && !flags.c); +const showWords = flags.w || (!flags.l && !flags.w && !flags.c); +const showBytes = flags.c || (!flags.l && !flags.w && !flags.c); + +async function countFile(file) { + try { + const content = await fs.readFile(file, 'utf-8'); + const lines = content.split('\n').filter(line => line.trim() !== '').length; + const words = content.trim().split(/\s+/).filter(Boolean).length; + const bytes = Buffer.byteLength(content, 'utf-8'); + + let output = ''; + if (showLines) output += `${lines} `; + if (showWords) output += `${words} `; + if (showBytes) output += `${bytes} `; + + output += file; + + console.log(output.trim()); + + return { lines, words, bytes }; + } catch (err) { + console.error(`Cannot access '${file}': ${err.message}`); + return null; + } +} + +async function main() { + const counts = await Promise.all(files.map(countFile)); + + const validCounts = counts.filter(c => c !== null); + + if (validCounts.length > 1) { + const totalLines = validCounts.reduce((sum, c) => sum + c.lines, 0); + const totalWords = validCounts.reduce((sum, c) => sum + c.words, 0); + const totalBytes = validCounts.reduce((sum, c) => sum + c.bytes, 0); + + let totalOutput = ''; + if (showLines) totalOutput += `${totalLines} `; + if (showWords) totalOutput += `${totalWords} `; + if (showBytes) totalOutput += `${totalBytes} `; + + totalOutput += 'total'; + + console.log(totalOutput.trim()); + } +} + +main(); From e1a0f746b195ede11d7cce7cb2efdf5ee78ac410 Mon Sep 17 00:00:00 2001 From: Fradoka Date: Sun, 31 Aug 2025 20:23:12 +0100 Subject: [PATCH 11/15] updated my_cat.js --- implement-shell-tools/cat/my_cat.js | 40 +++++++++++++++-------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/implement-shell-tools/cat/my_cat.js b/implement-shell-tools/cat/my_cat.js index 5125f9eda..76410a91f 100644 --- a/implement-shell-tools/cat/my_cat.js +++ b/implement-shell-tools/cat/my_cat.js @@ -5,37 +5,39 @@ const args = argv.slice(2); // skip node and script name let showLineNumb = false; let showNoBlankNumb = false -let file = ''; +const files = []; args.forEach(arg => { if (arg === '-n') { showLineNumb = true; } else if (arg === '-b') { - showLineNumb = false; showNoBlankNumb = true; }else { - file = arg; + files.push(arg); } }); -try { - let content = await fs.readFile(file, 'utf-8'); - const lines = content.split('\n'); +if (files.length === 0) { + console.error('No input file specified'); + process.exit(1); +} - if (showLineNumb) { - // Number all lines - content = lines.map((line, i) => `${i + 1}\t${line}`).join('\n'); - }else if (showNoBlankNumb) { - // Number only non-blank lines - let counter = 1; - content = lines.map(line => { - if (line.trim() === '') return ''; - return `${counter++}\t${line}`; - }).join('\n'); - } +for (const file of files) { + try { + let content = await fs.readFile(file, 'utf-8'); + const lines = content.split('\n'); - console.log (content) -} catch (err) { + if (showNoBlankNumb) { // Number only non-blank lines + let counter = 1; + content = lines.map(line => + line.trim() === '' ? '' : `${counter++}\t${line}` + ).join('\n'); + } else if (showLineNumb) { // Number all lines + content = lines.map((line, i) => `${i + 1}\t${line}`).join('\n'); + } + console.log(content); + } catch (err) { console.error(`Cannot access '${file}': ${err.message}`); + } } \ No newline at end of file From a83f327b0826967846ec3616cab583ebf915dd81 Mon Sep 17 00:00:00 2001 From: Fradoka Date: Sun, 31 Aug 2025 20:52:32 +0100 Subject: [PATCH 12/15] updated my_ls.js --- implement-shell-tools/ls/my_ls.js | 49 ++++++++++++++++--------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/implement-shell-tools/ls/my_ls.js b/implement-shell-tools/ls/my_ls.js index 03c0a9178..558499ba3 100644 --- a/implement-shell-tools/ls/my_ls.js +++ b/implement-shell-tools/ls/my_ls.js @@ -1,29 +1,32 @@ import { readdirSync } from 'node:fs'; import { argv } from 'node:process'; -const args = argv.slice(2); // skip node and script name - -let showAll = false; -let dir = '.'; //set dir to current directory - -// Parse args to check weather or not to show hidden files -//and ignore -1 because our output is already 1 per line -args.forEach(arg => { - if (arg === '-a') { - showAll = true; - } else if (arg !== '-1') { - dir = arg; //assumes anything else is our target directory - } -}); +const args = argv.slice(2); // skips node and script name -try { - let files = readdirSync(dir); // Reads the contents of the dir synchronously (readdirSync) +const flags = args.filter(arg => arg.startsWith('-')); +const operands = args.filter(arg => !arg.startsWith('-')); - if (!showAll) { - files = files.filter(file => !file.startsWith('.')); - } +let showAll = flags.includes('-a'); +let onePerLine = flags.includes('-1'); + +if (operands.length === 0) operands.push('.'); // defaults to current dir + +operands.forEach(dir => { + try { + let files = readdirSync(dir); // Reads the contents of the dir synchronously (readdirSync) - files.forEach(file => console.log(file)); -} catch (err) { - console.error(`Cannot access '${dir}': ${err.message}`); -} \ No newline at end of file + if (!showAll) { + files = files.filter(file => !file.startsWith('.')); + } + + files.sort((a, b) => a.localeCompare(b)); + + if (onePerLine) { + files.forEach(file => console.log(file)); + } else { + console.log(files.join(' ')); + } + } catch (err) { + console.error(`ls: cannot access '${dir}': ${err.message}`); + } +}); \ No newline at end of file From 7f7d707aba016127b19754f83e628c124451dffe Mon Sep 17 00:00:00 2001 From: Fradoka Date: Sun, 31 Aug 2025 21:56:00 +0100 Subject: [PATCH 13/15] updated my_wc.js --- implement-shell-tools/wc/my_wc.js | 51 ++++++++++++++++++------------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/implement-shell-tools/wc/my_wc.js b/implement-shell-tools/wc/my_wc.js index 14b8ac5ef..35c77e321 100644 --- a/implement-shell-tools/wc/my_wc.js +++ b/implement-shell-tools/wc/my_wc.js @@ -42,23 +42,30 @@ const showLines = flags.l || (!flags.l && !flags.w && !flags.c); const showWords = flags.w || (!flags.l && !flags.w && !flags.c); const showBytes = flags.c || (!flags.l && !flags.w && !flags.c); +function pad(num, width = 8) { + return num.toString().padStart(width, ' '); +} + +function formatOutput({ lines, words, bytes }, label) { + let output = ''; + if (showLines) output += pad(lines); + if (showWords) output += pad(words); + if (showBytes) output += pad(bytes); + output += ` ${label}`; + return output; +} + async function countFile(file) { try { const content = await fs.readFile(file, 'utf-8'); - const lines = content.split('\n').filter(line => line.trim() !== '').length; + const lines = content.split('\n').length - 1; const words = content.trim().split(/\s+/).filter(Boolean).length; const bytes = Buffer.byteLength(content, 'utf-8'); - let output = ''; - if (showLines) output += `${lines} `; - if (showWords) output += `${words} `; - if (showBytes) output += `${bytes} `; - - output += file; + const counts = { lines, words, bytes }; + console.log(formatOutput(counts, file)); - console.log(output.trim()); - - return { lines, words, bytes }; + return counts; } catch (err) { console.error(`Cannot access '${file}': ${err.message}`); return null; @@ -66,23 +73,23 @@ async function countFile(file) { } async function main() { - const counts = await Promise.all(files.map(countFile)); + // Instead of .map, I can explicitly collect promises using forEach(): + const promises = []; + files.forEach(file => { + promises.push(countFile(file)); + }); + const counts = await Promise.all(promises); const validCounts = counts.filter(c => c !== null); if (validCounts.length > 1) { - const totalLines = validCounts.reduce((sum, c) => sum + c.lines, 0); - const totalWords = validCounts.reduce((sum, c) => sum + c.words, 0); - const totalBytes = validCounts.reduce((sum, c) => sum + c.bytes, 0); - - let totalOutput = ''; - if (showLines) totalOutput += `${totalLines} `; - if (showWords) totalOutput += `${totalWords} `; - if (showBytes) totalOutput += `${totalBytes} `; - - totalOutput += 'total'; + const totals = { + lines: validCounts.reduce((sum, c) => sum + c.lines, 0), + words: validCounts.reduce((sum, c) => sum + c.words, 0), + bytes: validCounts.reduce((sum, c) => sum + c.bytes, 0), + } - console.log(totalOutput.trim()); + console.log(formatOutput(totals, 'total')); } } From a38f2e98ef93951e104faa0044c6103645d1d921 Mon Sep 17 00:00:00 2001 From: Fradoka Date: Mon, 1 Sep 2025 17:57:26 +0100 Subject: [PATCH 14/15] updated my_cat.js to address numbering for multiple files --- implement-shell-tools/cat/my_cat.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/implement-shell-tools/cat/my_cat.js b/implement-shell-tools/cat/my_cat.js index 76410a91f..6b50fd410 100644 --- a/implement-shell-tools/cat/my_cat.js +++ b/implement-shell-tools/cat/my_cat.js @@ -23,18 +23,21 @@ if (files.length === 0) { process.exit(1); } +// Global counters across all files +let lineCounter = 1; +let nonBlankCounter = 1; + for (const file of files) { try { let content = await fs.readFile(file, 'utf-8'); const lines = content.split('\n'); if (showNoBlankNumb) { // Number only non-blank lines - let counter = 1; content = lines.map(line => - line.trim() === '' ? '' : `${counter++}\t${line}` + line.trim() === '' ? '' : `${nonBlankCounter++}\t${line}` ).join('\n'); } else if (showLineNumb) { // Number all lines - content = lines.map((line, i) => `${i + 1}\t${line}`).join('\n'); + content = lines.map((line) => `${lineCounter++}\t${line}`).join('\n'); } console.log(content); } catch (err) { From 60983596008b018fb69e4f6a77248e7485a720e3 Mon Sep 17 00:00:00 2001 From: Fradoka Date: Tue, 2 Sep 2025 14:20:24 +0100 Subject: [PATCH 15/15] amended my_cat.js to address review on counter variable --- implement-shell-tools/cat/my_cat.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/implement-shell-tools/cat/my_cat.js b/implement-shell-tools/cat/my_cat.js index 6b50fd410..b23b575f4 100644 --- a/implement-shell-tools/cat/my_cat.js +++ b/implement-shell-tools/cat/my_cat.js @@ -24,8 +24,7 @@ if (files.length === 0) { } // Global counters across all files -let lineCounter = 1; -let nonBlankCounter = 1; +let counter = 1; for (const file of files) { try { @@ -34,10 +33,10 @@ for (const file of files) { if (showNoBlankNumb) { // Number only non-blank lines content = lines.map(line => - line.trim() === '' ? '' : `${nonBlankCounter++}\t${line}` + line.trim() === '' ? '' : `${counter++}\t${line}` ).join('\n'); } else if (showLineNumb) { // Number all lines - content = lines.map((line) => `${lineCounter++}\t${line}`).join('\n'); + content = lines.map((line) => `${counter++}\t${line}`).join('\n'); } console.log(content); } catch (err) {