diff --git a/individual-shell-tools/awk/script-01.sh b/individual-shell-tools/awk/script-01.sh index 8db4390af..15a354a39 100755 --- a/individual-shell-tools/awk/script-01.sh +++ b/individual-shell-tools/awk/script-01.sh @@ -4,3 +4,10 @@ set -euo pipefail # TODO: Write a command to output just the names of each player in `scores-table.txt`. # Your output should contain 6 lines, each with just one word on it. + +awk '{print $1}' scores-table.txt + +# **How it works:** +# - `awk` processes the file line by line +# - `$1` refers to the first field (column) on each line, which is the player's name +# - This gives you 6 lines, one name each \ 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..de8e85162 100755 --- a/individual-shell-tools/awk/script-02.sh +++ b/individual-shell-tools/awk/script-02.sh @@ -4,3 +4,13 @@ set -euo pipefail # TODO: Write a command to output the names of each player, as well as their city. # Your output should contain 6 lines, each with two words on it, separated by a space. + +awk '{print $1, $2}' scores-table.txt + +# **How it works:** +# - `$1` = player name +# - `$2` = city +# - The `,` between them prints a **space** between the two values + +#or you can also use, +#cut -d' ' -f1,2 scores-table.txt \ 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..147e7145d 100755 --- a/individual-shell-tools/awk/script-03.sh +++ b/individual-shell-tools/awk/script-03.sh @@ -5,3 +5,11 @@ set -euo pipefail # TODO: Write a command to output just the names of each player along with the score from their first attempt. # Your output should contain 6 lines, each with one word and one number on it. # The first line should be "Ahmed 1". + +awk '{print $1, $3}' scores-table.txt + +#how it works +#awk sees each word/number separated by spaces as a numbered field: +#awk reads the file line by line +# For each line, {print $1, $3} prints the 1st and 3rd fields +# The , between $1 and $3 adds a space between them \ 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..634b843c0 100755 --- a/individual-shell-tools/awk/script-04.sh +++ b/individual-shell-tools/awk/script-04.sh @@ -5,3 +5,12 @@ set -euo pipefail # TODO: Write a command to output just the names of each player in London along with the score from their last attempt. # Your output should contain 3 lines, each with one word and one number on it. # The first line should be "Ahmed 4". + +awk '$2 == "London" {print $1, $NF}' scores-table.txt + +# awk goes line by line and does two things: + +# Filter — $2 == "London" skips any line where the 2nd field isn't London +# Print — for matching lines, print the name ($1) and the last score ($NF) + +# $NF is the smart part — it automatically counts the fields on each line and picks the last one, so it doesn't matter that Leila has fewer scores than the others. \ 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..766dad125 100755 --- a/individual-shell-tools/awk/script-05.sh +++ b/individual-shell-tools/awk/script-05.sh @@ -5,3 +5,10 @@ set -euo pipefail # TODO: Write a command to output just the names of each player along with the number of times they've played the game. # Your output should contain 6 lines, each with one word and one number on it. # The first line should be "Ahmed 3". + +awk '{print $1, NF-2}' scores-table.txt + +# awk goes line by line and does two things: + +# Print the name — $1 grabs the first field +# Count the scores — NF is the total number of fields, minus 2 removes the name and city, leaving just the number of scores \ 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..f4d9aacdd 100755 --- a/individual-shell-tools/awk/script-06-stretch.sh +++ b/individual-shell-tools/awk/script-06-stretch.sh @@ -6,3 +6,13 @@ set -euo pipefail # TODO: Write a command to output the total of adding together all players' first scores. # Your output should be exactly the number 54. + + +awk '{sum += $3} END {print sum}' scores-table.txt + +# awk does two things: + +# sum += $3 — goes line by line, continuously adding each player's first score ($3) to a running total +# END {print sum} — once all lines are done, prints the final total + +# END is the key new concept — it's a special block that runs once after all lines have been read. \ 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..4b1ba6db2 100755 --- a/individual-shell-tools/awk/script-07-stretch.sh +++ b/individual-shell-tools/awk/script-07-stretch.sh @@ -7,3 +7,13 @@ set -euo pipefail # TODO: Write a command to output just the names of each player along with the total of adding all of that player's scores. # Your output should contain 6 lines, each with one word and one number on it. # The first line should be "Ahmed 15". The second line should be "Basia 37" + + +awk '{sum=0; for(i=3; i<=NF; i++) sum+=$i; print $1, sum}' scores-table.txt + +# **How it works:** + +# 1. **`sum=0`** — resets the total for each new player/line +# 2. **`for(i=3; i<=NF; i++)`** — loops through fields starting from `$3` (first score) up to `$NF` (last score), skipping name and city +# 3. **`sum+=$i`** — adds each score to the total +# 4. **`print $1, sum`** — prints the player name and their total \ 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 c85053e0f..ac4c0bd2d 100755 --- a/individual-shell-tools/cat/script-01.sh +++ b/individual-shell-tools/cat/script-01.sh @@ -4,3 +4,10 @@ set -euo pipefail # TODO: Write a command to output the contents of the helper-1.txt file inside the helper-files directory to the terminal. # The output of this command should be "Once upon a time...". + +cat ../helper-files/helper-1.txt + + + +# cat reads a file and prints its contents to the terminal +# helper-files/helper-1.txt is the relative path — it looks for a folder called helper-files in your current directory, then the file inside it \ 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..c404ecb68 100755 --- a/individual-shell-tools/cat/script-02.sh +++ b/individual-shell-tools/cat/script-02.sh @@ -11,3 +11,11 @@ set -euo pipefail # It looked delicious. # I was tempted to take a bite of it. # But this seemed like a bad idea... + + +cat ../helper-files/* + +# * is a wildcard that matches all files inside the helper-files directory +# So cat helper-files/* expands to all files at once, letting you call cat just once +# cat then prints them all in alphabetical file order + diff --git a/individual-shell-tools/cat/script-03.sh b/individual-shell-tools/cat/script-03.sh index 37573b0c1..8a517b0d2 100755 --- a/individual-shell-tools/cat/script-03.sh +++ b/individual-shell-tools/cat/script-03.sh @@ -9,3 +9,9 @@ set -euo pipefail # 1 It looked delicious. # 2 I was tempted to take a bite of it. # 3 But this seemed like a bad idea... + +cat -n ../helper-files/helper-3.txt + +# How it works: + +# cat -n is the same cat command but the -n flag adds line numbers to the output \ 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..2c0b25a93 100755 --- a/individual-shell-tools/cat/script-04-stretch.sh +++ b/individual-shell-tools/cat/script-04-stretch.sh @@ -13,3 +13,10 @@ set -euo pipefail # 3 It looked delicious. # 4 I was tempted to take a bite of it. # 5 But this seemed like a bad idea... + +cat -n ../helper-files/* + +# How it works: + +# cat -n with multiple files normally resets line numbers per file +# But with * wildcard, cat treats all files as one continuous stream, so line numbers keep incrementing across all files \ 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 fb05f42f2..f96f238d6 100755 --- a/individual-shell-tools/grep/script-01.sh +++ b/individual-shell-tools/grep/script-01.sh @@ -4,3 +4,10 @@ set -euo pipefail # TODO: Write a command to output every line in dialogue.txt said by the Doctor. # The output should contain 6 lines. + +grep "^Doctor:" dialogue.txt + +# grep — scans files and returns lines that match a pattern +# -c — instead of printing matching lines, prints the count of matches per file +# ^Doctor: — the pattern; ^ means start of line, so only matches lines where Doctor: is at the beginning +# *.txt — runs across all .txt files in the current directory \ 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..830ccf89b 100755 --- a/individual-shell-tools/grep/script-02.sh +++ b/individual-shell-tools/grep/script-02.sh @@ -4,3 +4,13 @@ set -euo pipefail # TODO: Write a command to output every line in dialogue.txt that contains the word Doctor (regardless of case). # The output should contain 9 lines. + +grep -in "Doctor" dialogue.txt + +# How it works: + +# grep — scans files and returns lines that match a pattern +# -i — makes the search case-insensitive, matching Doctor, doctor, DOCTOR etc. +# -n — shows the line number of each matching line +# "Doctor" — the pattern to search for +# dialogue.txt — the file to search in \ 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..a334d295e 100755 --- a/individual-shell-tools/grep/script-03.sh +++ b/individual-shell-tools/grep/script-03.sh @@ -4,3 +4,10 @@ set -euo pipefail # TODO: Write a command to output the number of lines in dialogue.txt that contain the word Doctor (regardless of case). # The output should be exactly the number 9. + +grep -ic "Doctor" dialogue.txt + +# How it works: + +# -i makes the search case-insensitive +# -c instead of printing the matching lines, it prints the count of matching lines \ 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..eb99aa784 100755 --- a/individual-shell-tools/grep/script-04.sh +++ b/individual-shell-tools/grep/script-04.sh @@ -4,3 +4,10 @@ set -euo pipefail # TODO: Write a command to output every line in dialogue.txt that does not contain the word "Hello" (regardless of case). # The output should contain 10 lines. + +grep -iv "Hello" dialogue.txt + +# How it works: + +# -i makes the search case-insensitive +# -v inverts the match — prints every line that does not contain the word "Hello" \ 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..a60bff5c2 100755 --- a/individual-shell-tools/grep/script-05.sh +++ b/individual-shell-tools/grep/script-05.sh @@ -4,3 +4,12 @@ set -euo pipefail # TODO: Write a command to output every line in dialogue.txt that contains the string "cure", as well as the line before that line. # The output should contain two pairs of two lines of text (with a separator between them). + + +grep -iB1 "cure" dialogue.txt + +# How it works: + +# -i makes the search case-insensitive +# -B1 prints 1 line Before each matching line +# The separator -- is automatically added by grep between each pair of results \ 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..05f8579ba 100755 --- a/individual-shell-tools/grep/script-06.sh +++ b/individual-shell-tools/grep/script-06.sh @@ -4,3 +4,10 @@ set -euo pipefail # TODO: Write a command to output the name of every `.txt` file in this directory which contains a line of dialogue said by the Doctor. # The output should contain two filenames. + +grep -l "Doctor" *.txt + +# How it works: + +# -l instead of printing the matching lines, it prints only the filename of each file that contains a match +# *.txt searches through all .txt files in the current directory \ 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..028973df1 100755 --- a/individual-shell-tools/grep/script-07.sh +++ b/individual-shell-tools/grep/script-07.sh @@ -4,3 +4,12 @@ set -euo pipefail # TODO: Write a command to output, for each `.txt` file in this directory, how many lines of dialogue the Doctor has. # The output should show that dialogue.txt contains 6 lines, dialogue-2.txt contains 2, and dialogue-3.txt contains 0. + + +grep -c "^Doctor" *.txt + +# How it works: + +# ^Doctor — ^ means "start of line", so it only matches lines where Doctor appears at the beginning, filtering out lines where Doctor is just mentioned mid-sentence +# -c — prints the count of matching lines per file +# *.txt — runs across all .txt files in the current directory diff --git a/individual-shell-tools/ls/script-01.sh b/individual-shell-tools/ls/script-01.sh index 241b62f5e..7f01ef69d 100755 --- a/individual-shell-tools/ls/script-01.sh +++ b/individual-shell-tools/ls/script-01.sh @@ -13,3 +13,8 @@ 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. + + +#-------answer-------------- + +# ls \ 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..6476d1c74 100755 --- a/individual-shell-tools/ls/script-02.sh +++ b/individual-shell-tools/ls/script-02.sh @@ -4,3 +4,10 @@ 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. + +#-------answer-------------- + +ls child-directory + +# ls — lists files and folders +# child-directory — the folder to look inside \ 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..ea9a64cb5 100755 --- a/individual-shell-tools/ls/script-03.sh +++ b/individual-shell-tools/ls/script-03.sh @@ -5,3 +5,10 @@ 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. + +#-------answer-------------- + +ls -R + +# ls — lists files and folders +# -R — recursively lists all files and folders inside every subdirectory too diff --git a/individual-shell-tools/ls/script-04.sh b/individual-shell-tools/ls/script-04.sh index 72f3817b3..e4f6945c3 100755 --- a/individual-shell-tools/ls/script-04.sh +++ b/individual-shell-tools/ls/script-04.sh @@ -21,3 +21,19 @@ 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. + + +#-------------answer-------------- + +# First exercise (sorted newest to oldest): +ls -t1 child-directory + +# Second exercise (sorted oldest to newest): +ls -tr1 child-directory + +# How it works: + +# -t — sorts by modification time, newest first +# -r — reverses the order, making it oldest first +# -1 — displays one file per line +# child-directory — the folder to list \ 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 d592970fc..d47b2ee49 100755 --- a/individual-shell-tools/sed/script-01.sh +++ b/individual-shell-tools/sed/script-01.sh @@ -5,3 +5,18 @@ 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.". + + +#---------------answer-------------- + +sed 's/i/I/g' input.txt + +# How it works: + +# sed — stream editor that transforms text +# s/i/I/g — the substitution command: + +# s — substitute +# i — the pattern to find +# I — the replacement +# g — globally replace all occurrences on each line, not just the first \ 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..aa0a2b4a0 100755 --- a/individual-shell-tools/sed/script-02.sh +++ b/individual-shell-tools/sed/script-02.sh @@ -5,3 +5,15 @@ 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". + +#---------------answer-------------- + +sed 's/[0-9]//g' input.txt + +# How it works: + +# s/[0-9]//g — the substitution command: + +# [0-9] — matches any digit (0 through 9) +# `` — replaces with nothing (deletes it) +# g — removes all occurrences on each line \ 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..7620a2d59 100755 --- a/individual-shell-tools/sed/script-03.sh +++ b/individual-shell-tools/sed/script-03.sh @@ -4,3 +4,13 @@ set -euo pipefail # TODO: Write a command to output input.txt removing any line which contains a number. # The output should contain 6 lines. + + +#---------------answer-------------- + +sed '/[0-9]/d' input.txt + +# How it works: + +# /[0-9]/ — matches any line containing a digit +# d — deletes the matching lines from the output \ 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..ef4723c44 100755 --- a/individual-shell-tools/sed/script-04.sh +++ b/individual-shell-tools/sed/script-04.sh @@ -4,3 +4,14 @@ 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. + + +#---------------answer-------------- + +sed "s/We'll/We will/g" input.txt + +# How it works: + +# sed — stream editor that transforms text line by line +# s/We'll/We will/g — substitute We'll with We will on every occurrence +# double quotes — used instead of single quotes because the pattern contains a ' \ 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..e77b29ec3 100755 --- a/individual-shell-tools/sed/script-05.sh +++ b/individual-shell-tools/sed/script-05.sh @@ -6,3 +6,17 @@ 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. + + +#---------------answer-------------- + +sed 's/^\([0-9]*\) \(.*\)/\2 \1/' input.txt + +# How it works: + +# sed — stream editor that transforms text line by line +# ^ — matches the start of the line +# \([0-9]*\) — captures the number as group 1 +# — matches the space between number and name +# \(.*\) — captures the rest of the line as group 2 +# \2 \1 — prints group 2 (name) first, then group 1 (number), swapping them \ 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..310888667 100755 --- a/individual-shell-tools/sed/script-06.sh +++ b/individual-shell-tools/sed/script-06.sh @@ -8,3 +8,16 @@ 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.". + + +#---------------answer-------------- + +sed 's/,\([^ ]\)/, \1/g' input.txt + +# How it works: + +# sed — stream editor that transforms text line by line +# , — matches a comma +# \([^ ]\) — captures any character that is not a space after the comma as group 1 +# , \1 — replaces with a comma, a space, then group 1 +# g — applies to all occurrences on each line \ 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 c9dd6e5df..6b7484151 100755 --- a/individual-shell-tools/wc/script-01.sh +++ b/individual-shell-tools/wc/script-01.sh @@ -4,3 +4,14 @@ 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. + + +#---------------answer-------------- + +wc -w ../helper-files/helper-3.txt + +# How it works: + +# wc — word count tool +# -w — counts only words (without this flag it also shows lines and bytes, which includes 92) +# ../helper-files/helper-3.txt — the file to count \ 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..d33c54bce 100755 --- a/individual-shell-tools/wc/script-02.sh +++ b/individual-shell-tools/wc/script-02.sh @@ -4,3 +4,14 @@ 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. + + +#---------------answer-------------- + +wc -l ../helper-files/helper-3.txt + + +# How it works: + +# wc — word count tool +# -l — counts only lines instead of words or bytes diff --git a/individual-shell-tools/wc/script-03.sh b/individual-shell-tools/wc/script-03.sh index 6b2e9d3d1..dd8a0bf6c 100755 --- a/individual-shell-tools/wc/script-03.sh +++ b/individual-shell-tools/wc/script-03.sh @@ -8,3 +8,14 @@ set -euo pipefail # 1 7 39 ../helper-files/helper-2.txt # 3 19 92 ../helper-files/helper-3.txt # 5 30 151 total + + +#---------------answer-------------- + +wc ../helper-files/* + +# How it works: + +# wc — without any flags, outputs lines, words, and characters for each file +# ../helper-files/* — runs across all files in the directory +# automatically adds a total line at the end when multiple files are given \ No newline at end of file diff --git a/number-systems/README.md b/number-systems/README.md index 77a3bde94..64eea8fdc 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 (binary 1111) How many bits would you need in order to store the numbers between 0 and 255 inclusive? -Answer: +Answer:8 bits How many bits would you need in order to store the numbers between 0 and 3 inclusive? -Answer: +Answer:2 bits How many bits would you need in order to store the numbers between 0 and 1000 inclusive? -Answer: +Answer:10 bits How can you test if a binary number is a power of two (e.g. 1, 2, 4, 8, 16, ...)? -Answer: +Answer:It has exactly one 1 bit Convert the decimal number 14 to hex. -Answer: +Answer: E Convert the decimal number 386 to hex. -Answer: +Answer:0x182 Convert the hex number 386 to decimal. -Answer: +Answer:902 Convert the hex number B to decimal. -Answer: +Answer:11 If reading the byte 0x21 as a number, what decimal number would it mean? -Answer: +Answer:33 If reading the byte 0x21 as an ASCII character, what character would it mean? -Answer: +Answer:! If reading the byte 0x21 as a greyscale colour, as described in "Approaches for Representing Colors and Images", what colour would it mean? -Answer: +Answer:Dark color If reading the bytes 0xAA00FF as an RGB colour, as described in "Approaches for Representing Colors and Images", what colour would it mean? -Answer: +Answer:purple If reading the bytes 0xAA00FF as a sequence of three one-byte decimal numbers, what decimal numbers would they be? -Answer: +Answer:255