29
\$\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\$
0

58 Answers 58

12
\$\begingroup\$

Haskell, 20 bytes

($[1..]).zipWith mod

Try it online!

A trick to flip I learned from a golf of Anders Kaseorg.

\$\endgroup\$
11
\$\begingroup\$

Operation Flashpoint scripting language, 73 bytes

f={l=_this;r=[];i=0;while{i<count l}do{r=r+[(l select i)%(i+1)];i=i+1};r}

Call with:

numList = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1];
hint format["%1\n%2", numList, numList call f];

Output:

\$\endgroup\$
2
  • 2
    \$\begingroup\$ What... this is a thing? \$\endgroup\$
    – JAD
    Commented Jun 15, 2017 at 12:35
  • 3
    \$\begingroup\$ @JarkoDubbeldam Yes. The game allows players to create their own scenarios, and there is an in-game scripting language designed to supplement mission designing. However, since the language is Turing-complete, you can do pretty much whatever you want with it. \$\endgroup\$
    – Steadybox
    Commented Jun 15, 2017 at 13:22
9
\$\begingroup\$

Python 2, 35 bytes

i=1
for x in input():print x%i;i+=1

Try it online!

Counts the index up manually, as per a tip of mine.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ If you can exit with an error (I forget if that's the default) you can shave a couple bytes. \$\endgroup\$ Commented Jun 14, 2017 at 22:39
8
\$\begingroup\$

Jelly, 2 bytes

%J

Try it online!

Explanation:

%J
 J List 1 .. len(input). This is results in a list of the indexes.
%  Modulo.

Basically, the code modulos the original list by the list of indexes.

\$\endgroup\$
2
  • 2
    \$\begingroup\$ As soon as I saw this question, I thought "that's %J in Jelly, I wonder if anyone has answered with that answer?". I guess someone else had the same idea :-D \$\endgroup\$
    – user62131
    Commented Jun 14, 2017 at 21:49
  • 1
    \$\begingroup\$ @ais523 You think you were the only one? Think again! \$\endgroup\$ Commented Jun 15, 2017 at 9:10
6
\$\begingroup\$

R, 24 18 bytes

pryr::f(x%%seq(x))

Evaluates to the function:

function (x) 
x%%seq(x)

Which uses seq_along() to create a vector of the same length as x, starting at 1, and then %% to take the modulo.

Default behaviour of seq when presented with a vector is seq(along.with = x) which is the same output as seq_along(x), but 6 bytes shorter.

\$\endgroup\$
2
  • \$\begingroup\$ seq(x) is a handy thing to have around, since I'm always using 1:length(x). \$\endgroup\$
    – Giuseppe
    Commented Jun 15, 2017 at 14:08
  • \$\begingroup\$ @Giuseppe Yeah I was kinda surprised as well. \$\endgroup\$
    – JAD
    Commented Jun 15, 2017 at 14:13
6
\$\begingroup\$

R, 27 bytes

x=scan();cat(x%%1:sum(1|x))

saved 5 bytes thanks to @Jarko

saved 4 more thanks to @Giuseppe

saved 2 more thanks to @Taylor Scott

Saved 2 more thanks to @returnbull

\$\endgroup\$
4
  • \$\begingroup\$ 35 it is - removed unneeded last paren \$\endgroup\$
    – Zahiro Mor
    Commented Jun 15, 2017 at 13:47
  • 1
    \$\begingroup\$ you don't need the ' ' (space) at the end of cat; that's the default separator \$\endgroup\$
    – Giuseppe
    Commented Jun 15, 2017 at 13:51
  • 2
    \$\begingroup\$ you can drop 2 bytes to get 33 by reducing this down to x<-scan();cat(x%%1:length(x)," ") -- oh and a couple formatting tips, 1) you only need 4 spaces to the left of your code for it to be properly indented and marked 2) you can add a <!-- language-all: lang-r --> flag before your code to have it be highlighted (though this changes little in this example) 3) you do not need the brakets around your language's name 4) oh and you don't need to make a comment when you make edits to a post \$\endgroup\$ Commented Jun 15, 2017 at 13:57
  • 2
    \$\begingroup\$ (1) You can use = instead of <- to save a byte. (2) The specification says "output" rather than "print", so you can probably drop the cat(), saving 5 bytes. (3) sum(1|x) is one byte shorter than length(x). \$\endgroup\$
    – rturnbull
    Commented Jun 16, 2017 at 13:51
6
\$\begingroup\$

MATL, 4, 3 bytes

tf\

Try it online!

One byte saved thanks to @LuisMendo!

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

APL (Dyalog), 5 bytes

⍳∘≢|⊢

Try it online!

 the indices

 of

 the length of the argument

| that modulus

 the argument

\$\endgroup\$
3
  • \$\begingroup\$ Always amazed that a "mainstream" language can be so economical. The APL way seems naturally to be code golf: e.g. (~T∊T∘.×T)/T←1↓⍳R ⍝ primes up to R or life←{↑1 ω∨.∧3 4=+/,¯1 0 1∘.⊖¯1 0 1∘.⌽⊂ω} ⍝ Game of Life \$\endgroup\$
    – user15259
    Commented Jun 14, 2017 at 22:27
  • \$\begingroup\$ @YiminRong You can do better: Primes to R: (⊢~∘.×⍨)1↓⍳R and GoL (in Version 16.0): K∊⍨⊢∘⊂⌺3 3 where K is a constant. \$\endgroup\$
    – Adám
    Commented Jun 14, 2017 at 22:31
  • \$\begingroup\$ @YiminRong Try the primes finder here! \$\endgroup\$
    – Adám
    Commented Jun 14, 2017 at 22:32
5
\$\begingroup\$

Cubix, 19 bytes

;ww.1I!@s%Ow;)Sow.$

Try it online!

    ; w
    w .
1 I ! @ s % O w
; ) S o w . $ .
    . .
    . .

Watch It Run

A fairly straight forward implementation.

  • 1 push 1 to the stack to start the index
  • I!@ get the integer input and halt if 0
  • s%Ow swap the index up, mod, output result and change lane
  • ;) remove result and increment index
  • Sow push 32, output space and change lane (heading down from o)
  • $O jump the output
  • w;w change lange, remove 32 from stack and change lane onto the I input
\$\endgroup\$
5
\$\begingroup\$

05AB1E, 2 bytes

ā%

Try it online! or Try all tests

ā  # Push the range(1, len(a) + 1)
 % # Mod each element in the input by the same one in this list
\$\endgroup\$
6
  • \$\begingroup\$ Interesting, I thought it'd be like DgL%, nice. \$\endgroup\$ Commented Jun 16, 2017 at 21:07
  • \$\begingroup\$ @carusocomputing I originally had gL% because I forgot about ā. \$\endgroup\$
    – Riley
    Commented Jun 16, 2017 at 21:57
  • \$\begingroup\$ mind going a bit more in-depth on ā for me? I believe I've never used it is it just like for each but in a 1 to n+1 manner like vy<code>}) but implied vy<code>})? \$\endgroup\$ Commented Jun 16, 2017 at 21:58
  • \$\begingroup\$ @carusocomputing it pushes an array with values 1 to the length of the popped array. It's equivalent to gL. TIO \$\endgroup\$
    – Riley
    Commented Jun 16, 2017 at 22:04
  • \$\begingroup\$ Does it also dupe the input? Or is implicit input now extended automatically to the closest available input? \$\endgroup\$ Commented Jun 16, 2017 at 22:06
4
\$\begingroup\$

Mathematica, 22 bytes

#&@@@Mod~MapIndexed~#&

One more Mathematica approach.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ MapIndexed@Mod is almost good enough :'( \$\endgroup\$
    – user61980
    Commented Jun 14, 2017 at 22:39
4
\$\begingroup\$

Starry, 75 70 bytes

      +`  , + +   *    +  + +      +*   +    *  .               + + .'

Try it online!

Explanation

This is an infinite loop that keeps reading numbers from the input and increasing a counter initiallized at 1. For each pair of input and counter, the modulus is computed and printed.

To end the loop when input has been exhausted, the following trick is used. When no more input is available, trying to read one more number gives a 0. Thus, we divide the read number by itself, and if it is 0 the program ends with an error. Else we discard the result and continue.

      +              Push 1. This is the initial value of the counter
`                    Mark label
  ,                  Read number from input and push it. Gives 0 if no more input
 +                   Duplicate top of the stack
 +                   Duplicate top of the stack
   *                 Pop two numbers and push their division. Error if divisor is 0
    +                Pop (discard) top of the stack
  +                  Swap top two numbers
 +                   Duplicate top of the stack
      +              Push 1
*                    Pop two numbers and push their sum. This increases the counter
   +                 Rotate stack down, to move increased counter to bottom
    *                Pop two numbers and push their modulus
  .                  Pop a number and print it as a number
               +     Push 10
 +                   Duplicate top of the stack
 .                   Pop a number (10) and print it as ASCII character (newline)
'                    If top of the stack is non-zero (it is, namely 10) go to label
\$\endgroup\$
3
\$\begingroup\$

Brachylog, 9 bytes

l⟦₁;?↔z%ᵐ

Try it online!

Explanation

l⟦₁          [1, ..., length(Input)]
   ;?↔z      Zip the Input with that range
       %ᵐ    Map mod
\$\endgroup\$
3
\$\begingroup\$

R, 22 bytes

pryr::f(x%%1:sum(x|1))

R performs 1:length(x) before doing the modulus.

\$\endgroup\$
3
  • \$\begingroup\$ Nice find with sum(x|1)! \$\endgroup\$
    – JAD
    Commented Jun 15, 2017 at 12:51
  • 1
    \$\begingroup\$ Just found out that using seq() instead of seq_along() does the same thing. So that is a few bytes shorter again. \$\endgroup\$
    – JAD
    Commented Jun 15, 2017 at 13:02
  • 1
    \$\begingroup\$ I was going to tell you that, but I didn't have the rep to comment. Glad you figured it out. \$\endgroup\$
    – Shayne03
    Commented Jun 15, 2017 at 13:12
3
\$\begingroup\$

Itr, 5 bytes

#äL¹%

online interpreter

Explanation

#     ; read the list from standard input
 äL   ; push its length
   ¹  ; converted to 1-based range
    % ; point-wise modulo
      ; implicitly output list

Itr, 4 bytes

#L¹%

The ä before the L is no-longer necessary in newer versions

\$\endgroup\$
3
\$\begingroup\$

Japt -m, 5 4 3 bytes

uVÄ

Try it

-1 byte thanks to ETHproductions

\$\endgroup\$
2
  • 1
    \$\begingroup\$ I think you can save a byte with ®%°T (actually, you could still use Y there if you wanted) \$\endgroup\$ Commented Jun 15, 2017 at 11:00
  • \$\begingroup\$ Aha. Thanks, @ETHproductions. \$\endgroup\$
    – Shaggy
    Commented Jun 15, 2017 at 11:02
2
\$\begingroup\$

Python 2, 42 bytes

lambda l:[v%(i+1) for i,v in enumerate(l)]

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ You can remove the space before the for \$\endgroup\$
    – Beta Decay
    Commented Jun 14, 2017 at 21:19
  • \$\begingroup\$ 41 bytes: lambda l:[v%-~i for i,v in enumerate(l)] \$\endgroup\$
    – ovs
    Commented Jun 14, 2017 at 21:24
  • 2
    \$\begingroup\$ Or, lambda l:[v%i for i,v in enumerate(l,1)]. \$\endgroup\$
    – xnor
    Commented Jun 14, 2017 at 21:26
2
\$\begingroup\$

Haskell, 22 bytes

zipWith(flip mod)[1..]

Try it online!

Also: flip(zipWith mod)[1..].

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

Mathematica, 21 bytes

#~Mod~Range@Length@#&

Try it online!

or 20 bytes (by Martin)

#~Mod~Range@Tr[1^#]&
\$\endgroup\$
3
  • \$\begingroup\$ Tr[1^#] for Length@#. \$\endgroup\$ Commented Jun 14, 2017 at 21:33
  • \$\begingroup\$ that one doesn't work on mathics, so I'm keeping them both \$\endgroup\$
    – ZaMoC
    Commented Jun 14, 2017 at 21:40
  • \$\begingroup\$ You are missing a # as the second last character in your first answer. \$\endgroup\$
    – Ian Miller
    Commented Jun 15, 2017 at 0:26
2
\$\begingroup\$

Excel VBA, 59 46 Bytes

Golfed

Anonymous VBE Immediate window funtion that takes a space ( ) delimited array string as input from range [A1] and output the numbers modulus their 1-based index in the starting list to the VBE immediate window

For Each n In Split([A1]):i=i+1:?n Mod i;:Next

Input / Output:

[A1]="10 9 8 7 6 5 4 3 2 1" ''# or manually set the value
For Each n In Split([A1]):i=i+1:?n Mod i;:Next
 0  1  2  3  1  5  4  3  2  1 

Old Subroutine version

Subroutine that takes input as a passed array and outouts to the VBE immediate window.

Sub m(n)
For Each a In n
i=i+1
Debug.?a Mod i;
Next
End Sub

Input / Ouput:

m Array(10,9,8,7,6,5,4,3,2,1)
 0  1  2  3  1  5  4  3  2  1 

Ungolfed

Option Private Module
Option Compare Binary
Option Explicit
Option Base 0 ''# apparently Option Base 1 does not work with ParamArrays

Public Sub modIndex(ParamArray n() As Variant)
    Dim index As Integer
    For index = LBound(n) To UBound(n)
        Debug.Print n(index) Mod (index + 1);
    Next index
End Sub

Input / Output:

Call modIndex(10,9,8,7,6,5,4,3,2,1)
 0  1  2  3  1  5  4  3  2  1 
\$\endgroup\$
2
\$\begingroup\$

Thunno 2, 2 bytes

ż%

Try it online!

Explanation

ż%  # Implicit input
 $  # Vectorised modulo
ż   # List [1..length]
    # Implicit output
\$\endgroup\$
2
\$\begingroup\$

Rust, 46 bytes

|v|v.iter().zip(1..).map(|(n,i)|n%i).collect()

Rust Playground

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Welcome to Code Golf, and nice first answer! \$\endgroup\$
    – The Thonnu
    Commented Aug 4, 2023 at 5:47
1
\$\begingroup\$

CJam, 9 bytes

{_,,:).%}

Anonymous block that expects an array on the stack and replaces it by the output array.

Try it online!

Explanation

{       }    e# Define block
 _           e# Duplicate
  ,          e# Length
   ,         e# Range, 0-based
    :)       e# Add 1 to each entry
      .%     e# Vectorized modulus
\$\endgroup\$
1
\$\begingroup\$

J, 9 bytes

>:@i.@#|[

1 ... n | original list

| is mod

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

JavaScript (ES6), 22 bytes

a=>a.map((x,y)=>x%++y)
\$\endgroup\$
1
\$\begingroup\$

AWK, 13

{print $1%NR}

Try it online.

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

tcl, 35

lmap l $L {puts [expr $l%[incr i]]}

demo

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

GNU APL 1.2, 9 bytes

(⍳⍴R)|R←⎕

APL operates from right to left, hence the parentheses.

R←⎕ assigns user input to vector R.

⍴R gives the length of the vector; ⍳⍴R gives a vector with all numbers from 1 to that length (so the indices).

| is the mod operator (a|b yields b%a). APL operates on arrays, so the code snippet a vector containing each element from the user's input mod its index.

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

Pari/GP, 22 bytes

a->[a[n]%n|n<-[1..#a]]

Try it online!

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

Pyth, 5

.e%bh

Online test.

    hk     # 1-based index of (implicit) lambda variable
   b       # element
  %        # element mod (1-based index)
.e    Q    # enumerated map over (implicit) input
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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