From 06113837c2d9e181746e5a43717a66aec24a58e1 Mon Sep 17 00:00:00 2001 From: Fradoka Date: Tue, 15 Jul 2025 06:06:30 +0100 Subject: [PATCH 01/11] 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/11] 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/11] 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/11] 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/11] 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/11] 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/11] 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/11] 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 0e07af9c6a36bd85f756537614dd3bd058d1bdbd Mon Sep 17 00:00:00 2001 From: Fradoka Date: Fri, 8 Aug 2025 13:24:32 +0100 Subject: [PATCH 09/11] implemented shell tools (cat, ls, wc) in python --- .vscode/settings.json | 5 +++ implement-shell-tools/cat/cat.py | 44 +++++++++++++++++++++ implement-shell-tools/ls/ls.py | 36 +++++++++++++++++ implement-shell-tools/wc/wc.py | 67 ++++++++++++++++++++++++++++++++ 4 files changed, 152 insertions(+) create mode 100644 .vscode/settings.json create mode 100755 implement-shell-tools/cat/cat.py create mode 100644 implement-shell-tools/ls/ls.py create mode 100644 implement-shell-tools/wc/wc.py diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 000000000..b242572ef --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "githubPullRequests.ignoredPullRequestBranches": [ + "main" + ] +} \ No newline at end of file diff --git a/implement-shell-tools/cat/cat.py b/implement-shell-tools/cat/cat.py new file mode 100755 index 000000000..ea05bdd6d --- /dev/null +++ b/implement-shell-tools/cat/cat.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python3 +import argparse + +# implement-shell-tools/cat/cat.py +def main(): + parser = argparse.ArgumentParser( + prog="cat", + description="Implements a simple version of the 'cat' command to read and display file contents." + ) + + parser.add_argument("-n", help="Number lines", action="store_true") + parser.add_argument("-b", help="Number non-blank lines", action="store_true") + parser.add_argument("path", nargs="+", help="The file to search") + + args = parser.parse_args() + + for file_path in args.path: + try: + with open(file_path, 'r') as file: + content = file.read() + lines = content.split("\n") + line_number = 1 + non_blank_line_number = 1 + + for i, line in enumerate(lines): + if i == len(lines) - 1 and line == "": # Skip the last empty line if it exists + break + prefix = "" + if args.n: + prefix = str(line_number).rjust(6) + " " + elif args.b and line != "": + prefix = str(non_blank_line_number).rjust(6) + " " + print(prefix + line) + line_number += 1 + if line != "": + non_blank_line_number += 1 + + except FileNotFoundError: + print(f"cat: {file_path}: No such file or directory") + except Exception as e: + print(f"cat: {file_path}: {e}") + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/implement-shell-tools/ls/ls.py b/implement-shell-tools/ls/ls.py new file mode 100644 index 000000000..4b123fe42 --- /dev/null +++ b/implement-shell-tools/ls/ls.py @@ -0,0 +1,36 @@ +import argparse +import os + +# implement-shell-tools/ls/ls.py +def main(): + parser = argparse.ArgumentParser( + prog="ls", + description="Implements a simple version of the 'ls' command to list files in a directory." + ) + + parser.add_argument("-1", help="", action="store_true") + parser.add_argument("-a", help="", action="store_true") + parser.add_argument("directory", nargs="?", default=".", help="The directory to search") + + args = parser.parse_args() + + try: + files = os.listdir(args.directory) + + if not args.a: + files = [f for f in files if not f.startswith(".")] + + files = sorted(files) + + for f in files: + print(f) + + except FileNotFoundError: + print(f"ls: {args.directory}: No such file or directory") + except NotADirectoryError: + print(args.directory) + except Exception as e: + print(f"ls: {args.directory}: {e}") + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/implement-shell-tools/wc/wc.py b/implement-shell-tools/wc/wc.py new file mode 100644 index 000000000..4787ed74c --- /dev/null +++ b/implement-shell-tools/wc/wc.py @@ -0,0 +1,67 @@ +import argparse +import sys + +# implement-shell-tools/wc/wc.py +def wc_file(path): + """Return (lines, words, bytes) for a given file.""" + with open(path, "r", encoding="utf-8") as f: + text = f.read() + lines = text.count("\n") + words = len(text.split()) + chars = len(text.encode("utf-8")) # byte count + return lines, words, chars + + +def main(): + parser = argparse.ArgumentParser( + prog="wc", + description="Implements a simple version of the 'wc' command to count lines, words, and characters in text files." + ) + + parser.add_argument("-l", action="store_true", help="Count lines") + parser.add_argument("-w", action="store_true", help="Count words") + parser.add_argument("-c", action="store_true", help="Count characters") + parser.add_argument("path", nargs="+", help="The files to read") + + args = parser.parse_args() + + if not (args.l or args.w or args.c): + args.l = args.w = args.c = True # Default to all counts if none specified + + total_lines = total_words = total_chars = 0 + + for file_path in args.path: + try: + lines, words, chars = wc_file(file_path) + total_lines += lines + total_words += words + total_chars += chars + + counts = [] + if args.l: + counts.append(str(lines)) + if args.w: + counts.append(str(words)) + if args.c: + counts.append(str(chars)) + counts.append(file_path) + print(" ".join(counts)) + + except FileNotFoundError: + print(f"wc: {file_path}: No such file or directory", file=sys.stderr) + except Exception as e: + print(f"wc: {file_path}: {e}", file=sys.stderr) + + if len(args.path) > 1: + counts = [] + if args.l: + counts.append(str(total_lines)) + if args.w: + counts.append(str(total_words)) + if args.c: + counts.append(str(total_chars)) + counts.append("total") + print(" ".join(counts)) + +if __name__ == "__main__": + main() \ No newline at end of file From 37708011f67920db83612875aae15e0cf7be7496 Mon Sep 17 00:00:00 2001 From: Fradoka Date: Mon, 1 Sep 2025 17:35:08 +0100 Subject: [PATCH 10/11] amended cat.py, ls.py, and wc.py --- implement-shell-tools/cat/cat.py | 5 +-- implement-shell-tools/ls/ls.py | 58 ++++++++++++++++++++++++-------- implement-shell-tools/wc/wc.py | 34 +++++++++---------- 3 files changed, 63 insertions(+), 34 deletions(-) diff --git a/implement-shell-tools/cat/cat.py b/implement-shell-tools/cat/cat.py index ea05bdd6d..5518c83b5 100755 --- a/implement-shell-tools/cat/cat.py +++ b/implement-shell-tools/cat/cat.py @@ -14,13 +14,14 @@ def main(): args = parser.parse_args() + line_number = 1 + non_blank_line_number = 1 + for file_path in args.path: try: with open(file_path, 'r') as file: content = file.read() lines = content.split("\n") - line_number = 1 - non_blank_line_number = 1 for i, line in enumerate(lines): if i == len(lines) - 1 and line == "": # Skip the last empty line if it exists diff --git a/implement-shell-tools/ls/ls.py b/implement-shell-tools/ls/ls.py index 4b123fe42..18e6ed0f8 100644 --- a/implement-shell-tools/ls/ls.py +++ b/implement-shell-tools/ls/ls.py @@ -1,36 +1,66 @@ +# implement-shell-tools/ls/ls.py import argparse import os +import sys +import shutil +import math + +def print_columns(items, force_single_column=False): + """Print items in columns unless -1 is passed or output is not a tty.""" + if force_single_column or not sys.stdout.isatty(): + for item in items: + print(item) + return + + # Get terminal width + term_width = shutil.get_terminal_size((80, 20)).columns + + if not items: + return + + # Longest filename length + spacing + max_len = max(len(f) for f in items) + 2 + cols = max(1, term_width // max_len) + rows = math.ceil(len(items) / cols) + + for r in range(rows): + row_items = [] + for c in range(cols): + i = c * rows + r + if i < len(items): + row_items.append(items[i].ljust(max_len)) + print("".join(row_items).rstrip()) -# implement-shell-tools/ls/ls.py def main(): parser = argparse.ArgumentParser( prog="ls", description="Implements a simple version of the 'ls' command to list files in a directory." ) - parser.add_argument("-1", help="", action="store_true") - parser.add_argument("-a", help="", action="store_true") + parser.add_argument("-1", help="List one file per line", action="store_true") + parser.add_argument("-a", help="Include hidden files", action="store_true") parser.add_argument("directory", nargs="?", default=".", help="The directory to search") args = parser.parse_args() try: - files = os.listdir(args.directory) + if os.path.isdir(args.directory): + files = os.listdir(args.directory) - if not args.a: - files = [f for f in files if not f.startswith(".")] - - files = sorted(files) + if not args.a: + files = [f for f in files if not f.startswith(".")] + + files = sorted(files) - for f in files: - print(f) + print_columns(files, force_single_column=args.__dict__["1"]) + else: + # If it's a file, just print the name + print(args.directory) except FileNotFoundError: - print(f"ls: {args.directory}: No such file or directory") - except NotADirectoryError: - print(args.directory) + print(f"ls: {args.directory}: No such file or directory", file=sys.stderr) except Exception as e: - print(f"ls: {args.directory}: {e}") + print(f"ls: {args.directory}: {e}", file=sys.stderr) if __name__ == "__main__": main() \ No newline at end of file diff --git a/implement-shell-tools/wc/wc.py b/implement-shell-tools/wc/wc.py index 4787ed74c..1eff9fa21 100644 --- a/implement-shell-tools/wc/wc.py +++ b/implement-shell-tools/wc/wc.py @@ -7,10 +7,24 @@ def wc_file(path): with open(path, "r", encoding="utf-8") as f: text = f.read() lines = text.count("\n") + if text and not text.endswith("\n"): + lines += 1 # Count last line if it doesn't end with newline words = len(text.split()) chars = len(text.encode("utf-8")) # byte count return lines, words, chars +def format_counts(lines, words, chars, args, label): + """Return formatted output string for wc counts.""" + parts = [] + if args.l: + parts.append(str(lines).rjust(8)) + if args.w: + parts.append(str(words).rjust(8)) + if args.c: + parts.append(str(chars).rjust(8)) + if label: + parts.append(label) + return " ".join(parts) def main(): parser = argparse.ArgumentParser( @@ -37,15 +51,7 @@ def main(): total_words += words total_chars += chars - counts = [] - if args.l: - counts.append(str(lines)) - if args.w: - counts.append(str(words)) - if args.c: - counts.append(str(chars)) - counts.append(file_path) - print(" ".join(counts)) + print(format_counts(lines, words, chars, args, file_path)) except FileNotFoundError: print(f"wc: {file_path}: No such file or directory", file=sys.stderr) @@ -53,15 +59,7 @@ def main(): print(f"wc: {file_path}: {e}", file=sys.stderr) if len(args.path) > 1: - counts = [] - if args.l: - counts.append(str(total_lines)) - if args.w: - counts.append(str(total_words)) - if args.c: - counts.append(str(total_chars)) - counts.append("total") - print(" ".join(counts)) + print(format_counts(total_lines, total_words, total_chars, args, "total")) if __name__ == "__main__": main() \ No newline at end of file From 3022163b7d9a2ca5d013b9c0f3e8cf4676387c6b Mon Sep 17 00:00:00 2001 From: Fradoka Date: Tue, 2 Sep 2025 14:42:08 +0100 Subject: [PATCH 11/11] amended cat.py to address counter review --- implement-shell-tools/cat/cat.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/implement-shell-tools/cat/cat.py b/implement-shell-tools/cat/cat.py index 5518c83b5..f32c9c2b9 100755 --- a/implement-shell-tools/cat/cat.py +++ b/implement-shell-tools/cat/cat.py @@ -14,8 +14,7 @@ def main(): args = parser.parse_args() - line_number = 1 - non_blank_line_number = 1 + counter = 1 for file_path in args.path: try: @@ -27,14 +26,14 @@ def main(): if i == len(lines) - 1 and line == "": # Skip the last empty line if it exists break prefix = "" - if args.n: - prefix = str(line_number).rjust(6) + " " - elif args.b and line != "": - prefix = str(non_blank_line_number).rjust(6) + " " + if args.b: + if line != "": + prefix = str(counter).rjust(6) + " " + counter += 1 + elif args.n: + prefix = str(counter).rjust(6) + " " + counter += 1 print(prefix + line) - line_number += 1 - if line != "": - non_blank_line_number += 1 except FileNotFoundError: print(f"cat: {file_path}: No such file or directory")