Skip to content

Commit

Permalink
[New Practice Exercise]: Reverse string (#713)
Browse files Browse the repository at this point in the history
* Add reverse-string exercise

* Fix json

* Fix files
  • Loading branch information
meatball133 authored Jan 23, 2024
1 parent a08f07a commit f4870fe
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 9 additions & 0 deletions exercises/practice/reverse-string/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -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"`.
5 changes: 5 additions & 0 deletions exercises/practice/reverse-string/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 4 additions & 0 deletions exercises/practice/reverse-string/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.DS_Store
/.build
/Packages
/*.xcodeproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
func reverseString(_ input : String) -> String {
return String(input.reversed())
}
17 changes: 17 additions & 0 deletions exercises/practice/reverse-string/.meta/config.json
Original file line number Diff line number Diff line change
@@ -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"
}
16 changes: 16 additions & 0 deletions exercises/practice/reverse-string/.meta/template.swift
Original file line number Diff line number Diff line change
@@ -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 -%}
}
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"
21 changes: 21 additions & 0 deletions exercises/practice/reverse-string/Package.swift
Original file line number Diff line number Diff line change
@@ -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"]),
]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
func reverseString(_ input : String) -> String {
// Write your code for the 'Reverse String' exercise in this file.
}
Original file line number Diff line number Diff line change
@@ -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")
}
}

0 comments on commit f4870fe

Please sign in to comment.