两次旋转


28

挑战说明

从一个字母的第一部分开始循环循环所有字母,从另一个字母的第二部分开始循环循环。其他字符保持不变。

例子

1:你好世界

Hello_world //Input
Hell     ld //Letters from first half of alphabet
    o wor   //Letters from second half of alphabet
     _      //Other characters
dHel     ll //Cycle first letters
    w oro   //Cycle second letters
     _      //Other characters stay
dHelw_oroll //Solution

2:代码高尔夫

codegolf
c deg lf
 o   o  

f cde gl
 o   o  

focdeogl

3 .:空字符串

(empty string) //Input
(empty string) //Output

输入值

您需要旋转的字符串。可能是空的。不包含换行符。

输出量

旋转的输入字符串,允许尾随换行符
可以写入屏幕或由函数返回。

规则

  • 不允许有漏洞
  • 这是代码高尔夫球,因此解决问题的最短代码以字节为单位
  • 程序必须返回正确的解决方案

1
提醒我,字母的前半部分是什么字母,字母的后半部分是什么字母?
user48538'9

但是,仍然是一个很好的挑战。
user48538'9

4
上半部分:ABCDEFGHIJKLMabcdefghijklm下半部分:NOPQRSTUVWXYZnopqrstuvwxyz
Paul Schmitz

有趣的是,代码高尔夫球变成了自己的字谜
骄傲的haskeller

Answers:


0

MATL,29个字节

FT"ttk2Y213:lM@*+)m)1_@^YS9M(

在线尝试!

说明

FT        % Push arrray [0 1]
"         % For each
  t       %   Duplicate. Takes input string implicitly in the first iteration
  tk      %   Duplicate and convert to lower case
  2Y2     %   Predefined string: 'ab...yz'
  13:     %   Generate vector [1 2 ... 13]
  lM      %   Push 13 again
  @*      %   Multiply by 0 (first iteration) or 1 (second): gives 0 or 13
  +       %   Add: this leaves [1 2 ... 13] as is in the first iteration and
          %   transforms it into [14 15 ... 26] in the second
  )       %   Index: get those letters from the string 'ab...yz'
  m       %   Ismember: logical index of elements of the input that are in 
          %   that half of the alphabet
  )       %   Apply index to obtain those elements from the input
  1_@^    %   -1 raised to 0 (first iteration) or 1 (second), i.e. 1 or -1
  YS      %   Circular shift by 1 or -1 respectively
  9M      %   Push the logical index of affected input elements again
  (       %   Assign: put the shifted chars in their original positions
          % End for each. Implicitly display


4

05AB1E44 43 42 字节

Оn2äø€J2ä©`ŠÃÁUÃÀVv®`yåiY¬?¦VëyåiX¬?¦Uëy?

说明

生成这两种情况的字母列表。 ['Aa','Bb', ..., 'Zz']

Оn2äø€J

分为两部分,并将副本存储在寄存器中。

2ä©

从输入中提取字母第一部分的一部分的字母,旋转它并存储在X中

`ŠÃÁU

从输入中提取字母第二半部分的字母,旋转并存储在Y中

ÃÀV

主循环

v                         # for each char in input
 ®`                       # push the lists of first and second half of the alphabet
   yåi                    # if current char is part of the 2nd half of the alphabet
      Y¬?                 # push the first char of the rotated letters in Y
         ¦V               # and remove that char from Y
           ëyåi           # else if current char is part of the 1st half of the alphabet
               X¬?        # push the first char of the rotated letters in X
                  ¦U      # and remove that char from X
                    ëy?   # else print the current char

在线尝试!

注:龙头Ð可以省略2sable一个41字节的解决方案。


4
<s>44</s>仍然看起来像44
岁。– KarlKastor


3

使用Javascript(ES6),155个 142 138字节

s=>(a=[],b=[],S=s,R=m=>s=s.replace(/[a-z]/gi,c=>(c<'N'|c<'n'&c>'Z'?a:b)[m](c)),R`push`,a.unshift(a.pop(b.push(b.shift()))),s=S,R`shift`,s)

编辑:通过使用保存3 4字节unshift()(受edc65的启发)

怎么运行的

R函数采用数组方法作为其参数m

R = m => s = s.replace(/[a-z]/gi, c => (c < 'N' | c < 'n' & c > 'Z' ? a : b)[m](c))

它首先与该push方法一起使用,以将提取的字符存储在a[](字母的前半部分)和b[](字母的后半部分)中。一旦旋转了这些数组,将使用将新字符注入最终字符串R()shift方法再次调用。

因此,语法略有不同:R`push`R`shift`

演示版

let f =
s=>(a=[],b=[],S=s,R=m=>s=s.replace(/[a-z]/gi,c=>(c<'N'|c<'n'&c>'Z'?a:b)[m](c)),R`push`,a.unshift(a.pop(b.push(b.shift()))),s=S,R`shift`,s)

console.log("Hello_world", "=>", f("Hello_world"));
console.log("codegolf", "=>", f("codegolf"));
console.log("HELLO_WORLD", "=>", f("HELLO_WORLD"));


多保存1个字节,避免逗号a.unshift(a.pop(b.push(b.shift())))
edc65'9


2

Python,211字节

x=input()
y=lambda i:'`'<i.lower()<'n'
z=lambda i:'m'<i.lower()<'{'
u=filter(y,x)
d=filter(z,x)
r=l=""
for i in x:
 if y(i):r+=u[-1];u=[i]
 else:r+=i
for i in r[::-1]:
 if z(i):l=d[0]+l;d=[i]
 else:l=i+l
print l

我能做的最好的。从STDIN中获取字符串,并将结果打印到STDOUT。

带有204个字节的替代方法,但不幸的是在每个字符之后打印换行符:

x=input()
y=lambda i:'`'<i.lower()<'n'
z=lambda i:'m'<i.lower()<'{'
f=filter
u=f(y,x)
d=f(z,x)
r=l=""
for i in x[::-1]:
 if z(i):l=d[0]+l;d=[i]
 else:l=i+l
for i in l:
 a=i
 if y(i):a=u[-1];u=[i]
 print a

1

Python 2,149字节

s=input();g=lambda(a,b):lambda c:a<c.lower()<b
for f in g('`n'),g('m{'):
 t='';u=filter(f,s)[-1:]
 for c in s:
  if f(c):c,u=u,c
  t=c+t
 s=t
print s

2
不知道是谁投票赞成您,但我通过投票再次将其设为0。欢迎来到PPCG!也许您可以在代码中添加解释或想法?我在此假设,在社区用户根据Beta Denay此答案中的@Dennis的评论进行编辑之后,将自动完成降票
凯文·克鲁伊森

1

JavaScript(ES6),144

使用parseInt底座36分隔上半部分,下半部分和其他部分。对于任何角色c,我都会进行评估,y=parseInt(c,36)以便

  • c '0'..'9' -> y 0..9
  • c 'a'..'m' or 'A'..'M' -> y 10..22
  • c 'n'..'z' or 'N'..'Z' -> y 23..35
  • c any other -> y NaN

因此,y=parseInt(c,36), x=(y>22)+(y>9)给出x==1上半年,x==2下半年和x==0其他任何结果(如NaN >任何数字为假)

第一步:将输入字符串映射到0,1或2的数组。同时,所有字符串字符都添加到3个数组中。在第一步结束时,阵列1和2沿相反方向旋转。

第二步:扫描映射的数组,重建一个输出字符串,从3个临时数组中获取每个字符。

s=>[...s].map(c=>a[y=parseInt(c,36),x=(y>22)+(y>9)].push(c)&&x,a=[[],p=[],q=[]]).map(x=>a[x].shift(),p.unshift(p.pop(q.push(q.shift())))).join``

少打高尔夫球

s=>[...s].map(
  c => a[ y = parseInt(c, 36), x=(y > 22) + (y > 9)].push(c) 
       && x,
  a = [ [], p=[], q=[] ]
).map(
  x => a[x].shift(),  // get the output char from the right temp array
  p.unshift(p.pop()), // rotate p
  q.push(q.shift())   // rotate q opposite direction
).join``

测试

f=
s=>[...s].map(c=>a[y=parseInt(c,36),x=(y>22)+(y>9)].push(c)&&x,a=[[],p=[],q=[]]).map(x=>a[x].shift(),p.unshift(p.pop()),q.push(q.shift())).join``

function update() {
  O.textContent=f(I.value);
}

update()
<input id=I oninput='update()' value='Hello, world'>
<pre id=O></pre>


0

Perl。53个字节

包括+1的 -p

使用STDIN上的输入运行:

drotate.pl <<< "Hello_world"

drotate.pl

#!/usr/bin/perl -p
s%[n-z]%(//g,//g)[1]%ieg;@F=/[a-m]/gi;s//$F[-$.--]/g

0

Python,142 133字节的

主题的一个更好的变化:

import re
def u(s,p):x=re.split('(?i)([%s])'%p,s);x[1::2]=x[3::2]+x[1:2];return ''.join(x)
v=lambda s:u(u(s[::-1],'A-M')[::-1],'N-Z')

松散:

import re
def u(s,p):
    x = re.split('(?i)([%s])'%p,s)  # split returns a list with matches at the odd indices
    x[1::2] = x[3::2]+x[1:2]
    return ''.join(x)

def v(s):
  w = u(s[::-1],'A-M')
  return u(w[::-1],'N-Z')

先前的解决方案:

import re
def h(s,p):t=re.findall(p,s);t=t[1:]+t[:1];return re.sub(p,lambda _:t.pop(0),s)
f=lambda s:h(h(s[::-1],'[A-Ma-m]')[::-1],'[N-Zn-z]')

松散:

import re
def h(s,p):                              # moves matched letters toward front
    t=re.findall(p,s)                    # find all letters in s that match p
    t=t[1:]+t[:1]                        # shift the matched letters
    return re.sub(p,lambda _:t.pop(0),s) # replace with shifted letter

def f(s):
    t = h(s[::-1],'[A-Ma-m]')            # move first half letters toward end
    u = h(t[::-1],'[N-Zn-z]')            # move 2nd half letters toward front
    return u

0

Ruby,89个字节

f=->n,q,s{b=s.scan(q).rotate n;s.gsub(q){b.shift}}
puts f[1,/[n-z]/i,f[-1,/[a-m]/i,gets]]

0

PHP,189字节

非常难以打高尔夫...这是我的建议:

for($p=preg_replace,$b=$p('#[^a-m]#i','',$a=$argv[1]),$i=strlen($b)-1,$b.=$b,$c=$p('#[^n-z]#i','',$a),$c.=$c;($d=$a[$k++])!=='';)echo strpos(z.$b,$d)?$b[$i++]:(strpos(a.$c,$d)?$c[++$j]:$d);
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.