-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add reverse-string exercise. Excluding grapheme cluster tests
- Loading branch information
Showing
10 changed files
with
230 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"authors": [ | ||
"glennj" | ||
], | ||
"files": { | ||
"solution": [ | ||
"lib/ReverseString.pm" | ||
], | ||
"test": [ | ||
"t/reverse-string.t" | ||
], | ||
"example": [ | ||
".meta/solutions/lib/ReverseString.pm" | ||
] | ||
}, | ||
"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" | ||
} |
38 changes: 38 additions & 0 deletions
38
exercises/practice/reverse-string/.meta/solutions/lib/ReverseString.pm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package ReverseString; | ||
|
||
use strict; | ||
use warnings; | ||
use experimental qw<signatures postderef postderef_qq>; | ||
|
||
use Exporter qw<import>; | ||
our @EXPORT_OK = qw<str_reverse>; | ||
|
||
sub str_reverse ($text) { | ||
my $rev = ''; | ||
my @chars = split '', $text; | ||
|
||
# the obvious boring solution | ||
# $rev = reverse $text | ||
|
||
# iterating | ||
# foreach my $char (@chars) { | ||
# $rev = $char . $rev; | ||
# } | ||
|
||
# iterating tersely | ||
# $rev = $_ . $rev for @chars; | ||
|
||
# iterating functionally | ||
# use List::Util qw(reduce); | ||
# $rev = reduce {$b . $a} '', @chars; | ||
|
||
# fewer iterations | ||
for ( my ( $i, $j ) = ( 0, $#chars ); $i < $j; $i++, $j-- ) { | ||
( $chars[$i], $chars[$j] ) = ( $chars[$j], $chars[$i] ); | ||
} | ||
$rev = join '', @chars; | ||
|
||
return $rev; | ||
} | ||
|
||
1; |
1 change: 1 addition & 0 deletions
1
exercises/practice/reverse-string/.meta/solutions/t/reverse-string.t
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../t/reverse-string.t |
48 changes: 48 additions & 0 deletions
48
exercises/practice/reverse-string/.meta/template-data.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
subs: str_reverse | ||
|
||
properties: | ||
reverse: | ||
test: |- | ||
use Data::Dmp; | ||
local $Data::Dmp::OPT_STRINGIFY_NUMBERS = 1; | ||
sprintf(<<'END', map {dmp($_)} $case->{input}{value}, $case->@{qw<expected description>}); | ||
is( | ||
str_reverse(%s), | ||
%s, | ||
%s, | ||
); | ||
END | ||
example: |- | ||
sub str_reverse ($text) { | ||
my $rev = ''; | ||
my @chars = split '', $text; | ||
# the obvious boring solution | ||
# $rev = reverse $text | ||
# iterating | ||
# foreach my $char (@chars) { | ||
# $rev = $char . $rev; | ||
# } | ||
# iterating tersely | ||
# $rev = $_ . $rev for @chars; | ||
# iterating functionally | ||
# use List::Util qw(reduce); | ||
# $rev = reduce {$b . $a} '', @chars; | ||
# fewer iterations | ||
for (my ($i, $j) = (0, $#chars); $i < $j; $i++, $j--) { | ||
($chars[$i], $chars[$j]) = ($chars[$j], $chars[$i]); | ||
} | ||
$rev = join '', @chars; | ||
return $rev; | ||
} | ||
stub: |- | ||
sub str_reverse ($text) { | ||
return undef; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# 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" | ||
|
||
[1bed0f8a-13b0-4bd3-9d59-3d0593326fa2] | ||
description = "wide characters" | ||
|
||
[93d7e1b8-f60f-4f3c-9559-4056e10d2ead] | ||
description = "grapheme cluster with pre-combined form" | ||
include = false | ||
|
||
[1028b2c1-6763-4459-8540-2da47ca512d9] | ||
description = "grapheme clusters" | ||
include = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package ReverseString; | ||
|
||
use v5.40; | ||
|
||
use Exporter qw<import>; | ||
our @EXPORT_OK = qw<str_reverse>; | ||
|
||
sub str_reverse ($text) { | ||
return undef; | ||
} | ||
|
||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#!/usr/bin/env perl | ||
use Test2::V0; | ||
|
||
use FindBin qw<$Bin>; | ||
use lib "$Bin/../lib", "$Bin/../local/lib/perl5"; | ||
|
||
use ReverseString qw<str_reverse>; | ||
|
||
is( # begin: c3b7d806-dced-49ee-8543-933fd1719b1c | ||
str_reverse(""), | ||
"", | ||
"an empty string", | ||
); # end: c3b7d806-dced-49ee-8543-933fd1719b1c | ||
|
||
is( # begin: 01ebf55b-bebb-414e-9dec-06f7bb0bee3c | ||
str_reverse("robot"), | ||
"tobor", | ||
"a word", | ||
); # end: 01ebf55b-bebb-414e-9dec-06f7bb0bee3c | ||
|
||
is( # begin: 0f7c07e4-efd1-4aaa-a07a-90b49ce0b746 | ||
str_reverse("Ramen"), | ||
"nemaR", | ||
"a capitalized word", | ||
); # end: 0f7c07e4-efd1-4aaa-a07a-90b49ce0b746 | ||
|
||
is( # begin: 71854b9c-f200-4469-9f5c-1e8e5eff5614 | ||
str_reverse("I'm hungry!"), | ||
"!yrgnuh m'I", | ||
"a sentence with punctuation", | ||
); # end: 71854b9c-f200-4469-9f5c-1e8e5eff5614 | ||
|
||
is( # begin: 1f8ed2f3-56f3-459b-8f3e-6d8d654a1f6c | ||
str_reverse("racecar"), | ||
"racecar", | ||
"a palindrome", | ||
); # end: 1f8ed2f3-56f3-459b-8f3e-6d8d654a1f6c | ||
|
||
is( # begin: b9e7dec1-c6df-40bd-9fa3-cd7ded010c4c | ||
str_reverse("drawer"), | ||
"reward", | ||
"an even-sized word", | ||
); # end: b9e7dec1-c6df-40bd-9fa3-cd7ded010c4c | ||
|
||
is( # begin: 1bed0f8a-13b0-4bd3-9d59-3d0593326fa2 | ||
str_reverse("\x{5B50}\x{732B}"), | ||
"\x{732B}\x{5B50}", | ||
"wide characters", | ||
); # end: 1bed0f8a-13b0-4bd3-9d59-3d0593326fa2 | ||
|
||
done_testing; |