Code Golf: $zcvt(str, "LEET")
Leet (or "1337"), also known as eleet or leetspeak, is a system of modified spellings used primarily on the Internet. It often uses character replacements in ways that play on the similarity of their glyphs via reflection or other resemblance. Additionally, it modifies certain words based on a system of suffixes and alternate meanings. There are many dialects or linguistic varieties in different online communities. Wikipedia
You'll have to translate a string into a LeetSpeak.
As usual shortest solution wins.
Input
"Listen"
Output
1|573n
Note
- You must not repeat the same letter consecutively. The order of keys in the reference table is important!
- example:
Eeee
must be3€3€
- example:
- The parameter could contain space
- The set of used punctuation is
,.!?
- Punctuation should be kept in your return string
- If a key does not exist for a character on referece table below, keep the character as is.
- example:
He
must beH3
- example:
- Use this code to check the result length
- You can also use this test case here
Reference
Letter | Key(s) |
---|---|
A | "4", "@" |
B | "|3", "8" |
C | "(", "<" |
E | "3", "€" |
G | "9", "6" |
I | "|", "]" |
K | "|<", "|{" |
L | "1", "£" |
O | "0", "*" |
S | "5", "$" |
T | "7", "+" |
X | "><", "}{" |
Z | "2", "~/_" |
Rules
- The signature of the contest entry MUST be:
Class codeGolf.Leet
{
ClassMethod Convert(a As %String) As %String
{
; your code here
}
}
- It is forbidden to modify class/signature, including but not limited to:
- Adding inheritance
- Setting default argument values
- Adding class elements (Parameters, Methods, Includes, etc).
- It is forbidden to refer to non-system code from your entry. For example, this is not a valid entry:
ClassMethod Convert(a As %String) As %String
{
q ##class(myPackage.myClass).test(a)
}
- The use of $ZWPACK and $ZWBPACK is also discouraged.
I've got an answer in 242 characters but I think there's a mistake in the test case:
Do $$$AssertEquals(##class(CodeGolf.Leet).Convert("Iris"), "|R]5")
The "n" doesn't get converted to uppercase but the "r" does. Surely it should be
Do $$$AssertEquals(##class(CodeGolf.Leet).Convert("Iris"), "|r]5")
and there's a rogue space at the end of "no no no no ".
Thanks for noticing this. Fixed the tests.
Here's my answer. Looks like I'm playing on my own. 242 Chars, not including comments.
/// Build a list where each letter that is to be replaced
/// is followed in the list by its two possible replacements.
/// The next piece indicates which was the last replacement used.
/// Note: Only uppercase letters are in the list so need to
/// translate before searching, using $ZU(28,L,5) to translate.
/// For each character in the input string find its list position.
/// Use $LISTFIND to find its position.
/// Test the position found with Position-1#4
/// Not found characters return 0, and 0-1#4 is non zero so do nothing with them
/// Positions 1,5,9,11,15,19,23,27,31,35,39,43, and 47 contain characters
/// that must be replaced. Position-1#4 is zero for these.
/// No test case or rule for any of the destination symbols already existing in
/// the input string. Introducing that will complicate things as would need to test
/// character before and after current one to try to avoid double letters.
/// When each character is found change the last used replacement indicator.
/// Then return the replacement characters.
/// If the replacement characters contained any of the replaceable characters
/// then you would have to work backwards from the end of the input string
/// to avoid getting in a loop. This would cost 1 character.
ClassMethod Convert(x As %String) As %String
{
S A=$lfs("A,4,@,,B,|3,8,,C,(,<,,E,3,€,,G,9,6,,I,|,],,K,|<,|{,,L,1,£,,O,0,*,,S,5,$,,T,7,+,,X,><,}{,,Z,2,~/_")
f i=1:1:$l(x){s $E(x,i)=$$r($e(x,i))} q x
r(L)S P=$lf(A,$zu(28,L,5)) q:P-1#4 L S $LI(A,P+3)='$LG(A,P+3) Q $LI(A,P+2-$LG(A,P+3))
}
Check for "ZzX"
Instead of "2~/_X", there should be "2~/_><"
I did have a 220 in one line but your answer is superb.
199 if I steal your ideas
Social networks
InterSystems resources
Log in or sign up
Log in or create a new account to continue
Log in or sign up
Log in or create a new account to continue
Log in or sign up
Log in or create a new account to continue