# Is it a new highscore?

## Challenge:

In the language of your choice, write a program that generates a leaderboard with random scores and outputs it, then take a new score as input and check if it can be placed in the leaderboard, and output which place it is in.

## Specifications:

• You must generate a list of 50 "random" scores, in descending order and output it
• The "random" scores will range from 0 to 255
• If 2 scores are the same, it doesn't matter which is placed first in the descending order
• If the input score isn't higher than any of the scores in the list, output 0 or a falsy statement. (the input will be 0 to 255, no need to check)
• If the score IS higher than a score in the list, output the position it would take in the leaderboards.
• Generate and output the leaderboards/list first in descending order, before taking input.

## Example I/O:

Random list: (the three dots are so I don't have to add 50 line text here. You should output each score. 12 is the 50th place)

201
192
178
174
...
...
12


Input:

180


Output:

3


(because this score would place no.3 in the leaderboards)

Input 2:

7


Output 2:

0


(because it is lower than everything on the leaderboard)

This is a so shortest code wins.

• What if I have score list 10 9 9 7 5 and I get an input of 9? – Erik the Outgolfer Aug 4 '17 at 17:27
• @EriktheOutgolfer If 2 scores are the same, it doesnt matter which is placed first in the descending order - I assume that applies for that case too. – Mr. Xcoder Aug 4 '17 at 17:28
• @Mr.Xcoder I think "the descending order" refers to the first 50 scores. – Erik the Outgolfer Aug 4 '17 at 17:28
• I thought the output could be 2, 3 or 4 in that case? – Erik the Outgolfer Aug 4 '17 at 17:31
• @EriktheOutgolfer My bad, you are correct. – P. Ktinos Aug 4 '17 at 17:40

# Python 2, 102 bytes

from random import*
lambda n,l=[randint(0,255)for i in[0]*50]:(l,[sorted(l+[n]).index(n),0][n>max(l)])


Try it online!

• Since you're already importing random, you can save 6 bytes by doing sample(range(256)*50,50) instead of [randint(0,255)for i in[0]*50] – Wondercricket Aug 4 '17 at 19:09
• This does not follow the specification bullet point: "Generate and output the leaderboards/list first in descending order, before taking input." - it'll have to be a full program to achieve this. – Jonathan Allan Aug 4 '17 at 23:54

# Python 3, 125 128 127 126 125 119 115 bytes

from random import*;x=sorted(choices(range(256),k=50));print(x[::-1]);print((sum(map(int(input()).__lt__,x))+1)%51)


Could probably be golfed quite a bit

Edit: Changed from sample to choices, as sample does not allow repetition. Pointed out by @Arnold Palmer

-2 bytes, thanks to @Arnold Palmer

-4 bytes, thanks to @Jonathan Allan

Try it online!

• Here's a 118 byte Python 2 one I just did that's a bit smaller. A straight conversion to Python 3 is larger than yours, but this shaves off 7 bytes. Also, sample forces unique elements, so your leaderboard will never have duplicates, which I believe is incorrect. For +3 you could just do sample(range(256)*50,50), but that's still technically not random. – Arnold Palmer Aug 4 '17 at 18:22
• You can save a byte if you do [0,sum(a>i for a in x)+1][i>x[0]] instead of [sum(a>i for a in x)+1,0][i<=x[0]] – Arnold Palmer Aug 4 '17 at 18:37
• I don't think you need the * in print(*x[::-1]) since generally printing the actual list of numbers is fine. – Arnold Palmer Aug 4 '17 at 18:49
• Nice work. You can save four bytes by using a map like so (avoiding creating i and using the for with a little overhead) – Jonathan Allan Aug 4 '17 at 23:50

# R, 69 bytes

n=scan();cat(r<-sort(sample(256,50,T)-1,T));if(any(r<n),sum(r>n),0)


reads n from STDIN; prints the list to STDOUT, and returns the index or 0.

Try it online!

Cleaner output format

# Jelly, 18 bytes

⁹ẋ50ḶX€ṢṚṄ€>ƓS‘%51


Try it online!

-3 thanks to Jonathan Allan.

Now with interactive I/O!

Full program. First 50 lines are the scores, last line is the index.

• I see, makes sense now! – Jonathan Allan Aug 4 '17 at 19:04
• How about ...Ṅ€>ƓS‘%51 for 3 less? – Jonathan Allan Aug 4 '17 at 19:26
• @JonathanAllan Nice. – Erik the Outgolfer Aug 4 '17 at 19:28

# Charcoal, 48 bytes

ＡＥ⁵⁰‽²⁵⁶ηＦ⁵⁰«⊞υ⌈ηＡ§η⌕η⌈η⁰»ＩυＤ⎚ＮθＩ﹪⁺¹Ｌ⪫Ｅυ× ›ιθω⁵¹


Try it online! Link is to verbose version of code. There's no Sort function in Charcoal, so I loop 50 times extracting the highest remaining element and then setting it to zero each time, using the ambiguous AssignAtIndex command. There is an experimental Reduce function in Charcoal but I'm too wary of it so I'm taking the length of a string instead. Unfortunately this gives me a 0-indexed answer to I have to add 1 modulo 51 to give the desired answer. At least generating the 50 random numbers was easy.

• well there's always ▷s⟦<args>⟧/▷s<arg> (sorted, sort is just sort), verbose is evalvar("s", [args]) – ASCII-only Aug 7 '17 at 1:57
• @ASCII-only I tried Assign(Reverse(evalvar("s", Map(50, Random(256)))), u); but it doesn't seem to work for some reason: Try it online! – Neil Aug 7 '17 at 9:47
• :/ this is because Clear clears all variables as well, oops – ASCII-only Aug 8 '17 at 0:28
• @ASCII-only Strange, because it seems to work with the "long" version... – Neil Aug 8 '17 at 0:39
• Oh that's because it's pushing to the hidden variable u which is not reset, I've changed it anyway so it will now be consistent after Dennis pulls Charcoal. – ASCII-only Aug 8 '17 at 0:41

# Mathematica, 105 bytes

(P=Position;Print[If[(d=Min@Nearest[s=Reverse@Sort@RandomSample[Range@251-1,50],#])<#,s~P~d,s~P~d+1],s])&


## TXR Lisp, 78 76 bytes

(opip(pos-if(op > @@1)(prinl[sort[(gun(rand 256))0..50]>]))(if @1(+ 1 @1)0))


Run:

1> (opip(pos-if(op > @@1)(prinl[sort[(gun(rand 256))0..50]>]))(if @1(+ 1 @1)0))
#<intrinsic fun: 0 param + variadic>
2> [*1 3]
(247 243 238 237 234 209 204 202 196 193 183 181 179 177 174 174
164 159 156 155 143 141 138 134 134 132 131 117 105 98 89 88
82 77 75 66 65 64 55 52 46 34 32 28 28 26 24 21 18 5)
0
3> [*1 3]
(252 250 241 236 234 229 228 219 210 206 205 204 193 188 184 168
166 164 163 160 158 158 148 138 137 130 123 118 111 103 97 93
92 85 81 73 69 60 60 53 52 44 38 31 19 16 15 12 10 1)
50
4> [*1 245]
(251 248 244 238 237 228 225 223 222 220 219 216 208 197 196 192
191 176 159 157 155 149 140 138 121 119 115 113 108 106 105 101
87 82 79 69 67 59 54 42 40 38 34 19 18 12 11 9 4 4)
3
5> [*1 255]
(255 241 228 228 226 224 222 222 217 214 211 197 197 193 191 185
176 175 173 166 162 142 141 139 132 132 122 122 110 110 109 106
104 83 82 81 81 79 74 69 69 65 61 60 59 51 41 37 25 14)
2
6> [*1 255]
(248 244 240 234 231 220 214 211 207 206 205 200 197 188 187 184
183 182 173 153 150 149 144 130 127 126 122 113 109 98 94 89
74 66 60 60 58 54 54 51 49 45 21 15 12 12 10 8 7 3)
1


# Ruby, 78 bytes

puts h=[*0..255].sample(50).sort.reverse
s=gets.to_i
p (h.index{|i|i<s}||-1)+1


## Javascript, 155148134115 101 bytes

Not very short, but:

l=[...Array(50)].map(x=>Math.random()*256|0).sort((a,b)=>b-a);console.log(l);f=x=>l.findIndex(v=>x>v)


Returns position when higher than at least one score. Otherwise -1.

Edit: apparently you don't have to output scores line by line. So shorter now.

l=[...Array(50)].map(x=>Math.random()*256|0).sort((a,b)=>b-a);console.log(l);f=x=>l.findIndex(v=>x>v)

console.log(f(200))
console.log(f(60))
console.log(f(30))
console.log(f(2))

14 bytes saved by @Downgoat

• Array(50).fill(0).map(x=>Math.round(Math.random()*256)) is one byte shorter than Array.from({length:50},x=>Math.floor(Math.random()*256)). – Florrie Aug 4 '17 at 21:53
• Even shorter: [...Array(50)].map(x=>Math.random()*256|0) – Downgoat Aug 4 '17 at 21:57
• @Downgoat I keep forgetting that spread operator... thanks! – Thomas W Aug 5 '17 at 9:13

# Gaia, 17 bytes

{256ṛ(}₅₀]ȯv:q<∆q


There was a small bug with the random function, so this won't work quite properly on the version on TIO, but will work locally.

Try it online!

### Exaplanation

{256ṛ(}            Get a random integer from 1 to 256 and decrement it.
₅₀          Do that 50 times.
]         Collect the results in a list.
ȯv       Sort in descending order.
:q     Copy and print.
<∆   Find the index of the first score that is less than the input (input
won't be read until this point).
q  Print that index.


Haskell is certainly the wrong tool for this job, far too long..

(sequence(randomRIO(0,255)<$[1..50])::IO[Int])>>=g.sort import System.Random import Data.List g l=mapM_ print(reverse l)>>getLine>>=print.(#l).read x#l=1+length(takeWhile(<x)l)  Try it online! # Bash + GNU utilities, 76 shuf -i0-255 -n50|sort -nr|tee s sed 1i$1i s|sort -nr|sed -n /i/=|dc -e?51%p


### Explanation

shuf -i0-255 -n50                            # generate random leaderboard
|sort -nr                   # sort it in descending numerical order
|tee s             # output and save to file s
sed 1i$1i s # insert new score with an "i" suffix |sort -nr # sort again |sed -n /i/= # output line number of line containing "i" |dc -e?51%p # mod 51 of line number  # Perl 5, 77 + 1 (-p) = 78 bytes @a=sort{$b<=>$a}map{int rand 255}1..50;say"@a";1while$a[$i++]>$_;$_=$i<51&&$i  Try it online! Outputs the scoreboard space separated on the first line. Outputs the position for the new input on the second line. Second output is blank (falsy) if the new score isn't high enough to make the leaderboard. • I don't think this meets the spec of taking input after printing the list (it also doesn't terminate but I'm not sure that's actually an issue) – theLambGoat Aug 4 '17 at 19:54 # Röda, 112 bytes {a=[seq(1,50)|[-abs(randomInteger()%256)]for _|sort]a|print-_ parseInteger _|indexOf-_,[sort(a+-_1)]|[(_+1)%51]}  Try it online! This reads the input from the input stream as a string (like if it was stdin). The answer could obviously be shorter if I can suppose that the input is a number object, as the parseInteger _ call would not be needed. I'm not sure if I can do that. # MATL, 18 bytes Thanks to Jonathan Allan for removing 1 byte and for a correction! 8W50&YrqSPtD<sQ51\  ### Example run The leaderboard is shown as a line of numbers separated by spaces. The gif only shows the first few, to keep image size reasonable. ### Explanation 8W % Push 2 raised to 8, that is, 256 50 % Push 50 &Yr % Random vector of 50 integers from 1 to 256, independently uniformly distributed q % Subtract 1 to each result. This makes the integers go from 0 to 255 SP % Sort, reverse. This sorts in descending order tD % Duplicate and immediately display < % Implicit input. Is it less than each entry in the sorted random vector? s % Sum. This counts how many entries are less than the input Q % Add 1 51\ % Modulo 51. Implicitly display  • Wouldn't SP sort and reverse? – Jonathan Allan Aug 4 '17 at 23:15 • Also wont this output 51 when it should output 0? – Jonathan Allan Aug 4 '17 at 23:17 • ... I think it should be 18? – Jonathan Allan Aug 4 '17 at 23:30 • @JonathanAllan You're right on both, thanks! I hadn't seen the 0 output requirement – Luis Mendo Aug 4 '17 at 23:50 # Java 8, 208 + 19 = 227 bytes It's golfs like these that make me question my choice of language. The I/O order specification prohibits the use of lambda parameters and return values for I/O, but I use a lambda anyway to avoid the main class boilerplate. The lambda can be assigned to Runnable. Byte count includes lambda and required import. import java.util.*;  ()->{int n,p=0;List<Integer>l=new Stack();for(;p++<50;l.sort((a,b)->b-a))l.add(new Random().nextInt(256));System.out.print(l);n=new Scanner(System.in).nextInt();for(int o:l)if(n>o)p--;System.out.print(p%51);}  ## Ungolfed lambda () -> { int n, p = 0; List<Integer> l = new Stack(); for (; p++ < 50; l.sort((a, b) -> b - a)) l.add(new Random().nextInt(256)); System.out.print(l); n = new Scanner(System.in).nextInt(); for (int o : l) if (n > o) p--; System.out.print(p % 51); }  It's pretty readable once ungolfed. My favorite part about this solution is that it sorts the scoreboard 50 times (whenever a new score is generated) in order to save one byte. This results in a noticeable delay while the board is generated. p is incremented to 51 as the scoreboard is populated, and then is decremented to the input score's ranking. The remainder by 51 is taken so that rank 51 is displayed as 0. Since each initial score is produced by a newly-seeded generator, the scores probably don't have the usual guarantees of randomness. But they seem random enough to me. # PHP, 119 bytes <?php while($c++<50)$l[]=rand(0,255);rsort($l);foreach($l as$s)echo"$s ";for($i=fgets(STDIN);$i<$l[++$p-1];);echo$p%51;


Try it online!

Please note, when trying it online, you are required to give the input before you have seen the starting leaderboard, but this is not how it actually runs from a CLI: the leaderboard is printed first and the program waits for the user input.

Explanation

<?php


Generate 50 random numbers and store in the leaderboard, $l. while ($c++ < 50) $l[] = rand(0, 255);  Sort them in descending order. rsort($l);


Print them out space-separated.

foreach ($l as$s) echo "$s ";  A loop, comparing the user input score $i with the score in position $p in the leaderboard. for (  Before we start, get the user input. $i = fgets(STDIN);


We loop from the top of the board ($l[0]) and keep looping for as long as $i is less than the score in that position.

$p is initially undeclared so we increment it before each comparison (so it starts as an integer), but have to subtract 1 for a 0 start. $i < $l[++$p - 1];
);


Print the resulting position. If $i is less than any score on the leaderboard $p will equal 51. Using modulus 51, results in 51 becoming 0, but the other positions remaining unchanged,

echo \$p % 51;


# Ly, 58 bytes

(50)[>0(255)?<1-]>ar&s&ulrns>1<[L[>"\n"ou;]ppl>1+<]"\n"o0u


Try it online!

Outputs a space-separated list of scores, then the position of the given input after a newline.

Explanation:

(50)[>0(255)?<1-]>    # simple loop to create list of 50 scores
ar&s&ulr              # arrange the list, reverse it, print it, then reverse it back
ns                    # take input and save it
>1<                   # prepare counter
[                     # while the stack is not empty
L[>"\n"ou;]       # check if the value is lower than the input, if so, output a newline and the counter, then terminate
ppl>1+<           # otherwise, pop a value off the stack and increase the counter
]
"\n"o0u               # output a newline and 0 if we finished the loop without terminating