Skip to main content
golfed a couple bytes off program
Source Link
ETHproductions
  • 50.1k
  • 6
  • 94
  • 240

JavaScript (ES7), 83 80 78 77 bytes

s=>s.repeat(l=s.length).replace(/./g,_=>s[(c-=--i**.5-l&1||-1)%l],c=-1,i=l*l)

Bonus ES3-compliant program:

for(s=prompt(r=""),c=-1,l=i=sl=s.length;i--;length,i=l*l;i;)forr+=s[(j=i*2+1;jc-=l-;)r+=s[Math.sqrt(c+=li-i&1||-)&1||-1)%l];alert(r)

Explanation

This takes advantage of the fact that the output for e.g. a length 5 string can be represented as:

abcdeabcd cbaedcb cdeab aed e
012345678 7654321 23456 543 4

where each digit represents an index in the string (starting at 0), modulo the length of the string. In other words, if n is the length of the string, we increment the index 2n - 1 times, then decrement it 2(n - 1) - 1 times, then increment it 2(n - 2) - 1 times, etc. This can be simplified to the following algorithm:

  • Start the index i at -1.
  • For each integer x in the range [n2..1]:
    • If floor(sqrt(x)) is of the same parity (even/odd) as n, increment i.
    • Otherwise, decrement i.
    • Add the character at index i mod n to the output.

This works because floor(sqrt(x)) switches parities after 2n - 1 iterations, then 2(n - 1) - 1 iterations, etc.

JavaScript (ES7), 83 80 78 77 bytes

s=>s.repeat(l=s.length).replace(/./g,_=>s[(c-=--i**.5-l&1||-1)%l],c=-1,i=l*l)

Bonus ES3-compliant program:

for(s=prompt(r=""),c=-1,l=i=s.length;i--;)for(j=i*2+1;j--;)r+=s[(c+=l-i&1||-1)%l];alert(r)

Explanation

This takes advantage of the fact that the output for e.g. a length 5 string can be represented as:

abcdeabcd cbaedcb cdeab aed e
012345678 7654321 23456 543 4

where each digit represents an index in the string (starting at 0), modulo the length of the string. In other words, if n is the length of the string, we increment the index 2n - 1 times, then decrement it 2(n - 1) - 1 times, then increment it 2(n - 2) - 1 times, etc. This can be simplified to the following algorithm:

  • Start the index i at -1.
  • For each integer x in the range [n2..1]:
    • If floor(sqrt(x)) is of the same parity (even/odd) as n, increment i.
    • Otherwise, decrement i.
    • Add the character at index i mod n to the output.

This works because floor(sqrt(x)) switches parities after 2n - 1 iterations, then 2(n - 1) - 1 iterations, etc.

JavaScript (ES7), 83 80 78 77 bytes

s=>s.repeat(l=s.length).replace(/./g,_=>s[(c-=--i**.5-l&1||-1)%l],c=-1,i=l*l)

Bonus ES3-compliant program:

for(s=prompt(r=""),c=-1,l=s.length,i=l*l;i;)r+=s[(c-=l-Math.sqrt(i--)&1||-1)%l];alert(r)

Explanation

This takes advantage of the fact that the output for e.g. a length 5 string can be represented as:

abcdeabcd cbaedcb cdeab aed e
012345678 7654321 23456 543 4

where each digit represents an index in the string (starting at 0), modulo the length of the string. In other words, if n is the length of the string, we increment the index 2n - 1 times, then decrement it 2(n - 1) - 1 times, then increment it 2(n - 2) - 1 times, etc. This can be simplified to the following algorithm:

  • Start the index i at -1.
  • For each integer x in the range [n2..1]:
    • If floor(sqrt(x)) is of the same parity (even/odd) as n, increment i.
    • Otherwise, decrement i.
    • Add the character at index i mod n to the output.

This works because floor(sqrt(x)) switches parities after 2n - 1 iterations, then 2(n - 1) - 1 iterations, etc.

fixed mistake in explanation, golfed a byte
Source Link
ETHproductions
  • 50.1k
  • 6
  • 94
  • 240

JavaScript (ES7), 83 80 78 77 bytes

s=>s.repeat(l=s.length).replace(/./g,_=>s[(c-=--i**.5-l&1||-1)%l],c=-1,i=l*l)

Bonus ES3-compliant program:

for(s=prompt(r=""),c=-1,l=i=s.length;i--;)for(j=i*2+1;j--;)r+=s[(c+=(lc+=l-i)%2*2i&1||-1)%l];alert(r)

Explanation

This takes advantage of the fact that the output for e.g. a length 5 string can be represented as:

abcdeabcd cbaedcb cdeab aed e
012345678 7654321 23456 543 4

where each digit represents an index in the string (starting at 0), modulo the length of the string. In other words, if n is the length of the string, we increment the index 2n - 1 times, then decrement it 2(n - 1) - 1 times, then increment it 2(n - 2) - 1 times, etc. This can be simplified to the following algorithm:

  • Start the index i at -1.
  • For each integer x in the range [n2..1]:
    • If floor(sqrt(x)) is of the same parity (even/odd) as n, increment i.
    • Otherwise, decrement i.
    • Add the character at index i mod n to the output.

This works because floor(sqrt(x)) switches parities after 2n - 1 iterations, then 2(n - 1) - 1 iterations, etc.

JavaScript (ES7), 83 80 78 77 bytes

s=>s.repeat(l=s.length).replace(/./g,_=>s[(c-=--i**.5-l&1||-1)%l],c=-1,i=l*l)

Bonus ES3-compliant program:

for(s=prompt(r=""),c=-1,l=i=s.length;i--;)for(j=i*2+1;j--;)r+=s[(c+=(l-i)%2*2-1)%l];alert(r)

Explanation

This takes advantage of the fact that the output for e.g. a length 5 string can be represented as:

abcdeabcd cbaedcb cdeab aed e
012345678 7654321 23456 543 4

where each digit represents an index in the string (starting at 0), modulo the length of the string. In other words, if n is the length of the string, we increment the index 2n - 1 times, then decrement it 2(n - 1) - 1 times, then increment it 2(n - 2) - 1 times, etc. This can be simplified to the following algorithm:

  • Start the index i at -1.
  • For each integer x in the range [n2..1]:
    • If floor(sqrt(x)) is of the same parity (even/odd) as n, increment i.
    • Otherwise, decrement i.
    • Add the character at index i mod n to the output.

This works because floor(sqrt(x)) switches parities after 2n - 1 iterations, then 2(n - 1) - 1 iterations, etc.

JavaScript (ES7), 83 80 78 77 bytes

s=>s.repeat(l=s.length).replace(/./g,_=>s[(c-=--i**.5-l&1||-1)%l],c=-1,i=l*l)

Bonus ES3-compliant program:

for(s=prompt(r=""),c=-1,l=i=s.length;i--;)for(j=i*2+1;j--;)r+=s[(c+=l-i&1||-1)%l];alert(r)

Explanation

This takes advantage of the fact that the output for e.g. a length 5 string can be represented as:

abcdeabcd cbaedcb cdeab aed e
012345678 7654321 23456 543 4

where each digit represents an index in the string (starting at 0), modulo the length of the string. In other words, if n is the length of the string, we increment the index 2n - 1 times, then decrement it 2(n - 1) - 1 times, then increment it 2(n - 2) - 1 times, etc. This can be simplified to the following algorithm:

  • Start the index i at -1.
  • For each integer x in the range [n2..1]:
    • If floor(sqrt(x)) is of the same parity (even/odd) as n, increment i.
    • Otherwise, decrement i.
    • Add the character at index i mod n to the output.

This works because floor(sqrt(x)) switches parities after 2n - 1 iterations, then 2(n - 1) - 1 iterations, etc.

fixed mistake in explanation, golfed a byte
Source Link
ETHproductions
  • 50.1k
  • 6
  • 94
  • 240

JavaScript (ES7), 83 80 7878 77 bytes

s=>s.repeat(l=s.length).replace(/./g,_=>s[(c-=--i**.5-l&1?-l&1||-c%l:++c%l]1)%l],c=-1,i=l*l)

Bonus ES3-compliant program:

for(s=prompt(r=""),c=-1,l=i=s.length;i--;)for(j=i*2+1;j--;)r+=s[(c+=(l-i)%2*2-1)%l];alert(r)

Explanation

This takes advantage of the fact that the output for e.g. a length 5 string can be represented as:

abcdeabcd cbaedcb cdeab aed e
012345678 7654321 23456 543 4

where each digit represents an index in the string (starting at 0), modulo the length of the string. In other words, if n is the length of the string, we increment the index 2n - 1 times, then decrement it 2(n - 1) - 1 times, then increment it 2(n - 2) - 1 times, etc. This can be simplified to the following algorithm:

  • Start the index i at -1.
  • For each integer x in the range [n2..1]:
    • If floor(sqrt(x)) is of the same parity (even/odd) as xn, increment i.
    • Otherwise, decrement i.
    • Add the character at index i mod n to the output.

This works because floor(sqrt(x)) switches parities after 2n - 1 iterations, then 2(n - 1) - 1 iterations, etc.

JavaScript (ES7), 83 80 78 bytes

s=>s.repeat(l=s.length).replace(/./g,_=>s[--i**.5-l&1?--c%l:++c%l],c=-1,i=l*l)

Bonus ES3-compliant program:

for(s=prompt(r=""),c=-1,l=i=s.length;i--;)for(j=i*2+1;j--;)r+=s[(c+=(l-i)%2*2-1)%l];alert(r)

Explanation

This takes advantage of the fact that the output for e.g. a length 5 string can be represented as:

abcdeabcd cbaedcb cdeab aed e
012345678 7654321 23456 543 4

where each digit represents an index in the string (starting at 0), modulo the length of the string. In other words, if n is the length of the string, we increment the index 2n - 1 times, then decrement it 2(n - 1) - 1 times, then increment it 2(n - 2) - 1 times, etc. This can be simplified to the following algorithm:

  • Start the index i at -1.
  • For each integer x in the range [n2..1]:
    • If floor(sqrt(x)) is of the same parity (even/odd) as x, increment i.
    • Otherwise, decrement i.
    • Add the character at index i mod n to the output.

This works because floor(sqrt(x)) switches parities after 2n - 1 iterations, then 2(n - 1) - 1 iterations, etc.

JavaScript (ES7), 83 80 78 77 bytes

s=>s.repeat(l=s.length).replace(/./g,_=>s[(c-=--i**.5-l&1||-1)%l],c=-1,i=l*l)

Bonus ES3-compliant program:

for(s=prompt(r=""),c=-1,l=i=s.length;i--;)for(j=i*2+1;j--;)r+=s[(c+=(l-i)%2*2-1)%l];alert(r)

Explanation

This takes advantage of the fact that the output for e.g. a length 5 string can be represented as:

abcdeabcd cbaedcb cdeab aed e
012345678 7654321 23456 543 4

where each digit represents an index in the string (starting at 0), modulo the length of the string. In other words, if n is the length of the string, we increment the index 2n - 1 times, then decrement it 2(n - 1) - 1 times, then increment it 2(n - 2) - 1 times, etc. This can be simplified to the following algorithm:

  • Start the index i at -1.
  • For each integer x in the range [n2..1]:
    • If floor(sqrt(x)) is of the same parity (even/odd) as n, increment i.
    • Otherwise, decrement i.
    • Add the character at index i mod n to the output.

This works because floor(sqrt(x)) switches parities after 2n - 1 iterations, then 2(n - 1) - 1 iterations, etc.

added 801 characters in body
Source Link
ETHproductions
  • 50.1k
  • 6
  • 94
  • 240
Loading
golfed 3 bytes
Source Link
ETHproductions
  • 50.1k
  • 6
  • 94
  • 240
Loading
Source Link
ETHproductions
  • 50.1k
  • 6
  • 94
  • 240
Loading