反转串!


11

您的任务:编写一个程序/函数,当给定仅包含ASCII字符的字符串时,将以反白的方式输出/返回该字符串。

例:

1)输入

Hello, World!

2)在输入中为唯一字符编号。(输入字符串之间用竖线(|)分隔,以提高可读性)

H|e|l|l|o|,| |W|o|r|l|d|!
1 2 3   4 5 6 7   8   9 10

3)对于重复字符,请查找该字符的第一个匹配项,并用与第一个相同的数字对重复的字符进行编号。

H|e|l|l|o|,| |W|o|r|l|d|!
1 2 3 3 4 5 6 7 4 8 3 9 10

4)颠倒字符串,但不颠倒数字。

!|d|l|r|o|W| |,|o|l|l|e|H
1 2 3 3 4 5 6 7 4 8 3 9 10

5)删除重复数字上方的字符。(已删除的字符用星号表示。)

!|d|l|*|o|W| |,|*|l|*|e|H
1 2 3 3 4 5 6 7 4 8 3 9 10

6)将被删除的字符替换为第一次出现在被删除字符结束的数字上的字符。

!|d|l|l|o|W| |,|o|l|l|e|H
1 2 3 3 4 5 6 7 4 8 3 9 10

7)输出

!dlloW ,olleH

测试用例:

Input -> Output
"Hello, World!" -> "!dlloW ,olleH"
"18464399" -> "99343488"
"Code Golf" -> "floG eloC"
"abcdefgABCDEFG" -> "GFEDCBAgfedcba"
"Mmm, marshmallows" -> "swwllwmhsrwm  mms"
"15147" -> "74751"

对于那些可以看到已删除帖子的人,沙箱在这里
“ SparklePony同志” 17年

字符串是否保证仅包含ASCII字符?
Leaky Nun

@LeakyNun是的,我会编辑。
“ SparklePony同志” 17年

Answers:


19

Pyth,1个字节

X

验证所有测试用例。

Pyth具有出色的内置功能:-)


Pyth 8  7字节

sm@_QxQ

验证所有测试用例。

这个怎么运作

这是更有趣的非内置方法。

sm@_QxQ  - Full program.

 m       - Map over the input.
  @_Q    - Get the index in the reversed input.
     xQ  - Of the first index of each character in the String.
s        - Join the list.

4
为什么Pyth具有内置功能?除了这个问题,它还有什么用?
杰里·耶利米

@JerryJeremiah您可以在此处阅读有关此功能的更多信息。它是Pyth中的转换函数,但是如果缺少第三个参数,则使用第二个参数的反函数。
Xcoder先生17年

6

Python 2中46 41个字节

-5字节归功于Artyer

lambda x:''.join(x[~x.find(n)]for n in x)

在线尝试!


1
您可以做lambda x:''.join(x[~x.find(n)]for n in x)而不是先反转然后再索引
Artyer


5

CJam,7个字节

q__W%er

在线尝试!

说明

q   e# Read all input.
__  e# Make two copies.
W%  e# Reverse the third copy.
er  e# Transliterate the input, mapping the input to its own reverse. Due
    e# to the way transliteration works in CJam, if a character in the source
    e# of the mapping is repeated, all but the first occurrence of that
    e# character are ignored.

我希望果冻能y像那样工作。
暴民埃里克(Erik the Outgolfer)'17年

@EriktheOutgolfer我希望爱丽丝 y那样工作。:P
Martin Ender

5

MATL,6个字节

PGt&m)

在线尝试!

           implicit input
P          flip
 Gt        paste input back to stack and dup
   &m      alternate ismember, returns duplicated indices
     )     apply indices to the reversed char array
           implicit output, print as char


3

爱丽丝,17个字节

/?.R?y.@
\i.!yDo/

在线尝试!

说明

/...@
\.../

这只是Ordinal模式下线性代码的常用模板。如果我们展开此程序,则实际程序将变成:

i.!?D.?.Ryyo

这里的想法类似于我的CJam答案。由于Alice没有简单的方法来索引带有整数的字符串,因此最简单的方法是通过音译来复制这种行为(y在Alice中)。但是,爱丽丝的音译语义比CJam的音译语义更广泛,这意味着爱丽丝不仅会忽略重复映射。例如,如果我们只想将音译Mmm, marshmallows反转,则将表示以下映射列表:

M -> s
m -> w
m -> o
, -> l
  -> l
m -> a
a -> m
r -> h
s -> s
h -> r
m -> a
a -> m
l ->  
l -> ,
o -> m
w -> m
s -> M

请注意,我们已经有了,例如m -> wm -> om -> am -> a。CJam只会丢弃除第一个映射以外的所有内容,但Alice会循环遍历这些内容。因此,第一个m映射到w,第二个映射到,o第五个映射到w依此类推。在这种情况下是没有帮助的,因为一般来说,如果我们执行yAAB(对于某些字符串AB)正如我们在CJam一样,我们总是只得到B爱丽丝。

那么,我们如何计算适用的映射y(即,如何手动丢弃重复的映射)?当然,通过使用另一个音译。:)

我们想要的映射源必须是输入的小数位(即重复数据删除的输入)。如果我们将上述映射应用于小结,则每个字符仅出现一次,因此我们仅使用每个重复映射中的第一个。因此,通过用输入及其反译来对小节进行音译,我们实际上只是在丢弃重复的映射。然后,我们可以将nub和这个新结果用作原始输入的映射。我敢肯定,这对某人是有意义的...

所以代码:

i   Read input.                   ["Mmm, marshmallows"]
.!  Store a copy on the tape.
?D  Push the nub of the input.    ["Mmm, marshmallows" "Mm, arshlow"]
.   Duplicate.                    ["Mmm, marshmallows" "Mm, arshlow" "Mm, arshlow"]
?   Retrieve input.               ["Mmm, marshmallows" "Mm, arshlow" "Mm, arshlow" "Mmm, marshmallows"]
.R  Push its reverse.             ["Mmm, marshmallows" "Mm, arshlow" "Mm, arshlow" "Mmm, marshmallows" "swollamhsram ,mmM"]
y   Transliterate.                ["Mmm, marshmallows" "Mm, arshlow" "swllmhsr mm"]]
y   Transliterate.                ["swwllwmhsrwm  mms"]
o   Output.                       []



3

JavaScript ES6 50字节

Justin Mariner节省了3个字节

s=>[...s].map(x=>s.slice(~s.indexOf(x))[0]).join``

测试一下:

f=s=>s.split``.map(x=>s.slice(~s.indexOf(x))[0]).join``

let inp = document.getElementById("inp");
let out = document.getElementById("out");

function change() {
  out.innerText = f(inp.value);
}

change();
<input id="inp" type="text" oninput="change()" value="Hello, World!" /><br>
<p id="out"></p>


2

R68 65字节

function(s)chartr(paste(rev(el(strsplit(s,''))),collapse=''),s,s)

验证测试用例!

Erik的05AB1E方法移植少3个字节。它不是第一个,但是我看到的是第一个。

旧版本:

function(s)paste(rev(s<-el(strsplit(s,'')))[match(s,s)],collapse='')

验证所有测试用例 -打印出矢量,其输入作为名称,输出在下面的引号中。

一个相当幼稚的实现,但是我不认为这会在R中变得更短(我希望对此有所误解)。它本质上是Rod的python答案的R端口,但它是独立开发的。

空洞的解释:

function(s){
 s <- el(strsplit(s,''))      # string to characters
 r <- rev(s)                  # reverse s
 idx <- match(s,s)            # indices of first matches
 r <- r[idx]                  # do the replacement of duplicates
 paste(r, collapse = "")      # characters to string
}                             # implicit return of last evaluated statement



0

Python191 128字节

w=input("")
c=[]
for x in range(0,len(word)):
  chars.append(word[x])
back = []
for x in chars:
  back.insert(0,x)
final = ""
for x in back:
  final = final + x
print(final)

在线尝试


4
嗨,欢迎来到PPCG!这是一个很好的答案,但它不是高尔夫球(它的字节数也超过了所需的数)。请尝试删除一些字节(例如,通过缩短变量名)并编辑您的帖子。
NoOneIsHere

0

Java 10、100 99 97字节

s->{var a=new char[256];int l=s.length,i=l;for(;i>0;a[s[--i]]=s[l+~i]);for(;i<l;s[i]=a[s[i++]]);}

@LeakyNun的C答案的端口。我怀疑如果不使用Java做类似的事情就可以缩短它的执行时间。
-1字节感谢@ceilingcat

输入为char[](字符数组),并修改此输入,而不返回一个新的字节来保存字节。

在这里尝试。


@ceilingcat谢谢!
凯文·克鲁伊森
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.