15
\$\begingroup\$

You might already be familiar with the game: Basketball FRVR in facebook. There are two types of score you can make:

  • A virgin-shot:(we call it so in our country :D) When the ball enters the basket without touching the rim or
  • A stale shot: When the ball enters the basket, but after touching the rim.

The game is over once you miss a shot. Stale shot always scores 2 points.The first in a run of consecutive virgin-shots scores 3 points, the second scores 6 points, and subsequent ones score 9 points each.

For example,

Hits             Total score 

S-V-V-V          2+3+6+9 = 20
S-V-V-V-V        2+3+6+9+9 = 29 (see how added score for consecutive virgin-
                                 shots                              
                                 saturates at the 5th hit)
S-V-S-V-V-S-V    2+3+2+3+6+2+3= 21

Using simple mathematics, it can easily be proven that any score greater than 1 n>1 can be scored in the game

The Challenge

For any given number 1<n<100, output all the possible ways to score n.

Rules:

  • You can either write a complete program or function(s), snippets however are not acceptable.
  • Hard-coding the output or loading from memory/other programs/website is strictly prohibited, you must compute the output in real-time.
  • The output must be a well-separated collection of string of Ss and Vs. For eg: ['SVS','SSV','VSS'] or {'SVS','SSV','VSS'}.
  • It is encouraged to, but the collection need not be sorted or contain only unique elements. Brownie points if you output sorted and unique collection of strings.

Winning condition:

  • This is so the shortest code in bytes wins.

Input/Output (Test-cases)

2: {'S'}
3: {'V'}
4: {'SS'}
12: {'SSSSSS', 'VSSVS', 'VSVSS', 'VSSSV', 'SSVSV', 'SVSVS', 'SVSSV'}
16: {'SSSVSVS', 'SSSVSSV', 'VSSSSSV', 'SSVSSVS', 'SVVSV', 'VSSSSVS', 'SVSVV', 'SVSSSSV', 'VVSSV', 'VSVVS', 'SVSVSSS', 'SVSSSVS', 'VSSVSSS', 'SSVSSSV', 'SSSSSSSS', 'VVSVS', 'VSSSVSS', 'SVSSVSS', 'VSVSSSS', 'SSSSVSV', 'SSVSVSS', 'VSSVV'}

Thanks to Peter Taylor for improving this question

\$\endgroup\$
6
  • 10
    \$\begingroup\$ In case anyone cares, as an American, I always called rimless (and backboardless) shots "swishes" (and Wikipedia backs me up here: Swish, a basketball shot that goes through the basket without touching the rim or backboard) \$\endgroup\$
    – Stephen
    Jul 5, 2017 at 14:43
  • 3
    \$\begingroup\$ I'm mostly just curious where they call them 'virgin shots'... I'm afraid to look it up behind my company's firewall xD \$\endgroup\$
    – bendl
    Jul 5, 2017 at 14:49
  • \$\begingroup\$ @bendl Nepal or Npl when censored. xD \$\endgroup\$ Jul 5, 2017 at 15:04
  • 1
    \$\begingroup\$ Very nice question—not hard, but definitely not trivial, and allows for different approaches :) \$\endgroup\$ Jul 5, 2017 at 17:17
  • 1
    \$\begingroup\$ I think 28 should be 29 in the second test case. \$\endgroup\$ Jul 5, 2017 at 17:34

6 Answers 6

4
\$\begingroup\$

Jelly, 30 bytes

o2,5ṁo8‘
Hṗ@€⁾VSẎðOḂŒgÇ€FS=ðÐf

A monadic link returning a list of the strings (lists of characters).

Try it online! - The footer calls the link and separates the entries by newlines since a full program's implicit output would smash them together.

How?

o2,5ṁo8‘ - Link 1, helper to form shot scores: list of shots grouped by type (S=1 and V=0)
         -                                e.g. [[1,1],[0,0,0,0,0],[1,1],[0],[1,1]]
 2,5     - 2 paired with 5 = [2,5]
o        - logical or (vectorises)             [[1,1],[2,5,0,0,0],[1,1],[2,5],[1,1]]
    ṁ    - mould like the input                [[1,1],[2,5,0,0,0],[1,1],[2],[1,1]]
     o8  - logical or with 8                   [[1,1],[2,5,8,8,8],[1,1],[2],[1,1]]
       ‘ - increment                           [[2,2],[3,6,9,9,9],[2,2],[3],[2,2]]

Hṗ@€⁾VSẎðOḂŒgÇ€FS=ðÐf - Link: number, total score, n
H                     - halve n
    ⁾VS               - literal ['V','S']
 ṗ@€                  - Cartesian power with swapped @rguments for €ach
                      -   ...i.e. for each in [1,2,...,floor(half n)]
                      -   yielding all strings of Vs and Ss from length 1 to floor(half n)
       Ẏ              - tighten (from a list of lists of lists to a list of lists)
        ð         ðÐf - filter keep those entries for which this yields a truthy value:
         O            -   cast to ordinals (S->83, V->86)
          Ḃ           -   modulo 2         (S->1, V->0)
           Œg         -   group equal runs (e.g. [0,0,1,1,0] -> [[0,0],[1,1],[0]])
             Ç€       -   call the last link (1) as a monad for €ach (transform to scores)
               F      -   flatten (make one list of scores)
                S     -   sum (calculate the total score of the string)
                 =    -   equals right argument (n)?

The resulting order is actually lexicographical in reverse, to have it forward sorted just reverse it by appending .

\$\endgroup\$
2
  • \$\begingroup\$ Nice. Your brownie 🍪. \$\endgroup\$ Jul 6, 2017 at 7:54
  • 1
    \$\begingroup\$ @officialaimm We usually call those cookies but whatever... \$\endgroup\$ Jul 6, 2017 at 12:00
4
\$\begingroup\$

JavaScript (ES6), 73 72 bytes

Prints all unique, lexically sorted combinations.

f=(n,p=0,s='')=>n?n>1&&f(n-2,0,s+'S')&f(n-(p-9?p+=3:9),p,s+'V'):alert(s)

Test cases

In this snippet, alert() has been replaced by console.log() for user-friendliness.

f=(n,p=0,s='')=>n?n>1&&f(n-2,0,s+'S')&f(n-(p-9?p+=3:9),p,s+'V'):console.log(s)

;[2, 3, 4, 12, 16]
.forEach(n => { console.log('[' + n + ']'); f(n); })

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Here's a crunchy brownie: 🍪 \$\endgroup\$ Jul 5, 2017 at 16:02
3
\$\begingroup\$

PHP>=7.1, 158 bytes

function b($s="",$g=0){global$r,$i;$g-$i?$g>$i?:b($s."2",$g+2)&b($s.($n=$s[-1]>2?min(9,$s[-1]+3):3),$g+$n):$r[]=strtr($s,2369,SVVV);}$i=$argn;b();print_r($r);

PHP Sandbox Online

Expanded

function b($s="",$g=0){
  global$r,$i;
  $g-$i
    ?$g>$i
      ?
      :b($s."2",$g+2) # recursive call add a S
      &b($s.($n=$s[-1]>2?min(9,$s[-1]+3):3),$g+$n) # recursive call add a V
    :$r[]=strtr($s,2369,SVVV); # add to result array if reach score replace integers with S and V
} 
$i=$argn; #short input variable
b(); #call the function
print_r($r); #print results
\$\endgroup\$
4
  • \$\begingroup\$ @officialaimm You must use a PHP Version equal greater than 7.1 \$\endgroup\$ Jul 5, 2017 at 15:28
  • 1
    \$\begingroup\$ @officialaimm you can change the version to the highest available version. At the first time you visit the site it is not set to a PHP 7.1 Version And the only output I reach for input=6 with a Version 7.1 is SSS \$\endgroup\$ Jul 5, 2017 at 15:39
  • \$\begingroup\$ Oh, its fine now.Anyways, its quite weird how different versions of PHP are giving different outputs for the same code though.. :D \$\endgroup\$ Jul 5, 2017 at 15:43
  • 1
    \$\begingroup\$ @officialaimm This depends on the new feature in PHP 7.1 that is use in this case. $string[-1] belongs to "" in all versions under 7.1 php.net/manual/en/language.types.string.php \$\endgroup\$ Jul 5, 2017 at 15:48
3
\$\begingroup\$

Python 3, 96 95 85 77 73 bytes

-1 byte thanks to @notjagan
-4 bytes thanks to @xnor

f=lambda x,s='',i=3:f(x-2,s+'S')+f(x-i,s+'V',3*(i<9)+i)if x>0 else[s]*-~x

Try it online!

\$\endgroup\$
2
  • 3
    \$\begingroup\$ You can remove a byte by changing x==0 to x<1. \$\endgroup\$
    – notjagan
    Jul 5, 2017 at 14:40
  • \$\begingroup\$ Recursive functions that print can usually be shortened to instead produce a list. In place of printing, return a list of length 1 or 0, and recursively sum the lists corresponding to the branches. \$\endgroup\$
    – xnor
    Jul 6, 2017 at 2:54
1
\$\begingroup\$

Mathematica, 95 102 105 112 bytes

If[#4==#2,Echo@#,If[#4>#2,#0[#<>"S",#2+2,0,#4];#0[#<>"V",#2+Min[3#3+3,9],#3+1,#4]]]&["",0,0,#]&

There should be a lot of golfing potential.

Bascially it is a recursive function that perform a DFS over all possible combinations.

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

Haskell, 75 bytes

n#v|n<1=[[]|n==0]|n>0=(('S':)<$>(n-2)#3)++(('V':)<$>(n-v)#min(v+3)9)
f=(#3)

Call f with the total score value. Uses a fairly straightforward depth-first search to find all the solutions. Sadly, Haskell's precedence rules seem to be out to get me today, so there are lots of parentheses here.

\$\endgroup\$
3
  • \$\begingroup\$ As @Laikoni pointed, VVVV should evaluate to 27 while you seem to evaluate it as 30. \$\endgroup\$ Jul 6, 2017 at 8:00
  • \$\begingroup\$ Fixed the truncation problem at the cost of a few bytes. Thanks, @Laikoni, for pointing that out. \$\endgroup\$ Jul 6, 2017 at 11:59
  • 1
    \$\begingroup\$ @Laikoni If I change <$> to map, I have to wrap the # call in parentheses, so it's a wash. \$\endgroup\$ Jul 6, 2017 at 12:00

Your Answer

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

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