Create a Leet Translator!

This is the place for ALL of the user submitted challenges. If you create a little challenge/mission/riddle/whatever, post it here.
Forum rules
Do not post missions that you did NOT create without proper citing.

Create a Leet Translator!

Post by -Ninjex- on Thu Aug 08, 2013 2:05 am
([msg=76780]see Create a Leet Translator![/msg])

This is part of the continuous programming challenges!

The challenge:
Level: Intermediate

Create a program that will convert plain text into leet text.
For example:
Chuck Norris wrote:Hello there!
#€££ø †#€Я€!

The program should also convert leet text into plain text. (This can be case insensitive as long as it's in English)
If the translator does not print out all characters converted or not, points may be reduced.
Points: 100

Extra points will be awarded if some of your leet characters are more than one character in length. i.e. H = |-|

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

I will start off by posting my code. I went with a different approach, and completed this with bash.

Code: Select all
#!/bin/bash
if [ "$1" == "-e" ]; then # if the cli argument is -e
   OPT="encrypt"; # set the option to encrypt
elif [ "$1" == "-d" ]; then # if the cli argument is -d
   OPT="decrypt"; # set the option to decrypt
else # else show the proper usage
        echo "Usage - Encrypt text: ./l33t.sh -e text";
        echo "Usage - Decrypt text: ./l33t.sh -d text";
        exit;
fi
#creating an array for leet text and plain text
declare -a LEET=('ɐ' 'ß' '©' 'Ð' '€' 'ƒ' '&' '#' 'I' '¿' 'K' '£' 'M' '?' 'ø' 'p' 'Q' 'Я' '§' '†' 'µ' '^' 'W' '×' '¥' 'z​' '1' '2' '3' '4' '5' '6' '7' '8' '9' '0' ' ');
declare -a ENG=('a' 'b' 'c' 'd' 'e' 'f' 'g' 'h' 'i' 'j' 'k' 'l' 'm' 'n' 'o' 'p' 'q' 'r' 's' 't' 'u' 'v' 'w' 'x' 'y' 'z' '1' '2' '3' '4' '5' '6' '7' '8' '9' '0' ' ');
echo -n "Please enter a string to $OPT: "; # asking for user input
read INPUT; # grab the input
WORDS=${INPUT,,};
while IFS= read -r letter; # for each character in the input (check the grep near done)
do
   for i in {0..36} # for each item in the array
   do
      if [ "$OPT" == "encrypt" ]; then # if the option is set to encrypt
         FIND=${ENG[$i]}; # the array to look through is the plain array
      elif [ "$OPT" == "decrypt" ]; then # else the array to look through is the leet text array
         FIND=${LEET[$i]};
      fi

      if [ "$OPT" == "encrypt" ]; then # if the option is set to encrypt
         if [ "$FIND" == "$letter" ]; then # if our character is in the plain array
            ENCRYPTED+=${LEET[$i]}; # Add to Encrypted that values leet transformation
            FOUND="true";
            break;
         fi
      elif [ "$OPT" == "decrypt" ]; then # else do the same thing except with oposite arrays
         if [ "$FIND" == "$letter" ]; then
                                ENCRYPTED+=${ENG[$i]};
            FOUND="true";
            break;
                        fi
      fi
   done
   if [ "$FOUND" != "true" ]; then # checking if the character is in the leet list
      ENCRYPTED+=$letter; # if it isn't, add the letter to the string
   fi
   FOUND=''; # reset our boolean
done < <(grep -o . <<< $WORDS)
echo "$ENCRYPTED"; # echo the result
Last edited by -Ninjex- on Sun Aug 11, 2013 3:09 pm, edited 2 times in total.
image
For those that know
K: 0x2CD8D4F9
User avatar
-Ninjex-
Moderator
Moderator
 
Posts: 1691
Joined: Sun Sep 02, 2012 8:02 pm
Blog: View Blog (0)


Re: Create a Leet Translator!

Post by pretentious on Thu Aug 08, 2013 7:13 am
([msg=76781]see Re: Create a Leet Translator![/msg])

After about 20 minutes, i gave up. I hate that feeling where i get everything looking sexy only to realize that C has trouble with unicode. Would probably have to go for cstrings with the whole "|-|" as you described, and reimplament half the code i've done.
Code: Select all
#include <stdio.h>
const char leet[]={'ɐ', 'ß', '©', 'Ð', '€', 'ƒ', '&', '#', 'I', '¿', 'K', '£', 'M', '?', 'ø', 'p', 'Q', 'Я', '§', '†', 'µ', '^', 'W', '×', '¥', 'z​', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', ' '};
const char eng[]={'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', ' '};
int character;
void decrypt(){
while((character = getchar())){
int count;
for (count = 0;count < 36;count++){
if((int)leet[count]==character)
   printf("%c",eng[count]);
}
}
}
void encrypt(){;
while((character = getchar())){
int count;
for (count = 0;count < 36;count++){
if((int)eng[count]==character)
   printf("%c",leet[count]);
}
}
}
void usage(){
printf("leet [-e|-d]\n");
}
int main(int argc, char *argv[]){
   if(argc == 2){
      char choice = argv[1][1];
      switch(choice){
      case 'e': encrypt();
      break;
      case 'd': decrypt();
      break;
      default: usage();
      }
   }else{
      usage();
   }

   return 0;
}


edit: did it again in java which uses 2 bytes for characters
Code: Select all

import java.util.Scanner;

public class Leet {
    static Scanner input = new Scanner(System.in);
static char leet[]={'ɐ', 'ß', '©', 'Ð', '€', 'ƒ', '&', '#', 'I', '¿', 'K', '£', 'M', '?', 'ø', 'p', 'Q', 'Я', '§', '†', 'µ', '^', 'W', '×', '¥', 'z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', ' '};
static char eng[]={'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', ' '};
int character;

static void decrypt(){
    while(input.hasNext()){
        String line = input.nextLine();
        char[] c = line.toCharArray();
        boolean found = false;
        for(int i = 0; i < line.length(); i ++){
            for(int z = 0; z < 36; z ++){
               if((int)c[i] ==(int)leet[z]){
                   found = true;
                 System.out.print(eng[z]);
               }
            }
            if(found == false){
                System.out.print(c);
            }
        }
    }
}
static void encrypt(){
    while(input.hasNext()){
        String line = input.nextLine();
        char[] c = line.toCharArray();
        boolean found = false;
        for(int i = 0; i < line.length(); i ++){
            for(int z = 0; z < 36; z ++){
               if((int)c[i] ==(int)eng[z]){
                   found = true;
                 System.out.print(leet[z]);
               }
            }
            if(found == false){
                System.out.print(c);
            }
        }
    }
}
static void usage(){
System.out.println("leet [-e|-d]\n");
}
    public static void main(String[] args) {
        if(args.length == 1){
                if(args[0].compareTo("-e")==0){
                    encrypt();
                }else if(args[0].compareTo("-d")==0){
                    decrypt();
                }
   }else{
      System.out.println(args.length);
      usage();
   }
    }
}
Goatboy wrote:Oh, that's simple. All you need to do is dedicate many years of your life to studying security.

IF you feel like exchanging ASCII arrays, let me know ;)
Can you say brainwashing It's a non stop disco
User avatar
pretentious
Addict
Addict
 
Posts: 1217
Joined: Wed Mar 03, 2010 12:48 am
Blog: View Blog (0)


Re: Create a Leet Translator!

Post by -Ninjex- on Thu Aug 08, 2013 3:15 pm
([msg=76785]see Re: Create a Leet Translator![/msg])

Pretentious, good work.
The C++ program however was failing to display the Unicode characters correctly for me.
The Java program seemed to work when converting characters. However, there are a few things that should be fixed. So I will judge the points from the Java program.
1. The program completely ignores the fact of spaces.
2. The program does not take into account every character even if it's not a convertible character, for example (hello! produces #€££ø instead of #€££ø!)
3. The program doesn't work for capital letters, try and make it case insensitive.
As a side note, it would be better to append a new line feed after every conversion so that it's not jumbled up.

For now, I will give you 50 points.
If you can fix the above errors, you will get the other 50 points.
image
For those that know
K: 0x2CD8D4F9
User avatar
-Ninjex-
Moderator
Moderator
 
Posts: 1691
Joined: Sun Sep 02, 2012 8:02 pm
Blog: View Blog (0)


Re: Create a Leet Translator!

Post by pretentious on Fri Aug 09, 2013 12:22 am
([msg=76790]see Re: Create a Leet Translator![/msg])

Yeah the C version failed. I could try and deal with some libraries or using W_char or something as suggested online but there really aren't enough hours left in my life. A million 'who's line is it anyway' points to anyone who gets this figured out.
With Java on the other hand, I've mad some adjustments. I removed the numbers from the translation arrays since they are the same in both, I also converted all input to lower case since there isn't really a leet alphabet equivilant, i figured that was safe, though for the the d and r, there was so i had to change that. I added the new line statement and realized the ignoring of spaces was a pretty major logical error that i guess i didn't see last night.
Code: Select all
import java.util.Scanner;

public class Leet {
static Scanner input = new Scanner(System.in);
static char leet[]={'ɐ', 'ß', '©', 'd', '€', 'ƒ', '&', '#', 'I', '¿', 'K', '£', 'M', '?', 'ø', 'p', 'Q', 'я', '§', '†', 'µ', '^', 'W', '×', '¥', 'z'};
static char eng[]={'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
int character;

static void decrypt(){
     while(input.hasNext()){
        String line = input.nextLine();
        line = line.toLowerCase();
        char[] c = line.toCharArray();
        boolean found;
        for(int i = 0; i < line.length(); i ++){
            found = false;
            for(int z = 0; z < 26; z ++){
               if((int)c[i] ==(int)leet[z]){
                   found = true;
                 System.out.print(eng[z]);
               }
            }
            if(found == false){
                System.out.print(c[i]);
            }
        }
        System.out.println();
    }
}
static void encrypt(){
    while(input.hasNext()){
        String line = input.nextLine();
        line = line.toLowerCase();
        char[] c = line.toCharArray();
        boolean found;
        for(int i = 0; i < line.length(); i ++){
            found = false;
            for(int z = 0; z < 26; z ++){
               if((int)c[i] ==(int)eng[z]){
                   found = true;
                 System.out.print(leet[z]);
               }
            }
            if(found == false){
                System.out.print(c[i]);
            }
        }
        System.out.println();
    }
}
static void usage(){
System.out.println("leet [-e|-d]\n");
}
    public static void main(String[] args) {
        if(args.length == 1){
                if(args[0].compareTo("-e")==0){
                    encrypt();
                }else if(args[0].compareTo("-d")==0){
                    decrypt();
                }
   }else{
      usage();
   }
    }
}
Goatboy wrote:Oh, that's simple. All you need to do is dedicate many years of your life to studying security.

IF you feel like exchanging ASCII arrays, let me know ;)
Can you say brainwashing It's a non stop disco
User avatar
pretentious
Addict
Addict
 
Posts: 1217
Joined: Wed Mar 03, 2010 12:48 am
Blog: View Blog (0)


Re: Create a Leet Translator!

Post by -Ninjex- on Fri Aug 09, 2013 1:05 am
([msg=76791]see Re: Create a Leet Translator![/msg])

Great work it checks out from my end, the program follows all the given rules! You will receive the additional 50 points I promised.
I really wish to see more people hop onto these challenges, as I am debating on listing more if so.
Thanks for participating in this challenge!
image
For those that know
K: 0x2CD8D4F9
User avatar
-Ninjex-
Moderator
Moderator
 
Posts: 1691
Joined: Sun Sep 02, 2012 8:02 pm
Blog: View Blog (0)


Re: Create a Leet Translator!

Post by VvGHOSTvV on Fri Aug 09, 2013 5:02 am
([msg=76793]see Re: Create a Leet Translator![/msg])

.-. working on it...
"In our country we have a problem, the government"
User avatar
VvGHOSTvV
New User
New User
 
Posts: 47
Joined: Thu Jul 19, 2012 7:09 pm
Location: Oklahoma
Blog: View Blog (0)


Re: Create a Leet Translator!

Post by -Ninjex- on Fri Aug 09, 2013 6:33 am
([msg=76794]see Re: Create a Leet Translator![/msg])

Hehe, someone kind of spiked my interest to do this in PHP, so here it is:
Language: PHP
Using: php5-cli
Run from the command line
Code: Select all
<?php

$convert = array(
'a' => '4', 'b' => '8', 'c' => 'c', 'd' => 'd',
'e' => '3', 'f' => '|=', 'g' => '6', 'h' => '|-|',
'i' => '1', 'j' => ']', 'k' => '|<', 'l' => '7',
'm' => 'M', 'n' => '|\|', 'o' => '0', 'p' => '9',
'q' => 'Q', 'r' => '|2', 's' => '5', 't' => '+',
'u' => '|_|', 'v' => '\/', 'w' => 'W', 'x' => 'X',
'y' => '\'/', 'z' => 'Z', ' ' => ' ',
);

while(true) {
fputs(STDOUT, 'Enter a choice, [e]ncrypt [d]ecrypt [q]uit [e/d/q]: ');
$choice = trim(fgets(STDIN));
if($choice == 'e') $choice = "encrypt";
elseif($choice == 'd') $choice = "decrypt";
elseif($choice == 'q') { echo "Thank you for using the program, c '/4 74+3|2!\n"; exit; }
else echo "You entered an invalid choice! Please use options: [e/d/q]\n";
if($choice == 'encrypt' || $choice == 'decrypt') {
   fputs(STDOUT, "Please enter the string to $choice: ");
   $string = strtolower(trim(fgets(STDIN)));
   convert($convert, $string, $choice);
   }
}

function convert($array, $string, $choice) {
   if($choice == "encrypt") cipher($array, $string);
   else decipher($array, $string);
}

function cipher($convert, $plain) {
   $answer = $plain;
   foreach($convert as $plain => $l337) {
      $answer = str_replace($plain, $l337, $answer);
   } echo "$answer\n";
}


function decipher($convert, $l337) {
   $answer = $l337;
   foreach($convert as $plain => $l337) {
      $answer = str_replace($l337, $plain, $answer);
   } echo "$answer\n";
}
?>
image
For those that know
K: 0x2CD8D4F9
User avatar
-Ninjex-
Moderator
Moderator
 
Posts: 1691
Joined: Sun Sep 02, 2012 8:02 pm
Blog: View Blog (0)


Re: Create a Leet Translator!

Post by mShred on Sun Aug 11, 2013 2:37 pm
([msg=76806]see Re: Create a Leet Translator![/msg])

**UPDATED
Psh.. Forgot to add a decrypting section. It was extremely pretty before I hashed it all together to add the decrypt.

Code: Select all
<?php
        $norm = array(NULL,'a','b','c','d','e','f','g','h','i','j','k','l','m','n',
                'o','p','q','r','s','t','u','v','w','x','y','z');
                /* NULL place holder since the first key in the array will return
                        NULL and fail on the loop*/

        $leet = array(NULL,'@','ß','¢','Ð','3','f','&','#','¡','¿','K','£','M','ñ',
                '°','þ','¿','®','§','÷','µ','^','W','×','¥','z');

        $out = null;
        $p = null;

        echo("Enter 'e' for encrypt or 'd' for decrypt:\r\n");
        $p = substr(fgets(STDIN),0,-1);

        if($p != 'e' && $p != 'd') { die("You Fail.\r\n"); }

        echo("Type a word in le engrish:\r\n");
        $word = fgets(STDIN);

        if($p == 'e') {
                foreach(str_split(trim($word),1) as $l) {
                        if((($key = array_search(strtolower($l),$norm)) == NULL)
                                || ((ctype_alnum($l) == FALSE) && ($l != ' '))) { $out .= $l;
                                        } else { $out .= $leet[$key]; }
                }
        } elseif($p == 'd') {
                foreach(str_split(trim($word),1) as $l) {
                        if(($key = array_search($l,$leet)) == NULL) {
                                $out .= $l;
                        } else { $out .= $norm[$key]; }
                }
        }

        echo("$out\r\n");
                /* FIN */
?>
User avatar
mShred
Addict
Addict
 
Posts: 1899
Joined: Tue Jun 22, 2010 4:22 pm
Blog: View Blog (2)


Re: Create a Leet Translator!

Post by centip3de on Sun Aug 11, 2013 3:32 pm
([msg=76807]see Re: Create a Leet Translator![/msg])

+1 for the initiative, although an easy challenge. Done in Python and Scala (might do one in D if I get around to it). As a bonus, the Scala one can encrypt or decrypt from the same input (i.e. there is no "Encrypt/Decrypt/Quit", just "Start/Quit").

Python:
Code: Select all
# -*- coding: utf-8 -*-
import sys
from string import *

while(1):
  userInput = raw_input("[E]ncode/[D]ecode/[Q]uit: ")
  if(userInput == "Q" or userInput == "q"):
    sys.exit(0)

  startInput = raw_input("Begin string: ").decode(sys.stdin.encoding)
  english = "abcdefghijklmnopqrstuvwxyz!@#$%^&*()_+-={}|[]\:\";'<>?,./` "
  leet =  u"48ç∂3ƒ6#1]¬7M~o9Q®5+U^WX¥Z!@#$%^&*()_+-={}|[]\:\";'<>?,./` "
  newString = ""

  if(userInput == "E" or userInput == "e"):
   for char in startInput.lower():
     i = english.index(char)
     newString += leet[i]
   print newString

  elif(userInput == "D" or userInput == "d"):
    for char in startInput:
      i = leet.index(char)
      newString += english[i]
    print newString

  else:
    print "Unknown command: " + userInput



Scala:
Code: Select all
package org;

object Main
{   
  val dict = Map("a" -> "4", "b" -> "8", "c" -> "ç", "d" -> "∂",
              "e" -> "3", "f" -> "ƒ", "g" -> "6", "h" -> "#",
              "i" -> "1", "j" -> "]", "k" -> "¬", "l" -> "7",
              "m" -> "M", "n" -> "~", "o" -> "0", "p" -> "9",
              "q" -> "Q", "r" -> "®", "s" -> "5", "t" -> "+",
              "u" -> "µ", "v" -> "^", "w" -> "W", "x" -> "X",
              "y" -> "¥", "z" -> "Z", " " -> " ", "4" -> "a",
              "8" -> "b", "8" -> "c", "ç" -> "d", "3" -> "e",
              "ƒ" -> "f", "6" -> "g", "!" -> "!", "," -> ",",
              "@" -> "@", "#" -> "#", "$" -> "$", "%" -> "%",
              "^" -> "^", "&" -> "&", "*" -> "*", "(" -> "(",
              ")" -> ")", "_" -> "_", "-" -> "-", "=" -> "=",
              "+" -> "+", "[" -> "[", "]" -> "]", "{" -> "{",
              "}" -> "}", "\\" -> "\\", "|" -> "|", "\"" -> "\"",
              "'" -> "'", ";" -> ";", ":" -> ":", "." -> ".",
              "<" -> "<", ">" -> ">", "?" -> "?", "~" -> "~",
              "#" -> "h", "1" -> "i", "]" -> "j", "¬¨" -> "k",
              "7" -> "l", "M" -> "m", "~" -> "n", "0" -> "o",
              "9" -> "p", "Q" -> "q", "®" -> "r", "5" -> "s",
              "+" -> "t", "µ" -> "u", "^" -> "v", "W" -> "w",
              "X" -> "x", "¥" -> "y", "Z" -> "z", "`" -> "`");
 
  def start()
  {
    var bar:String = "";
    for(input <- io.Source.stdin.getLines)
    {
       input.foreach(x => bar += dict(x.toLower.toString));
       println(bar);
       bar = "";
    }
  }
 
  def main(args:Array[String])
  {
    print("[S]tart/[Q]uit: ");
    val choice = Console.readLine() match
    {
      case "s" | "S" => start();
      case "q" | "Q" => System.exit(0);
    }
   
  }
}

Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rick Cook
User avatar
centip3de
Addict
Addict
 
Posts: 1479
Joined: Fri Aug 20, 2010 5:46 pm
Blog: View Blog (0)


Re: Create a Leet Translator!

Post by -Ninjex- on Sun Aug 11, 2013 4:27 pm
([msg=76808]see Re: Create a Leet Translator![/msg])

@mShred, looks great for encrypting, but you still need to add the decryption for this. I will give you 50 points until completion

@centip3de, both codes seem to be doing the job of encrypting and decrypting, except with symbols not found in your leet array (i.e. !@#$) Since you submitted the answer in two languages, that are partially correct, you get: 60 for the incomplete programs and another 10 for submitting two answers, for a total of 70 points. Fix the errors on one and you will get another 50 points, however if you fix both you will get 60 points.
image
For those that know
K: 0x2CD8D4F9
User avatar
-Ninjex-
Moderator
Moderator
 
Posts: 1691
Joined: Sun Sep 02, 2012 8:02 pm
Blog: View Blog (0)


Next

Return to User Submitted

Who is online

Users browsing this forum: No registered users and 0 guests