डबल रोटेशन


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
मुझे याद दिलाएं, वर्णमाला के पहले आधे से कौन से अक्षर हैं, दूसरे से कौन से अक्षर हैं?
14:

लेकिन फिर भी, अच्छी चुनौती।
14:

4
फर्स्ट हाफ: ABCDEFGHIJKLMabcdefghijklm सेकंड हाफ: NOPQRSTUVWXYZnopqrstuvwxyz
पॉल

अजीब बात है कि कोडगॉल्फ खुद का एक विपर्यय बन जाता है
गर्वित हेकलर

जवाबों:


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

05AB1E , 44 43 42 बाइट्स

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

व्याख्या

दोनों मामलों के वर्णमाला के अक्षरों की एक सूची तैयार करें। ['Aa','Bb', ..., 'Zz']

Оn2äø€J

2 भागों में विभाजित करें और रजिस्टर में एक कॉपी स्टोर करें।

2ä©

इनपुट से अक्षरों को निकालें जो वर्णमाला के 1 आधे हिस्से का हिस्सा हैं, इसे घुमाएं और एक्स में स्टोर करें ।

`ŠÃÁU

वर्ण से वर्णों को निकालें जो वर्णमाला के 2 आधे भाग का हिस्सा हैं, इसे घुमाएं और वाई में स्टोर करें ।

ÃÀ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 की तरह दिखता है
KarlKastor


3

जावास्क्रिप्ट (ईएस 6), 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


2

पायथन, 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

पायथन 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 की टिप्पणी के आधार पर समुदाय उपयोगकर्ता द्वारा बीटा डेके द्वारा संपादित किए जाने के बाद स्वचालित रूप से किया गया था ।
केविन क्रूज़सेन

1

जावास्क्रिप्ट (ईएस 6), 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

पर्ल। 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

पायथन, 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')

ungolfed:

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]')

ungolfed:

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);
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.