Reverse the gender of a string: Difference between revisions
m
→{{header|Wren}}: Minor tidy
m (changed "sex" to gender, added whitespace.) |
m (→{{header|Wren}}: Minor tidy) |
||
(8 intermediate revisions by 8 users not shown) | |||
Line 7:
The returned string should contain this initial string, with all references to be gender switched.
<
He was a soul stripper. He took my heart!</
{{Template:Strings}}
<br><br>
Line 14 ⟶ 17:
{{trans|Kotlin}}
<
V words = [‘She’, ‘she’, ‘Her’, ‘her’, ‘hers’, ‘He’, ‘he’, ‘His’, ‘his’, ‘him’]
V repls = [‘He_’, ‘he_’, ‘His_’, ‘his_’, ‘his_’, ‘She_’, ‘she_’, ‘Her_’, ‘her_’, ‘her_’]
Line 26 ⟶ 29:
print(reverse_gender(‘He was a soul stripper. He took her heart!’))
print(reverse_gender(‘She wants what's hers, he wants her and she wants him!’))
print(reverse_gender(‘Her dog belongs to him but his dog is hers!’))</
{{out}}
Line 34 ⟶ 37:
He wants what's his, she wants his and he wants her!
His dog belongs to her but her dog is his!
</pre>
=={{header|Action!}}==
<syntaxhighlight lang="action!">DEFINE PTR="CARD"
DEFINE COUNT="10"
PTR ARRAY words(COUNT)
PTR ARRAY repls(COUNT)
PROC Init()
words(0)="She" repls(5)=words(0)
words(1)="she" repls(6)=words(1)
words(2)="Her" repls(7)=words(2)
words(3)="her" repls(8)=words(3)
words(4)="hers" repls(9)=words(4)
words(5)="He" repls(0)=words(5)
words(6)="he" repls(1)=words(6)
words(7)="His" repls(2)=words(7)
words(8)="his" repls(3)=words(8)
words(9)="him" repls(4)=words(9)
RETURN
PROC Append(CHAR ARRAY text,suffix)
BYTE POINTER srcPtr,dstPtr
BYTE len
len=suffix(0)
IF text(0)+len>255 THEN
len=255-text(0)
FI
IF len THEN
srcPtr=suffix+1
dstPtr=text+text(0)+1
MoveBlock(dstPtr,srcPtr,len)
text(0)==+suffix(0)
FI
RETURN
BYTE FUNC IsAlpha(CHAR c)
IF c>='A AND c<='Z OR c>='a AND c<='z THEN
RETURN (1)
FI
RETURN (0)
BYTE FUNC NextItem(CHAR ARRAY text BYTE start,word CHAR ARRAY res)
BYTE i
i=start
WHILE i<=text(0) AND IsAlpha(text(i))=word
DO i==+1 OD
SCopyS(res,text,start,i-1)
RETURN (i)
BYTE FUNC WordIndex(CHAR ARRAY text)
BYTE i
FOR i=0 TO COUNT-1
DO
IF SCompare(text,words(i))=0 THEN
RETURN (i)
FI
OD
RETURN (255)
PROC ReverseGender(CHAR ARRAY in,out)
CHAR ARRAY s(256)
BYTE start,word,index
out(0)=0
start=1 word=1
WHILE start<=in(0)
DO
start=NextItem(in,start,word,s)
index=WordIndex(s)
word=1-word
IF index=255 THEN
Append(out,s)
ELSE
Append(out,repls(index))
FI
OD
RETURN
PROC Test(CHAR ARRAY in)
CHAR ARRAY res(256)
ReverseGender(in,res)
PrintF("Input: ""%S""%E%E",in)
PrintF("Output: ""%S""%E%E%E",res)
RETURN
PROC Main()
Init()
Test("She was a soul stripper. She took his heart!")
Test("She wants what's hers, he wants her and she wants him!")
Test("She, she, Her, her, hers, He, he, His, his, him")
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Reverse_the_gender_of_a_string.png Screenshot from Atari 8-bit computer]
<pre>
Input: "She was a soul stripper. She took his heart!"
Output: "He was a soul stripper. He took her heart!"
Input: "She wants what's hers, he wants her and she wants him!"
Output: "He wants what's him, she wants his and he wants hers!"
Input: "She, she, Her, her, hers, He, he, His, his, him"
Output: "He, he, His, his, him, She, she, Her, her, hers"
</pre>
Line 40 ⟶ 155:
{{trans|Kotlin}}
<
ret: new str
entries: ["She" "she" "Her" "her" "hers" "He" "he" "His" "his" "him"]
Line 46 ⟶ 161:
loop.with:'i entries 'entry ->
replace 'ret
return replace ret "_" ""
Line 54 ⟶ 169:
print reverseGender "He was a soul stripper. He took her heart!"
print reverseGender "She wants what's hers, he wants her and she wants him!"
print reverseGender "Her dog belongs to him but his dog is hers!"</
{{out}}
Line 67 ⟶ 182:
To avoid swapping words which have already been swapped, thereby nullifying the original swap, I've appended an underscore to each replacement word and then removed all the underscores when all swaps have been made. This assumes, of course, that the text didn't include any underscores to start with.
<
Function isWordChar(s As String) As Boolean
Line 113 ⟶ 228:
Print
Print "Press any key to quit"
Sleep</
{{out}}
Line 124 ⟶ 239:
=={{header|Go}}==
<
import (
Line 145 ⟶ 260:
fmt.Println(t)
fmt.Println(reverseGender(t))
}</
{{out}}
Line 161 ⟶ 276:
{{works with|汉语拼音}}
{{works with|文言文}}
<syntaxhighlight lang
=={{header|J}}==
Line 175 ⟶ 290:
So, for now, we limit ourselves to the simple case specified in the task example, and do not even do all that great of a job there, either:
<
And, the task example:
<
He was a soul stripper. He took my heart!
cheaptrick cheaptrick 'She was a soul stripper. She took my heart!'
She was a soul stripper. She took my heart!</
=={{header|Java}}==
{{trans|J}}
<
public static void main(String[] args) {
Line 201 ⟶ 316:
return s;
}
}</
<pre>He was a soul stripper. He took my heart!
She was a soul stripper. She took my heart!</pre>
=={{header|jq}}==
''Adapted from [[#Wren|Wren]]''
{{works with|jq}}
'''Also works with both jaq and gojq, the Rust and Go implementations of jq'''
In this entry, we use the Unicode character classes for "open", "close",
and "other" punctuation, but show the test for a small set of specific
punctuation characters.
<syntaxhighlight lang="jq">
def swaps: {
"She": "He", "she": "he", "Her": "His", "her": "his", "hers": "his", "He": "She",
"he": "she", "His": "Her", "his": "her", "him": "her"
};
def isPunctuation:
type == "string" and
length == 1 and
# test("[!\"#%&'()*,-./:;?@\\[\\]\\\\_{}¡§«¶·»¿]")
# open, close, other
test("\\p{Ps}|\\p{Pe}|\\p{Po}|")
;
def reverseGender:
reduce splits(" *") as $word ([];
swaps[$word] as $s
| if $s then . + [$s]
elif ($word[-1:] | isPunctuation)
then swaps[$word[:-1]] as $s
| if $s then . + [$s + $word[-1:]]
else . + [$word]
end
else . + [$word]
end)
| join(" ");
def sentences: [
"She was a soul stripper. She took his heart!",
"He was a soul stripper. He took her heart!",
"She wants what's hers, he wants her and she wants him!",
"Her dog belongs to him but his dog is hers!"
];
sentences[]
| reverseGender
</syntaxhighlight>
{{Output}}
As for [[#Wren|Wren]].
=={{header|Julia}}==
{{trans|Kotlin}}
<
const MARKER = "\0"
Line 227 ⟶ 391:
@show ReverseGender.reverse("He was a soul stripper. He took her heart!")
@show ReverseGender.reverse("She wants what's hers, he wants her and she wants him!")
@show ReverseGender.reverse("Her dog belongs to him but his dog is hers!")</
=={{header|Kotlin}}==
This program uses a similar approach to the FreeBASIC entry:
<
fun reverseGender(s: String): String {
Line 249 ⟶ 413:
println(reverseGender("She wants what's hers, he wants her and she wants him!"))
println(reverseGender("Her dog belongs to him but his dog is hers!"))
}</
{{out}}
Line 258 ⟶ 422:
His dog belongs to her but her dog is his!
</pre>
=={{header|Lua}}==
Sufficient for the task as worded, but without attempting to go beyond (because several indeterminate cases exist). It does at least demonstrate an idiomatic way of doing multiple simultaneous substring substitutions.
<syntaxhighlight lang="lua">function sufficient(s)
local xref = { She="He", He="She" }
return (s:gsub("(%w+)", function(s) return xref[s] or s end))
end
s = "She was a soul stripper. She took my heart!"
print(sufficient(s))
print(sufficient(sufficient(s)))
print(sufficient(sufficient(s)) == s)</syntaxhighlight>
{{out}}
<pre>He was a soul stripper. He took my heart!
She was a soul stripper. She took my heart!
true</pre>
=={{header|MiniScript}}==
<
return w[0].upper + w[1:]
end function
Line 291 ⟶ 470:
test "He was a soul stripper. He took her heart!"
test "She wants what's hers, he wants her and she wants him!"
test "Her dog belongs to him but his dog is hers!"</
{{out}}
Line 305 ⟶ 484:
BEFORE: Her dog belongs to him but his dog is hers!
AFTER: His dog belongs to her but her dog is his!</pre>
=={{header|Nim}}==
{{trans|Kotlin}}
<syntaxhighlight lang="nim">import re, strutils
const
Words = ["She", "she", "Her", "her", "hers", "He", "he", "His", "his", "him"]
Repls = ["He_", "he_", "His_", "his_" ,"his_", "She_", "she_", "Her_", "her_", "her_"]
func reverseGender(s: string): string =
result = s
for i, word in Words:
let r = re(r"\b" & word & r"\b")
result = result.replace(r, Repls[i])
result = result.replace("_", "")
echo reverseGender("She was a soul stripper. She took his heart!")
echo reverseGender("He was a soul stripper. He took her heart!")
echo reverseGender("She wants what's hers, he wants her and she wants him!")
echo reverseGender("Her dog belongs to him but his dog is hers!")</syntaxhighlight>
{{out}}
<pre>He was a soul stripper. He took her heart!
She was a soul stripper. She took his heart!
He wants what's his, she wants his and he wants her!
His dog belongs to her but her dog is his!</pre>
=={{header|Objeck}}==
{{trans|Java}}
<
function : Main(args : String[]) ~ Nil {
s := "She was a soul stripper. She took my heart!";
Line 326 ⟶ 531:
}
}
</syntaxhighlight>
Output:
Line 336 ⟶ 541:
=={{header|Perl}}==
A minimal implementation, using a hash to manage swaps. But this approach breaks down if, for instance, 'him' were to replace 'me', as 'her' can't be used to map to both 'his' and 'him'.
<
'she' => 'he',
'his' => 'her',
Line 353 ⟶ 558:
$original = qq{She was this soul sherpa. She took his heart! They say she's going to put me on a shelf.\n};
print $swapped = gender_swap($original);
print $reverted = gender_swap($swapped);</
{{out}}
<pre>He was this soul sherpa. He took her heart! They say he's going to put me on a shelf.
Line 362 ⟶ 567:
words is a pair-list, ie "she","he" maps both ways, with first-upper-case handled too, and<br>
replacing the words right->left means no need to fiddle with indexes when lengths differ.
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">constant</span> <span style="color: #000000;">words</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"she"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"he"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"his"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"her"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"him"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"her"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"hers"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"his"</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">reverse_gender</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">ch</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">wordend</span>
<span style="color: #004080;">bool</span> <span style="color: #000000;">inword</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">wordch</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">to</span> <span style="color: #000000;">0</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">ch</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">?</span><span style="color: #008000;">' '</span><span style="color: #0000FF;">:</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
<span style="color: #000000;">wordch</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">not</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ch</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" .,'!\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">inword</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #000000;">wordch</span> <span style="color: #008080;">then</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">word</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">lower</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">wordend</span><span style="color: #0000FF;">])</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">word</span><span style="color: #0000FF;">,</span><span style="color: #000000;">words</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">k</span> <span style="color: #008080;">then</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">rep</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">k</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)?</span><span style="color: #000000;">k</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">:</span><span style="color: #000000;">k</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)]</span>
<span style="color: #000080;font-style:italic;">-- if s[i+2..wordend]=rep[2..$] then -- might be wanted here
--
<span style="color: #008080;">if</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]!=</span><span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">][</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span> <span style="color: #000000;">rep</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">upper</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rep</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">])</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">wordend</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rep</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">inword</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">wordch</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">inword</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span>
<span style="color: #000000;">wordend</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">i</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">s</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">tests</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"She was a soul stripper. She took my heart!\n"</span><span style="color: #0000FF;">,</span>
<span style="color: #008000;">"Her dog belongs to him but his dog is hers!\n"</span><span style="color: #0000FF;">}</span> <span style="color: #000080;font-style:italic;">-- ha ha!</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">ti</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tests</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span>
<span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">reverse_gender</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">rr</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">reverse_gender</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">&</span><span style="color: #000000;">r</span><span style="color: #0000FF;">&</span><span style="color: #000000;">rr</span><span style="color: #0000FF;">&</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 413 ⟶ 620:
=={{header|PowerShell}}==
{{trans|J}} (Made more PowerShelly.)
<syntaxhighlight lang="powershell">
function Switch-Gender ([string]$String)
{
Line 432 ⟶ 639:
Switch-Gender "She was a soul stripper. She took my heart!"
Switch-Gender (Switch-Gender "She was a soul stripper. She took my heart!")
</syntaxhighlight>
{{Out}}
<pre>
Line 440 ⟶ 647:
=={{header|Python}}==
<
# -*- coding: utf-8 -*- #
Line 563 ⟶ 770:
return "".join([ word+switch[gen] for word,gen in zip(text[::2],text[1::2])])+text[-1]
print rev_gender(text)</
'''Output:'''
<pre>
Line 578 ⟶ 785:
=={{header|Racket}}==
<
#lang at-exp racket
Line 673 ⟶ 880:
by the cannibal propensity he nourished in his untutored youth.
}))
</syntaxhighlight>
{{out}}
<pre>
Line 695 ⟶ 902:
For extremely limited circumstances such as this example, this should suffice. Notice case matching and replication. Handles contractions, but ignores embedded matching text.
<syntaxhighlight lang="raku"
{{out}}
<pre>He was a soul stripper. He took my heart! They say he's going to put me on a shelf.</pre>
Line 706 ⟶ 913:
Given the large size of the table (list), it would make more sense to put the words in a separate file instead of coding them in-line (within the computer program).
<
parse value linesize()-1 with sw @ @. !. /*get screen width, nullify some vars.*/
parse arg old
Line 1,409 ⟶ 1,616:
if $\='' then say strip($)
say
return</
This REXX program makes use of '''LINESIZE''' REXX program (or BIF) which is used to determine the screen width (or linesize) of the terminal (console).
<br>The '''LINESIZE.REX''' REXX program is included here ──► [[LINESIZE.REX]]. <br>
Line 1,448 ⟶ 1,655:
=={{header|Ring}}==
<
# Project : Reverse the gender of a string
Line 1,516 ⟶ 1,723:
next
return alist2
</syntaxhighlight>
Output:
<pre>
Line 1,534 ⟶ 1,741:
=={{header|Scala}}==
{{Out}}Best seen running in your browser either by [https://scalafiddle.io/sf/cpBaoMf/0 ScalaFiddle (ES aka JavaScript, non JVM)] or [https://scastie.scala-lang.org/0dajvapgRRChRZaZgpeqnQ Scastie (remote JVM)].
<
val s = "She was a soul stripper. She took my heart!"
println(cheapTrick(s))
Line 1,545 ⟶ 1,752:
}
}</
=={{header|Sidef}}==
<
maleS femaleS, maleness femaleness, him her, himself herself, his her, his
hers, he she, Mr Mrs, Mister Missus, Ms Mr, Master Miss, MasterS MistressES,
Line 1,616 ⟶ 1,823:
func reverse_gender(text) {
text.gsub(gen_re, { |a| copy_case(a, dict{a.lc}) })
}</
Example:
<
{{out}}
<pre>He was a soul stripper. He took my heart!</pre>
=={{header|Tcl}}==
<
apply {{} {
global genderMap genderRE
Line 1,677 ⟶ 1,884:
string map $genderMap &
]}]
}</
Demonstrating:
<
puts [reverseGender "When a new-hatched savage running wild about his native
woodlands in a grass clout, followed by the nibbling goats, as if
Line 1,688 ⟶ 1,895:
who were the wives of unconquerable warriors. There was excellent
blood in his veins-royal stuff; though sadly vitiated, I fear,
by the cannibal propensity he nourished in his untutored youth."]</
{{out}}
<pre>
Line 1,706 ⟶ 1,913:
=={{header|Wren}}==
{{libheader|Wren-str}}
<
var swaps = {
Line 1,735 ⟶ 1,942:
]
for (sentence in sentences) System.print(reverseGender.call(sentence))</
{{out}}
|