4
\$\begingroup\$

A math Olympiad will be held, and participants are being registered. The highest number of participants is 100. Each participant is given an ID number. It is given in a sequence like \$100, 97, 94, 91, 88, ...., 1\$, and when the first sequence is over, then \$99, 96, 93, 90, 87, ...., 3\$ sequence and so on.

Let's assume one of the participant's ID number is \$k\$ and he/she is the \$n^{th}\$ participant. Given the value of \$k\$, return the value of \$n\$.

Test Cases

59 -> 81
89 -> 71
16 -> 29
26 -> 92
63 -> 47
45 -> 53
91 -> 4
18 -> 62
19 -> 28

There will be no leading zero in input. Standard loopholes apply, shortest code wins. In output there can be trailing whitespace.

\$\endgroup\$
10
  • 3
    \$\begingroup\$ Can you specify how this sequence works? Also this would work better under standard sequence rules in my opinion. \$\endgroup\$ Apr 21 at 2:32
  • 5
    \$\begingroup\$ I doubt it. It's very easy anyway. \$\endgroup\$ Apr 21 at 2:36
  • 4
    \$\begingroup\$ These test cases are awful. They are extremely inconvenient for testing and there is a duplicate. Did you generate them randomly? \$\endgroup\$ Apr 21 at 2:45
  • 4
    \$\begingroup\$ Just so we're absolutely clear here, from what I can infer the third sequence id the final one and goes 98,95,...,2? Because if not then you need to specify it in the description. \$\endgroup\$
    – user100690
    Apr 21 at 5:18
  • 3
    \$\begingroup\$ 2 -> 100 should definitely be a test case (since it catches an edge-case if using a mod 100). \$\endgroup\$ Apr 21 at 16:27

16 Answers 16

8
\$\begingroup\$

Python 2, 23 bytes

lambda n:1-199*~n/3%~99

Try it online!

I kind-of just tried stuff until I got something that worked.

\$\endgroup\$
6
\$\begingroup\$

Python 2, 27 bytes

Some magic formula after a bit of trial and error.

lambda n:~-~n%3*33+34-~-n/3

Try it online!

\$\endgroup\$
6
\$\begingroup\$

Husk, 11 9 bytes

€ΣTC3ṫ100

Try it online! or Verify all tests

-2 bytes, borrowing Unrelated String's idea.

Explanation

€ΣTC3ṫ100
     ṫ100 [100..1]
   C3     slices of 3
  T       transpose
 Σ        join
€         index of input
\$\endgroup\$
5
\$\begingroup\$

Jelly, 9 bytes

ȷ2RUs3ZFi

Try it online!

   U         Reverse
  R          the inclusive range from 1 to
ȷ2           100.
    s3       Split it into slices of length 3,
      Z      zip the slices,
       F     flatten the columns,
        i    and find the index of the input in the resulting list.
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Right, idk how I didn't think of using zip. I was thinking it'd be convenient if there were a way to take each block of three and then go through them column-wise... \$\endgroup\$
    – hyper-neutrino
    Apr 21 at 3:25
5
\$\begingroup\$

JavaScript (V8), 33 20 bytes

Thanks to @tsh for -13

n=>101+~n%3*33-n/3|0

Try it online!

Old:

n=>(n=100-n)%3*33+n/3+1+!!(n%3)|0

Try it online!

I don't know why this works. Don't question it.

\$\endgroup\$
2
  • \$\begingroup\$ Do you need |0 at the end? \$\endgroup\$
    – radrow
    Apr 21 at 6:06
  • 1
    \$\begingroup\$ After some math simplify: Maybe n=>101+~n%3*33-n/3|0 \$\endgroup\$
    – tsh
    Apr 21 at 9:44
4
\$\begingroup\$

J, 28 27 26 24 bytes

(_,;(</.~3|])i._100)i.<:

Try it online!

-2 bytes thanks to Bubbler

\$\endgroup\$
1
  • 1
    \$\begingroup\$ 24 bytes using parenthesized noun expression. \$\endgroup\$
    – Bubbler
    Apr 21 at 4:08
4
\$\begingroup\$

Desmos, 30 32 31 bytes

ceil(mod(67.1-101\frac n3,100))

Try it here!

+2 bytes to account for edge case at 2
-1 byte thanks to Jonathan Allan!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ @JonathanAllan well, darn. I've always thought there should be an equivalent to \$\mod n\$ where multiples of \$n\$ go to \$n\$ instead of \$0\$, but no such operation exists... this workaround will have to do. \$\endgroup\$
    – hakr14
    Apr 21 at 17:56
  • 1
    \$\begingroup\$ @JonathanAllan brilliant, thanks! \$\endgroup\$
    – hakr14
    Apr 21 at 18:36
3
\$\begingroup\$

Pari/GP, 23 bytes

p(n)=-n\3+(1-n)%3*33+35

Try it online!

2 more bytes saved thanks to Dominic van Essen

\$\endgroup\$
5
  • \$\begingroup\$ Nice! Looks like you can swap -n+1 for n-1 to save 1 byte... \$\endgroup\$ Apr 21 at 16:20
  • \$\begingroup\$ No, I want a function f: f=(0,1,2) for n=(100,99,98). To do that I need to reverse the results given by n%3, making the results (2,0,1) instead of (1,0,2) and cycle them to the right, giving (0,1,2). But I clearly should have done (1-n) instead of (-n+1), is it too late to fix it? \$\endgroup\$
    – Joe Slater
    Apr 22 at 2:15
  • \$\begingroup\$ You're right: that was a typo by me. Of course not too late to fix it - many of us 'fix' our answers continuously (look at all the crossed-out previous byte scores)! Just click 'edit' below the post. If you like (there's no requirement), you can leave the old score of 26 crossed out by surrounding it with <s> & </s>... \$\endgroup\$ Apr 22 at 5:07
  • \$\begingroup\$ Done, thanks for the tip! \$\endgroup\$
    – Joe Slater
    Apr 22 at 6:09
  • \$\begingroup\$ Also: if you rearrange by pre-dividing 99 and 105 by 3, you'll save 2 more bytes (and end-up with the same formula that I used, but arriving by a different route!)... \$\endgroup\$ Apr 22 at 7:00
2
\$\begingroup\$

Jelly, 12 bytes

101ḶUẋ3m3ḟ0i

Try it online!

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

Python 3.8 (pre-release), 34 bytes

-8 bytes thanks to ovs!

lambda a:101-round(-~a%3*33.3+a/3)

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ [1,2,0][a%3] can be golfed to -~a%3* and int(...+.5) is the same as round(...). 34 bytes \$\endgroup\$
    – ovs
    Apr 21 at 6:46
2
\$\begingroup\$

Vyxal, 11 10 bytes

Thanks to @Razetime for -1 byte by writing 199 as ⁺b.

ꜝ⁺b*3ḭ₁N%⌐

Try it Online!

Vyxal port of @xnor's Python answer, with a bit of extra golfing.

Explanation:

            # Note: 'X' denotes current value.

            # Implicit input
ꜝ           # ( X + 1 ) * -1
 ⁺b*        # X * 199
    3ḭ      # X // 3
      ₁N%   # X % -100
         ⌐  # 1 - X
            # Implicit output
```
\$\endgroup\$
1
1
\$\begingroup\$

Charcoal, 18 bytes

I⊕÷﹪⁻²⁰¹×¹⁰¹N³⁰⁰¦³

Try it online! Link is to verbose version of code. Explanation: Implements the formula n=1+(201-101*k)%300/3, which I found through trial and error.

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

Japt, 13 bytes

Lõ Ôó3 c aU Ä
Lõ            // Create the range [1..100],
   Ô          // reverse it and
    ó3        // group by every third item.
       c      // Flatten the result, then
         aU Ä // return the index of input plus one.

Try it here.

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

R, 30 bytes

k=scan();35+-k%/%3+33*(1-k)%%3

Try it online!

There are already 12 other answers in various languages before I managed to post this, and - amazingly for what seems such a simple task - they mostly seem to use different formulas/strategies to each other, and all appear to be different to this one.

I suspect this means that if I go-through all the other approaches, I'll find that at least some of them will turn-out to be golfier than this one, though...

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

Arn -IF, 12 bytes

Found a few bugs while writing this, lol. This is 0-indexed (e.g. 59 -> 80), I'm not sure if that's okay so I can change the answer if it isn't (somebody asked in the comments but there was no response).

&»¨)¢►†3K1v&

Explained

Unpacked: e2.~::%3-1& .@

I see some answers explain like this:

  e2.~    Descending range from 100 to 1
  ::%3-1  Sort in chunks of 3
  & .@    Bind the transpose operator to the output

And others like this:

    e2   1 * 10 ^ 2 = 100
  .~     Descending range
::       Sort adjacently while the right is truthy
      _    The currently value
    %      Modulo
      3    Three
  -        Minus
    1      One
&        Bind
  .@     The transpose operation

So I might as well include both. Either way, there is an implicit :_ and :i surrounding the program. The space in & .@ is required because &. is a symbol in the language already. I admit the flags are a bit cheaty, so for anybody curious this would be 15 bytes without the two flags.

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

05AB1E, 11 bytes

тLR3ôζ˜þIk>

Port of @UnrelatedString's Jelly answer.

Try it online or verify the entire sequence.

Alternatively, a port of @xnor's Python answer (which only works in the legacy version of 05AB1E due to the negative modulo) would be 11 bytes as well:

$±Ƶ$*3÷т(%-

Try it online or verify the entire sequence.

Explanation:

тLR          # Push a list in the range [100,1]
   3ô        # Split it into parts of size 3
     ζ       # Zip/transpose the list, using a space as filler
      ˜      # Flatten this list of triplets
       þ     # Remove the spaces by only leaving digits
        Ik   # Get the (0-based) index of the input in this list
          >  # Increase it by 1 to make it a 1-based index
             # (after which the result is output implicitly)

$            # Push 1 and the input
 ±           # Get the Bitwise-NOT of that: -input-1
  Ƶ$*        # Multiply it by 199
     3÷      # Integer-divide it by 3
       т(%   # Modulo -100
$         -  # Subtract it from the 1 we pushed at the start
             # (after which the result is output implicitly)

See this 05AB1E tip of mine (section How to compress large integers?) to understand why Ƶ$ is 199.

\$\endgroup\$

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.