Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions src/main/java/org/fundacionjala/coding/ovidio/ToLeetSpeak.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package org.fundacionjala.coding.ovidio;

import java.util.HashMap;
import java.util.Map;

/**
* Your task is to write function toLeetSpeak that converts a regular english sentence to Leetspeak.
* More about LeetSpeak You can read at wiki -> https://en.wikipedia.org/wiki/Leet
* Consider only uppercase letters (no lowercase letters, no numbers) and spaces.
* For example:
* toLeetSpeak("LEET") returns "1337"
* {
* A : '@',
* B : '8',
* C : '(',
* D : 'D',
* E : '3',
* F : 'F',
* G : '6',
* H : '#',
* I : '!',
* J : 'J',
* K : 'K',
* L : '1',
* M : 'M',
* N : 'N',
* O : '0',
* P : 'P',
* Q : 'Q',
* R : 'R',
* S : '$',
* T : '7',
* U : 'U',
* V : 'V',
* W : 'W',
* X : 'X',
* Y : 'Y',
* Z : '2'
*/
public class ToLeetSpeak {

/**
*
*/
private static final Map<Character, Character> ALPHABET = new HashMap<Character, Character>() {
{
put(' ', ' ');
put('A', '@');
put('B', '8');
put('C', '(');
put('D', 'D');
put('E', '3');
put('F', 'F');
put('G', '6');
put('H', '#');
put('I', '!');
put('J', 'J');
put('K', 'K');
put('L', '1');
put('M', 'M');
put('N', 'N');
put('O', '0');
put('P', 'P');
put('Q', 'Q');
put('R', 'R');
put('S', '$');
put('T', '7');
put('U', 'U');
put('V', 'V');
put('W', 'W');
put('X', 'X');
put('Y', 'Y');
put('Z', '2');

}
};

/**
* Method for translation.
*
* @param speak Receive the sentence.
* @return String the translation of speak.
*/
String toLeetSpeak(final String speak) {
StringBuilder translation = new StringBuilder();
for (Character a : speak.replaceAll("^[^A-Z]", "").toCharArray()) {
translation.append(ALPHABET.get(a) == null ? "" : ALPHABET.get(a));
}
return translation.toString();
}
}
82 changes: 82 additions & 0 deletions src/main/java/org/fundacionjala/coding/ovidio/Welcome.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package org.fundacionjala.coding.ovidio;

import java.util.HashMap;
import java.util.Map;

/**
* Welcome!
* Your start-up's BA has told marketing that your website has a large audience in Scandinavia
* and surrounding countries. Marketing thinks it would be great to welcome visitors to the site in
* their own language. Luckily you already use an API that detects the user's location, so this is an
* easy win.
* <p>
* The Task
* <p>
* Think of a way to store the languages as a database (eg an object). The languages are listed below
* so you can copy and paste!
* Write a 'welcome' function that takes a parameter 'language' (always a string), and returns a
* greeting - if you have it in your database. It should default to English if the language is not
* in the database, or in the event of an invalid input.
* The Database
* <p>
* english: "Welcome",
* czech: "Vitejte",
* danish: "Velkomst",
* dutch: "Welkom",
* estonian: "Tere tulemast",
* finnish: "Tervetuloa",
* flemish: "Welgekomen",
* french: "Bienvenue",
* german: "Willkommen",
* irish: "Failte",
* italian: "Benvenuto",
* latvian: "Gaidits",
* lithuanian: "Laukiamas",
* polish: "Witamy",
* spanish: "Bienvenido",
* swedish: "Valkommen",
* welsh: "Croeso"
* <p>
* Possible invalid inputs include:
* <p>
* IP_ADDRESS_INVALID - not a valid ipv4 or ipv6 ip address
* IP_ADDRESS_NOT_FOUND - ip address not in the database
* IP_ADDRESS_REQUIRED - no ip address was supplied
*/
public class Welcome {

private static final Map<String, String> LANGUAGES = new HashMap<String, String>() {
{
put("english", "Welcome");
put("czech", "Vitejte");
put("danish", "Velkomst");
put("dutch", "Welkom");
put("estonian", "Tere tulemast");
put("finnish", "Tervetuloa");
put("flemish", "Welgekomen");
put("french", "Bienvenue");
put("german", "Willkommen");
put("irish", "Failte");
put("italian", "Benvenuto");
put("latvian", "Gaidits");
put("lithuanian", "Laukiamas");
put("polish", "Witamy");
put("spanish", "Bienvenido");
put("swedish", "Valkommen");
put("welsh", "Croeso");

}
};

/**
* Return the greet.
*
* @param language receive the language.
* @return String greet .
*/

String greet(String language) {
return LANGUAGES.get(language) == null ? LANGUAGES.get("english") : LANGUAGES.get(language);
}
}

34 changes: 34 additions & 0 deletions src/test/java/org/fundacionjala/coding/ovidio/ToLeetSpeakTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.fundacionjala.coding.ovidio;

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
* Created by Simon on 22/08/2017.
*/
public class ToLeetSpeakTest {
private ToLeetSpeak toLeetSpeak;

/**
* Execute before each test method.
*/
@Before
public void setUp() {
toLeetSpeak = new ToLeetSpeak();
}

/**
* Example Test.
*/
@Test
public void testSomething() {
assertEquals("1337", toLeetSpeak.toLeetSpeak("LEET"));
assertEquals("(0D3W@R$", toLeetSpeak.toLeetSpeak("CODEWARS"));
assertEquals("#3110 W0R1D", toLeetSpeak.toLeetSpeak("HELLO WORLD"));
assertEquals("10R3M !P$UM D010R $!7 @M37", toLeetSpeak.toLeetSpeak("LOREM IPSUM DOLOR SIT AMET"));
String result = "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG";
assertEquals("7#3 QU!(K 8R0WN F0X JUMP$ 0V3R 7#3 1@2Y D06", toLeetSpeak.toLeetSpeak(result));
}
}
33 changes: 33 additions & 0 deletions src/test/java/org/fundacionjala/coding/ovidio/WelcomeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.fundacionjala.coding.ovidio;

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
* Created by Simon on 22/08/2017.
*/
public class WelcomeTest {
public static final String MESSAGE = "Your function should have returned 'Welcome'. Try again.";
private Welcome welcome;

/**
* Execute before each test method.
*/
@Before
public void setUp() {
welcome = new Welcome();
}

/**
* Example Test.
*/
@Test
public void exampleTest() {

assertEquals(MESSAGE, "Welcome", welcome.greet("english"));
assertEquals(MESSAGE, "Welkom", welcome.greet("dutch"));
assertEquals(MESSAGE, "Welcome", welcome.greet("IP_ADDRESS_INVALID"));
}
}