wiki/_next/data/Ie9t5zutrXP6Of75Cb5xF/challenges.exercism.bash.json
2022-07-29 15:41:17 +00:00

1 line
36 KiB
JSON

{"pageProps":{"metadata":{"date":"\"2021-05-11 15:06:43 +08:00\"","date_modified":"\"2021-06-22 17:50:43 +08:00\"","language":"en","source":""},"title":"Exercism track: Bash","hast":{"type":"root","children":[{"type":"element","tagName":"nav","properties":{"className":"toc"},"children":[{"type":"element","tagName":"ol","properties":{"className":"toc-level toc-level-1"},"children":[{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h1","properties":{"id":"hello-world"},"children":[{"type":"text","value":"Hello world"}]}]},"properties":{"className":"toc-item toc-item-h1"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h1","href":"/challenges.exercism.bash#hello-world"},"children":[{"type":"text","value":"Hello world"}]},{"type":"element","tagName":"ol","properties":{"className":"toc-level toc-level-2"},"children":[{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h2","properties":{"id":"solution"},"children":[{"type":"text","value":"Solution"}]}]},"properties":{"className":"toc-item toc-item-h2"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h2","href":"/challenges.exercism.bash#solution"},"children":[{"type":"text","value":"Solution"}]}]}]}]},{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h1","properties":{"id":"leap"},"children":[{"type":"text","value":"Leap"}]}]},"properties":{"className":"toc-item toc-item-h1"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h1","href":"/challenges.exercism.bash#leap"},"children":[{"type":"text","value":"Leap"}]},{"type":"element","tagName":"ol","properties":{"className":"toc-level toc-level-2"},"children":[{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h2","properties":{"id":"solution-1"},"children":[{"type":"text","value":"Solution"}]}]},"properties":{"className":"toc-item toc-item-h2"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h2","href":"/challenges.exercism.bash#solution-1"},"children":[{"type":"text","value":"Solution"}]}]}]}]},{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h1","properties":{"id":"reverse-string"},"children":[{"type":"text","value":"Reverse string"}]}]},"properties":{"className":"toc-item toc-item-h1"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h1","href":"/challenges.exercism.bash#reverse-string"},"children":[{"type":"text","value":"Reverse string"}]},{"type":"element","tagName":"ol","properties":{"className":"toc-level toc-level-2"},"children":[{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h2","properties":{"id":"solution-2"},"children":[{"type":"text","value":"Solution"}]}]},"properties":{"className":"toc-item toc-item-h2"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h2","href":"/challenges.exercism.bash#solution-2"},"children":[{"type":"text","value":"Solution"}]}]}]}]},{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h1","properties":{"id":"two-fer"},"children":[{"type":"text","value":"Two-fer"}]}]},"properties":{"className":"toc-item toc-item-h1"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h1","href":"/challenges.exercism.bash#two-fer"},"children":[{"type":"text","value":"Two-fer"}]},{"type":"element","tagName":"ol","properties":{"className":"toc-level toc-level-2"},"children":[{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h2","properties":{"id":"solution-3"},"children":[{"type":"text","value":"Solution"}]}]},"properties":{"className":"toc-item toc-item-h2"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h2","href":"/challenges.exercism.bash#solution-3"},"children":[{"type":"text","value":"Solution"}]}]}]}]},{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h1","properties":{"id":"error-handling"},"children":[{"type":"text","value":"Error handling"}]}]},"properties":{"className":"toc-item toc-item-h1"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h1","href":"/challenges.exercism.bash#error-handling"},"children":[{"type":"text","value":"Error handling"}]},{"type":"element","tagName":"ol","properties":{"className":"toc-level toc-level-2"},"children":[{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h2","properties":{"id":"solution-4"},"children":[{"type":"text","value":"Solution"}]}]},"properties":{"className":"toc-item toc-item-h2"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h2","href":"/challenges.exercism.bash#solution-4"},"children":[{"type":"text","value":"Solution"}]}]}]}]},{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h1","properties":{"id":"resistor-color-duo"},"children":[{"type":"text","value":"Resistor color duo"}]}]},"properties":{"className":"toc-item toc-item-h1"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h1","href":"/challenges.exercism.bash#resistor-color-duo"},"children":[{"type":"text","value":"Resistor color duo"}]},{"type":"element","tagName":"ol","properties":{"className":"toc-level toc-level-2"},"children":[{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h2","properties":{"id":"solution-5"},"children":[{"type":"text","value":"Solution"}]}]},"properties":{"className":"toc-item toc-item-h2"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h2","href":"/challenges.exercism.bash#solution-5"},"children":[{"type":"text","value":"Solution"}]}]}]}]},{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h1","properties":{"id":"resistor-color-trio"},"children":[{"type":"text","value":"Resistor color trio"}]}]},"properties":{"className":"toc-item toc-item-h1"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h1","href":"/challenges.exercism.bash#resistor-color-trio"},"children":[{"type":"text","value":"Resistor color trio"}]},{"type":"element","tagName":"ol","properties":{"className":"toc-level toc-level-2"},"children":[{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h2","properties":{"id":"solution-6"},"children":[{"type":"text","value":"Solution"}]}]},"properties":{"className":"toc-item toc-item-h2"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h2","href":"/challenges.exercism.bash#solution-6"},"children":[{"type":"text","value":"Solution"}]}]}]}]},{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h1","properties":{"id":"raindrops"},"children":[{"type":"text","value":"Raindrops"}]}]},"properties":{"className":"toc-item toc-item-h1"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h1","href":"/challenges.exercism.bash#raindrops"},"children":[{"type":"text","value":"Raindrops"}]},{"type":"element","tagName":"ol","properties":{"className":"toc-level toc-level-2"},"children":[{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h2","properties":{"id":"initial-working-solution"},"children":[{"type":"text","value":"Initial working solution"}]}]},"properties":{"className":"toc-item toc-item-h2"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h2","href":"/challenges.exercism.bash#initial-working-solution"},"children":[{"type":"text","value":"Initial working solution"}]}]},{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h2","properties":{"id":"findings-after-solution"},"children":[{"type":"text","value":"Findings after solution"}]}]},"properties":{"className":"toc-item toc-item-h2"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h2","href":"/challenges.exercism.bash#findings-after-solution"},"children":[{"type":"text","value":"Findings after solution"}]}]}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"These are my submitted solutions for the Bash track in Exercism.\nThe document structure here is quite simple.\nEach problem is its own header with the question and solution properly separated with its own subsection.\n"}]},{"type":"element","tagName":"h1","properties":{"id":"hello-world"},"children":[{"type":"text","value":"Hello world"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The classical introductory exercise. Just say \"Hello, World!\".\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"\"Hello, World!\" is the traditional first program for beginning programming in a new language or environment.\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The objectives are simple:\n"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Write a function that returns the string \"Hello, World!\".\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Run the test suite and make sure that it succeeds.\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Submit your solution and check it at the website.\n"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"solution"},"children":[{"type":"text","value":"Solution"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I interpreted the comments and created a function out of it without realizing the \"raw\" script is completely fine.\nWhoops!\n"}]},{"type":"element","tagName":"pre","properties":{"className":["src-block"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-bash"]},"children":[{"type":"text","value":"main() {\n echo \"Hello, World!\"\n}\n\nmain\n"}]}]},{"type":"element","tagName":"pre","properties":{"className":["fixed-width"]},"children":[{"type":"text","value":"Hello, World!"}]},{"type":"element","tagName":"p","properties":{},"children":[]},{"type":"element","tagName":"h1","properties":{"id":"leap"},"children":[{"type":"text","value":"Leap"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Given a year, report if it is a leap year.\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The tricky thing here is that a leap year in the Gregorian calendar occurs:\n"}]},{"type":"element","tagName":"div","properties":{"className":["exampe"]},"children":[{"type":"text","value":"on every year that is evenly divisible by 4\n except every year that is evenly divisible by 100\n unless the year is also evenly divisible by 400\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example, 1997 is not a leap year, but 1996 is.\n1900 is not a leap year, but 2000 is.\n"}]},{"type":"element","tagName":"h2","properties":{"id":"solution-1"},"children":[{"type":"text","value":"Solution"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I made it as a \"raw\" script this time realizing the thing.\n"}]},{"type":"element","tagName":"pre","properties":{"className":["src-block"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-bash"]},"children":[{"type":"text","value":"set -eo pipefail\n\nfunction help() {\n echo \"Usage: leap.sh <year>\"\n}\ntrap 'help' ERR\n\nif test $# -lt 1 || test $# -gt 1\nthen\n help && exit 1\nfi\n\nyear=$1\n\n# This is a check whether the input is a year.\n# The year is expected to be an integer and printf throws an error if the specifier does not match the input.\n# Pretty odd way but it is clever, don't you think?\nprintf \"%d\" $year 1>/dev/null 2>/dev/null\n\nif test $(expr $year % 4) -eq 0\nthen\n if test $(expr $year % 100) -eq 0 && test $(expr $year % 400) -ne 0\n then\n echo \"false\"\n exit 0\n fi\n echo \"true\"\nelse echo \"false\"\nfi\n"}]}]},{"type":"element","tagName":"h1","properties":{"id":"reverse-string"},"children":[{"type":"text","value":"Reverse string"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Reverse a string\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example: input: \"cool\" output: \"looc\"\n"}]},{"type":"element","tagName":"h2","properties":{"id":"solution-2"},"children":[{"type":"text","value":"Solution"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Well, it's certain very trivial.\n"}]},{"type":"element","tagName":"pre","properties":{"className":["src-block"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-bash"]},"children":[{"type":"text","value":"echo \"$@\" | rev\n"}]}]},{"type":"element","tagName":"h1","properties":{"id":"two-fer"},"children":[{"type":"text","value":"Two-fer"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Two-fer or 2-fer is short for two for one.\nOne for you and one for me.\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Given a name, return a string with the message:\n"}]},{"type":"element","tagName":"div","properties":{"className":["exampe"]},"children":[{"type":"text","value":"One for name, one for me.\nWhere \"name\" is the given name.\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"However, if the name is missing, return the string:\n"}]},{"type":"element","tagName":"div","properties":{"className":["exampe"]},"children":[{"type":"text","value":"One for you, one for me.\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here are some examples:\n"}]},{"type":"element","tagName":"table","properties":{},"children":[{"type":"element","tagName":"thead","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"th","properties":{},"children":[{"type":"text","value":"Name"}]},{"type":"element","tagName":"th","properties":{},"children":[{"type":"text","value":"String to return"}]}]}]},{"type":"element","tagName":"tbody","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Alice"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"One for Alice, one for me."}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Bob"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"One for Bob, one for me."}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{},"children":[]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"One for you, one for me."}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Zaphod"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"One for Zaphod, one for me."}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"solution-3"},"children":[{"type":"text","value":"Solution"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This could be solved with the "},{"type":"element","tagName":"a","properties":{"href":"https://www.gnu.org/software/bash/manual/bash.html#Shell-Parameter-Expansion"},"children":[{"type":"text","value":"parameter expansion"}]},{"type":"text","value":" syntax.\n"}]},{"type":"element","tagName":"pre","properties":{"className":["src-block"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-bash"]},"children":[{"type":"text","value":"echo \"One for ${1:-\"you\"}, one for me.\"\n"}]}]},{"type":"element","tagName":"h1","properties":{"id":"error-handling"},"children":[{"type":"text","value":"Error handling"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Implement various kinds of error handling and resource management.\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An important point of programming is how to handle errors and close resources even if errors occur.\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This exercise requires you to handle various errors.\nBecause error handling is rather programming language specific you'll have to refer to the tests for your track to see what's exactly required.\n"}]},{"type":"element","tagName":"h2","properties":{"id":"solution-4"},"children":[{"type":"text","value":"Solution"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The test script is basically requiring to write a Bash script that requires one argument and printing "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"Hello, $ARG"}]},{"type":"text","value":".\n"}]},{"type":"element","tagName":"pre","properties":{"className":["src-block"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-bash"]},"children":[{"type":"text","value":"function help() {\n echo \"Usage: error_handling.sh <person>\"\n}\n\nif test ! $# -eq 1; then\n help && exit 1\nfi\n\necho \"Hello, $1\"\n"}]}]},{"type":"element","tagName":"h1","properties":{"id":"resistor-color-duo"},"children":[{"type":"text","value":"Resistor color duo"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you want to build something using a Raspberry Pi, you'll probably use resistors. For this exercise, you need to know two things about them:\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Each resistor has a resistance value.\nResistors are small - so small in fact that if you printed the resistance value on them, it would be hard to read.\nTo get around this problem, manufacturers print color-coded bands onto the resistors to denote their resistance values.\nEach band has a position and a numeric value.\nFor example, if they printed a brown band (value 1) followed by a green band (value 5), it would translate to the number 15.\nIn this exercise you are going to create a helpful program so that you don't have to remember the values of the bands.\nThe program will take color names as input and output a two digit number, even if the input is more than two colors!\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The band colors are encoded as follows:\n"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Black: 0\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Brown: 1\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Red: 2\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Orange: 3\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Yellow: 4\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Green: 5\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Blue: 6\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Violet: 7\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Grey: 8\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"White: 9\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"From the example above: brown-green should return 15 brown-green-violet should return 15 too, ignoring the third color.\n"}]},{"type":"element","tagName":"h2","properties":{"id":"solution-5"},"children":[{"type":"text","value":"Solution"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Remember, it only prints the first two values.\n"}]},{"type":"element","tagName":"pre","properties":{"className":["src-block"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-bash"]},"children":[{"type":"text","value":"declare -A colors\ncolors['black']=0\ncolors['brown']=1\ncolors['red']=2\ncolors['orange']=3\ncolors['yellow']=4\ncolors['green']=5\ncolors['blue']=6\ncolors['violet']=7\ncolors['grey']=8\ncolors['white']=9\n\nfunction errorf {\n printf \"$@\\n\" && exit 1\n}\n\noutput=\"\"\nfor color in ${@:1}; do\n test ${colors[$color]} || errorf \"invalid color\"\n output=\"$output$(printf ${colors[$color]})\"\ndone\n\necho ${output:0:2}\n"}]}]},{"type":"element","tagName":"h1","properties":{"id":"resistor-color-trio"},"children":[{"type":"text","value":"Resistor color trio"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you want to build something using a Raspberry Pi, you'll probably use resistors. For this exercise, you need to know only three things about them:\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Each resistor has a resistance value.\nResistors are small - so small in fact that if you printed the resistance value on them, it would be hard to read. To get around this problem, manufacturers print color-coded bands onto the resistors to denote their resistance values.\nEach band acts as a digit of a number. For example, if they printed a brown band (value 1) followed by a green band (value 5), it would translate to the number 15. In this exercise, you are going to create a helpful program so that you don't have to remember the values of the bands. The program will take 3 colors as input, and outputs the correct value, in ohms. The color bands are encoded as follows:\n"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Black: 0\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Brown: 1\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Red: 2\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Orange: 3\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Yellow: 4\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Green: 5\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Blue: 6\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Violet: 7\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Grey: 8\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"White: 9\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In resistor-color duo you decoded the first two colors. For instance: orange-orange got the main value 33. The third color stands for how many zeros need to be added to the main value. The main value plus the zeros gives us a value in ohms. For the exercise it doesn't matter what ohms really are. For example:\n"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"orange-orange-black would be 33 and no zeros, which becomes 33 ohms.\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"orange-orange-red would be 33 and 2 zeros, which becomes 3300 ohms.\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"orange-orange-orange would be 33 and 3 zeros, which becomes 33000 ohms.\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"(If Math is your thing, you may want to think of the zeros as exponents of 10. If Math is not your thing, go with the zeros. It really is the same thing, just in plain English instead of Math lingo.)\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This exercise is about translating the colors into a label:\n"}]},{"type":"element","tagName":"div","properties":{"className":["exampe"]},"children":[{"type":"text","value":"\"... ohms\"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So an input of \"orange\", \"orange\", \"black\" should return:\n"}]},{"type":"element","tagName":"div","properties":{"className":["exampe"]},"children":[{"type":"text","value":"\"33 ohms\"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When we get more than a thousand ohms, we say \"kiloohms\". That's similar to saying \"kilometer\" for 1000 meters, and \"kilograms\" for 1000 grams. So an input of \"orange\", \"orange\", \"orange\" should return:\n"}]},{"type":"element","tagName":"div","properties":{"className":["exampe"]},"children":[{"type":"text","value":"\"33 kiloohms\"\n"}]},{"type":"element","tagName":"h2","properties":{"id":"solution-6"},"children":[{"type":"text","value":"Solution"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Initially not happy with the solution as I feel it's too hacky.\nIndeed, that feeling is verified when I saw "},{"type":"element","tagName":"a","properties":{"href":"https://exercism.io/tracks/bash/exercises/resistor-color-trio/solutions/56beec9e73814d84914335d9ff58d121"},"children":[{"type":"text","value":"this solution"}]},{"type":"text","value":".\nIt's pretty nice and learnt a few tricks from that code, too — that other assignment operators exist (e.g., "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"/="}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"*="}]},{"type":"text","value":"), test if a variable is set with "},{"type":"element","tagName":"code","properties":{"className":["inline-code"]},"children":[{"type":"text","value":"test -v"}]},{"type":"text","value":", and a simple separation of functions.\n"}]},{"type":"element","tagName":"pre","properties":{"className":["src-block"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-bash"]},"children":[{"type":"text","value":"declare -A colors\ncolors['black']=0\ncolors['brown']=1\ncolors['red']=2\ncolors['orange']=3\ncolors['yellow']=4\ncolors['green']=5\ncolors['blue']=6\ncolors['violet']=7\ncolors['grey']=8\ncolors['white']=9\n\nfunction errorf {\n printf \"$@\\n\" && exit 1\n}\n\noutput=\"\"\nfor color in \"$1\" \"$2\"; do\n test ${colors[$color]} || errorf \"invalid color\"\n output=\"$output$(printf ${colors[$color]})\"\ndone\n# If there's a zero in front, it will be recognized as an octal so better be careful with that.\noutput=$(echo $output | sed --regexp-extended \"s|^0||\")\n\ntest ${colors[$3]} || errorf \"invalid color\"\nmultiplier=$((10 ** ${colors[$3]}))\nresistance=$(($output * $multiplier))\n\n# The arbitrary limits are based from the test suite.\nif test \"$resistance\" -ge $((10 ** 10)); then\n echo $(($resistance / (10 ** 9))) gigaohms\nelif test \"$resistance\" -ge $((10 ** 6)); then\n echo $(($resistance / (10 ** 6))) megaohms\nelif test \"$resistance\" -ge 2000; then\n echo $(($resistance / (10 ** 3))) kiloohms\nelse\n echo \"$resistance\" ohms\nfi\n"}]}]},{"type":"element","tagName":"h1","properties":{"id":"raindrops"},"children":[{"type":"text","value":"Raindrops"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Your task is to convert a number into a string that contains raindrop sounds corresponding to certain potential factors. A factor is a number that evenly divides into another number, leaving no remainder. The simplest way to test if a one number is a factor of another is to use the modulo operation.\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The rules of raindrops are that if a given number:\n"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"has 3 as a factor, add 'Pling' to the result.\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"has 5 as a factor, add 'Plang' to the result.\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"has 7 as a factor, add 'Plong' to the result.\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"does not have any of 3, 5, or 7 as a factor, the result should be the digits of the number.\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Examples:\n"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"28 has 7 as a factor, but not 3 or 5, so the result would be \"Plong\".\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"30 has both 3 and 5 as factors, but not 7, so the result would be \"PlingPlang\".\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"34 is not factored by 3, 5, or 7, so the result would be \"34\".\n"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"initial-working-solution"},"children":[{"type":"text","value":"Initial working solution"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"After finding out that Bash arithmetic expression exists, we'll have to use the modulo operator.\n"}]},{"type":"element","tagName":"pre","properties":{"className":["src-block"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-bash"]},"children":[{"type":"text","value":"n=$1\nvalid=0\nfunction is_factor {\n local factor=$1\n shift\n\n local msg=$@\n\n [[ $(( $n % $factor )) -eq 0 ]] && printf $msg && valid=1\n}\n\nis_factor 3 \"Pling\"\nis_factor 5 \"Plang\"\nis_factor 7 \"Plong\"\n\n[[ $valid -eq 0 ]] && echo $n || printf '\\n'\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"findings-after-solution"},"children":[{"type":"text","value":"Findings after solution"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"a","properties":{"href":"https://exercism.io/my/solutions/04bcb4f6de304cd891afd87f1fcf9830"},"children":[{"type":"text","value":"solution"}]},{"type":"text","value":" was approved by the mentor.\nThey did point out a few details such as "},{"type":"element","tagName":"code","properties":{"className":["inline-code"]},"children":[{"type":"text","value":"[\\[...]]"}]},{"type":"text","value":" being string-oriented.\nYou can also use\n"}]},{"type":"element","tagName":"pre","properties":{"className":["src-block"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-bash"]},"children":[{"type":"text","value":"# Instead of this which compares numbers...\nn=10\nfactor=10\n\n[[ $(( $n % $factor )) -eq 0 ]] && echo \"Hello\"\n\n# Do it like this...\n(( $n % $factor == 0 )) && echo \"Hello\"\n"}]}]},{"type":"element","tagName":"pre","properties":{"className":["fixed-width"]},"children":[{"type":"text","value":"Hello\nHello"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"\nSecondly, assigning a vector variable (i.e., "},{"type":"element","tagName":"code","properties":{"className":["inline-code"]},"children":[{"type":"text","value":"$@"}]},{"type":"text","value":") to a scalar variable can be problematic.\nIt is advisable to put the vector variable to another vector variable instead.\n"}]},{"type":"element","tagName":"pre","properties":{"className":["src-block"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-bash"]},"children":[{"type":"text","value":"msg=$@ # Wrong\nmsg=(\"$@\") # Right\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Apparently, Bash really is a picky interpreter.\nI really like "},{"type":"element","tagName":"a","properties":{"href":"https://exercism.io/tracks/bash/exercises/raindrops/solutions/b882430d0b4841a4acaf7c8d5ba24a24"},"children":[{"type":"text","value":"this simple solution"}]},{"type":"text","value":".\nIf improved with the tips given by the mentor, this is my improved solution.\n"}]},{"type":"element","tagName":"pre","properties":{"className":["src-block"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-bash"]},"children":[{"type":"text","value":"if (( $1 % 3 == 0 )); then\n result+=\"Pling\"\nfi\nif (( $1 % 5 == 0 )); then\n result+=\"Plang\"\nfi\nif (( $1 % 7 == 0 )); then\n result+=\"Plong\"\nfi\n\necho ${result:-$1}\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"But without entirely rewriting my solution, here's the improved solution of it.\n"}]},{"type":"element","tagName":"pre","properties":{"className":["src-block"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-bash"]},"children":[{"type":"text","value":"n=$1\nvalid=0\nfunction is_factor {\n local factor=$1 msg=$2\n\n (( n % factor == 0 )) && printf $msg && valid=1\n}\n\nis_factor 3 \"Pling\"\nis_factor 5 \"Plang\"\nis_factor 7 \"Plong\"\n\n(( valid == 0 )) && echo $n || printf '\\n'\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Also, the "},{"type":"element","tagName":"a","properties":{"href":"https://github.com/koalaman/shellcheck/wiki/SC2124"},"children":[{"type":"text","value":"ShellCheck wiki"}]},{"type":"text","value":" is a pretty handy resource.\n"}]}]},"backlinks":[]},"__N_SSG":true}