Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

It's Friday... so let's go golfing! Write code that determines the player's scoring on a hole in a game of golf. The code can be either a function or entire program. As the genre suggests, shortest code wins.

Input (parameters or stdin, your choice):

  • An integer representing the hole's par, guaranteed to be between 3 and 6
  • An integer representing the golfer's score, guaranteed to be between 1 and 64

Output (print to stdout or return, trailing newline allowed but not required, your choice):

  • if score is 1, output "Hole in one"
  • if score == par - 4 and par > 5, output "Condor"
  • if score == par - 3 and par > 4, output "Albatross"
  • if score == par - 2 and par > 3, output "Eagle"
  • if score == par - 1, output "Birdie"
  • if score == par, output "Par"
  • if score == par + 1, output "Bogey"
  • if score == par + 2, output "Double Bogey"
  • if score == par + 3, output "Triple Bogey"
  • if score > par + 3, output "Haha you loser"

EDIT Congrats to Dennis on having the shortest answer!

share|improve this question
32  
I always wondered what was after triple bogey. – ThisSuitIsBlackNot Mar 4 at 19:27
2  
Incidentally the largest par is 7 not 6. – Joshua Mar 4 at 20:57
4  
@Joshua I was temporarily confused about why you commented instead of edited your own post. Then it hit me. :P – Eᴀsᴛᴇʀʟʏ Iʀᴋ Mar 4 at 21:25
    
@RikerW the two Josh's names are as close as their reputation :D – cat Mar 4 at 21:55
2  
Can the input be in any order? – Doorknob Mar 5 at 1:26

15 Answers 15

up vote 5 down vote accepted

Jelly, 61 bytes

_«4ị“Ƙḷ“&SẆ@ẓ“&T¡UQ“½⁽Ð'÷ṿɼ“½Œż“¡œM“v⁵“¥⁻c“£Ḋ⁶»
瓵ḣ⁻×⁵ñBƑ»’?

Try it online!

Background

This uses Jelly's static dictionary compression. You can find a compressor here. This way,

“Ƙḷ“&SẆ@ẓ“&T¡UQ“½⁽Ð'÷ṿɼ“½Œż“¡œM“v⁵“¥⁻c“£Ḋ⁶»

and

“Bogey“Double Bogey“Triple Bogey“Haha you loser“Condor“Albatross“Eagle“Birdie“Par”

as well as

“µḣ⁻×⁵ñBƑ»

and

“Hole in one”

are equivalent.

How it works

_«4ị“Ƙḷ“&SẆ@ẓ“&T¡UQ“½⁽Ð'÷ṿɼ“½Œż“¡œM“v⁵“¥⁻c“£Ḋ⁶»  Helper link. Arguments: score, par

_                                                Subtract the par from the score.
 «4                                              Cap the difference at 4.
   ị                                             Index into the list at the right.
    “Ƙḷ“&SẆ@ẓ“&T¡UQ“½⁽Ð'÷ṿɼ“½Œż“¡œM“v⁵“¥⁻c“£Ḋ⁶»  Yield a list of strings.


瓵ḣ⁻×⁵ñBƑ»’?  Main link. Arguments: score, pair

            ?  If...
           ’   the decremented score if non-zero:
ç                Call the helper link on both input arguments.
 “µḣ⁻×⁵ñBƑ»      Else, return “Hole in one”.
share|improve this answer
    
Congrats on the shortest answer! – Josh Mar 15 at 2:52

PHP 5.3+, 173 167 166 159 156 151 127 121 bytes

echo[Condor,Albatross,Eagle,Birdie,Par,$b=Bogey,"Double $b","Triple $b","Haha you loser"][min(4+$s-$p,8)]?:"Hole in one";
Notice-free version, 139 137 bytes
echo$s-1?["Condor","Albatross","Eagle","Birdie","Par",$b="Bogey","Double $b","Triple $b","Haha you loser"][min(4+$s-$p,8)]:"Hole in one";

Set $score and $par variables before the echo and you're off.

exploded view
echo [Condor,
      Albatross,
      Eagle,
      Birdie,
      Par,
      $b = Bogey,
      "Double $b",
      "Triple $b",
      "Haha you loser"][ min( 4+$s-$p,8 ) ]
  ?: "Hole in one";

Edits
-6: Not storing the array, just calling it if need be.
-1: Flipping the ternary around.
-7: The lowest $s-$p with $s>1 is -4, so the max() isn't necessary, since 4+$s-$p >= 0.
-3: Array -> explode(), thanks CoolestVeto!
-5: Cheaty string literal undefined constant plus $r[-1] -> false, thanks Ismael Miguel!
-24: Moving from an explode() function to a printf/%s setup, with some tweaks, more thanks to Ismael Miguel for the change of direction.
-6: Swerve, we're back to echo again!

share|improve this answer
1  
Can you turn it into one string and split by an arbitrary character? – VTCAKAVSMoACE Mar 4 at 17:17
    
@CoolestVeto As a matter of fact, I can. Weirdly, it saves fewer bytes than expected, but any bytes are better than no bytes! – ricdesi Mar 4 at 17:20
1  
You can replace $b="Bogey"; with $b=Bogey; to save 2 bytes. Also, replace your $s-1? ... : ...; with an echo ... ?: ...;. Here's the 151 bytes long version: function g($s,$p){$b=Bogey;echo explode("-","Condor-Albatross-Eagle-Birdie-Par-$b-Double $b-Triple $b-Haha you loser")[min(4+$s-$p,8)]?:"Hole in one";} – Ismael Miguel Mar 4 at 19:09
    
Had no idea you could cheat string literals, neat. – ricdesi Mar 4 at 19:15
1  
Here's a (possibly) working and shorter one: printf([Condor,Albatross,Eagle,Birdie,Par,'%s','Double %s','Triple %s','Haha you loser'][min(4+$argv[1]-$argv[2],8)]?:'Hole in one',Bogey);. Basically: Removed the function declaration, removed the explore, removed the quotes, used an array and used $argv. The final code is 139 bytes long. – Ismael Miguel Mar 4 at 19:39

05AB1E, 91 90 bytes

Code:

-5+U“¥Ê€†€µ“ª"0Bogey"ДCondor Albatross²è Birdie Par ÿ‹¶ÿ½¿ÿ”ð¡“Haha€î loser“X0¹1Qm*@0ð:ðÛ

Explanation:

Part 1:

-5+                          # Computes Score - Par + 5
   U                         # Store in X
    “¥Ê€†€µ“ª                # Short for "Hole in one"
             "0Bogey"        # Push this string
                     Ð       # Triplicate

Part 2:

”Condor Albatross²è Birdie Par ÿ‹¶ÿ½¿ÿ”ð¡

This is the same as "Condor Albatross Eagle Birdie Par 0Bogey Double0Bogey Triple0Bogey" using string compression and string interpolation. We then split on spaces, using ð¡.

Part 3:

“Haha€î loser“                # Push "Haha you loser"
              X               # Push X
               0¹1Qm          # Compute 0 ^ (score == 1), this translates 1 to 0 and 
                                everything else to 1.
                    *         # Multiply the top two items
                     @        # Get the string from that position
                      0ð:     # Replace zeros with spaces
                         ðÛ   # Trim off leading spaces

Discovered a lot of bugs, uses CP-1252 encoding.

Try it online!

share|improve this answer
    
...Whoa. Nicely done. – cat Mar 4 at 20:49
2  
@tac Thanks! :) – Adnan Mar 4 at 20:59
    
This code kinda looks like the random Unicode messages that you get when you install a non-English .exe thing (if you have English as your language in Windows things). Looks spectacular, though! – DerpfacePython Mar 9 at 6:49
    
@DerpfacePython Haha, the random unicode messages are partially part of the code and the other part is part of a compressed message. – Adnan Mar 9 at 19:13

Vitsy, 131 bytes

D1-)["eno ni eloH"rZ;]r-5+D9/([X9]mZ
"rodnoC"
"ssortablA"
"elgaE"
"eidriB"
"raP"
"yegoB"
4m" elbuoD"
4m" elpirT"
"resol uoy ahaH"

Explanation:

D1-)["eno ni eloH"rZ;]r-5+D9/([X9]mZ
D1-)[                ]      If the second input is 1, do the bracketed stuff.
     "eno ni eloH"rZ;       Output "Hole in one" and quit.
r                           Reverse the stack.
 -                          Subtract the top two items.
  5+                        Add 5 to fix for negative values of score.
    D9/([  ]                If the result of that is greater than 8, do the stuff in brackets.
         X                  Remove the top item.
          9                 Push 9. This forces any items greater than 8 to be 9.
            m               Execute this number line.
             Z              Output everything in the stack.

This works by figuring out what the score is relative to the par, then executing different lines (and gaining different strings) thereof.

Try It Online!

Verbose Mode (for poops and giggles):

duplicate top item;
push 1;
subtract top two;
if (int) top is not 0;
begin recursive area;
toggle double quote;
push 14;
eval(stack);
capture stack as object with next;
 ;
eval(stack);
push input item;
 ;
push 14;
push length of stack;
capture stack as object with next;
push all ints between second to top and top;
toggle double quote;
reverse stack;
output stack as chars;
generic exit;
end recursive area;
reverse stack;
subtract top two;
push 5;
add top two;
duplicate top item;
push 9;
divide top two;
if (int) top is 0;
begin recursive area;
remove top;
push 9;
end recursive area;
goto top method;
output stack as chars;
:toggle double quote;
reverse stack;
capture stack as object with next;
push 13;
eval(stack);
capture stack as object with next;
push cosine of top;
toggle double quote;
:toggle double quote;
push inverse sine of top;
push inverse sine of top;
capture stack as object with next;
reverse stack;
push inverse tangent of top;
push 10;
push 11;
push length of stack;
push inverse cosine of top;
toggle double quote;
:toggle double quote;
push 14;
push length of stack;
g;
push 10;
push e;
toggle double quote;
:toggle double quote;
push 14;
push input item;
push 13;
reverse stack;
push input item;
B;
toggle double quote;
:toggle double quote;
reverse stack;
push 10;
push pi;
toggle double quote;
:toggle double quote;
push number of stacks;
push 14;
g;
capture stack as object with next;
B;
toggle double quote;
:push 4;
goto top method;
toggle double quote;
 ;
push 14;
push length of stack;
push 11;
flatten top two stacks;
capture stack as object with next;
duplicate top item;
toggle double quote;
:push 4;
goto top method;
toggle double quote;
 ;
push 14;
push length of stack;
push whether (int) top item is prime;
push input item;
reverse stack;
push tangent of top;
toggle double quote;
;
;
:toggle double quote;
reverse stack;
push 14;
push inverse sine of top;
capture stack as object with next;
push length of stack;
 ;
flatten top two stacks;
capture stack as object with next;
push number of stacks;
 ;
push 10;
factorize top item;
push 10;
push all ints between second to top and top;
toggle double quote;
share|improve this answer
    
"Push 9. This forces any items greater than 8 to be 9." Wat? – cat Mar 4 at 20:33
    
@tac This is to force the input that would result in "Haha you loser" to go to the line containing "Haha you loser". – VTCAKAVSMoACE Mar 4 at 20:42
    
Wow... nice job! – ardaozkal Mar 5 at 12:38

JavaScript (ES6), 125 124 bytes

p=>s=>"Hole in one,Condor,Albatross,Eagle,Birdie,Par,Bogey,Double Bogey,Triple Bogey".split`,`[s-1&&s-p+5]||"Haha you loser"

Assign to a variable e.g. f=p=>s=>, then call it like so: f(6)(2) Par first, then score.

May be able to be shortened by combining the "Bogey"s.

share|improve this answer
    
An example of combining the bogeys is: ",Double ,Triple ".split`,`[k-1]+"Bogey" where k=s-p. – zyabin101 Mar 4 at 19:03
    
Can I use your approach in my solution? – zyabin101 Mar 4 at 19:07
    
@zyabin101 that isn't discouraged, as long as it isn't outright plagiarism – cat Mar 4 at 21:32
    
@zyabin101 Thanks, I'll see if that makes it any shorter. And yes, feel free to use this approach in your answer. – ETHproductions Mar 5 at 15:55
    
I'm already using this. – zyabin101 Mar 5 at 16:16

LittleLua - 160 Bytes (non-competitive)

r()P=I B="Bogey"r()Z={"Hole in one","Condor","Albatross","Eagle","Birdie","Par",B,"Double "..B,"Triple "..B,"Haha, you loser"}p(I=='1'a Z[1]or Z[I-P+6]or Z[10])

I'm relatively certain I did this right.

Accepts two integers, par and player's score.

The version of Little Lua that I used to make this was uploaded after this challenge was posted, but was not edited afterwards. It's relatively obvious from the code that nothing has been added to simplify this challenge

LittleLua Info:

Once I am satisfied with the built ins and the functionality of Little Lua, source will be available along with an infopage.

LittleLua V0.02

share|improve this answer
1  
This is non-competitive because the file was uploaded to Dropbox 2 hours after the challenge was posted. – Mego Mar 4 at 21:58
2  
github.com is much better at code hosting... – cat Mar 4 at 22:28
    
Absolutely, I just havent had a chance to set it up. – Skyl3r Mar 4 at 22:29

Mouse-2002, 223 207 bytes

Removing comments would likely help...

??s:p:s.1=["Hole in one"]s.p.4-=p.5>["Condor"]s.p.3-=p.4>["Albatross"]s.p.2-=p.3>["Eagle"]s.p.1-=["Birdie"]s.p.=["Par"]s.p.1+=["Bogey"]s.p.2+=["Double Bogey"]s.p.2+=["Double Bogey"]s.p.3+>["Haha you loser"]$

Ungolfed:

? ? s: p:

s. 1 = [
  "Hole in one"
]

~ 1
s. p. 4 - = p. 5 > [
  "Condor"
]

~ 2
s. p. 3 - = p. 4 > [
  "Albatross"
]

~ 3
s. p. 2 - = p. 3 > [
  "Eagle"
]

~ 4
s. p. 1 - = [
  "Birdie"
]

~ 5
s. p. = [
  "Par"
]

~ 6
s. p. 1 + = [
  "Bogey"
]

~ 7
s. p. 2 + = [
  "Double Bogey"
]

~ 8
s. p. 2 + = [
  "Double Bogey"
]

s. p. 3 + > [
  "Haha you loser"
]


$
share|improve this answer

bash, 150 136 bytes

b=Bogey
(($2<2))&&echo Hole in one||tail -$[$2-$1+5]<<<"Haha you loser
Triple $b
Double $b
$b
Par
Birdie
Eagle
Albatross
Condor"|head -1

Test run:

llama@llama:...code/shell/ppcg74767golfgolf$ for x in {1..11}; do bash golfgolf.sh 6 $x; done                                                          
Hole in one
Condor
Albatross
Eagle
Birdie
Par
Bogey
Double Bogey
Triple Bogey
Haha you loser
Haha you loser

Thanks to Dennis for 14 bytes!

share|improve this answer

Python 2, 186 179 158 bytes

def c(s,p):a="Bogey";print["Condor","Albatross","Eagle","Birdie","Par",a,"Double "+a,"Triple "+a,"Haha you loser","Hole in one"][([[s-p+4,8][s-p>3],9][s==1])]

EDIT 1: added the missing "hole in one" case...

EDIT 2: golfed out some bytes (thanks to tac)

share|improve this answer
1  
A lambda would be shorter, also see tips for golfing in Python – cat Mar 4 at 22:21
    
you can drop the space between 4 and else – cat Mar 4 at 22:22
1  
If you modify the algorithm, you can just index a list rather than a dict – cat Mar 4 at 22:23
    
you can drop the space between print and {, and if you use a semicolon to put the a= and print on the same line, you can shave a byte of whitespace – cat Mar 4 at 22:24
1  
@tac actually "c=lambda x,y:" is longer than "def c(x,y):", thanks for the others suggestions – Max Mar 4 at 22:59

Haskell - 131 bytes (counting newline)

1%p="Hole in one"
s%p=lines"Condore\nAlbatros\nEagle\nBirdie\nPar\nBogey\nDouble Bogey\nTriple Bogey\nHaha you loser"!!min(4-p+s)8

lines is the only way I can think of to golf in a list of strings that have to contain spaces with access only to Prelude so stuck with two character delimiters.

Still, Haskell isn't usually this competitive (against general languages at least).

share|improve this answer
    
You can import anything you like, not just the builtins – cat Mar 6 at 17:19

C, 198 Bytes

main(){char s=8,p=4,m[]="Hole in one.Condor.Albatross.Eagle.Birdie.Par.Bogey.Double Bogey.Triple Bogey.Haha you loser",*t,*x,i=0;for(x=m;t=strtok(x,".");x=0,i++)if((s-1?s-p>3?9:s-p+5:0)==i)puts(t);}
share|improve this answer

Japt, 97 bytes

`Ho¤  e
CÆBr
AlßNoss
Eag¤
Bir¹e
P
zD½e zTp¤ zHa y lo r`rz"Bogey
" ·g9m´V©V-U+6

Contains a bunch of unprintables. Test it online!

How it works

`Ho¤  e\nCÆBr\nAlßNoss\nEag¤\nBir¹e\nP\nzD½e zTp¤ zHa y lo r`                        rz"Bogey\n" ·  g9m´ V© V-U+6
"Hole in one\nCondor\nAlbatross\nEagle\nBirdie\nPar\nzDouble zTriple zHaha you loser"rz"Bogey\n" qR g9m--V&&V-U+6

              // Implicit: U = par, V = score
"..."         // Take this long, compressed string.
rz"Bogey\n"   // Replace each instance of "z" with "Bogey\n".
qR            // Split at newlines.

--V&&V-U+6    // If V is 1, take 0; otherwise, take V-U+5.
9m            // Take the smaller of this and 9.
g             // Get the item at this index in the previous list of words.
              // Implicit output
share|improve this answer

Python 2.7.2, 275 bytes

s=int(input())
p=int(input())
a="Bogey"
if s==1:b="Hole in one"
elif p-4==s:b="Condor"
elif p-3==s:b="Albatross"
elif p-2==s:b="Eagle"
elif p-1==s:b="Birdie"
elif p==s:b="Par"
elif p+1==s:b=a
elif p+2==s:b="Double "+a
elif p+3==s:b="Triple "+a
else:b="Haha you loser"
print b

Ungolfed/explained:

score = int(input())
par = int(input)
foo = "Bogey" # a shortcut for 3 of the outputs
if score == 1:
    output = "Hole in one"
elif par - 4 == score:
    output = "Condor"
...
elif par == score:
    output = "Par"
elif par + 1 == score:
    output = foo # See what I mean?
elif par + 2 == score:
    output = "Double " + foo # Huh? Huh?
elif par + 3 == score:
    output = "Triple " + foo # That makes 3.
else:
    output = "Haha you loser"
print output # Make sense?

My second answer, ironically both are in Python. Golfing tips appreciated!

share|improve this answer

Python 2.7, 226 bytes

p,s=input()
b="Bogey"
l={s==1:"Hole in one",5<p==s+4:"Condor",4<p==s+3:"Albatross",3<p==s+2:"Eagle",s==p-1:"Birdie",s==p:"Par",s==p+1:b,s==p+2:"Double "+b,s==p+3:"Triple "+b,s>p+3:"Haha you loser"}
for r in l:
 if r:print l[r]

Hard to come up with a short python code when you're late to the party, best I could think of.

share|improve this answer
    
The last two lines should be changed to one: [print r for r in l if r] – cat Mar 6 at 17:04

Python 2, 302 284 bytes

i=input();j=input()
if j==1:print"Hole in one"
if(j==i-4)&(i>5):print"Condor"
if(j==i-3)&(i>4):print"Albatross"
if(j==i-2)&(i>3):print"Eagle"
if j==i-1:print"Birdie"
if j==i:print"Par"
if j>i:
 k=j-i
 if k<4:
  print["","Double ","Triple "][k-1]+"Bogey"
 else:
  print"Haha you loser"

If leading white space was allowed, that'd be 282 bytes:

i=input();j=input()
if j==1:print"Hole in one"
if(j==i-4)&(i>5):print"Condor"
if(j==i-3)&(i>4):print"Albatross"
if(j==i-2)&(i>3):print"Eagle"
if j==i-1:print"Birdie"
if j==i:print"Par"
if j>i:
 k=j-i
 if k<4:
  print["","Double","Triple"][k-1],"Bogey"
 else:
  print"Haha you loser"
share|improve this answer
12  
The use of a string array would seriously help you here. – VTCAKAVSMoACE Mar 4 at 17:39
    
Agreed, this seemed really unoptimized. Any combination of terms/results would shorten the answer. – ricdesi Mar 5 at 6:46
    
Why do you need raw_input()? Can't you just use input()? – OldBunny2800 Mar 5 at 23:38
    
@ricdesi I combined the bogeys. – zyabin101 Mar 6 at 14:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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