एक स्ट्रिंग Zigzagify


46

एक प्रोग्राम (या फ़ंक्शन) लिखें जो किसी भी मुद्रण योग्य ASCII वर्णों के गैर-रिक्त स्ट्रिंग में लेता है ।

प्रत्येक वर्ण की जोड़ी के साथ स्ट्रिंग में वर्णों की एक ज़िगज़ैगिंग श्रृंखला को प्रिंट (या वापस) करें:

  • /यदि पहला वर्ण सामान्य ASCII क्रम में दूसरे वर्ण से पहले होता है। जैसे

      B
     /
    A
    
  • \यदि पहला चरित्र सामान्य ASCII क्रम में दूसरे वर्ण के बाद होता है। जैसे

    B
     \
      A
    
  • -यदि पहला और दूसरा अक्षर समान हैं। जैसे

    A-A
    

तो आउटपुट के लिए Programming Puzzles & Code Golfहोगा

                                                        o    
                                                       / \   
  r                         z-z               o   e   G   l  
 / \                       /   \             / \ / \ /     \ 
P   o   r   m-m   n       u     l   s   &   C   d           f
     \ / \ /   \ / \     /       \ / \ / \ /                 
      g   a     i   g   P         e                          
                     \ /                                     
                                                             

यदि इनपुट स्ट्रिंग में केवल एक वर्ण है, तो आउटपुट केवल वह वर्ण होगा।

आपका कार्यक्रम व्यवहार करना चाहिए , /, \, और -अन्य सभी पात्रों के रूप में सिर्फ एक ही।

जैसे -\//-- \ //- उत्पादन करना चाहिए:

      \                      
     / \                     
    -   /-/                  
   /       \                 
 -          ---   \   /-/    
               \ / \ /   \   
                          -  
                           \ 
                             

आउटपुट में एक भी वैकल्पिक अनुलिपि न्यूलाइन को छोड़कर कोई बाहरी नहीं होना चाहिए। (ध्यान दें कि ऊपर के उदाहरण में खाली रेखा स्ट्रिंग में अंतिम स्थान रखती है और इसलिए बाहरी नहीं है।) किसी भी व्यवस्था में किसी भी रेखा पर रिक्त स्थान हो सकते हैं।

बाइट्स में सबसे छोटा कोड जीतता है।

एक और उदाहरण - इनपुट:

3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679

आउटपुट:

                          9   9       8   6   6                                                                                                                                                            
                         / \ / \     / \ / \ / \                                                                                                                                                           
            9   6       8   7   3   3   4   2   4     8       9       8-8                                                                                                                                  
           / \ / \     /         \ /             \   / \     / \     /   \                                                                                                                                 
      4   5   2   5   5           2               3-3   3   7   5   2     4   9       9   9-9   7                                                                                                          
     / \ /         \ /                                   \ /     \ /       \ / \     / \ /   \ / \                                                                                                         
3   1   1           3                                     2       0         1   7   6   3     3   5       8                             8   6                                                              
 \ /                                                                             \ /               \     / \                           / \ / \                                                             
  .                                                                               1                 1   5   2   9             9   3   7   1   4   6   8                                                   9
                                                                                                     \ /     \ / \           / \ / \ /         \ / \ / \                                                 / 
                                                                                                      0       0   7   9     5   2   0           0   2   6       9-9               8   5   4             7  
                                                                                                                   \ / \   /                             \     /   \             / \ / \ / \           /   
                                                                                                                    4   4-4                               2   8     8           4   2   3   2     7   6    
                                                                                                                                                           \ /       \         /             \   / \ /     
                                                                                                                                                            0         6   8   3               1-1   0      
                                                                                                                                                                       \ / \ /                             
                                                                                                                                                                        2   0                              

जवाबों:


8

पायथ, 69 बाइट्स

aY,JhzZVtzaY,@"-\/"K-<NJ>N~JN=+ZKaY,N=+ZK;jbCmX*\ h-e=GSeMYhG-edhGhdY

प्रदर्शन। लंबे समय तक इनपुट अभी भी काम करते हैं, लेकिन वे निश्चित-चौड़ाई वाले आउटपुट बॉक्स में बहुत अच्छे नहीं लगते हैं।

मैं Y[चरित्र, ऊँचाई] टुपल्स की सूची बनाकर शुरू करता हूँ । यह उदाहरण [['P', 0], ['/', -1], ['r', -2], ['\\', -1], ['o', 0], ['\\', 1], ['g', 2]]में जल्दी है Programming Puzzles & Code Golf

मैं तब उपयुक्त लंबाई के रिक्त स्थान का तार बनाता हूं, चरित्र को उपयुक्त स्थान पर सम्मिलित करता हूं, स्थानांतरित करता हूं, नई रेखाओं पर जुड़ता हूं और प्रिंट करता हूं।


7

जूलिया, 297 बाइट्स

s->(l=length;d=sign(diff([i for i=s]));J=join([[string(s[i],d[i]>0?:'/':d[i]<0?:'\\':'-')for i=1:l(d)],s[end]]);D=reshape([d d]',2l(d));L=l(J);E=extrema(cumsum(d));b=2sumabs(E)+1;A=fill(" ",L,b);c=b-2E[2];for (i,v)=enumerate(J) A[i,c]="$v";i<l(D)&&(c-=D[i])end;for k=1:b println(join(A'[k,:]))end)

Ungolfed:

function f(s::String)
    # Get the direction for each slash or dash
    # +1 : /, -1 : \, 0 : -
    d = sign(diff([i for i in s]))

    # Interleave the string with the slashes as an array
    t = [string(s[i], d[i] > 0 ? '/' : d[i] < 0 ? '\\' : '-') for i = 1:length(d)]

    # Join the aforementioned array into a string
    J = join([t, s[end]])

    # Interleave D with itself to duplicate each element
    D = reshape(transpose([d d]), 2*length(d))

    # Get the length of the joined string
    L = length(J)

    # Get the maximum and minimum cumulative sum of the differences
    # This determines the upper and lower bounds for the curve
    E = extrema(cumsum(d))

    # Get the total required vertical size for the output curve
    b = 2*sumabs(E) + 1

    # Get the beginning vertical position for the curve
    c = b - 2*E[2]

    # Construct an array of spaces with dimensions corresponding
    # to the curve rotated 90 degrees clockwise
    A = fill(" ", L, b)

    # Fill the array with the curve from top to bottom
    for (i,v) = enumerate(J)
        A[i,c] = "$v"
        i < length(D) && (c -= D[i])
    end

    # Print out the transposed matrix
    for k = 1:b
        println(join(transpose(A)[k,:]))
    end
end

5

जावास्क्रिप्ट (ईएस 6), 360 331 316 302 बाइट्स

यहाँ मेरा चौथा प्रयास है:

s=>{r=[],c=s[m=w=n=0];for(i in s)(i?(d=s[++i])>c?++n:c>d?--n:n:n)<m&&m--,n>w&&w++,c=d;for(i=0,n=w*2;i<(w-m)*2+1;r[i++]=[...' '.repeat(l=s.length*2-1)]);for(i=0;i<l;i++)i%2?(A=s[C=(i-1)/2])<(B=s[C+1])?r[--n,n--][i]='/':A>B?r[++n,n++][i]='\\':r[n][i]='-':r[n][i]=s[i/2];return r.map(x=>x.join``).join`
`}

दूसरों में से कुछ के रूप में कम नहीं है, लेकिन मैं इसके लिए अभी से संतुष्ट हूं।

ओह, तो आप इसका परीक्षण करना चाहते हैं? ठीक है, यहाँ तुम जाओ:

z=s=>{r=[],c=s[m=w=n=0];for(i in s)(i?(d=s[++i])>c?++n:c>d?--n:n:n)<m&&m--,n>w&&w++,c=d;for(i=0,n=w*2;i<(w-m)*2+1;r[i++]=[...' '.repeat(l=s.length*2-1)]);for(i=0;i<l;i++)i%2?(A=s[C=(i-1)/2])<(B=s[C+1])?r[--n,n--][i]='/':A>B?r[++n,n++][i]='\\':r[n][i]='-':r[n][i]=s[i/2];return r.map(x=>x.join``).join('<br>')};

input=document.getElementById("input");
p=document.getElementById("a");
input.addEventListener("keydown", function(){
  setTimeout(function(){p.innerHTML = "<pre>"+z(input.value)+"</pre>";},10);
})
<form>Type or paste your text here: <input type="text" id="input"/></form>

<h3>Output:</h3>
<p id="a"></p>

मज़े करो!

अपडेट:

अद्यतन 1: विभिन्न प्रकार की विशिष्ट तकनीकों के साथ 29 बाइट्स को बंद किया गया।

अद्यतन 2: शुरू से ही क्षैतिज रूप से स्ट्रिंग का निर्माण करके 15 से अधिक बाइट्स को घेरे हुए, जैसा कि ऊर्ध्वाधर तारों की एक सरणी बनाने और उन्हें चारों ओर स्विच करने के लिए विरोध किया गया था, जो कि उसने पहले किया था।

अद्यतन 3: 14 अधिक बाइट्स सहेजे गए।

जल्द ही और अधिक गोल्फ!


आप को बदल कर एक बाइट बचा सकता है '\n'की तरह एक टेम्पलेट तार के साथ इस
jrich

@UndefinedFunction हाँ, मैंने उस ट्रिक को पहले भी इस्तेमाल किया है, लेकिन इसे पिछली रात में रखना भूल गया। याद दिलाने के लिए शुक्रिया!
ETHproductions

आपके forछोरों को बहुत कुछ किया जा सकता है। आवश्यक कोड के एक पूरे ब्लॉक को बर्बाद न करें i++। इसके बजाय, forवहां अपना अधिकांश कोड चलाएं । इसके अलावा, आपको कोड की एकल लाइनों के आसपास ब्रेसिज़ की आवश्यकता नहीं है।
नहीं कि चार्ल्स

यह भी लगता है कि आपके द्वारा उपयोग किया जाने वाला एकमात्र तरीका lगणना करना है s.length*2-1और आप दो बार ऐसा करते हैं। इसके बजाय उस मूल्य को क्यों नहीं संचित करें?
नहीं कि चार्ल्स

1
@NotthatCharles सुझावों के लिए धन्यवाद! मैंने अभी-अभी संशोधित एल्गोरिथम आज़माया है और इसे और अधिक परेशान नहीं करना है। इसमें <br>बस इतना है कि यह HTML संस्करण में दिखाई देता है; यदि आप बारीकी से देखते हैं, तो मैं वास्तविक प्रविष्टि के बजाय एक टेम्पलेट स्ट्रिंग का उपयोग करता हूं। इसके अलावा, यह कोई आवश्यकता नहीं है: "प्रिंट (या वापसी) ..."
21

3

पायथन, 393 बाइट्स

def z(n,h=[]):
 for j in range(len(n)):h.append(sum(cmp(ord(n[i]),ord(n[i+1]))for i in range(j)))
 h=[j-min(h)for j in h]
 for y in range(max(h)*2+2):
  s=""
  for x in range(len(n)):
   if h[x]*2==y:s+=n[x]
   else:s+=" "
   if x==len(n)-1:continue
   c=" "
   if h[x]<h[x+1]and h[x]*2==y-1:c="\\"
   if h[x]>h[x+1]and h[x]*2==y+1:c="/"
   if h[x]==h[x+1]and h[x]*2==y:c="-"
   s+=c
  print s

ऐसे दोड़ो: z("Zigzag")


3

जावास्क्रिप्ट (ईएस 6), 202

टेम्पलेट स्ट्रिंग्स का उपयोग करना। इंडेंटेशन स्पेस और न्यूलाइन्स की गिनती नहीं की गई है, सिवाय इसके कि बैकटिक्स के अंदर की आखिरी न्यूलाइन महत्वपूर्ण और गिनी जाती है।

सामान्य नोट: किसी भी EcmaScript 6 अनुरूप ब्राउज़र (विशेष रूप से क्रोम नहीं MSIE। मैंने फ़ायरफ़ॉक्स पर परीक्षण किया, सफ़ारी 9 जा सकता है पर स्निपेट चलाने का परीक्षण करें।)

f=z=>
  [...z].map(c=>
    (d=0,x=w+c,p&&(
      c<p?o[d=1,g='\\ ',r+=2]||o.push(v,v)
      :c>p?(d=-1,g='/ ',r?r-=2:o=[v,v,...o]):x='-'+c,
      o=o.map((o,i)=>o+(i-r?i-r+d?b:g:x),v+=b)
    ),p=c)
  ,v=w=' ',o=[z[p=r=0]],b=w+w)&&o.join`
`

Ungolfed=z=>
(
  v=' ',o=[z[0]],r=0,p='',
  [...z].map(c=>{
    if (p) {
      if (c < p) {
        if (! o[r+=2])
          o.push(v,v)
        o = o.map((o,i)=>o+(i==r ? ' '+c : i==r-1 ? '\\ ' : '  '))
      } else if (c > p) {
        if (r == 0)
          o = [v,v,...o]
        else
          r -= 2
        o = o.map((o,i)=>o+(i==r ? ' '+c : i==r+1 ? '/ ' : '  '))
      } else {
        o = o.map((o,i)=>o+(i==r ? '-'+c : '  '))
      }
      v += '  '
    }
    p = c
  }),
  o.join`\n`
)

out=x=>O.innerHTML+=x+'\n'

test = [
"Programming Puzzles & Code Golf",  
"-\\//-- \\ //- ",  
"3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679"]

test.forEach(t=>out(t+"\n"+f(t)))
<pre id=O></pre>


2

सीजेएम, 79 बाइट्स

l__0=\+2ew::-:g_0\{+_}%);_$0=fm2f*_$W=)S*:E;]z{~\_)"/-\\"=2$@-E\@t@@E\@t}%(;zN*

इसे ऑनलाइन आज़माएं

यह आउटपुट कॉलम को कॉलम द्वारा बनाता है, और परिणाम को पंक्ति द्वारा पंक्ति में लाने के लिए अंत में परिणाम भेजता है। यह कुल मिलाकर काफी दर्दनाक था।

स्पष्टीकरण:

l__   Get input and create a couple of copies.
0=\+  Prepend copy of first letter, since the following code works only with
      at least two letters.
2ew   Make list with pairs of letters.
::-   Calculate differences between pairs...
:g    ... and the sign of the differences.
_0\   Prepare for calculating partial sums of signs by copying list and
      pushing start value 0.
{     Loop for calculating partial sums.
  +_    Add value to sum, and copy for next iteration.
}%    End of loop for partial sums. We got a list of all positions now.
);    Pop off extra copy of last value.
_$0=  Get smallest value.
fm    Subtract smallest value to get 0-based positions for letters.
2f*   Multiply them by 2, since the offsets between letters are 2.
_$W=  Get largest position.
)     Increment by 1 to get height of result.
S*    Build an empty column.
:E;   Store it in variable E.
]     We got the input string, list of relative offsets, and list of
      absolute positions now. Wrap these 3 lists...
z     ... and transpose to get triplets of [letter offset position].
{     Loop over triplets.
  ~     Unwrap triplet.
  \     Swap offset to front.
  _)    Copy and increment so that offset is in range 0-2.
  "/-\\"  List of connection characters ordered by offset.
  =     Pick out connection character for offset.
  2$@   Get position and copy of offset to top.
  -     Subtract to get position of connection character.
  E     Empty column.
  \@    Shuffle position and character back to top. Yes, this is awkward.
  t     Place connection character in empty line. Done with first column.
  @@    Shuffle letter and position to top.
  E     Empty column.
  \@    Stack shuffling again to get things in the right order.
  t     Place letter in empty line. Done with second column.
}%    End of main loop for triplets.
(;    Pop off first column, which is an extra connector.
z     Transpose the whole thing for output by row.
N*    Join with newlines.

1

पर्ल 5, 230 214

@A=split(//,pop);$y=$m=256;map{$c=ord$_;$d=$c<=>$p;$t=$d>0?'/':$d<0?'\\':'-';$B[$x++][$y-=$d]=$t;$B[$x++][$y-=$d]=$_;$m=$y,if$m>$y;$M=$y,if$M<$y;$p=$c}@A;for$j($m..$M){for$i(1..$x){$s.=$B[$i][$j]||$"}$s.=$/}print$s

परीक्षा

$ perl zigzag.pl "zigge zagge hoi hoi hoi"
z
 \
  i
   \
    g-g
       \
        e   z   g-g       o       o       o
         \ / \ /   \     / \     / \     / \
              a     e   h   i   h   i   h   i
                     \ /     \ /     \ /


$ 

1

के, 86६

{-1@+((d#\:" "),'1_,/("\\-/"1+e),'x)@\:!|/d:(|/b)+-:b:1_+\,/2#'e:{(x>0)-x<0}@-':6h$x;}  

k){-1@+((d#\:" "),'1_,/("\\-/"1+e),'x)@\:!|/d:(|/b)+-:b:1_+\,/2#'e:{(x>0)-x<0}@-':6h$x;} "Programming Puzzles & Code Golf"
                                                        o
                                                       / \
  r                         z-z               o   e   G   l
 / \                       /   \             / \ / \ /     \
P   o   r   m-m   n       u     l   s   &   C   d           f
     \ / \ /   \ / \     /       \ / \ / \ /
      g   a     i   g   P         e
                     \ /

Ungolfed:

f:{
    dir:{(x>0)-x<0}-':[*a;a:"i"$x];          //directional moves (-1, 0, 1)
    chars:1_,/("\\-/"1+dir),'x;              //array of input string combined with directional indicators
    depths:(|/b)+-:b:1_+\,/2#'dir;           //depth for each char, normalised to start at 0
    -1@+((depths#\:" "),'chars)@\:!|/depths; //Pad each character by the calculated depths, extend each string to a uniform length and transpose
    }

1

रूबी, 158

हिस्टोक्रेट की बदौलत 6 बाइट्स बचाए । धन्यवाद!

->s,*i{i[x=n=k=(4*m=s=~/$/).times{i<<'  '*m}/2][j=0]=l=s[/./]
$'.chars{|c|i[k-=d=c<=>l][j+1]=%w{- / \\}[d]
i[k-=d][j+=2]=l=c
n,x=[x,n,k].minmax}
puts i[n..x]}

1
आप का उपयोग कर एक खाली सरणी के लिए सेट कर सकते हैं ->s,*i{। और अगर आप के s[0]साथ प्रतिस्थापित करते हैं s[/./], मुझे लगता है कि आप के s[1..-1]साथ बदल सकते हैं $'
हिस्टोक्रेट

@ हिस्टोक्रेट ग्रेट! धन्यवाद! मुझे लगा कि आपको मल्टी-परम लैम्ब्डा घोषणाओं के लिए पार्न्स की आवश्यकता है, लेकिन जाहिर है कि यह केवल जेएस है।
नहीं कि चार्ल्स

0

नम्पी के साथ पायथन: 218 बाइट्स

यह सुन्न आयात करने के लिए 19 बाइट बर्बाद करने के लिए लायक है।

golfed:

from numpy import*
z=zip
r=raw_input()
s=sign(diff(map(ord,r[0]+r)))
c=cumsum(s)
p=2*(max(c)-c)+1
for L in z(*[c.rjust(i).ljust(max(p))for _ in z(z(p+s,array(list('-/\\'))[s]),z(p,r))for i,c in _][1:]):print''.join(L)

Ungolfed:

from numpy import *

letters = raw_input()
#letters = 'Programming Puzzles & Code Golf'
s = sign(diff(map(ord, letters[0] + letters)))
c = cumsum(s)
lines = array(list('-/\\'))[s]

letter_heights = 2 * (max(c) - c) + 1
line_heights = letter_heights + s

columns = [symbol.rjust(height).ljust(max(letter_heights))
    for pair in zip(                    # interleave two lists of (height, symbol) pairs...
        zip(line_heights,   lines),
        zip(letter_heights, letters)
    )
    for height, symbol in pair          # ... and flatten.
][1:]                                   # leave dummy '-' out
for row in zip(*columns):
    print ''.join(row)
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.