25
\$\begingroup\$

A simple one: Take a list of positive integers as input and output the numbers modulus their 1-based index in the list.

If the input integers are {a, b, c, d, e, f, g} then the output should be {a%1, b%2, c%3, d%4, e%5, f%6, g%7} where % is the modulus operator.


Test cases:

10  9  8  7  6  5  4  3  2  1
 0  1  2  3  1  5  4  3  2  1

8 18  6 11 14  3 15 10  6 19 12  3  7  5  5 19 12 12 14  5
0  0  0  3  4  3  1  2  6  9  1  3  7  5  5  3 12 12 14  5

1
0

1  1
0  1
\$\endgroup\$

48 Answers 48

1
2
1
\$\begingroup\$

C, 52 bytes

i;f(n,l)int*l;{for(i=0;i++<n;)printf("%d ",*l++%i);}

Try it online!

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Ruby, 25 bytes

->x{x.map{|y|$.+=1;y%$.}}

Try it online!

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Perl 6, 7 bytes

*Z%1..*

Test it

Expanded:

*      # WhateverCode lambda (this is the parameter)
Z[%]   # zipped using infix modulus operator (&infix:«%»)
1 .. * # Range starting from 1 (doesn't stop)
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Common Lisp, 57 bytes

(defun m(l &aux(i 0))(mapcar(lambda(x)(mod x(incf i)))l))

Try it online!

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Python 3, 46 bytes

lambda x:[0]+[x[i]%i for i in range(1,len(x))]

Simples ;)

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Shouldn't it be range(1,len(x)+1)? But then you're better off doing range(len(x)) and doing lambda x:[x[i]%-~i for i in range(len(x))]. \$\endgroup\$ – Erik the Outgolfer Jun 15 '17 at 9:17
  • \$\begingroup\$ @EriktheOutgolfer Well you could do that if you want an IndexError... \$\endgroup\$ – Beta Decay Jun 15 '17 at 9:39
  • \$\begingroup\$ Does not raise any error for me. \$\endgroup\$ – Erik the Outgolfer Jun 15 '17 at 9:43
  • \$\begingroup\$ Oh, and your solution, including my golf, is also compatible with Python 2. \$\endgroup\$ – Erik the Outgolfer Jun 15 '17 at 9:44
0
\$\begingroup\$

Python, 41 bytes

f=lambda l,i=1:l and[l[0]%i]+f(l[1:],i+1)

Try it online!

A recursive solution. One byte longer than using enumerate. Other attempts:

f=lambda l,i=1:l and[l.pop(0)%i]+f(l,i+1)
f=lambda l,i=0:l[i:]and[l[i]%-~i]+f(l,i+1)
f=lambda l:l and f(l[:-1])+[l[-1]%len(l)]

The last one really wants to use pop but the order of evaluation is wrong.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Save five bytes by printing with def f(a,m=1):print a[m-1]%m;f(a,m+1), but I see that still does not beat your 35 byte full program solution. \$\endgroup\$ – Jonathan Allan Jun 14 '17 at 22:09
0
\$\begingroup\$

Go, 68 bytes

Go isn't hopelessly defeated (kinda is, but nvm that) because it has that nice range which is the equivalent of Python's enumerate().

func f(l[]int)(r[]int){for i,v:=range l{r=append(r,v%(i+1))};return}

Try it online!

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

PHP, 37 bytes

<?foreach($_GET as$v)echo$v%++$k." ";

Try it online!

Output separeted by an underscore

PHP, 35 bytes

<?foreach($_GET as$v)echo$v%++$k,_;

Try it online!

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Perl 6, 18 bytes

{map (*+1)R%*,.kv}

Accepts the list as its sole argument.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

k, 12 bytes

{(1+!#x)!'x}

Try it online.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Perl6, 22 21 bytes

say $_%++$ for @*ARGS

And a more readable version:

# Store initial index state
# The "++$" in the golf version increments an anonymous state variable for this.
my $index = 0;
# Iterate over the command-line arguments
for @*ARGS -> $number {
    # Increment the current index
    $index++;
    # Print out the result for this element
    say $number % $index;
}

Usage:

perl6 whatever-you-named-it.p6 [space-separated numbers go here]

Note that if we don't need to actually print the output, we can remove the initial say, saving 4 bytes to bring the total down to 17.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Aceto, 12 bytes

%pLI
ILOM
ri

It's a bit hard to see because there's no line break after the result is printed. Input is taken one number at a time, seperated by linebreaks.

Explanation:

Read a line and cast it to an integer:

ri

Load quick storage (initially an empty string) and increment it (autocasts to int; --> 1)

IL

Calculate and print modulo, load quick storage again, increment and save it:

%pLI
   M

Go back to the start:

  O

Example in- and output:

10
0
9
1
8
2
7
3
6
1
5
5
4
4
3
3
2
2
1
1

For one byte the output can look nicer:

%pLI
ILnM
riO
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Actually, 6 bytes

;ru@♀%

Try it online!

Explanation:

;ru@♀%
;ru     push [1, len(L)]
   @♀%  modulo of pairs from each list ([L[0]%1, L[1]%2, ...])
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Ruby, 40 bytes

->q{q.each_with_index.map{|n,i|n%(i+1)}}
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Swift 4, 18 bytes

zip(a,1...).map(%)
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Clojure, 25 bytes

#(map mod %(rest(range)))

(range) generates an infinite sequence of integers starting from 0, rest drops the first, and mapping with multiple sequences applies the operation to corresponding elements (and stops with the shorter sequence).

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

jq, 28 characters

to_entries[]|.value%(.key+1)

Sample run:

bash-4.4$ jq 'to_entries[]|.value%(.key+1)' <<< '[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]'
0
1
2
3
1
5
4
3
2
1

Try in jq‣play

For +5 characters you can enclose all the code in brackets and ask for compact output to get JSON output. But that not seems to be required.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

NewStack, 3 bytes

ᵢ%ṅ

The breakdown:

ᵢ    Append user's input to the stack
 %ṅ  Mod stack by 1-based index of each element
  
| improve this answer | |
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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