You have come across an old Indian manuscript, one that describes mounds of buried treasure. The manuscript also tells you the location of the treasure, except that some crucial numbers have been encoded indirectly into the text. You figure out that the text uses a 'Kaadi' system, a restricted subset of the more common 'Katapayadi' system.

(The Katapayadi system is an ancient Indian system to encode numerals as letters, often used as mnemonics to remember long numbers.)

Your task here is to decode text encoded in the Kaadi system and print out the numerical value.


Input characters

The Kaadi system is based on the Katapayadi system's rules, but uses only the first row of consonants. Your text here has been transliterated to Latin alphabet, and is known to contain only:

  • vowels 'a','e','i','o','u'
  • consonants 'g','k','c','j', and their capital forms (to represent the aspirated form of those consonants), and 'ṅ' and 'ñ'.

(You may choose to receive and handle 'ṅ' as 'ng' and 'ñ' as 'ny' if that's more convenient in your language.)

Value assignment

In this system,

  1. each consonant when followed by a vowel has a digit associated with it. These are:

    'k'=>1, 'K'=>2,
    'g'=>3, 'G'=>4,
    'c'=>6, 'C'=>7,
    'j'=>8, 'J'=>9,

Note however, that these values apply only when these consonants are followed by a vowel. kacCi has the same value as kaCi (ka,Ci=(1,7)) since the middle c is unaccompanied by a vowel.

  1. Additionally, an initial vowel or sequence of two vowels represents a 0. aikaCi would be: ai,ka,Ci = (0,1,7)

  2. Extra vowels anywhere else in the middle of the text have no value: kauCia is the same as kaCi, the extra vowels can be ignored.

Final numeric value

Once the digit values of the letters have been figured out, the final numerical value is obtained as the reverse order of those digits i.e. the first digit from the text is the least significant digit in the final value.

GucCi has Gu and Ci, so (4, 7), so the final value is 74.
kakakaGo is (1,1,1,4), so the the answer is 4111.
guṅKo is (3,2), so encodes 23. (gungKo if using ASCII-equivalent.)


  • A string containing a Kaadi-encoded text
    • will contain only vowels and the above consonants
    • the vowels are always in lowercase and occur in groups of no more than 2
    • you may choose to accept the letters for 5 and 0 either as their Unicode characters 'ṅ' and 'ñ' or as their ASCII equivalents 'ng' and 'ny' (they're in lowercase in either form)
    • you may assume there are no spaces or punctuations


  • The numerical value of the text, as given by the above rules
    • for empty input, an empty output or any false-y output in your language of choice is acceptable, in addition to 0
    • for invalid input (input with anything other than vowels and the above consonants), the output is undefined - anything goes

Test cases

=> 74
=> 4111
=> 913720
=> 0 //OR empty/falsey output
=> 0
=> 1964783210
=> 2891
=> 23
=> 104

(the last ones can be:

=> 23
=> 104

if you prefer that.)

Standard rules for I/O and loopholes apply. May the best golfer win!

  • 1
    \$\begingroup\$ Can aiaKaci be input? (3 leading vowels) \$\endgroup\$ Commented Jun 8, 2018 at 17:53
  • \$\begingroup\$ There is also no test case which ends in a consonant; can we assume the input will always end in a vowel? \$\endgroup\$ Commented Jun 8, 2018 at 19:21
  • \$\begingroup\$ No for 3 leading vowels. In fact, I don't think it makes sense for 3 consequent vowels to occur anywhere in the input (2 English vowels are sometimes needed to represent one Sanskrit vowel sound, but never 3). Can I add that in as an input constraint now or is it too late? \$\endgroup\$
    – Sundar R
    Commented Jun 8, 2018 at 19:25
  • 2
    \$\begingroup\$ It's not too late to clarify that the input won't have 3 leading vowels. That won't break any submissions, and I doubt anyone have written longer code to take that into account, and if they have they can simply remove it. Nice first question by the way! :) \$\endgroup\$ Commented Jun 8, 2018 at 19:41
  • 2
    \$\begingroup\$ In case it helps in some languages: ord(c)%47%10 gives a unique index in [0..9] for each consonant. (With ord("ṅ")=7749 and ord("ñ")=241.) \$\endgroup\$
    – Arnauld
    Commented Jun 8, 2018 at 20:29

10 Answers 10


JavaScript (ES6), 83 bytes


Try it online!


We use the following regular expression to match either the beginning of the string or one of the Kaadi consonants, followed by a vowel:


For each match in the input string, we invoke the following callback function that takes the content c of the capturing group as parameter:

(_, c) => o = (s + s).search(c) % 10 + o

We find the value of the consonant by looking for its position in the regular expression (coerced to a string by adding it to itself).

The consonants are ordered in such a way that their value is equal to their position modulo 10:

string   : /  (  ^  |  [  ṅ  c  C  j  J  ñ  k  K  g  G  ]  )  [  a  e  i  o  u  ]  /  g
position : 0  1  2  3  4  5  6  7  8  9  10 11 12 13 14 15 ...
modulo 10: -  -  -  -  -  5  6  7  8  9  0  1  2  3  4  -  ...

When we match the beginning of the string instead of a consonant, c is an empty string whose position in the regular expression is 0 -- which, conveniently, is the expected result in that case.

Finally, we insert this digit at the beginning of the output string o.


Retina, 41 bytes



Try it online! Link includes test cases. Explantion:


Translate the consonants that are followed by vowels.


Handle a leading vowel.


Delete everything else.


Reverse the result.


Python 2, 93 bytes

lambda s,h=u'ñkKgGṅcCjJ'.find:''.join(`h(c)`*(0>h(v)<h(c))for c,v in zip(u'ñ'+s,s))[::-1]

An unnamed function accepting a Unicode string which returns a string representation of the base ten result.

Try it online!


Java 8, 136 126 bytes

s->{for(int i=s.length,t;i-->0;)if("aeiou".contains(s[i]))System.out.print(i<1?0:(t="ñkKgGṅcCjJ".indexOf(s[i-1]))<0?"":t);}

Try it online.


s->{                           // Method with String-array parameter and String return-type
  for(int i=s.length,t;i-->0;) //  Loop backwards over the input-characters
    if("aeiou".contains(s[i])) //   If the current character is a vowel:
      System.out.print(        //    Print:
         i<1?                  //     If we're at the first character:
          0                    //      Print a 0
         :                     //     Else:
                               //      If the character before the vowel is also a vowel:
           ""                  //       Print nothing
          :                    //      Else:
           t);}                //       Print the correct digit of the consonant

Jelly, 27 bytes


Try it online!

Jelly has built-in for... 1-byte .


Żµ             Prepend 0 to the string.
  e€  œp       Split at...
    Øẹ           the vowels. (0 is not a vowel)

ṫ€0            For each sublist `l` takes `l[-1:]`.
                 If the initial list is empty the result is empty,
                 otherwise the result is a list contain the last element.
   F           Flatten. (concatenate the results)

“kKgGṅcCjJ”iⱮ  Find the index of each character in the list.
                 Get 0 if not found (i.e., for `0` or `ñ`)
UḌ             Upend (reverse) and then convert from decimal.
  • \$\begingroup\$ I didn't find a way to save bytes with that, but O%47%10 gives a unique index in [0...9] for each consonant. (Which means that O%47 with the implicit modulo of would allow to pick up the correct value in an array of 10 entries.) \$\endgroup\$
    – Arnauld
    Commented Jun 8, 2018 at 20:22
  • 1
    \$\begingroup\$ @Arnauld 6;µe€Øẹœpṫ€0O%47ị“ ʠḷ’Œ?’¤Ṛ but also 27 unless it can be golfed. \$\endgroup\$ Commented Jun 9, 2018 at 1:13

Python 2, 101 bytes

lambda s,S=u'ñkKgGṅcCjJ':''.join(`S.find(c)`for c,n in zip(u'ñ'+s,s)if c in(n in'aeiou')*S)[::-1]

Try it online!

Python 3, 104 102 bytes

lambda s,S='ñkKgGṅcCjJ':''.join(str(S.find(c))for c,n in zip('ñ'+s,s)if c in(n in'aeiou')*S)[::-1]

Try it online!


  • -3 bytes, thanks to Rod
  • \$\begingroup\$ Well, you can if you add the header, but I forgot the unicode prefix in the strings, after all, it would save a single byte \$\endgroup\$
    – Rod
    Commented Jun 8, 2018 at 14:47
  • \$\begingroup\$ @Rod, ah thanks, I'd forgotten about the unicode prefix :) \$\endgroup\$
    – TFeld
    Commented Jun 10, 2018 at 10:22

JavaScript (Node.js), 126 bytes

_=>(l=_.match(/[kgñṅcj][aeiou]/gi))?l.map(a=>"ñkKgGṅcCjJ".indexOf(a[0])).reverse``.join``+(/[ aiueo]/.test(_[0])?0:''):0

Try it online!

  • 1
    \$\begingroup\$ you can save a few bytes by changing x=[..."ñkKgGṅcCjJ"] to just x="ñkKgGṅcCjJ" since indexOf works with Strings too \$\endgroup\$
    – WaffleCohn
    Commented Jun 8, 2018 at 14:32
  • 1
    \$\begingroup\$ This doesn't seem to treat initial vowels as 0, so fails the test cases 3 and 6 (as can be seen in the Output on TIO). \$\endgroup\$
    – Sundar R
    Commented Jun 8, 2018 at 14:45
  • \$\begingroup\$ @sundar My bad, Fixed. \$\endgroup\$ Commented Jun 8, 2018 at 14:58

Red, 152 143 bytes

func[s][t:"ñkKgGṅcCjJ"c: charset t
d: copy{}parse s[opt[1 2 not c(alter d 0)]any[copy n c not c(insert
d(index? find/case t n)- 1)| skip]]d]

Try it online!


f: func[s] [
    t: "ñkKgGṅcCjJ"
    c: charset t
    d: copy {}
    parse s [
        opt [ 1 2 not c (alter d 0) ]
        any [ 
              copy n c not c (insert d (index? find/case t n) - 1)
            | skip 

MATL, 48 47 45 bytes


Try it online!

('b' instead of 'd' to save a byte)
(-2 bytes thanks to Luis Mendo)

MATLAB (and hence MATL) treating strings as a dumb series of bytes made porting @TFeld's Python solution harder than I imagined (maybe a straight loop solution would have been easier here?). Ended up using the alternate 'ng', 'ny' input method, and replacing ng with b at the beginning for easier processing.


        % Implicit input (assume 'junyiKonge')
 'ng'   % string literal
 98     % 'b'
 Zt     % replace substring with another (stack: 'junyiKobe')
 t      % duplicate that (stack: 'junyiKobe' 'junyiKobe')
 'y'    % string literal
 w      % swap elements in stack so 'y' goes before input (stack: 'junyiKobe' 'y' 'junyiKobe')
 h      % horizontal concatenation (prepend 'y' to input string) (stack: 'junyiKobe' 'yjunyiKobe')
 w      % swap stack (stack: 'yjunyiKobe' 'junyiKobe')
 11Y2   % place 'aeiou' in stack (stack: 'yjunyiKobe' 'junyiKobe' 'aeiou')
 m      % set places with a vowel to True i.e. 1 (stack: 'yjunyiKobe' 0 1 0 1 0 1 0 0 1)
 )      % index into those places (stack: 'jyKd')
 'ykKgGdcCjJ' % string literal
 tfq    % generate numbers 0 to 9 (stack: 'jyKd' 'ykKgGdcCjJ' 0 1 2 3 4 5 6 7 8 9)
 XE     % replace elements in first array which are found in second,
        %  with corresponding elements from third
 t10<)  % keep only elements that are less than 10 (removes extraneous vowels)
 o      % convert from string to double (numeric) array (stack: 8 0 2 5)
 P      % flip the order of elements (stack: 5 2 0 8)
        % (implicit) convert to string and display
  • \$\begingroup\$ Thanks, done. Do you know if MATLAB/Octave has anything to index into/iterate through a string across Unicode codepoints instead of bytes? Doesn't look like it, their Unicode support seems atrociously bad in general, but maybe I missed something. \$\endgroup\$
    – Sundar R
    Commented Jul 6, 2018 at 7:32
  • 1
    \$\begingroup\$ Also, the MATL manual mentions " if input is a string or character array" in a couple of places - are those two different things? Anything to do with the fancy new double-quoted strings in MATLAB? \$\endgroup\$
    – Sundar R
    Commented Jul 6, 2018 at 7:34
  • 1
    \$\begingroup\$ Matlab has unicode2native, but I think that's now what you want. I agree, Unicode support in MATLAB is not the best. And don't get me started with Octave :-D As for MATL, it was designed before the new string data type existed in MATLAB (and I don't like it much anyway), so in MATL a "string" is the same as it used to be in old MATLAB versions: a row vector of chars. I've made a note to clarify that in the documentation, thanks for noticing! \$\endgroup\$
    – Luis Mendo
    Commented Jul 6, 2018 at 9:05

Stax, 27 bytes


Run and debug it


Your Answer

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

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