Sinusoidal text

30

1

Goal: Write a program or function which prints an input string in a sinusoidal shape.

The ASCII sinusoid

Here is one period of the sinusoid:

         .......                                 
      ...       ...                              
    ..             ..                            
   .                 .                           
  .                   .                          
 .                     .                         
.                       .                       .
                         .                     . 
                          .                   .  
                           .                 .   
                            ..             ..    
                              ...       ...      
                                 .......         

Note that there is exactly one dot on each column.

  • Each character in the input string will replace a dot in the shape above, from left to right.
  • Spaces in the input have to be outputted like normal characters, in place of a dot.
  • The starting character corresponds to the leftmost dot in the figure above.
  • This is only one period, inputs can be longer than the number of dots above.

Input

  • Inputs are ASCII strings that contain only characters between ASCII decimal 32 (space) and ASCII decimal 126 (Tilde ~).
  • Inputs will always be one line only (no linebreaks).
  • Inputs can be taken via STDIN, function parameters, command line arguments, or anything similar.

Output

  • Output must be printed exactly like they are in the test cases given.
  • Trailing spaces on lines are allowed as long as the length of the line with those trailing spaces does not exceed the length of the longest line (the one with the last character on it).
  • No leading/trailing line allowed.

Test cases

  • Input: .................................................

Output:

         .......                                 
      ...       ...                              
    ..             ..                            
   .                 .                           
  .                   .                          
 .                     .                         
.                       .                       .
                         .                     . 
                          .                   .  
                           .                 .   
                            ..             ..    
                              ...       ...      
                                 .......         
  • Input: Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Output:

         ng Puzz                                         ion and                                         siasts                                          stratio           
      mmi       les                                   est        an                                   thu       and                                   egi       n r        
    ra              &                               qu             sw                               en              c                                r             eq      
   g                                                                 e                                               o                             o                 u     
  o                   C                           a                   r                           e                   d                           n                   i    
 r                     o                                                                         l                     e                                               r   
P                       d                       s                       s                       z                                               ,                       e  
                         e                     i                         i                     z                         g                     e                         d 
                                                                          t                   u                           o                   e                           .
                           G                 e                             e                 p                             l                 r                             
                            ol             ng                                f             g                                fe              f                              
                              f S       cha                                   or        min                                   rs.       00%                                
                                 tack Ex                                         program                                          It's 1                                   
  • Input: Short text.

Output:

         t.
      tex  
    t      
   r       
  o        
 h         
S          
  • Input: The quick brown fox jumps over the lazy dog

Output:

          brown                            
      ick       fox                        
    qu              j                      
                     u                     
  e                   m                    
 h                     p                   
T                       s                  

                          o                
                           v               
                            er             
                               th       dog
                                 e lazy    

Scoring

This is , so the shortest program or function in bytes wins.

Fatalize

Posted 2015-07-26T11:45:38.940

Reputation: 32 976

This is what I was thinking of – Beta Decay – 2015-07-26T12:52:45.700

Oh I see it is indeed somewhat similar. – Fatalize – 2015-07-26T12:56:09.257

Incidentally, your wave is not quite sinusoidal. (Naturally I tried using a sin function to reproduce it but the positions are a little off.) – David Z – 2015-07-27T11:09:03.663

@DavidZ That doesn't surprise me, I eyeballed the ASCII shape. Can you even get something sinusoidal without any "gap" in the columns (i.e. exactly one dot per column)? – Fatalize – 2015-07-27T11:11:06.980

Sure, it's like plotting: you can calculate the height of a sine wave at each 1/48th of a period, round it, and put the letter at that height. It only differs from yours by one spot in each of a few columns. – David Z – 2015-07-27T11:14:02.067

Taking sin(x*pi/24)^a*b and rounding away from infinity generates the right wave for some 0 < a < 1 and 5 < b < 6! – Lynn – 2015-07-27T13:39:01.807

Also related

– Digital Trauma – 2015-07-27T15:49:35.910

4I spent a couple minutes entertaining myself by moving the scroll bar on test case 2's output back and forth really fast. – mbomb007 – 2015-07-27T16:41:53.410

What should you output for the input "Short"? Should there be two leading newlines? – FryAmTheEggman – 2015-07-27T21:39:07.337

@FryAmTheEggman That would be acceptable I guess. This edge case is not in the test cases so I can't change the rules since there are already a lot of answers. – Fatalize – 2015-07-27T21:41:15.287

Answers

7

Pyth, 59 bytes (57 characters)

Xjb.sC.>V+R*12\ Xz\ C9*+-L12K+JsM._+6jC\཈2tP_JKlz]*dlzC9d

Demonstration.

A binary lookup table is encoded inside , which has value 3912. This is converted to binary, giving [1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0]. This is treated as the differences between consecutive heights. By prepending a 6, forming all prefixes and mapping each to its sum, the first quarter of the wave is generated.

sM._+6jC\཈2 evaluates to [6, 7, 8, 9, 10, 10, 11, 11, 11, 12, 12, 12, 12] as described above. Then, the code concatenates on the reverse of this string to form the first half of the wave, and then subtracts it from 12 to give the entire wave.

Then, we form lines of each input character followed by 12 spaces. This line is rotated to the right by the wave height parameter corresponding to that location, and then the lines are transposed and joined on newlines.

Then, we strip off leading and trailing blank lines. However, we can't strip off leading or trailing blank lines that have spaces from the input. This is implemented by replacing spaces in the input with tabs (C9), which can't be in the input, stripping blank lines, and turning the tabs back into spaces.

isaacg

Posted 2015-07-26T11:45:38.940

Reputation: 39 268

@FryAmTheEggman Fixed, at the cost of 16 bytes. – isaacg – 2015-07-28T00:18:29.690

12

Python 2, 156 bytes

l=map(int,"654322111%08d1122345"%1);l+=[12-c for c in l]
def f(t):
 h=len(t);o=bytearray(' '*h+'\n')*13;i=0
 for c in t:o[i-~h*l[i%48]]=c;i+=1
 print o[:-1]

Explanation

  • The whole code simply makes a block of spaces (o) and replaces the right spaces with the letters of the input t.

  • The variable l stores a list of offsets from the top. So that the nth character of t should be on line l[n].

  • The bytearray o serves as a mutable string, since strings are immutable in python.

  • -~h is the same as h+1 but saves space because I don't need parentheses.

Alex L

Posted 2015-07-26T11:45:38.940

Reputation: 761

7

Java, 219 209 199 bytes

void p(char[]s){int r=6,c;String t="";for(;r>-7;r--,t+='\n')for(c=0;c<s.length;c++)t+=(s(c%48)==r?s[c]:' ');System.out.println(t);}int s(int a){return a<4?a:a<6?4:a<9?5:a<15?6:a<24?s(24-a):-s(a-24);}

I'm still a newbie here, and hope that it is compliant to the rules to introduce a sub-function (when the bytes of this function are counted, of course). If not, I'll try to convert the sin function into some clever array lookup...

public class SinusText
{
    public static void main(String[] args)
    {
        SinusText s = new SinusText();
        s.p(".................................................".toCharArray());
        s.p("Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.".toCharArray());
        s.p("Short text.".toCharArray());
        s.p("The quick brown fox jumps over the lazy dog".toCharArray());
    }
    void p(char[]s){int r=6,c;String t="";for(;r>-7;r--,t+='\n')for(c=0;c<s.length;c++)t+=(s(c%48)==r?s[c]:' ');System.out.println(t);}int s(int a){return a<4?a:a<6?4:a<9?5:a<15?6:a<24?s(24-a):-s(a-24);}
}

Marco13

Posted 2015-07-26T11:45:38.940

Reputation: 1 131

1For some string-based challenges it also saves if you take input as a char[]. Here, it would get rid of the () on length and eliminate charAt() as well. If I'm reading it right, you can also use print() instead of println() to save a couple more. – Geobits – 2015-07-27T05:21:54.543

@Geobits These are the degrees of freedom that I was not aware of. The task description talks about a "String", so I thought that it had to be "THE" String representation of the respective language. ... – Marco13 – 2015-07-27T08:35:53.893

Yea, I asked about it on meta some time back. Here's a link for reference: http://meta.codegolf.stackexchange.com/q/2214/14215

– Geobits – 2015-07-27T08:37:29.990

Thanks, it's 209 then. (Maybe I'll try to squeeze out a few more bytes later. The "sin" function still looks too verbose...) – Marco13 – 2015-07-27T08:41:58.897

1Hmm, not a huge improvement, but you can cut 10 by doing the whole thing modulus 48. Change the end to ...a<24?s(24-a):-s(a-24); and call it with s(c%48). – Geobits – 2015-07-27T14:28:51.017

Yes, with things like that. 10 bytes are not much absolutely, but relatively ;-) I thought that it should also be possible to exploit the symmetry of the curve (actually, one only needs the "shape" of the first quarter of the curve!), but I'll have to fiddle around a bit more here to get this right... – Marco13 – 2015-07-27T15:59:33.580

4

Perl, 222 bytes

$n[$_%13].=substr$l[$_/13],$_%13,1for 0..13*(@l=map{(map{sprintf"%013b",$_}@t=(64,128,256,512,(1024)x2,(2048)x3),(4096)x7,reverse@u=(32,16,8,4,4,2,2,2),(1)x7,(reverse@u),@t)[$-++%48]=~s/./$&?$_:$"/egr}<>=~/./g);$,=$/;say@n

Requires -E for say, stores the positions as integers cast to binary numbers and the flipping of the array is probably not very efficient bytes-wise. Also I'm sure there's plenty of savings to make, so I'll keep poking and prodding.

Example output:

$perl -E '$n[$_%13].=substr$l[$_/13],$_%13,1for 0..13*(@l=map{(map{sprintf"%013b",$_}@t=(64,128,256,512,(1024)x2,(2048)x3),(4096)x7,reverse@u=(32,16,8,4,4,2,2,2),(1)x7,(reverse@u),@t)[$-++%48]=~s/./$&?$_:$"/egr}<>=~/./g);$,=$/;say@n' <<< '.................................................'
         .......                                 
      ...       ...                              
    ..             ..                            
   .                 .                           
  .                   .                          
 .                     .                         
.                       .                       .
                         .                     . 
                          .                   .  
                           .                 .   
                            ..             ..    
                              ...       ...      
                                 .......         

$perl -E '$n[$_%13].=substr$l[$_/13],$_%13,1for 0..13*(@l=map{(map{sprintf"%013b",$_}@t=(64,128,256,512,(1024)x2,(2048)x3),(4096)x7,reverse@u=(32,16,8,4,4,2,2,2),(1)x7,(reverse@u),@t)[$-++%48]=~s/./$&?$_:$"/egr}<>=~/./g);$,=$/;say@n' <<< 'Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It'\''s 100% free, no registration required.'
         ng Puzz                                         ion and                                         siasts                                          stratio           
      mmi       les                                   est        an                                   thu       and                                   egi       n r        
    ra              &                               qu             sw                               en              c                                r             eq      
   g                                                                 e                                               o                             o                 u     
  o                   C                           a                   r                           e                   d                           n                   i    
 r                     o                                                                         l                     e                                               r   
P                       d                       s                       s                       z                                               ,                       e  
                         e                     i                         i                     z                         g                     e                         d 
                                                                          t                   u                           o                   e                           .
                           G                 e                             e                 p                             l                 r                             
                            ol             ng                                f             g                                fe              f                              
                              f S       cha                                   or        min                                   rs.       00%                                
                                 tack Ex                                         program                                          It's 1                                   

Dom Hastings

Posted 2015-07-26T11:45:38.940

Reputation: 16 415

3

JavaScript, 251 243 224 220 217

Really simple implementation: it uses a string of characters to represent the y-position of each character on the wave (offset by a, which is ASCII code 97). It then iterates through all possible rows; if the y-value of the current row is the same as the y-position on the wave, it writes a character from the string. There's also a cleanup at the end to remove the row if it turned out to be completely blank.

Note that the output will appear wonky in the alert() window if it's not using a monospaced font, you can change it to console.log() to verify the output is correct.

s=prompt(o=[])
for(y=i=0;y<13;++y){o[i]=""
for(x=0;x<s.length;++x)o[i]+=y=="gfedccbbbaaaaaaabbbccdefghijkklllmmmmmmmlllkkjih".charCodeAt(x%48)-97?s[x]:" "
if(o[i++].trim().length<1)o.splice(--i,1)}
alert(o.join("\n"))

EDIT1: ++ and -- exist.

EDIT2: Blank line removal is now done in the same loop as the rest, saving 17 characters. Didn't need those brackets either, for an extra 2 characters.

EDIT3: No need to declare the waveform as a variable, saving 4 characters.

EDIT4: As pointed out by Dom Hastings in the comments, the byte count included the carriage return as well as the newline character, updated the byte counts for all revisions to exclude the carriage return.

EDIT5: Saved 3 bytes courtesy of Dom Hastings. I haven't implemented the o.splice fix as this fails to remove the blank lines (on my end, at least).

Sean Latham

Posted 2015-07-26T11:45:38.940

Reputation: 1 423

1Nice work! A few places you can save a couple more bytes: Replace: if(o[i++].trim().length<1)o.splice(--i,1) with o.splice(i-(t=!o[i++].match(/\s/)),t), for -4, s=prompt() o=[] with: s=prompt(o=[]), -1 and for(y=0,i=0;y<13;++y){o[i]="" with for(y=i=0;y<13;++y){o[i]="", -2. It's probably possible to combine your for loops too to save more... One last thing, it's worth noting as well that I only have 220 for your current byte count, so your 225 might be windows \r\n instead of just \n which I assume you can ignore (please correct me if I'm wrong)... – Dom Hastings – 2015-07-27T14:53:16.417

Good catch on the carriage return! Next time I won't trust Notepad++ so much :) – Sean Latham – 2015-07-27T16:05:28.160

I think I have it reduced to 166. Can anyone else verify? I changed the array behavior to a log throughout the program. i used a short circuit instead of an if statement, and got rid of the brackets by putting the log at the end of the first for loop. for(s=prompt(),y=0;y<13;y++,v.trim()&&console.log(v))for(v="",x=0;x<s.length;x++)v+=y=="gfedccbbbaaaaaaabbbccdefghijkklllmmmmmmmlllkkjih".charCodeAt(x%48)-97?s[x]:" " – Vartan – 2015-07-28T21:25:37.853

using falsy zero to replace == with subtraction, 165 char

for(s=prompt(y=0);y<13;y++,v.trim()&&console.log(v))for(v="",x=0;x<s.length;x++)v+="gfedccbbbaaaaaaabbbccdefghijkklllmmmmmmmlllkkjih".charCodeAt(x%48)-97-y?" ":s[x] – Vartan – 2015-07-28T21:52:56.733

I'm having trouble pasting it from my comment so heres a pastebin http://bit.ly/1VQgGXw 217->166 = 76%

– Vartan – 2015-07-30T22:12:00.257

Good work, though I think you should post it as your own answer considering how different it is from my implementation! – Sean Latham – 2015-07-31T08:47:07.960

3

Matlab, 133, 130 bytes

The one liner:

s=input('');y=ceil(5.6*sin(0:pi/24:pi-.1).^.9);l=[-y y]+7;n=numel(s);t=repmat(' ',13,n);for k=1:n;t(l(mod(k-1,48)+1),k)=s(k);end;t

And the expanded version:

function f(s)
    y=ceil(5.6*sin(0:pi/24:pi-.1).^.9);l=[-y y]+7;  %// calculate the line number for each column position
    n=numel(s);                                     %// number of character in input
    t=repmat(' ',13,n);                             %// Create a blank canvas of whitespace characters
    for k=1:n
        t(l(mod(k-1,48)+1),k)=s(k);                 %// place each input character where it should be
    end
    t                                               %// force the output display

The one liner takes input from the console (stdin) and is 130 bytes. The expanded version replace the console input by a function definition (+1 byte) but is much more comfortable to use for the test case in a loop:


Description:

The line index of each character is calculated for a half period, then mirrored and concatenated to have a full period.
We create a blank background of whitespace character (same length as the input string. We place each character according to its position in the relevant line. If the input string is longer than one period, the mod (modulo) operator wraps that so we don't get out of bound when requesting the line number.


Test case:

Save the function version under textsine.m in your path, then run:

s = {'.................................................';...
    'Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It''s 100% free, no registration required.';...
    'Short text.';...
    'The quick brown fox jumps over the lazy dog'};

for txtcase=1:4
    textsine(s{txtcase,1})
end

will output:

t =

         .......                                 
      ...       ...                              
    ..             ..                            
   .                 .                           
  .                   .                          
 .                     .                         
.                       .                       .
                         .                     . 
                          .                   .  
                           .                 .   
                            ..             ..    
                              ...       ...      
                                 .......         


t =

         ng Puzz                                         ion and                                         siasts                                          stratio           
      mmi       les                                   est        an                                   thu       and                                   egi       n r        
    ra              &                               qu             sw                               en              c                                r             eq      
   g                                                                 e                                               o                             o                 u     
  o                   C                           a                   r                           e                   d                           n                   i    
 r                     o                                                                         l                     e                                               r   
P                       d                       s                       s                       z                                               ,                       e  
                         e                     i                         i                     z                         g                     e                         d 
                                                                          t                   u                           o                   e                           .
                           G                 e                             e                 p                             l                 r                             
                            ol             ng                                f             g                                fe              f                              
                              f S       cha                                   or        min                                   rs.       00%                                
                                 tack Ex                                         program                                          It's 1                                   


t =

         t.
      tex  
    t      
   r       
  o        
 h         
S          








t =

          brown                            
      ick       fox                        
    qu              j                      
                     u                     
  e                   m                    
 h                     p                   
T                       s                  

                          o                
                           v               
                            er             
                               th       dog
                                 e lazy    

if you want to test the one liner version with input from stdin, your input has to be entered as one single string, so you'd have to enclose your input between ' characters. Example:

'Short text.'   %//   valid input
Short text.     %// INVALID input

Thanks Luis Mendo for shaving up 3 bytes ;-)

Hoki

Posted 2015-07-26T11:45:38.940

Reputation: 271

@LuisMendo, thanks for the 3 bytes saved :-). I explained how to input a proper string so the simple s=input(''); would still work. – Hoki – 2015-07-29T13:16:05.563

2

Scala 377 characters

first cut. probably can get a better formula to translate x to y

(s:String)⇒s.zipWithIndex.map(t⇒(t._1,t._2,t._2%48 match{
case i if i<5⇒6-i
case 5|19⇒2
case 6|7|8|16|17|18⇒1
case i if i<16⇒0
case i if i<29⇒i%20+2
case 29|43⇒10
case 30|31|32|40|41|42⇒11
case i if i<40⇒12
case i if i>43⇒10-i%44
})).groupBy(_._3).toSeq.map{case(y,xs)⇒(""→0/:xs.sortBy(_._2)){case((p,l),(c,x,_))⇒(p+" "*(x-l-1)+c)→x}._1→y}.sortBy(_._2).map(_._1).mkString("\n")

gilad hoch

Posted 2015-07-26T11:45:38.940

Reputation: 717

1

Common Lisp, 205 bytes

(lambda(s &aux p v o)(dotimes(r 13)(setf o 0 p v v(round(*(/ 24 pi)(+(asin(-(/ r 6)1))pi))))(when p(map()(lambda(c)(princ(if(some(lambda(k)(<= p(mod k 48)(1- v)))`(,o,(- 23 o)))c" "))(incf o))s)(terpri))))

Tests

See http://pastebin.com/raw.php?i=zZ520FTU

Remarks

Print the output line by line, computing the indices in the strings that should be printed using the inverse sine function asin. The output do not match exactly the expected inputs in the question, but since OP acknowledges that the example outputs are not real sine, I guess this is ok. At least, there is always only one character written for each column.

coredump

Posted 2015-07-26T11:45:38.940

Reputation: 6 292

1

Python 2, 172 bytes

This isn't as good as Alex L's answer, but it's pretty close. Takes input from standard input and works best in a .py file.

l=map(int,bin(9960000)[2:]);l+=[-c for c in l];s=6;o=[];i=9
for c in raw_input():b=[' ']*13;b[s]=c;o+=[b];s+=l[i%48];i+=1
print''.join(sum(zip(*o+['\n'*13])[::-1],())[:-1])

I decided to build the output transposed (each column is a row) and then transpose the result, since in python the transpose of a matrix is map(*m).

  • l: The binary representation of 9960000 (after chopping off the "0b" from bin) is 100101111111101001000000. This is the "step" of the sine wave each column, starting at the very last character of the very lowest point. I copy this list, negate each number, and tack it onto the end of itself to form what is effectively a derivative of the function.
  • s: This is the variable that keeps track of which row (column in the transpose) the next character gets inserted into.
  • o: End output, transposed
  • i: Keeps track of sine wave period. Starts at 9 since l is shifted slightly.

In the for loop, I create a list of 13 spaces (I was using bytearrays but lists of characters turn out to have a shorter print statement), then replace the sth character with the input character. Append b to the end of o, add the appropriate step to s, and increment i.

I had hoped the print statement would be as simple as \n'.join(*zip(o)), but no such luck. zip(*o+['\n'*13])[::-1] appends a column of newlines and then reverses and transposes the whole thing (without the reversing, the sine wave is upside down), sum(...,()) concatenates the tuples together into one tuple of characters, and then ''.join(...) concatenates the characters and prints it.

Other things I tried were making a 12-character array of spaces and inserting the new character into the appropriate place, and replacing l+=[-c for c in l]; with some sort of math with some sort of multiplication of 1 and -1 with the result of indexing into l, but nothing I could come up with ended up being shorter.

Alex Van Liew

Posted 2015-07-26T11:45:38.940

Reputation: 473

0

Mathematica, 131 bytes

i=[input string];c=Characters@i;l=Length@c;StringJoin@Riffle[StringJoin@@@SparseArray[Table[{7-Round[6 Sin[.13(x-1)]],x},{x,l}]->c,{13,l}," "],"\n"]

That's 131 characters, including the three for i=foo;. That seemed a reasonable way to take the input; I could have put it straight into the definition of c and saved a few strokes, but that feels unfair.

It's pretty straightforward -- almost even readable. It splits the string into a list of characters, and then puts those characters into a sparse array at the positions determined from the Table (any spot in the array not given a character defaults to a space). The lines are assembled separately, then newlines sprinkled between them. The final StringJoin stitches it all up.

NB: Like some other solutions, this may not actually be valid because it produces a real sinusoid rather than the (beautiful) handcrafted example.

Tests:

(*i=Programming Puzzles...*)
         ng Puzz                                          on and                                          iasts                                           tration          
       mi       le                                     sti       a                                      us      and                                     is        r        
     am           s                                   e           ns                                  th            c                                 eg           eq      
    r               &                               qu              we                              en               o                               r               u     
  og                  C                                               r                                               d                            o                  ir   
 r                     o                          a                                               e                    e                          n                     e  
P                       d                       s                       si                       l                       g                                               d 
                         e                     i                          t                    zz                         o                     ,                         .
                           G                                               e                  u                            lf                 ee                           
                            o               ge                               f               p                               e               r                             
                             lf           an                                  or           g                                  rs            f                              
                                St      ch                                       p      min                                     .        0%                                
                                  ack Ex                                          rogram                                          It's 10                                  
(*i=.... ...*)
         .......                                 
       ..       ..                               
     ..           ..                             
    .               .                            
  ..                 ..                          
 .                     .                         
.                       .                       .
                         ..                    . 
                           .                  .  
                            .               ..   
                             ..           ..     
                               ...      ..       
                                  ......         
(*i= Short text.*)
         t.
       ex  
      t    
    t      
  or       
 h         
S          





(*i=The quick...*)              
          brown                            
       ck       fo                         
     ui           x                        
    q               j                      
  e                  um                    
 h                     p                   
T                       s                  
                          o                
                           v               
                            e              
                             r            g
                               the      do 
                                   lazy    

hYPotenuser

Posted 2015-07-26T11:45:38.940

Reputation: 707