What is the best way to modify an array at a given index? – user1502040
That's a good question.
There's no direct way to assign a value to an array element in GolfScript, so, one way or another, you're going to have to rebuild the whole array.
The shortest general way I know to insert a new value x
at index i
in an array is to split the array at the given index and append x
to the first half before joining them together again:
.i<[x]+\i>+
(11 chars) - insert the value x
into array at (0-based) index i
To replace the value at index i
with x
, we just need to shorten the second half of the array by one element:
.i<[x]+\i)>+
(12 chars) - replace the element at (0-based) index i
with the value x
Alternatively, shortening the first half instead will effectively do the same, but with 1-based indexing, which may sometimes be preferable:
.i(<[x]+\i>+
(12 chars) - replace the element at (1-based) index i
with the value x
In all the examples above, if x
is a number, the square brackets around it may be omitted to save two characters, since it will be auto-coerced into an array by +
anyway:
.i<x+\i>+
(9 chars) - insert the number x
into array at (0-based) index i
.i<x+\i)>+
(10 chars) - replace the element at (0-based) index i
with the number x
.i(<x+\i>+
(10 chars) - replace the element at (1-based) index i
with the number x
The brackets may also be omitted if either x
or the input "array" (or both) are actually strings, in which case the result will also be coerced into a string (using the usual array → string conversion rules).
Ps. As a special case, if we know that the array has between i
and 2 × i
elements, we can insert a new element x
at the (0-based) index i
with i/[x]*
(6 chars). What this actually does is split the array into chunks of up to i
elements and insert x
between each chunk. Note that, in this case, the brackets are necessary even if x
is a number.
Pps. An alternative approach is to use dynamically named variables. For example,
'foo' 42 ':x'\+~
will assign the value 'foo'
to the variable x42
, while
42 'x'\+~
will retrieve it.
You can optimize this further by omitting the x
prefix and just assigning directly to the numeric literals — this is perfectly legal in GolfScript, and allows you to save one char from the assignment code and shorten the retrieval code to just `~
(or nothing at all, if the index is constant!). The down side, of course, is that assigning to a numeric literal will override the value of that literal anywhere else in your code. Often, though, the use of number literals can be avoided (or at least restricted to the beginning of the program, before any of them are reassigned), in which case this trick is perfectly fine.
Another question: is there a nice way to transform
... x
into... [x]
? The best I can see is[.;]
. – Peter Taylor – 2012-06-08T12:51:46.797@Peter: If
x
is a number, then[]+
works and is one char shorter. And of course, ifx
is the only thing on stack, then just]
will do. – Ilmari Karonen – 2012-06-09T12:32:26.567I'd like to ask the best ways to do: min, max, and absolute value. All my solutions seem to take way more characters than they should. – Claudiu – 2014-03-30T03:53:42.740
What is the best way to modify an array at a given index? – user1502040 – 2014-04-02T16:13:54.580
@user1502040: Answered below. (If anyone knows a better way, please share!)
– Ilmari Karonen – 2014-04-02T20:38:47.480