4 kyu
Brainfuck Translator
278 of 825Varveyn
Description:
Introduction
Brainfuck is one of the most well-known esoteric programming languages. But it can be hard to understand any code longer that 5 characters. In this kata you have to solve that problem.
Description
In this kata you have to write a function which will do 3 tasks:
- Optimize the given Brainfuck code.
- Check it for mistakes.
- Translate the given Brainfuck programming code into C programming code.
More formally about each of the tasks:
-
Your function has to remove from the source code all useless command sequences such as:
'+-', '<>', '[]'
. Also it must erase all characters except+-<>,.[]
.Example:
"++--+." -> "+." "[][+++]" -> "[+++]" "<>><" -> ""
-
If the source code contains unpaired braces, your function should return
"Error!"
string. -
Your function must generate a string of the C programming code as follows:
-
Sequences of the X commands
+
or-
must be replaced by\*p += X;\n
or\*p -= X;\n
.Example:
"++++++++++" -> "\*p += 10;\n" "------" -> "\*p -= 6;\n"
-
Sequences of the Y commands
>
or<
must be replaced byp += Y;\n
orp -= Y;\n
.Example:
">>>>>>>>>>" -> "p += 10;\n" "<<<<<<" -> "p -= 6;\n"
-
.
command must be replaced byputchar(\*p);\n
.Example:
".." -> "putchar(\*p);\nputchar(\*p);\n"
-
,
command must be replaced by\*p = getchar();\n
.Example:
"," -> "\*p = getchar();\n"
-
[
command must be replaced byif (\*p) do {\n
.]
command must be replaced by} while (\*p);\n
.Example:
"[>>]" -> if (\*p) do {\n p += 2;\n } while (\*p);\n
-
Each command in the code block must be shifted 2 spaces to the right accordingly to the previous code block.
Example:
"[>>[<<]]" -> if (\*p) do {\n p += 2;\n if (\*p) do {\n p -= 2;\n } while (\*p);\n } while (\*p);\n
-
Sequences of the X commands
Examples
Input:
+++++[>++++.<-]
Output:
*p += 5;
if (*p) do {
p += 1;
*p += 4;
putchar(*p);
p -= 1;
*p -= 1;
} while (*p);
Esoteric Languages
Strings
Similar Kata:
Stats:
Created | Feb 1, 2017 |
Published | Feb 7, 2017 |
Warriors Trained | 8720 |
Total Skips | 4355 |
Total Code Submissions | 20200 |
Total Times Completed | 825 |
C++ Completions | 148 |
C Completions | 188 |
Python Completions | 278 |
Java Completions | 75 |
JavaScript Completions | 133 |
PHP Completions | 38 |
CoffeeScript Completions | 8 |
Total Stars | 384 |
% of votes with a positive feedback rating | 94% of 229 |
Total "Very Satisfied" Votes | 203 |
Total "Somewhat Satisfied" Votes | 23 |
Total "Not Satisfied" Votes | 3 |
Total Rank Assessments | 5 |
Average Assessed Rank | 2 kyu |
Highest Assessed Rank | 2 kyu |
Lowest Assessed Rank | 3 kyu |