11
3
Create a bidirectional Gematria calculator, for any given sequence of Unicode characters as the alphabet.
Gematri-What?
Gematria is a system of assigning numerical values to symbols, developed by ancient Greeks and adopted by ancient Jews. It is in a way like ASCII or Unicode, it's just non-linear... See the following table (full table is available in the link above):
Index Letter Letter name Value
--------------------------
0 א "Alef" 1
1 ב "Bet" 2
...
8 ט "Tet" 9
9 י "Yud" 10
10 כ "Kaf" 20
...
17 צ "Tsady" 90
18 ' "Kuf" 100
19 ר "Resh" 200
...
The names of letters are not important, only their index in the "Array" of the alphabet and the respective numerical value. The Hebrew alphabet only has 22 letters (not including "final" letters), so the maximum available value is 400.
If we borrow this system to the English alphabet (A-Z) we will end up with A=1, B=2... L=30... U=300... Z=800.
Two things we need to know.
One of the most important features in this system is calculating the "Gematria Value" of a word, by summing up its letters' values. (Some say there is a mystical connection between words, or phrases (when the value of space is zero) - that share the same Gematria Value).
Any non-negative Integer can be represented in symbols. For example (and let's stay with the English alphabet for now) the value of 32 is LB (L=30 + B=2). The value of 1024 is ZTKD (800+200+20+4. Note that ZSSKD is also 1024, but that's not a legal representation, since it can be compacted).
The Challenge
Write a program / a function / a code snippet in your language-of-choice, which is first set up with an alphabet (see API below) and then accept an argument. That argument may be an Integer, or a word/phrase. If it's an Integer - your program should output/return its representation in the alphabet symbols - the most compacted one (see (2) above). If it is a word or a phrase, your program should output/return the Gematria Value (by summing up symbols' values, not counting whitespaces, see (1) above).
API
Your program/function should accept 3 arguments. You can get them from STDIN, or as function arguments, you can even assume they are variables which initialized programmatically prior to your function invocation.
- First argument - the first character (in Unicode) of the alphabet.
- Second argument - the last character (in Unicode) of the alphabet.
- Third argument - An Integer, to be represented in symbols, OR a phrase which is created by the given alphabet.
Output / Return Value: Depending on the third argument, as explained above.
Assumptions
- First two arguments will always be one character long each, and the second will always be grater than the first.
- The sequence (first until last, inclusive) will never include any of the values 30-39 (which represent the digits 0-9), otherwise it will make the third argument ambiguous. EDIT: It won't contain space as well, since in phrases spaces are counted as zeros.
- The third argument, in case it's a phrase, may only contain spaces and letters of the given alphabet. Empty string is not a valid input (you can assume it's not empty). In case it's an Integer, you can assume it's a positive Integer.
Examples
Input Output
A Z CODE GOLF 175
a s 512 sssssjb
A B 7 BBBA
≐ ⊐ ≤≫ ≥ 1700
Scoring
Score = upvotes - length/100.0
Your code should be short, but more-importantly popular. Negative scores can also play along. The winner will be the answer with highest score in a week from now, 2014-11-29 19:20:00 UTC.
I retagged your question to the catch-all [tag:code-challenge] as I think the scoring is sufficiently different from either code golf or a standard popularity contest. – Martin Ender – 2014-11-22T19:26:31.260
Ok. That's a lot of tags :) thanks. – Jacob – 2014-11-22T19:27:32.683
What is space itself is included in the include list created by the first two characters ? – Optimizer – 2014-11-22T21:10:47.343
Also, what do you mean by the second assumption ? ASCII code for 0 is not 30. – Optimizer – 2014-11-22T21:11:44.747
@Optimizer it is in hex... – feersum – 2014-11-23T07:57:38.190
@Optimizer, you're right. I will add to assumption that space won't be there as well. – Jacob – 2014-11-23T11:36:07.000
The hebrew letter names are a bit incorrect (it is "Tsadik" not "Tsady") (I am a native hebrew speaker) – proud haskeller – 2014-11-23T12:50:29.757
I also feel like adding a column for the actual letters would be nice – proud haskeller – 2014-11-23T12:52:05.000
1@proudhaskeller this is a common mistake since you learn in kindergarten "peh tsady kuf resh" which sounds like tsadik kuf... You are welcome to confirm this with the Academy for Hebrew. – Jacob – 2014-11-23T12:54:15.860
@Jacob Oh. Well, personally I don't really care about the academy. But I am still surprised. I guess it's like over here they sometimes say that native english speakers might have problems with english syntax. – proud haskeller – 2014-11-23T12:56:26.553
@Jacob also I feel that the development of languages shouldn't be governed by some academy - natural progression is the way languages become what they are now. In the far past "Tsady" was named a bit differently, and it is how it should be. – proud haskeller – 2014-11-23T13:03:30.983