Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

deleted 452 characters in body
Source Link
totallyhuman
  • 17.4k
  • 3
  • 34
  • 89

Explanation (slightly outdated)

The algorithm is pretty simple. Let's assume there are \$n = 3\$ contestants and Alice's rank \$k = 1\$. First, it generates a list of possible rankings by taking the Cartesian product of \$[0, 3)\$ with itself \3ドル\$ times.The ranks that follow will be 0-indexing the ranksindexed because it's more convenient to work with.

[(0, 0, 0), (0, 0, 1), (0, 0, 2),
 (0, 1, 0), (0, 1, 1), (0, 1, 2),
 (0, 2, 0), (0, 2, 1), (0, 2, 2),
 (1, 0, 0), (1, 0, 1), (1, 0, 2),
 (1, 1, 0), (1, 1, 1), (1, 1, 2),
 (1, 2, 0), (1, 2, 1), (1, 2, 2),
 (2, 0, 0), (2, 0, 1), (2, 0, 2),
 (2, 1, 0), (2, 1, 1), (2, 1, 2),
 (2, 2, 0), (2, 2, 1), (2, 2, 2)]

Then First, it selects the potentialgenerates a list of possible rankings where Alice's rank, or the first rank listedin the list, is \1ドル\$\0ドル\$. Of course, it's still 0-indexed, soThis is done by taking the following lists out rankings that startCartesian product of the singleton list \$[0]\$ with \0ドル\$ instead\$n - 1 = 2\$ copies of \$[0, 3)\$.

After thatThen, it knocks out invalid rankings by checking if each rank is equal to "the number of other participants who performed strictly better than them", or in other words, the number of ranks lower than the rank in question.

Explanation (slightly outdated)

The algorithm is pretty simple. Let's assume there are \$n = 3\$ contestants and Alice's rank \$k = 1\$. First, it generates a list of possible rankings by taking the Cartesian product of \$[0, 3)\$ with itself \3ドル\$ times. 0-indexing the ranks because it's more convenient to work with.

[(0, 0, 0), (0, 0, 1), (0, 0, 2),
 (0, 1, 0), (0, 1, 1), (0, 1, 2),
 (0, 2, 0), (0, 2, 1), (0, 2, 2),
 (1, 0, 0), (1, 0, 1), (1, 0, 2),
 (1, 1, 0), (1, 1, 1), (1, 1, 2),
 (1, 2, 0), (1, 2, 1), (1, 2, 2),
 (2, 0, 0), (2, 0, 1), (2, 0, 2),
 (2, 1, 0), (2, 1, 1), (2, 1, 2),
 (2, 2, 0), (2, 2, 1), (2, 2, 2)]

Then, it selects the potential rankings where Alice's rank, or the first rank listed, is \1ドル\$. Of course, it's still 0-indexed, so the following lists out rankings that start with \0ドル\$ instead.

After that, it knocks out invalid rankings by checking if each rank is equal to "the number of other participants who performed strictly better than them", or in other words, the number of ranks lower than the rank in question.

Explanation

The algorithm is pretty simple. Let's assume there are \$n = 3\$ contestants and Alice's rank \$k = 1\$. The ranks that follow will be 0-indexed because it's more convenient to work with. First, it generates a list of possible rankings where Alice's rank, or the first rank in the list, is \0ドル\$. This is done by taking the Cartesian product of the singleton list \$[0]\$ with \$n - 1 = 2\$ copies of \$[0, 3)\$.

Then, it knocks out invalid rankings by checking if each rank is equal to "the number of other participants who performed strictly better than them", or in other words, the number of ranks lower than the rank in question.

added 26 characters in body
Source Link
totallyhuman
  • 17.4k
  • 3
  • 34
  • 89

Python 3, (削除) 124 (削除ここまで) 119(削除) 119 (削除ここまで) 117 bytes

lambda n,k:sum(all(r==sum(q<r for q in s)for r in s)*(s[0]==k-1)for s in product([k-1],*[range(n)]*n]*(n-1)))
from itertools import*

Try it online! Try it online!

Explanation (slightly outdated)

Python 3, (削除) 124 (削除ここまで) 119 bytes

lambda n,k:sum(all(r==sum(q<r for q in s)for r in s)*(s[0]==k-1)for s in product(*[range(n)]*n))
from itertools import*

Try it online!

Explanation

Python 3, (削除) 124 (削除ここまで) (削除) 119 (削除ここまで) 117 bytes

lambda n,k:sum(all(r==sum(q<r for q in s)for r in s)for s in product([k-1],*[range(n)]*(n-1)))
from itertools import*

Try it online!

Explanation (slightly outdated)

added 91 characters in body
Source Link
totallyhuman
  • 17.4k
  • 3
  • 34
  • 89

Python 3, 124(削除) 124 (削除ここまで) 119 bytes

-5 bytes thanks to enzo .

lambda n,k:sum(all(r==sum(q<r for q in s)for r in s)and *(s[0]==k-1for1)for s in product(range*[range(n),repeat=n]*n))
from itertools import*

Try it online! Try it online!

Python 3, 124 bytes

lambda n,k:sum(all(r==sum(q<r for q in s)for r in s)and s[0]==k-1for s in product(range(n),repeat=n))
from itertools import*

Try it online!

Python 3, (削除) 124 (削除ここまで) 119 bytes

-5 bytes thanks to enzo .

lambda n,k:sum(all(r==sum(q<r for q in s)for r in s)*(s[0]==k-1)for s in product(*[range(n)]*n))
from itertools import*

Try it online!

added 9 characters in body
Source Link
totallyhuman
  • 17.4k
  • 3
  • 34
  • 89
Loading
added 16 characters in body
Source Link
totallyhuman
  • 17.4k
  • 3
  • 34
  • 89
Loading
added 1209 characters in body
Source Link
totallyhuman
  • 17.4k
  • 3
  • 34
  • 89
Loading
deleted 1 character in body
Source Link
totallyhuman
  • 17.4k
  • 3
  • 34
  • 89
Loading
Source Link
totallyhuman
  • 17.4k
  • 3
  • 34
  • 89
Loading

AltStyle によって変換されたページ (->オリジナル) /