| Bytes | Lang | Time | Link |
|---|---|---|---|
| 129 | PHP | 161007T162900Z | Jör |
| 348 | Racket | 161008T041737Z | rnso |
| 031 | MATL | 161007T212143Z | Luis Men |
| 033 | 05AB1E | 161007T175005Z | Emigna |
| 040 | APL | 161007T163806Z | marinus |
| 080 | JavaScript ES6 | 161007T110835Z | Arnauld |
PHP, 129 Bytes
12 Bytes saved by @Titus Thank You
string = $argv[1],ratio = $argv[2], direction = $argv[3] inward = 0 ,Outward =1
for($i=0;$i+1<2*$l=strlen($t=($x=$argv)[1]);)echo$i%2?str_pad("",$x[2]*abs($x[3]*(0^$l/2+1)-($i++>=$l?$l-$i/2:$i/2))):$t[$i++/2];
Racket 348 bytes
(define(f s)(let*((c #\space)(sp(λ(l)(define ol'())(for((i(length l)))(for((j i))
(set! ol(cons c ol)))(set! ol(cons(list-ref l i)ol)))(for((n(floor(/(length l)2))))
(set! ol(cons c ol)))ol))(m(floor(/(string-length s)2)))(s1(sp(string->list(substring s 0 m)
)))(s2(sp(reverse(string->list(substring s m))))))(list->string(append(reverse s1)s2))))
Ungolfed:
(define(f s)
(let* ((c #\space)
(sp (λ (l) ; subfn to add increasing spaces to list of characters
(define ol '())
(for ((i (length l)))
(for ((j i))
(set! ol (cons c ol)))
(set! ol (cons (list-ref l i)ol)))
(for ((n (floor(/ (length l)2))))
(set! ol (cons c ol)))
ol))
(m (floor (/ (string-length s) 2))) ; find midpoint
(s1 (sp (string->list (substring s 0 m)))) ; add spaces to first part
(s2 (sp (reverse (string->list (substring s m)))))) ; add spaces to second part
(list->string (append (reverse s1) s2)) ; re-combine 2 parts
))
Testing:
(f "INTERSTELLAR")
Output:
"I N T E R S T E L L A R"
MATL, 31 bytes
nq:tPvX<i?tX>Qw-]*kQ1whYs''1Gb(
Inputs are: string; 0 or 1 for inward or outward increasing; multiplier.
Explanation
Consider inputs 'INTERSTELLAR', 1, 0.5 as an example.
nq: % Input string implicitly. Push [1 2 ... N-1] where N is the string length
% STACK: [1 2 3 4 5 6 7 8 9 10 11]
tP % Duplicate, reverse
% STACK: [1 2 3 4 5 6 7 8 9 10 11], [11 10 9 8 7 6 5 4 3 2 1]
vX< % Vertically concatenate. Minimum of each column
% STACK: [1 2 3 4 5 6 5 4 3 2 1]
i % Input direction flag
% STACK: [1 2 3 4 5 6 5 4 3 2 1], 1
? % If input flag was 1 (meaning outward increasing)
tX> % Duplicate. Maximum
% STACK: [1 2 3 4 5 6 5 4 3 2 1], 6
Q % Add 1
% STACK: [1 2 3 4 5 6 5 4 3 2 1], 7
w- % Swap. Subtract
% STACK: [6 5 4 3 2 1 2 3 4 5 6]
] % End
*k % Input multiplier implicitly. Multiply. Round down
% STACK: [3 2 2 1 1 0 1 1 2 2 3]
Q % Add 1
% STACK: [4 3 3 2 2 1 2 2 3 3 4]
1wh % Prepend a 1
% STACK: [1 4 3 3 2 2 1 2 2 3 3 4]
Ys % Cumulative sum
% STACK: [1 5 8 11 13 15 16 18 20 23 26 30]
'' % Push empty string
% STACK: [1 5 8 11 13 15 16 18 20 23 26 30], ''
1G % Push input string again
% STACK: [1 5 8 11 13 15 16 18 20 23 26 30], '', 'INTERSTELLAR'
b % Bubble up
% STACK: '', 'INTERSTELLAR', [1 5 8 11 13 15 16 18 20 23 26 30]
( % Assign the characters from the top string into the empty string at the
% given positions. Intermediate positions are filled with character 0,
% which is displayed as a space
% STACK: 'I N T E R ST E L L A R'
% Dispaly implicitly
05AB1E, 33 bytes
Uses CP-1252 encoding.
Gap multiplier is taken as negative when outwards increasing.
g;>Î.S<_²**ÄU¹vyð²¹g<;N.S*X+DUï×J
APL, 40 bytes
{⍵\⍨0~⍨∊1,⍨1,¨-⌊⍺×(1+⌈/-+)⍣⍺⍺(⌽⌊+)⍳⍴1↓⍵}
This takes the string as its right argument, the ratio as its left argument and the direction as its left operand (0 for inward and 1 for outward).
1 (0 {⍵\⍨0~⍨∊1,⍨1,¨-⌊⍺×(1+⌈/-+)⍣⍺⍺(⌽⌊+)⍳⍴1↓⍵}) 'INTERSTELLAR'
I N T E R S T E L L A R
0.5 (0 {⍵\⍨0~⍨∊1,⍨1,¨-⌊⍺×(1+⌈/-+)⍣⍺⍺(⌽⌊+)⍳⍴1↓⍵}) 'INTERSTELLAR'
IN T E R S T E L L AR
2 (1 {⍵\⍨0~⍨∊1,⍨1,¨-⌊⍺×(1+⌈/-+)⍣⍺⍺(⌽⌊+)⍳⍴1↓⍵}) 'CODEGOLF'
C O D E G O L F
0.4 (1 {⍵\⍨0~⍨∊1,⍨1,¨-⌊⍺×(1+⌈/-+)⍣⍺⍺(⌽⌊+)⍳⍴1↓⍵}) 'CODEGOLF'
C O DEGO L F
Explanation:
⍳⍴1↓⍵: get a list of numbers from 1 to N-1, where N is the length of the string(⌽⌊+): invert the list, and at each position, get the lowest number of both lists (this gives the sizes of the gaps if increasing inwards)(1+⌈/-+)⍣⍺⍺: subtract each number in the list from the highest number in the list, and add 1. Do this⍺⍺times. (If⍺⍺=0, nothing will happen, and if⍺⍺=1, this will give the sizes of the gaps if increasing outwards.)-⌊⍺×: multiply each gap by⍺, round it downwards, and negate it.∊1,⍨1,¨: add a 1 in front of each gap, and a 1 at the very end of the list.0~⍨: remove any zeroes.⍵\⍨: use the resulting list to expand⍵. Expand (\) works in the following manner: for each positive number, the current character is replicated that many times, and for each negative number, that many spaces are inserted, with the caveat that0and¯1do the same thing, which is why all the zeroes had to be removed earlier.
JavaScript (ES6), 86 82 81 80 bytes
Input is expected in currying syntax f(s)(r), with:
s= stringr= ratio + direction: a negative float for inward or a positive float for outward
let f =
s=>r=>s.replace(/./g,(c,i)=>c+' '.repeat(n+=i<l?-r:r),l=s.length/2,n=r>0&&l*r+r)
console.log(f("INTERSTELLAR")(-1));
console.log(f("INTERSTELLAR")(-0.5));
console.log(f("CODEGOLF")(2));
console.log(f("CODEGOLF")(0.4));