-7
\$\begingroup\$

Introduction

For as long as programmers have been programming and as long as English teachers have been teaching, there's a controversy over the datasets called Arrays and Lists (or hashes).

Arrays have almost always had their first index at 0. Which, for the programmer, is understandable because of math. But along the way, as more and more languages have appeared, and more and more people start to program, they can't wrap their mind around the array.

They know it's a table of indexes with values assigned to each index, but they don't know why it starts at 0, because 0 typically means nothing. 0 is a quantitative value that we don't see (or can, but mostly in context).

Task

It's simple: Make your arrays/list/hashes/whatever start at one

This doesn't require special tinkering to the compiler/interpreter. Make a user's array have their expected values at position 1.

Loopholes aren't allowed.

Examples

A basic example (written in Pseudo):

list = [1,2,3]
lis = [null]
value = 1
Loop list Through 3 Times as Index_Value
    lis[value] = Index_Value
    value += 1 
print(lis) // or return lis, doesn't matter

Rules

  1. No standard loopholes allowed
  2. Languages that have arrays/lists/hashes that start at one OR are configurable to have settable index numbers.
  3. Input will be given, you may expect a array. However, if the array requires certain types, assume the array is filled with integers. Basically, test for [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] (I do encourage any answers that aren't for competition)
  4. Output must be the fixed array. If the fixed array is assigned to value a, then a[1] == 1
  5. Anything can be in the a[0] index, the user will not care. Though, preferably, BUT NOT REQUIRED, the value should be null or some other comparable value.

This is , so the shortest answer in bytes wins.

\$\endgroup\$

closed as unclear what you're asking by Jo King, NoOneIsHere, Sriotchilism O'Zaic, betseg, LyricLy Mar 30 '18 at 6:10

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 6
    \$\begingroup\$ So... a function that takes an array and prepends 0/null? \$\endgroup\$ – Jo King Mar 30 '18 at 3:57
  • 2
    \$\begingroup\$ There seems to be some "close-as-unclear-without-comment" recently. Anyway -- 1 "Languages that have arrays/lists/hashes that start at one OR are configurable to have settable index numbers." What do you want to say? That's a noun, not a sentence. 2 Assuming language features is obviously unclear. Not every programming languages have array or index. \$\endgroup\$ – user202729 Mar 30 '18 at 9:49
  • \$\begingroup\$ I can't see anything in the question which would rule out a 0-byte answer in any language which has arrays. \$\endgroup\$ – Peter Taylor Mar 30 '18 at 14:02
4
\$\begingroup\$

Haskell, 4 bytes

(0:)

Try it online!

Just prepends 0.

data List a = Cons a (List a) | Emp
    deriving (Eq)

instance Show a => Show (List a) where
    show Emp            = "[]"
    show (Cons x Emp)   = "[" ++ show x ++ "]"
    show (Cons x xs)    = "[" ++ show x ++ ", " ++ tail (show xs)

len              :: Integral b => List a -> b
len Emp           = 0
len (Cons x xs)   = succ $ len xs

pre              :: a -> List a -> List a
pre               = Cons

app              :: a -> List a -> List a
app x Emp         = pre x Emp
app x (Cons y ys) = pre y $ app x ys

(!)              :: Integral b => List a -> b -> a
Emp ! i           = error "index too large"
(Cons x xs) ! 1   = x
(Cons x xs) ! i   = xs ! (pred i)

Try it online!

\$\endgroup\$
2
\$\begingroup\$

C Macro, 17 bytes

#define F(t)(t-1)
\$\endgroup\$
1
\$\begingroup\$

Inform 7, 97 bytes

This challenge seems like one that Inform 7 could actually accomplish, despite its enormous verbosity!

To decide what list of numbers is f (L - a list of numbers): add 0 at entry 1 in L; decide on L.

Prepends a zero.

\$\endgroup\$
1
\$\begingroup\$

Java (OpenJDK 8), 67 bytes

a->{int c[]=new int[a.length+1],i=1;for(int u:a)c[i++]=u;return c;}

Try it online!

This was a fun little challenge in Java since Java arrays are static in size, but it seems like this is significantly less challenging in every language that can just slap a zero on for the first- excuse me, zeroth element

Explanation:

int i=1,c[]=new int[a.length+i];    ////Initialize counter and declare new array with length: 1 + input array length
for(int u:a)    //Loop through each element in input array
  c[i++]=u;    //Increment counter after shifting each element up one spot in the output array
return c;    //return new array starting at index 1

Credit:

-3 bytes thanks to Kevin Cruijssen!

\$\endgroup\$
  • 1
    \$\begingroup\$ -3 bytes by combining int and int[]: a->{int c[]=new int[a.length+1],i=1;for(int u:a)c[i++]=u;return c;} \$\endgroup\$ – Kevin Cruijssen Mar 30 '18 at 12:25
  • \$\begingroup\$ Too bad System.arraycopy is so verbose.. Try it online, 74 bytes. \$\endgroup\$ – Kevin Cruijssen Mar 30 '18 at 12:29
0
\$\begingroup\$

Python 3, 14 bytes

lambda s:[0]+s

Puts a zero at the start of the list. None would be a better null value but it's three bytes longer.

\$\endgroup\$
0
\$\begingroup\$

JavaScript, 10 bytes

a=>[,...a]

a[0] is a hole after fix

\$\endgroup\$
  • \$\begingroup\$ And any holes in the input are filled with undefined :P \$\endgroup\$ – ETHproductions Mar 30 '18 at 14:23
0
\$\begingroup\$

Perl 5 -p, 6 bytes

#!/usr/bin/perl -p
s//0 /

Try it online!

Prepends 0

The controversy mostly exist because people confuse counting and naming :-)

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.