From 06113837c2d9e181746e5a43717a66aec24a58e1 Mon Sep 17 00:00:00 2001 From: Fradoka Date: Tue, 15 Jul 2025 06:06:30 +0100 Subject: [PATCH 1/9] 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 241b62f5..92631c6c 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 d0a5a10f..2346ae30 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 781216d2..f4382670 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 72f3817b..1d6bf63d 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 2/9] 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 c85053e0..7d0f34ba 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 01bbd5ea..2cff2c63 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 37573b0c..c14b1037 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 00fe3c48..73640a35 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 3/9] 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 c9dd6e5d..acc39b8d 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 8feeb1a6..32a6cb5d 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 6b2e9d3d..cf4fc6b7 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 4/9] 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 fb05f42f..54169a1c 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 df6f8564..2cde17d1 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 5383fe57..38637aae 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 80ee0477..ab2566f4 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 1eb53818..eb0b06cd 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 5670e3b6..2686fe8c 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 9670ebad..a6ca13ff 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 5/9] 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 3eba6fa4..ed6ca0f0 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 abdd64d0..cdd2432c 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 dd284a29..11b1e827 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 0052ac6c..cf3a2301 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 2dcc91a0..922d14b5 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 0b939017..82de1099 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 6/9] 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 8db4390a..336b3fd5 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 5956be9b..828fabb0 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 af7c6e8b..f8561ed5 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 bf15703c..15ca5e93 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 d1680cb0..29aa79ae 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 0201e637..bf9e60a4 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 3f715588..38eb8bed 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 7/9] 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 77a3bde9..328312ba 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 8/9] 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 336b3fd5..8db4390a 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 828fabb0..5956be9b 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 f8561ed5..af7c6e8b 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 15ca5e93..bf15703c 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 29aa79ae..d1680cb0 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 bf9e60a4..0201e637 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 38eb8bed..3f715588 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 7d0f34ba..c85053e0 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 2cff2c63..01bbd5ea 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 c14b1037..37573b0c 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 73640a35..00fe3c48 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 54169a1c..fb05f42f 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 2cde17d1..df6f8564 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 38637aae..5383fe57 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 ab2566f4..80ee0477 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 eb0b06cd..1eb53818 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 2686fe8c..5670e3b6 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 a6ca13ff..9670ebad 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 92631c6c..241b62f5 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 2346ae30..d0a5a10f 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 f4382670..781216d2 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 1d6bf63d..72f3817b 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 ed6ca0f0..3eba6fa4 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 cdd2432c..abdd64d0 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 11b1e827..dd284a29 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 cf3a2301..0052ac6c 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 922d14b5..2dcc91a0 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 82de1099..0b939017 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 acc39b8d..c9dd6e5d 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 32a6cb5d..8feeb1a6 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 cf4fc6b7..6b2e9d3d 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 328312ba..77a3bde9 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 2ed227bacb78d0cfcb067f1e78a1e387f1d8cb32 Mon Sep 17 00:00:00 2001 From: Fradoka Date: Wed, 14 Jan 2026 21:38:34 +0000 Subject: [PATCH 9/9] prep done --- introduction-to-typescript | 1 + .../classes-and-objects-and-methods.py | 18 +++++ sprint-5-prep/dataclasses-chapter.py | 26 ++++++ sprint-5-prep/enums-chapter.py | 81 +++++++++++++++++++ sprint-5-prep/generics-chapter.py | 19 +++++ sprint-5-prep/how-we-use-types.md | 28 +++++++ sprint-5-prep/inheritance-chapter.py | 37 +++++++++ sprint-5-prep/type-checking-with-mypy.py | 30 +++++++ sprint-5-prep/type-guided-refactoring.py | 43 ++++++++++ 9 files changed, 283 insertions(+) create mode 160000 introduction-to-typescript create mode 100644 sprint-5-prep/classes-and-objects-and-methods.py create mode 100644 sprint-5-prep/dataclasses-chapter.py create mode 100644 sprint-5-prep/enums-chapter.py create mode 100644 sprint-5-prep/generics-chapter.py create mode 100644 sprint-5-prep/how-we-use-types.md create mode 100644 sprint-5-prep/inheritance-chapter.py create mode 100644 sprint-5-prep/type-checking-with-mypy.py create mode 100644 sprint-5-prep/type-guided-refactoring.py diff --git a/introduction-to-typescript b/introduction-to-typescript new file mode 160000 index 00000000..f8d3a14c --- /dev/null +++ b/introduction-to-typescript @@ -0,0 +1 @@ +Subproject commit f8d3a14c4b4e8ec068697935a14063630d2679f5 diff --git a/sprint-5-prep/classes-and-objects-and-methods.py b/sprint-5-prep/classes-and-objects-and-methods.py new file mode 100644 index 00000000..ce9dcc7e --- /dev/null +++ b/sprint-5-prep/classes-and-objects-and-methods.py @@ -0,0 +1,18 @@ +from datetime import date + +class Person: + def __init__(self, name: str, dOB: date, preferred_operating_system: str): + self.name = name + self.dOB = dOB + self.preferred_operating_system = preferred_operating_system + + def is_adult(self) -> bool: + today = date.today() + eighteen_years_ago = date(today.year - 18, today.month, today.day) + return self.dOB <= eighteen_years_ago + +imran = Person("Imran", date(1992, 7, 21), "Ubuntu") +print(imran.name) + +print(imran.is_adult()) + diff --git a/sprint-5-prep/dataclasses-chapter.py b/sprint-5-prep/dataclasses-chapter.py new file mode 100644 index 00000000..03d4c20c --- /dev/null +++ b/sprint-5-prep/dataclasses-chapter.py @@ -0,0 +1,26 @@ +from dataclasses import dataclass +from datetime import date + +@dataclass (frozen=True) +class Person: + name: str + DOB: date + preferred_operating_system: str + + def is_adult(self) -> bool: + today = date.today() + eighteen_years_ago = date(today.year - 18, today.month, today.day) + return self.DOB <= eighteen_years_ago + +imran = Person("Imran", date(1992, 7, 21), "Ubuntu") +print(imran.name) + +imran2 = Person("Imran", date(1992, 7, 21), "Ubuntu") +print(imran == imran2) + +eliza = Person("Eliza", date(1982, 4, 5), "Arch Linux") +print(eliza.name) + + +print(imran.is_adult()) +print(eliza.is_adult()) diff --git a/sprint-5-prep/enums-chapter.py b/sprint-5-prep/enums-chapter.py new file mode 100644 index 00000000..0764e065 --- /dev/null +++ b/sprint-5-prep/enums-chapter.py @@ -0,0 +1,81 @@ +import sys +from sys import stderr +from dataclasses import dataclass +from enum import Enum +from typing import List + +class OperatingSystem(Enum): + MACOS = "macos" + ARCH = "arch linux" + UBUNTU = "ubuntu" + +@dataclass(frozen=True) +class Laptop: + id: int + manufacturer: str + model: str + screen_size_in_inches: float + operating_system: OperatingSystem + +@dataclass(frozen=True) +class Person: + name: str + age: int + preferred_operating_system: OperatingSystem + +available_laptops = [ + Laptop(id=1, manufacturer="Dell", model="XPS", screen_size_in_inches=13, operating_system=OperatingSystem.ARCH), + Laptop(id=2, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system=OperatingSystem.UBUNTU), + Laptop(id=3, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system=OperatingSystem.UBUNTU), + Laptop(id=4, manufacturer="Apple", model="macBook", screen_size_in_inches=13, operating_system=OperatingSystem.MACOS), + Laptop(id=5, manufacturer="HP", model="Spectre", screen_size_in_inches=13, operating_system=OperatingSystem.UBUNTU), + Laptop(id=6, manufacturer="Lenovo", model="ThinkPad", screen_size_in_inches=14, operating_system=OperatingSystem.ARCH), + Laptop(id=7, manufacturer="Apple", model="MacBook Pro", screen_size_in_inches=16, operating_system=OperatingSystem.MACOS), + Laptop(id=8, manufacturer="Dell", model="Inspiron", screen_size_in_inches=15, operating_system=OperatingSystem.UBUNTU), + Laptop(id=9, manufacturer="Asus", model="ZenBook", screen_size_in_inches=13, operating_system=OperatingSystem.ARCH), +] + + + +def error_exit(message: str, code: int =1) -> None: + print(f"Error: {message}", file=stderr) + sys.exit(code) + +def number_of_laptops_per_os() -> dict[OperatingSystem, int]: + os_count: dict[OperatingSystem, int] = { + OperatingSystem.MACOS: 0, + OperatingSystem.ARCH: 0, + OperatingSystem.UBUNTU: 0, + } + for laptop in available_laptops: + os_count[laptop.operating_system] += 1 + return os_count + +def main() -> None: + name: str = input("Enter your name: ").strip() + if not name: + error_exit("Name cannot be empty.") + + try: + age: int = int(input("Enter your age: ").strip()) + if age <= 0: + error_exit("Age must be a positive integer.") + except ValueError: + error_exit("Invalid age. Please enter a positive integer.") + + try: + preferred_operating_system: OperatingSystem = OperatingSystem(input("Enter your preferred operating system (macOS, Arch Linux, Ubuntu): ").strip().lower()) + except ValueError: + error_exit("Invalid operating system. Choose from macOS, Arch Linux, Ubuntu.") + + person = Person(name=name, age=age, preferred_operating_system=preferred_operating_system) + + os_count = number_of_laptops_per_os() + print(f"Hi {person.name}, there are {os_count[preferred_operating_system]} laptops available with {preferred_operating_system.value}.") + max_os, max_count = max(os_count.items(), key=lambda item: item[1]) + print(f"The operating system with the most laptops is {max_os.value} with {max_count} laptops.") + print ("person:", person) + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/sprint-5-prep/generics-chapter.py b/sprint-5-prep/generics-chapter.py new file mode 100644 index 00000000..796e653b --- /dev/null +++ b/sprint-5-prep/generics-chapter.py @@ -0,0 +1,19 @@ +from dataclasses import dataclass + +@dataclass(frozen=True) +class Person: + name: str + age: int + children: list["Person"] + +fatma = Person(name="Fatma", children=[], age=5) +aisha = Person(name="Aisha", children=[], age=3) + +imran = Person(name="Imran", children=[fatma, aisha], age=35) + +def print_family_tree(person: Person) -> None: + print(person.name) + for child in person.children: + print(f"- {child.name} ({child.age})") + +print_family_tree(imran) diff --git a/sprint-5-prep/how-we-use-types.md b/sprint-5-prep/how-we-use-types.md new file mode 100644 index 00000000..e9938e15 --- /dev/null +++ b/sprint-5-prep/how-we-use-types.md @@ -0,0 +1,28 @@ +```python +def half(value): + return value / 2 + +def double(value): + return value * 2 + +def second(value): + return value[1] + +print(double(22)) +print(double("hello")) +print(double("22")) +``` +# Exercise 1: + +Prediction: +double("22") will raise a typeError because "22" is a string. + +Actual Result: +double ("22") actually returned "2222" so it actually resulted in a string repetition "2222" +this shows that the '*2' sign on strings will repeat them + +# Exercise 2: + +the bug on the code is the naming of the function. you can't call a function double() but then +the math is [value *3]. so we should either rename it triple() or change the method to [value *2] +depending on the context and what we are trying to achieve. \ No newline at end of file diff --git a/sprint-5-prep/inheritance-chapter.py b/sprint-5-prep/inheritance-chapter.py new file mode 100644 index 00000000..e11b5410 --- /dev/null +++ b/sprint-5-prep/inheritance-chapter.py @@ -0,0 +1,37 @@ +class Parent: + def __init__(self, first_name: str, last_name: str): + self.first_name = first_name + self.last_name = last_name + + def get_name(self) -> str: + return f"{self.first_name} {self.last_name}" + + +class Child(Parent): + def __init__(self, first_name: str, last_name: str): + super().__init__(first_name, last_name) + self.previous_last_names = [] + + def change_last_name(self, last_name) -> None: + self.previous_last_names.append(self.last_name) + self.last_name = last_name + + def get_full_name(self) -> str: + suffix = "" + if len(self.previous_last_names) > 0: + suffix = f" (née {self.previous_last_names[0]})" + return f"{self.first_name} {self.last_name}{suffix}" + +person1 = Child("Elizaveta", "Alekseeva") +print(person1.get_name()) +print(person1.get_full_name()) +person1.change_last_name("Tyurina") +print(person1.get_name()) +print(person1.get_full_name()) + +person2 = Parent("Elizaveta", "Alekseeva") +print(person2.get_name()) +# print(person2.get_full_name()) +# person2.change_last_name("Tyurina") +print(person2.get_name()) +# print(person2.get_full_name()) \ No newline at end of file diff --git a/sprint-5-prep/type-checking-with-mypy.py b/sprint-5-prep/type-checking-with-mypy.py new file mode 100644 index 00000000..274a5c0d --- /dev/null +++ b/sprint-5-prep/type-checking-with-mypy.py @@ -0,0 +1,30 @@ +def open_account(balances: dict[str, int], name: str, amount: float) -> None: + balances[name] = int(amount*100) + +def sum_balances(accounts: dict[str, int]) -> int: + total = 0 + for name, pence in accounts.items(): + print(f"{name} had balance {pence}p") + total += pence + return total + +def format_pence_as_string(total_pence: int) -> str: + if total_pence < 100: + return f"{total_pence}p" + pounds = int(total_pence / 100) + pence = total_pence % 100 + return f"£{pounds}.{pence:02d}" + +balances = { + "Sima": 700, + "Linn": 545, + "Georg": 831, +} + +open_account(balances, "Tobi", 9.13) +open_account(balances, "Olya", 7.13) + +total_pence = sum_balances(balances) +total_string = format_pence_as_string(total_pence) + +print(f"The bank accounts total {total_string}") diff --git a/sprint-5-prep/type-guided-refactoring.py b/sprint-5-prep/type-guided-refactoring.py new file mode 100644 index 00000000..2eb2d935 --- /dev/null +++ b/sprint-5-prep/type-guided-refactoring.py @@ -0,0 +1,43 @@ +from dataclasses import dataclass +from typing import List + +@dataclass(frozen=True) +class Person: + name: str + age: int + preferred_operating_systems: List[str] + + +@dataclass(frozen=True) +class Laptop: + id: int + manufacturer: str + model: str + screen_size_in_inches: float + operating_system: str + + +def find_possible_laptops(laptops: List[Laptop], person: Person) -> List[Laptop]: + possible_laptops = [] + for laptop in laptops: + for os in person.preferred_operating_systems: + if laptop.operating_system == os: + possible_laptops.append(laptop) + return possible_laptops + + +people = [ + Person(name="Imran", age=22, preferred_operating_systems=["Ubuntu", "Arch Linux"]), + Person(name="Eliza", age=34, preferred_operating_systems=["Arch Linux"]), +] + +laptops = [ + Laptop(id=1, manufacturer="Dell", model="XPS", screen_size_in_inches=13, operating_system="Arch Linux"), + Laptop(id=2, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system="Ubuntu"), + Laptop(id=3, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system="ubuntu"), + Laptop(id=4, manufacturer="Apple", model="macBook", screen_size_in_inches=13, operating_system="macOS"), +] + +for person in people: + possible_laptops = find_possible_laptops(laptops, person) + print(f"Possible laptops for {person.name}: {possible_laptops}")