二重回転


28

チャレンジの説明

アルファベットの最初の部分のすべての文字を一方向に循環させ、アルファベットの後半の文字をもう一方の方向に循環させます。他のキャラクターはそのままです。

1:Hello world

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

codegolf
c deg lf
 o   o  

f cde gl
 o   o  

focdeogl

3 .:空の文字列

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

入力

回転する必要がある文字列。空の場合があります。改行は含まれません。

出力

入力文字列を回転させ、末尾の改行を許可
画面に書き込むか、関数によって返すことができます。

ルール

  • 抜け穴はありません
  • これはコードゴルフなので、問題を解決するバイト単位の最短コードが勝ちます
  • プログラムは正しいソリューションを返さなければなりません

1
アルファベットの前半からの文字、後半からの文字は何ですか?
user48538

しかし、それでも良い挑戦です。
user48538

4
前半:ABCDEFGHIJKLMabcdefghijklm後半:NOPQRSTUVWXYZnopqrstuvwxyz
ポールシュミッツ

codegolfがそれ自体のアナグラムになることをおもしろい
誇り高いhaskeller

回答:


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つの部分に分割し、コピーをレジスタに保存します。

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

オンラインでお試しください!

注:41バイトのソリューションの2sableでは、先頭Ðを省略できます。


4
<s>44</s>それでも44のように見えます
。–カールカストール


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、最終文字列に新しい文字を注入するメソッドで2回呼び出されます。

したがって、少し変わった構文: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


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へようこそ!おそらく、コードの説明やアイデアを追加できますか?ここでは、この回答の@Dennisのコメントに基づいて、コミュニティユーザーがBeta Decayで編集した後、ダウン投票が自動的に行われたと想定しています
ケビンCruijssen

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、任意の数がfalse>)

最初のステップ:入力文字列は0、1または2の配列にマッピングされます。一方、すべての文字列文字は3つの配列に追加されます。この最初のステップの終わりに、アレイ1と2は反対方向に回転します。

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

ルビー、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);
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.