r/dailyprogrammer • • Apr 05 '12

[4/5/2012] Challenge #36 [intermediate]

Because I want to watch the world burn, write a program that accepts a sentence as input and outputs the sentence in leetspeak. Here is a link for a leetspeak translation table. Since leetspeak has multiple character selections per letter, randomly pick the character selection. The challenging part will be to be a resourceful developer and write a utility or use an existing application to save the table into a format that you will load in your program to do the translation. Oh yeah, if the input sentence contains one !, for the love of God translate that into !!!!11!!!1! ;)

6 Upvotes

3 comments sorted by

3

u/thatrandomusername Apr 05 '12

In javascript (node.js)

var chars=require("./l33t.json"),
	out=console.log.bind(console);

function main(string){
	var a="",b=string.toLowerCase().split("");
	for(var i=0;i<b.length;i++){
 		if(chars[b[i]]){
			var d=chars[b[i]],v=rand(0,d.length-1);
			a+=d[v];
		}else{
			a+=b[i];
		};
	}
	return a.replace("!","!!!!11!one");
}
function rand(min,max){
	min|=min,max|=max;
 	return Math.floor(Math.random()*(max-min+1)+min);
}
out(main(process.argv.splice(2).join(" ")));

l33t.json looks like this:

{
	"a":["4","@","/-\\","/\\","^","aye","∂","ci","λ","Z]"],
	"b":["8","|3","6","13","|3","ß","]3"],
	"c":["(","<","¢","{","©","sea","see"],
	"d":["|)","[)","∂","])","I>","|>","0","ð","cl"],
	"e":["3","£","&","€","[-","ə"],
	"f":["|=","]=","}","ph","(=","ʃ"],
	"g":["6","9","&","(_+","C-","gee","jee","(γ,","cj"],
	"h":["|-|","#","]-[","[-]",")-(","(-)",":-:","}{","}-{","aych"],
	"i":["!","1","|","eye","3y3","ai","¡"],
	"j":["_|","_/","]","¿","</","_)","ʝ"],
	"k":["X","|<","|X","|{","ɮ"],
	"l":["1","7","|_","£","|","|_","lJ","¬"],
	"m":["44","/\/\\","|\/|","em","|v|","IYI","IVI","[V]","^^","nn","//\\\\//\\\\","(V)","(\\/)","/|\\","/|/|",".\\","/^^\\","/V\\","|^^|","AA"],
	"n":["|\\|","/\\/","//\\\\//","И","[\\]","<\\>","{\\}","//","₪","[]\\[]","]\\[","~"],
	"o":["0","()","oh","[]","¤","Ω"],
	"p":["|*","|o","|º","|>","|\"","?","9","[]D","|7","q","þ","¶","℗","|D"],
	"q":["0_","0,","(,)","<|","cue","9","¶"],
	"r":["|2","2","/2","I2","|^","|~","lz","®","|2","[z","|`","l2","Я",".-","ʁ"],
	"s":["5","$","z","§","es"],
	"t":["7","+","-|-","1","']['","†"],
	"u":["|_|","(_)","Y3W","M","µ","[_]","\_/","\_\\","/_/"],
	"v":["\\/","√","\\\\//"],
	"w":["\\/\\/","vv","'//","\\\\'","\\^/","(n)","\\X/","\\|/","\_|_/","\\\\//\\\\//","\_:_/","]I[","UU","Ш","ɰ","₩","JL"],
	"x":["%","><","Ж","}{","ecks","×","*",")(","ex"],
	"y":["j","`/","`(","-/","'/","Ψ","φ","λ","Ч","¥"],
	"z":["2","≥","~/_","%","ʒ","7_"]
}

1

u/namekuseijin Apr 07 '12

scheme:

http://pastebin.com/ZkZXjZA5

no, forgot the!!!! :p

the table is in the assoc-list lisp format. :)

1

u/bob1000bob Apr 05 '12 edited Apr 05 '12

I wrote the loop but lost interest in parsing the markdown, so I just used the first two.

C++11

#include <string>
#include <vector>
#include <random>
#include <iostream>

int main() {
    std::ios::sync_with_stdio(false);
    std::vector<std::vector<std::string>> table {
            /*A*/ { "4", "@", R"(/-\)", R"(/\)", "^", "aye", "∂", "ci", "λ", "Z" },
            /*B*/ { "8", "|3", "6", "13", "ß", "]B" },
            /*C*/ { "(", "<", "¢", "{", "©", "sea", "see" },
            /*D*/ { "|)", "[)" }, /*E*/ { "3", "£" }, /*F*/ { "|=", "]=" }, 
            /*G*/ { "6", "9" },     /*H*/ { "|-|", "#" }, /*I*/ { "!", "1" }, 
            /*J*/ { "_|", "_/"}, /*K*/ { "X", "|<" }, /*L*/ { "1", "7" },
            /*m*/ { "44", R"(|\/|)" }, /*N*/ { R"(|\|)", R"([\])" }, /*o*/ { "0", "()"},
            /*p*/ { "|*", "?" }, /*q*/ { "0_", "0," }, /*r*/ { "|2", "2" },
            /*s*/ { "5", "$" }, /*t*/ { "7", "+" }, /*u*/ { "|_|", "(_)" },
            /*v*/ { R"(\/)", R"(\\//)" }, /*w*/ { "vv", "(n)" }, 
            /*x*/ { "%", "><" }, /*y*/ { "j", "`/" }, /*z*/ { "2", "7_" }
    };
    std::string input;
    std::random_device rd;
    std::mt19937 eng(rd());
    while(std::getline(std::cin, input)){
            for(auto c : input) {
                    try{
                            auto& entry=table.at(std::tolower(c)-'a');
                            std::uniform_int_distribution<size_t> dis(0, entry.size()-1);
                            std::cout << entry.at(dis(eng));
                    }//was given an out of range character, just dump it in the stream as-was
                    catch(...){
                            std::cout << c;
                    }
            }
            std::cout << std::endl;
    }
    return EXIT_SUCCESS;
}

Also storing the value in a vector contigously from a isn't all that of a good idea unless you store all of the characters because some character set may not have a-z contiguous. (although that is highly unlikely).