From f4870fee3a635a3d9d1e668567a78c74b722b3c8 Mon Sep 17 00:00:00 2001 From: meatball <69751659+meatball133@users.noreply.github.com> Date: Tue, 23 Jan 2024 15:56:23 +0100 Subject: [PATCH] [New Practice Exercise]: Reverse string (#713) * Add reverse-string exercise * Fix json * Fix files --- config.json | 8 +++++ .../reverse-string/.docs/instructions.md | 9 +++++ .../reverse-string/.docs/introduction.md | 5 +++ exercises/practice/reverse-string/.gitignore | 4 +++ .../ReverseString/ReverseStringExample.swift | 3 ++ .../practice/reverse-string/.meta/config.json | 17 +++++++++ .../reverse-string/.meta/template.swift | 16 +++++++++ .../practice/reverse-string/.meta/tests.toml | 28 +++++++++++++++ .../practice/reverse-string/Package.swift | 21 +++++++++++ .../Sources/ReverseString/ReverseString.swift | 3 ++ .../ReverseStringTests.swift | 36 +++++++++++++++++++ 11 files changed, 150 insertions(+) create mode 100644 exercises/practice/reverse-string/.docs/instructions.md create mode 100644 exercises/practice/reverse-string/.docs/introduction.md create mode 100644 exercises/practice/reverse-string/.gitignore create mode 100644 exercises/practice/reverse-string/.meta/Sources/ReverseString/ReverseStringExample.swift create mode 100644 exercises/practice/reverse-string/.meta/config.json create mode 100644 exercises/practice/reverse-string/.meta/template.swift create mode 100644 exercises/practice/reverse-string/.meta/tests.toml create mode 100644 exercises/practice/reverse-string/Package.swift create mode 100644 exercises/practice/reverse-string/Sources/ReverseString/ReverseString.swift create mode 100644 exercises/practice/reverse-string/Tests/ReverseStringTests/ReverseStringTests.swift diff --git a/config.json b/config.json index 9566e9ab0..b388192eb 100644 --- a/config.json +++ b/config.json @@ -298,6 +298,14 @@ "difficulty": 1, "topics": ["optional_values", "text_formatting"] }, + { + "slug": "reverse-string", + "name": "Reverse String", + "uuid": "f030605e-e971-4153-8927-b4046742fc71", + "practices": [], + "prerequisites": [], + "difficulty": 1 + }, { "slug": "accumulate", "name": "Accumulate", diff --git a/exercises/practice/reverse-string/.docs/instructions.md b/exercises/practice/reverse-string/.docs/instructions.md new file mode 100644 index 000000000..0ff4198e4 --- /dev/null +++ b/exercises/practice/reverse-string/.docs/instructions.md @@ -0,0 +1,9 @@ +# Instructions + +Your task is to reverse a given string. + +Some examples: + +- Turn `"stressed"` into `"desserts"`. +- Turn `"strops"` into `"sports"`. +- Turn `"racecar"` into `"racecar"`. diff --git a/exercises/practice/reverse-string/.docs/introduction.md b/exercises/practice/reverse-string/.docs/introduction.md new file mode 100644 index 000000000..02233e075 --- /dev/null +++ b/exercises/practice/reverse-string/.docs/introduction.md @@ -0,0 +1,5 @@ +# Introduction + +Reversing strings (reading them from right to left, rather than from left to right) is a surprisingly common task in programming. + +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. diff --git a/exercises/practice/reverse-string/.gitignore b/exercises/practice/reverse-string/.gitignore new file mode 100644 index 000000000..02c087533 --- /dev/null +++ b/exercises/practice/reverse-string/.gitignore @@ -0,0 +1,4 @@ +.DS_Store +/.build +/Packages +/*.xcodeproj diff --git a/exercises/practice/reverse-string/.meta/Sources/ReverseString/ReverseStringExample.swift b/exercises/practice/reverse-string/.meta/Sources/ReverseString/ReverseStringExample.swift new file mode 100644 index 000000000..556fd6e4b --- /dev/null +++ b/exercises/practice/reverse-string/.meta/Sources/ReverseString/ReverseStringExample.swift @@ -0,0 +1,3 @@ +func reverseString(_ input : String) -> String { + return String(input.reversed()) +} diff --git a/exercises/practice/reverse-string/.meta/config.json b/exercises/practice/reverse-string/.meta/config.json new file mode 100644 index 000000000..6bcb1e808 --- /dev/null +++ b/exercises/practice/reverse-string/.meta/config.json @@ -0,0 +1,17 @@ +{ + "authors": ["meatball133"], + "files": { + "solution": [ + "Sources/ReverseString/ReverseString.swift" + ], + "test": [ + "Tests/ReverseStringTests/ReverseStringTests.swift" + ], + "example": [ + ".meta/Sources/ReverseString/ReverseStringExample.swift" + ] + }, + "blurb": "Reverse a given 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" +} diff --git a/exercises/practice/reverse-string/.meta/template.swift b/exercises/practice/reverse-string/.meta/template.swift new file mode 100644 index 000000000..f95fb3215 --- /dev/null +++ b/exercises/practice/reverse-string/.meta/template.swift @@ -0,0 +1,16 @@ +import XCTest +@testable import {{exercise|camelCase}} +class {{exercise|camelCase}}Tests: XCTestCase { + let runAll = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false + + {% for case in cases %} + {% if forloop.first -%} + func test{{case.description |camelCase }}() { + {% else -%} + func test{{case.description |camelCase }}() throws { + try XCTSkipIf(true && !runAll) // change true to false to run this test + {% endif -%} + XCTAssertEqual(reverseString("{{case.input.value}}"), "{{case.expected}}") + } + {% endfor -%} +} diff --git a/exercises/practice/reverse-string/.meta/tests.toml b/exercises/practice/reverse-string/.meta/tests.toml new file mode 100644 index 000000000..0b04c4cd7 --- /dev/null +++ b/exercises/practice/reverse-string/.meta/tests.toml @@ -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" diff --git a/exercises/practice/reverse-string/Package.swift b/exercises/practice/reverse-string/Package.swift new file mode 100644 index 000000000..34dd46c5e --- /dev/null +++ b/exercises/practice/reverse-string/Package.swift @@ -0,0 +1,21 @@ +// swift-tools-version:5.3 + +import PackageDescription + +let package = Package( + name: "ReverseString", + products: [ + .library( + name: "ReverseString", + targets: ["ReverseString"]) + ], + dependencies: [], + targets: [ + .target( + name: "ReverseString", + dependencies: []), + .testTarget( + name: "ReverseStringTests", + dependencies: ["ReverseString"]), + ] +) diff --git a/exercises/practice/reverse-string/Sources/ReverseString/ReverseString.swift b/exercises/practice/reverse-string/Sources/ReverseString/ReverseString.swift new file mode 100644 index 000000000..13b9f4c09 --- /dev/null +++ b/exercises/practice/reverse-string/Sources/ReverseString/ReverseString.swift @@ -0,0 +1,3 @@ +func reverseString(_ input : String) -> String { + // Write your code for the 'Reverse String' exercise in this file. +} diff --git a/exercises/practice/reverse-string/Tests/ReverseStringTests/ReverseStringTests.swift b/exercises/practice/reverse-string/Tests/ReverseStringTests/ReverseStringTests.swift new file mode 100644 index 000000000..59a8cf4ef --- /dev/null +++ b/exercises/practice/reverse-string/Tests/ReverseStringTests/ReverseStringTests.swift @@ -0,0 +1,36 @@ +import XCTest + +@testable import ReverseString + +class ReverseStringTests: XCTestCase { + let runAll = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false + + func testAnEmptyString() { + XCTAssertEqual(reverseString(""), "") + } + + func testAWord() throws { + try XCTSkipIf(true && !runAll) // change true to false to run this test + XCTAssertEqual(reverseString("robot"), "tobor") + } + + func testACapitalizedWord() throws { + try XCTSkipIf(true && !runAll) // change true to false to run this test + XCTAssertEqual(reverseString("Ramen"), "nemaR") + } + + func testASentenceWithPunctuation() throws { + try XCTSkipIf(true && !runAll) // change true to false to run this test + XCTAssertEqual(reverseString("I'm hungry!"), "!yrgnuh m'I") + } + + func testAPalindrome() throws { + try XCTSkipIf(true && !runAll) // change true to false to run this test + XCTAssertEqual(reverseString("racecar"), "racecar") + } + + func testAnEvenSizedWord() throws { + try XCTSkipIf(true && !runAll) // change true to false to run this test + XCTAssertEqual(reverseString("drawer"), "reward") + } +}