Skip to content

Commit 1b86573

Browse files
authored
Merge pull request #79 from codingthat/sync-and-add-reverse-string-tests
Sync and add `reverse-string` tests
2 parents d7aa402 + 5aa87e2 commit 1b86573

File tree

6 files changed

+70
-22
lines changed

6 files changed

+70
-22
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Instructions append
2+
3+
## Handling grapheme clusters
4+
5+
- The [built-in `RegEx` class][regex] can be used to find grapheme clusters.
6+
- The regular expression pattern `\X` matches grapheme clusters.
7+
- Backslashes (`\` characters) need to be escaped in regular strings (`"\\X"`), but not in [raw strings][literals] (`r"\X"`).
8+
- `RegEx` has a [method][search_all] that returns an array of `RegExMatch`es.
9+
- Each match object has [a method][get_string] that returns the search result as a string.
10+
11+
[regex]: https://docs.godotengine.org/en/stable/classes/class_regex.html
12+
[literals]: https://docs.godotengine.org/en/stable/tutorials/scripting/gdscript/gdscript_basics.html#literals
13+
[search_all]: https://docs.godotengine.org/en/stable/classes/class_regex.html#class-regex-method-search-all
14+
[get_string]: https://docs.godotengine.org/en/stable/classes/class_regexmatch.html#class-regexmatch-method-get-string
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# Instructions
22

3-
Reverse a string
3+
Your task is to reverse a given string.
44

5-
For example:
6-
input: "cool"
7-
output: "looc"
5+
Some examples:
6+
7+
- Turn `"stressed"` into `"desserts"`.
8+
- Turn `"strops"` into `"sports"`.
9+
- Turn `"racecar"` into `"racecar"`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Introduction
2+
3+
Reversing strings (reading them from right to left, rather than from left to right) is a surprisingly common task in programming.
4+
5+
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.
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
func reverse(str):
2-
var result = ""
3-
for i in range(str.length() - 1, -1, -1):
4-
result += str[i]
5-
return result
1+
func reverse(original):
2+
var regex = RegEx.create_from_string("\\X")
3+
var clusters = regex.search_all(original).map(func(m): return m.get_string())
4+
clusters.reverse()
5+
return "".join(clusters)

exercises/practice/reverse-string/.meta/tests.toml

+9
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,12 @@ description = "a palindrome"
2626

2727
[b9e7dec1-c6df-40bd-9fa3-cd7ded010c4c]
2828
description = "an even-sized word"
29+
30+
[1bed0f8a-13b0-4bd3-9d59-3d0593326fa2]
31+
description = "wide characters"
32+
33+
[93d7e1b8-f60f-4f3c-9559-4056e10d2ead]
34+
description = "grapheme cluster with pre-combined form"
35+
36+
[1028b2c1-6763-4459-8540-2da47ca512d9]
37+
description = "grapheme clusters"
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,52 @@
1-
func test_empty_string(solution_script):
2-
var str = ""
1+
func test_empty_originaling(solution_script):
2+
var original = ""
33
var expected = ""
4-
return [solution_script.reverse(str), expected]
4+
return [solution_script.reverse(original), expected]
55

66

77
func test_a_word(solution_script):
8-
var str = "robot"
8+
var original = "robot"
99
var expected = "tobor"
10-
return [solution_script.reverse(str), expected]
10+
return [solution_script.reverse(original), expected]
1111

1212

1313
func test_a_capitalized_word(solution_script):
14-
var str = "Ramen"
14+
var original = "Ramen"
1515
var expected = "nemaR"
16-
return [solution_script.reverse(str), expected]
16+
return [solution_script.reverse(original), expected]
1717

1818

1919
func test_a_sentence_with_punctuation(solution_script):
20-
var str = "I'm hungry!"
20+
var original = "I'm hungry!"
2121
var expected = "!yrgnuh m'I"
22-
return [solution_script.reverse(str), expected]
22+
return [solution_script.reverse(original), expected]
2323

2424

2525
func test_a_palindrome(solution_script):
26-
var str = "racecar"
26+
var original = "racecar"
2727
var expected = "racecar"
28-
return [solution_script.reverse(str), expected]
28+
return [solution_script.reverse(original), expected]
2929

3030

3131
func test_an_even_sized_word(solution_script):
32-
var str = "drawer"
32+
var original = "drawer"
3333
var expected = "reward"
34-
return [solution_script.reverse(str), expected]
34+
return [solution_script.reverse(original), expected]
35+
36+
37+
func test_wide_characters(solution_script):
38+
var original = "子猫"
39+
var expected = "猫子"
40+
return [solution_script.reverse(original), expected]
41+
42+
43+
func test_grapheme_cluster_with_pre_combined_form(solution_script):
44+
var original = "Würstchenstand"
45+
var expected = "dnatsnehctsrüW"
46+
return [solution_script.reverse(original), expected]
47+
48+
49+
func test_grapheme_clusters(solution_script):
50+
var original = "ผู้เขียนโปรแกรม"
51+
var expected = "มรกแรปโนยขีเผู้"
52+
return [solution_script.reverse(original), expected]

0 commit comments

Comments
 (0)