Skip to content

Commit

Permalink
Add reverse-string exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
BNAndras committed Jan 15, 2024
1 parent 3b8a2ca commit 73ad0a0
Show file tree
Hide file tree
Showing 13 changed files with 133 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@
"difficulty": 2,
"topics": null
},
{
"slug": "reverse-string",
"name": "Reverse String",
"uuid": "58c93fbe-5c57-41d0-9085-6007a488aaca",
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "rna-transcription",
"name": "RNA Transcription",
Expand Down
7 changes: 7 additions & 0 deletions exercises/practice/reverse-string/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Instructions

Reverse a string

For example:
input: "cool"
output: "looc"
19 changes: 19 additions & 0 deletions exercises/practice/reverse-string/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"BNAndras"
],
"files": {
"solution": [
"reverse_string.ml"
],
"test": [
"test.ml"
],
"example": [
".meta/example.ml"
]
},
"blurb": "Reverse a string.",
"source": "Introductory challenge to reverse an input string",
"source_url": "https://medium.freecodecamp.org/how-to-reverse-a-string-in-javascript-in-3-different-ways-75e4763c68cb"
}
3 changes: 3 additions & 0 deletions exercises/practice/reverse-string/.meta/example.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
let reverse_string str =
let len = String.length str in
String.init len (fun i -> str.[len - 1 - i])
28 changes: 28 additions & 0 deletions exercises/practice/reverse-string/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[c3b7d806-dced-49ee-8543-933fd1719b1c]
description = "an empty string"

[01ebf55b-bebb-414e-9dec-06f7bb0bee3c]
description = "a word"

[0f7c07e4-efd1-4aaa-a07a-90b49ce0b746]
description = "a capitalized word"

[71854b9c-f200-4469-9f5c-1e8e5eff5614]
description = "a sentence with punctuation"

[1f8ed2f3-56f3-459b-8f3e-6d8d654a1f6c]
description = "a palindrome"

[b9e7dec1-c6df-40bd-9fa3-cd7ded010c4c]
description = "an even-sized word"
9 changes: 9 additions & 0 deletions exercises/practice/reverse-string/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
default: clean test

test:
dune runtest

clean:
dune clean

.PHONY: clean
16 changes: 16 additions & 0 deletions exercises/practice/reverse-string/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
(executable
(name test)
(libraries base ounit2))

(alias
(name runtest)
(deps (:x test.exe))
(action (run %{x})))

(alias
(name buildtest)
(deps (:x test.exe)))

(env
(dev
(flags (:standard -warn-error -A))))
1 change: 1 addition & 0 deletions exercises/practice/reverse-string/dune-project
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(lang dune 1.1)
2 changes: 2 additions & 0 deletions exercises/practice/reverse-string/reverse_string.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
let reverse_string _ =
failwith "'reverse_string' is missing"
1 change: 1 addition & 0 deletions exercises/practice/reverse-string/reverse_string.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
val reverse_string : string -> string
23 changes: 23 additions & 0 deletions exercises/practice/reverse-string/test.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
open Base
open OUnit2
open Reverse_string

let ae exp got _test_ctxt = assert_equal exp got ~printer:Fn.id

let tests = [
"an empty string" >::
ae "" (reverse_string "");
"a word" >::
ae "tobor" (reverse_string "robot");
"a capitalized word" >::
ae "nemaR" (reverse_string "Ramen");
"a sentence with punctuation" >::
ae "!yrgnuh m'I" (reverse_string "I'm hungry!");
"a palindrome" >::
ae "racecar" (reverse_string "racecar");
"an even-sized word" >::
ae "reward" (reverse_string "drawer");
]

let () =
run_test_tt_main ("reverse-string tests" >::: tests)
1 change: 1 addition & 0 deletions templates/reverse-string/dune-project.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(lang dune 1.1)
15 changes: 15 additions & 0 deletions templates/reverse-string/test.ml.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
open Base
open OUnit2
open Reverse_string

let ae exp got _test_ctxt = assert_equal exp got ~printer:Fn.id

let tests = [
{{#cases}}
"{{description}}" >::
ae {{#input}}{{expected}} (reverse_string {{value}}){{/input}};
{{/cases}}
]

let () =
run_test_tt_main ("reverse-string tests" >::: tests)

0 comments on commit 73ad0a0

Please sign in to comment.