56
\$\begingroup\$

Players of the traditional board game Go measure their skill in a system of ranks:

  • Players new to the game are ranked 30th kyū (written 30k) and progress counting down to 1st kyū (written 1k). These are considered the student ranks.
  • A player may promote from 1st kyū to 1st dan rank (written 1d), and then progress counting up to 7th dan rank (written 7d). These are the master ranks.
  • Exceptionally skilled players past 7d may promote to the 1st professional dan rank 1p, and progress counting up to 9th professional dan rank (written 9p). This is the highest rank.

In short: ranks are ordered 30k < 29k < ··· < 1k < 1d < 2d < ··· < 7d < 1p < 2p < ··· < 9p.

Task

Given two strings among {30k, …, 1k, 1d, …, 7d, 1p, …, 9p} as input, output the higher rank of the two. (If they are equal, simply output either input.)

(As usual, I/O is flexible — your answer may be a function or a full program, reading input in any reasonable fashion and producing output in any reasonable fashion.)

This is : the objective is to minimize your code's byte count.

Test cases

(Format: input1 input2 output.)

29k 9k    9k
21k 27k   21k
6d 1p     1p
5d 17k    5d
1k 1d     1d
1d 1d     1d
1d 2d     2d
9p 1d     9p
2d 30k    2d
1p 1k     1p
1d 1p     1p
1p 2d     1p
7p 8p     8p
30k 30k   30k
\$\endgroup\$
4
  • \$\begingroup\$ Can the inputs require leading zeroes? I.e. 04k \$\endgroup\$ Jul 7, 2018 at 21:04
  • 6
    \$\begingroup\$ No; while I'm flexible about I/O methods, I'm afraid I won't allow any variation in the input strings themselves — I think they are a perfect level of "interesting" as-is. (I won't allow 4 k or 4K or so, either.) \$\endgroup\$
    – Lynn
    Jul 7, 2018 at 21:10
  • 2
    \$\begingroup\$ Are we allowed to take the inputs as (int, string) pairs? \$\endgroup\$
    – user48543
    Jul 7, 2018 at 21:15
  • 9
    \$\begingroup\$ No; again, the spirit of the challenge is to manipulate the exact text strings 30k, 29k, 1k, 1d et cetera, so I won't allow any variation there. \$\endgroup\$
    – Lynn
    Jul 7, 2018 at 21:26

37 Answers 37

1
2
1
\$\begingroup\$

R, 83 bytes

function(v,k=-1,d=1,p=10)v[1+do.call("<",as.list(parse(t=gsub('(\\D)','*\\1',v))))]

Try it online!

Inspired by this answer by Zahiro Mor and digEmAll's comments on the answer. OP has not replied to comments so I am posting it separately, adding my own do.call twist to it to save additional bytes.

do.call takes arguments to the function as a list, which we do not need to eval before passing - a parse is sufficient. Still, as.list costs a lot of bytes...

Still not as golfy as digEmAll's chartr answer!

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

[R], 87 [bytes]

update:

using JayCe's golfing: f=function(v,k=-1,d=1,p=10)v[eval(parse(t=paste(gsub('(\\D)','*\\1',v),collapse='<')))+1]

[R], 150 [bytes]

a long answer I find interesting could save a lot of bytes if not sticking to the eval method. inputs are s1 and s2:

f=function(s)eval(parse(t=sub("\\D+",paste0("*",c(-1,1,8)[match(tail(el(strsplit(s,"")),1), c("k","d","p"))]),s)));c(s1,s2)[as.numeric(f(s1)<f(s2))+1]
\$\endgroup\$
4
  • 2
    \$\begingroup\$ The eval approach can be improved like this -> Try it online! \$\endgroup\$
    – digEmAll
    Jul 8, 2018 at 17:19
  • 1
    \$\begingroup\$ nice! evaluating the inequality..... I did not think of that :) \$\endgroup\$
    – Zahiro Mor
    Jul 9, 2018 at 11:29
  • \$\begingroup\$ You can update your answer based on @digEmAll's comment - nice approach! \$\endgroup\$
    – JayCe
    Aug 17, 2018 at 0:53
  • 1
    \$\begingroup\$ And it's golfable to 87 chars due to R's lexical scoping rules. \$\endgroup\$
    – JayCe
    Aug 17, 2018 at 1:56
1
\$\begingroup\$

JavaScript (Node.js), 53 bytes

a=>b=>(s=([d,q])=>-d+q|{p:8*d,d,k:-d}[q])(a)>s(b)?a:b

Try it online!

Optimized from tsh's comment in https://codegolf.stackexchange.com/a/168183/

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

Pyth, 15 bytes

Also uses Arnauld's method.

eo*^x"_dp"eN3.v

Try it here!

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

Jelly, 20 bytes

6ị⁾kpiị“¡µ£‘’×ṖV$µÞṪ

Try it online!

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

Kotlin, 256 bytes

{f:String,s:String->{val e={s:String->if(s.length==2)"0$s"
else s}
val d=e(f)
val n=e(s)
val r=when(d[2]){'k'->if(n[2]=='k')if(d<n)f
else s
else s
'd'->if(n[2]=='d')if(d<n)s
else f
else if(n[2]=='k')f
else s
else->if(n[2]=='p')if(d<n)s
else f
else f}
r}()}

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ 46 \$\endgroup\$
    – ASCII-only
    Apr 3, 2019 at 2:03
0
\$\begingroup\$

Go, 168 bytes

import(."strings"
."strconv")
func g(s string)int{i:=IndexByte("_dp", s[1])
j,_:=Atoi(s[:len(s)-1])
return i*i*i*j}
func f(a,b string)string{if g(a)>g(b){b=a}
return b}

Translation of Arnaud's idea. Had to be done for the sake of the language name.

\$\endgroup\$
1
2

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.