Skip to content

Commit 6263b96

Browse files
[Scala] Reverse String
1 parent 96c15c1 commit 6263b96

File tree

6 files changed

+104
-1
lines changed

6 files changed

+104
-1
lines changed

scala/build.sbt

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ lazy val root = (project in file("."))
77
`saddle-points`, `simple-linked-list`, `pythagorean-triplet`, `queen-attack`, `nucleotide-count`, `complex-numbers`,
88
`custom-set`, `binary-search`, grains, `linked-list`, `binary-search-tree`, sublist, `largest-series-product`,
99
`zebra-puzzle`, raindrops, bowling, strain, `sum-of-multiples`, `protein-translation`, sieve, `rna-transcription`,
10-
darts, `high-scores`, `difference-of-squares`, pangram, isogram, anagram, `matching-brackets`)
10+
darts, `high-scores`, `difference-of-squares`, pangram, isogram, anagram, `matching-brackets`, `reverse-string`)
1111

1212
lazy val `all-your-base` = project
1313
lazy val allergies = project
@@ -44,6 +44,7 @@ lazy val `protein-translation` = project
4444
lazy val `pythagorean-triplet` = project
4545
lazy val `queen-attack` = project
4646
lazy val raindrops = project
47+
lazy val `reverse-string` = project
4748
lazy val `rna-transcription` = project
4849
lazy val `robot-name` = project
4950
lazy val `robot-simulator` = project

scala/reverse-string/HELP.md

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Help
2+
3+
## Running the tests
4+
5+
To run the tests, run the command `sbt test` in the exercise's directory.
6+
See the [tests page](https://exercism.org/docs/tracks/scala/tests) for more information.
7+
8+
## Skipped tests
9+
10+
Initially, only the first test will be enabled.
11+
This is to encourage you to solve the exercise one step at a time.
12+
Once you get the first test passing, remove the `pending` keyword from the beginning of the next test and work on getting that test passing.
13+
14+
## Submitting your solution
15+
16+
You can submit your solution using the `exercism submit src/main/scala/ReverseString.scala` command.
17+
This command will upload your solution to the Exercism website and print the solution page's URL.
18+
19+
It's possible to submit an incomplete solution which allows you to:
20+
21+
- See how others have completed the exercise
22+
- Request help from a mentor
23+
24+
## Need to get help?
25+
26+
If you'd like help solving the exercise, check the following pages:
27+
28+
- The [Scala track's documentation](https://exercism.org/docs/tracks/scala)
29+
- The [Scala track's programming category on the forum](https://forum.exercism.org/c/programming/scala)
30+
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
31+
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
32+
33+
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
34+
35+
Please see the [learning](https://exercism.org/docs/tracks/scala/learning) and [installation](https://exercism.org/docs/tracks/scala/installation) pages if you need any help.

scala/reverse-string/README.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Reverse String
2+
3+
Welcome to Reverse String on Exercism's Scala Track.
4+
If you need help running the tests or submitting your code, check out `HELP.md`.
5+
6+
## Introduction
7+
8+
Reversing strings (reading them from right to left, rather than from left to right) is a surprisingly common task in programming.
9+
10+
For example, in bioinformatics, reversing the sequence of DNA or RNA strings is often important for various analyses, such as finding complementary strands or identifying palindromic sequences that have biological significance.
11+
12+
## Instructions
13+
14+
Your task is to reverse a given string.
15+
16+
Some examples:
17+
18+
- Turn `"stressed"` into `"desserts"`.
19+
- Turn `"strops"` into `"sports"`.
20+
- Turn `"racecar"` into `"racecar"`.
21+
22+
## Source
23+
24+
### Created by
25+
26+
- @BNAndras
27+
28+
### Based on
29+
30+
Introductory challenge to reverse an input string - https://medium.freecodecamp.org/how-to-reverse-a-string-in-javascript-in-3-different-ways-75e4763c68cb

scala/reverse-string/build.sbt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
scalaVersion := "2.13.6"
2+
3+
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.10" % "test"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
object ReverseString {
2+
def reverse(str: String): String =
3+
str.indices.map(i => str.charAt(str.length - i - 1)).mkString
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import org.scalatest.funsuite.AnyFunSuite
2+
import org.scalatest.matchers.should.Matchers
3+
4+
5+
class ReverseStringTest extends AnyFunSuite with Matchers {
6+
7+
test("an empty string") {
8+
ReverseString.reverse("") should be ("")
9+
}
10+
11+
test("a word") {
12+
ReverseString.reverse("robot") should be ("tobor")
13+
}
14+
15+
test("a capitalized word") {
16+
ReverseString.reverse("Ramen") should be ("nemaR")
17+
}
18+
19+
test("a sentence with punctuation") {
20+
ReverseString.reverse("I'm hungry!") should be ("!yrgnuh m'I")
21+
}
22+
23+
test("a palindrome") {
24+
ReverseString.reverse("racecar") should be ("racecar")
25+
}
26+
27+
test("an even-sized word") {
28+
ReverseString.reverse("drawer") should be ("reward")
29+
}
30+
}

0 commit comments

Comments
 (0)