Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Submit typescript and python challenge #2625

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
node_modules
yarn.lock
package-lock.json
pnpm-lock.yaml
Gemfile.lock
Binary file added python/__pycache__/dictionary.cpython-312.pyc
Binary file not shown.
Binary file added python/__pycache__/functions.cpython-312.pyc
Binary file not shown.
114 changes: 114 additions & 0 deletions python/dictionary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
from typing import Dict

braille_to_english: Dict[str, str] = {
"O.....": "a",
"O.O...": "b",
"OO....": "c",
"OO.O..": "d",
"O..O..": "e",
"OOO...": "f",
"OOOO..": "g",
"O.OO..": "h",
".OO...": "i",
".OOO..": "j",
"O...O.": "k",
"O.O.O.": "l",
"OO..O.": "m",
"OO.OO.": "n",
"O..OO.": "o",
"OOO.O.": "p",
"OOOOO.": "q",
"O.OOO.": "r",
".OO.O.": "s",
".OOOO.": "t",
"O...OO": "u",
"O.O.OO": "v",
".OOO.O": "w",
"OO..OO": "x",
"OO.OOO": "y",
"O..OOO": "z",
".....O": "capital",
".O...O": "decimal",
".O.OOO": "number",
"......": " "
}

braille_num_to_english: Dict[str, str] = {
"O.....": "1",
"O.O...": "2",
"OO....": "3",
"OO.O..": "4",
"O..O..": "5",
"OOO...": "6",
"OOOO..": "7",
"O.OO..": "8",
".OO...": "9",
".OOO..": "0",
"..OO.O": ".",
"..O...": ",",
"..O.OO": "?",
"..OOO.": "!",
"..OO..": ":",
"..O.O.": ";",
"....OO": "-",
".O..O.": "/",
".OO..O": "<",
"O..OO.": ">",
"O.O..O": "(",
".O.OO.": ")"
}

english_to_braille: Dict[str, str] = {
"a": "O.....",
"b": "O.O...",
"c": "OO....",
"d": "OO.O..",
"e": "O..O..",
"f": "OOO...",
"g": "OOOO..",
"h": "O.OO..",
"i": ".OO...",
"j": ".OOO..",
"k": "O...O.",
"l": "O.O.O.",
"m": "OO..O.",
"n": "OO.OO.",
"o": "O..OO.",
"p": "OOO.O.",
"q": "OOOOO.",
"r": "O.OOO.",
"s": ".OO.O.",
"t": ".OOOO.",
"u": "O...OO",
"v": "O.O.OO",
"w": ".OOO.O",
"x": "OO..OO",
"y": "OO.OOO",
"z": "O..OOO",
" ": "......",
"1": "O.....",
"2": "O.O...",
"3": "OO....",
"4": "OO.O..",
"5": "O..O..",
"6": "OOO...",
"7": "OOOO..",
"8": "O.OO..",
"9": ".OO...",
"0": ".OOO..",
"capital": ".....O",
"decimal": ".O...O",
"number": ".O.OOO",
".": "..OO.O",
",": "..O...",
"?": "..O.OO",
"!": "..OOO.",
":": "..OO..",
";": "..O.O.",
"-": "....OO",
"/": ".O..O.",
"<": ".OO..O",
">": "O..OO.",
"(": "O.O..O",
")": ".O.OO."
}
63 changes: 63 additions & 0 deletions python/functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from dictionary import braille_to_english, braille_num_to_english, english_to_braille
from typing import Tuple
import re

def is_braille(input: str) -> bool:
lookFor: Tuple[str, str] = (".", "O")

if any(char in input for char in lookFor):
return True
return False


def translate_braille_to_english(braille: str) -> str:
english: str = ""
capital: bool = False
integer: bool = False
decimal: bool = False

for char in range(0, len(braille), 6):
braille_char = braille[char:char + 6]
translated_char = braille_to_english.get(braille_char, '')
translated_num = braille_num_to_english.get(braille_char, '')

if translated_char == "capital":
capital = True
integer = False
elif translated_char == "number":
integer = True
elif translated_char == "decimal":
decimal = True
else:
if capital:
english+= translated_char.upper()
capital = False
elif integer and re.findall(r'[a-j]', translated_char):
english += translated_num
elif decimal:
english+= translated_char
decimal = False
else:
english+= translated_char

return english.strip()


def translate_english_to_braille(english: str) -> str:
braille: str = ""
integer: bool = False

for char in english:
if re.findall(r'[A-Z]', char):
braille+= english_to_braille.get("capital", '')
braille+= english_to_braille.get(char.lower(), '')
elif re.findall(r'[0-9]', char):
if not integer:
integer = True
braille+= english_to_braille["number"]
braille+= english_to_braille[char]
else:
braille+= english_to_braille[char]
integer = False

return braille.strip()
15 changes: 15 additions & 0 deletions python/translator.py
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
from functions import is_braille, translate_braille_to_english, translate_english_to_braille
import sys

def main():
input_str = " ".join(sys.argv[1::])
output = None

if not is_braille(input_str):
output = translate_english_to_braille(input_str)
else:
output = translate_braille_to_english(input_str)
print(output)


if __name__ == "__main__":
main()
114 changes: 114 additions & 0 deletions typescript/dictionary.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// This file contains the "dictionaries" for two-way translation between English and Braille

export const brailleToEnglish: Record<string, string> = {
"O.....": "a",
"O.O...": "b",
"OO....": "c",
"OO.O..": "d",
"O..O..": "e",
"OOO...": "f",
"OOOO..": "g",
"O.OO..": "h",
".OO...": "i",
".OOO..": "j",
"O...O.": "k",
"O.O.O.": "l",
"OO..O.": "m",
"OO.OO.": "n",
"O..OO.": "o",
"OOO.O.": "p",
"OOOOO.": "q",
"O.OOO.": "r",
".OO.O.": "s",
".OOOO.": "t",
"O...OO": "u",
"O.O.OO": "v",
".OOO.O": "w",
"OO..OO": "x",
"OO.OOO": "y",
"O..OOO": "z",
".....O": "capital",
".O...O": "decimal",
".O.OOO": "number",
"......": " "
}

export const brailleNumToEnglish: Record<string, string> = {
"O.....": "1",
"O.O...": "2",
"OO....": "3",
"OO.O..": "4",
"O..O..": "5",
"OOO...": "6",
"OOOO..": "7",
"O.OO..": "8",
".OO...": "9",
".OOO..": "0",
"..OO.O": ".",
"..O...": ",",
"..O.OO": "?",
"..OOO.": "!",
"..OO..": ":",
"..O.O.": ";",
"....OO": "-",
".O..O.": "/",
".OO..O": "<",
"O..OO.": ">",
"O.O..O": "(",
".O.OO.": ")"
}

export const englishToBraille: Record<string, string> = {
"a": "O.....",
"b": "O.O...",
"c": "OO....",
"d": "OO.O..",
"e": "O..O..",
"f": "OOO...",
"g": "OOOO..",
"h": "O.OO..",
"i": ".OO...",
"j": ".OOO..",
"k": "O...O.",
"l": "O.O.O.",
"m": "OO..O.",
"n": "OO.OO.",
"o": "O..OO.",
"p": "OOO.O.",
"q": "OOOOO.",
"r": "O.OOO.",
"s": ".OO.O.",
"t": ".OOOO.",
"u": "O...OO",
"v": "O.O.OO",
"w": ".OOO.O",
"x": "OO..OO",
"y": "OO.OOO",
"z": "O..OOO",
" ": "......",
"1": "O.....",
"2": "O.O...",
"3": "OO....",
"4": "OO.O..",
"5": "O..O..",
"6": "OOO...",
"7": "OOOO..",
"8": "O.OO..",
"9": ".OO...",
"0": ".OOO..",
"capital": ".....O",
"decimal": ".O...O",
"number": ".O.OOO",
".": "..OO.O",
",": "..O...",
"?": "..O.OO",
"!": "..OOO.",
":": "..OO..",
";": "..O.O.",
"-": "....OO",
"/": ".O..O.",
"<": ".OO..O",
">": "O..OO.",
"(": "O.O..O",
")": ".O.OO."
}
Loading
Loading