Avatar of murdho

murdho's solution

to Reverse String in the Crystal Track

Published at Feb 24 2019 · 0 comments
Instructions
Test suite
Solution

Reverse a string

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

Setup

Follow the setup instructions for Crystal here:

http://exercism.io/languages/crystal

More help installing can be found here:

http://crystal-lang.org/docs/installation/index.html

Making the Test Suit Pass

Execute the tests with:

$ crystal spec

In each test suite all but the first test have been skipped.

Once you get a test passing, you can unskip the next one by changing pending to it.

Source

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

Submitting Incomplete Solutions

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

reverse_string_spec.cr

require "spec"
require "../src/*"

describe "ReverseString" do
  it "an empty string" do
    ReverseString.reverse("").should eq("")
  end

  pending "a word" do
    ReverseString.reverse("robot").should eq("tobor")
  end

  pending "a capitalized word" do
    ReverseString.reverse("Ramen").should eq("nemaR")
  end

  pending "a sentence with punctuation" do
    ReverseString.reverse("I'm hungry!").should eq("!yrgnuh m'I")
  end

  pending "a palindrome" do
    ReverseString.reverse("racecar").should eq("racecar")
  end
end
class ReverseString
  def self.reverse(input, result = "")
    return result + input if input.size < 2

    reverse(input[0..-2], result + input[-1])
  end
end

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?