🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of pratiktri

pratiktri's solution

to Reverse String in the Bash Track

Published at Jul 30 2020 · 0 comments
Instructions
Test suite
Solution

Reverse a string

For example: input: "cool" output: "looc"

Run the tests with:

bats reverse_string_test.sh

After the first test(s) pass, continue by commenting out or removing the [[ $BATS_RUN_SKIPPED == true ]] || skip annotations prepending other tests.

To run all tests, including the ones with skip annotations, run:

BATS_RUN_SKIPPED=true bats reverse_string_test.sh

Source

Introductory challenge to reverse an input string https://medium.freecodecamp.org/how-to-reverse-a-string-in-javascript-in-3-different-ways-75e4763c68cb

External utilities

Bash is a language to write "scripts" -- programs that can call external tools, such as sed, awk, date and even programs written in other programming languages, like Python. This track does not restrict the usage of these utilities, and as long as your solution is portable between systems and does not require installation of third party applications, feel free to use them to solve the exercise.

For an extra challenge, if you would like to have a better understanding of the language, try to re-implement the solution in pure Bash, without using any external tools. Note that there are some types of problems that bash cannot solve, such as performing floating point arithmetic and manipulating dates: for those, you must call out to an external tool.

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

reverse_string_test.sh

#!/usr/bin/env bash

# local version: 1.2.0.1

@test "an empty string" {
  #[[ $BATS_RUN_SKIPPED == "true" ]] || skip
  run bash reverse_string.sh ""

  (( status == 0 ))
  [[ $output == "" ]]
}

@test "a word" {
  [[ $BATS_RUN_SKIPPED == "true" ]] || skip
  run bash reverse_string.sh "robot"

  (( status == 0 ))
  [[ $output == "tobor" ]]
}

@test "a capitalised word" {
  [[ $BATS_RUN_SKIPPED == "true" ]] || skip
  run bash reverse_string.sh "Ramen"

  (( status == 0 ))
  [[ $output == "nemaR" ]]
}

@test "a sentence with punctuation" {
  [[ $BATS_RUN_SKIPPED == "true" ]] || skip
  run bash reverse_string.sh "I'm hungry!"

  (( status == 0 ))
  [[ $output == "!yrgnuh m'I" ]]
}

@test "a palindrome" {
  [[ $BATS_RUN_SKIPPED == "true" ]] || skip
  run bash reverse_string.sh "racecar"

  (( status == 0 ))
  [[ $output == "racecar" ]]
}

@test "an even-sized word" {
  [[ $BATS_RUN_SKIPPED == "true" ]] || skip
  run bash reverse_string.sh "drawer"

  (( status == 0 ))
  [[ $output == "reward" ]]
}

# bash-specific test: Focus the student's attention on the effects of 
# word splitting and filename expansion:
# https://www.gnu.org/software/bash/manual/bash.html#Shell-Expansions

@test "avoid globbing" {
  [[ $BATS_RUN_SKIPPED == "true" ]] || skip
  run bash reverse_string.sh " a *  b"

  (( status == 0 ))
  [[ $output == "b  * a " ]]
}
#!/usr/bin/env bash

  main () {
      input="$1"

      while [[ -n "${input}" ]];
      do
        echo -n "${input: -1}"
        input="${input: 0: -1}"
      done
  }

  main "$@"

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?