4 kyu

Brainfuck Translator

39 of 832Varveyn
Description
Loading description...
Esoteric Languages
Strings
  • Please sign in or sign up to leave a comment.
  • Itsacon Avatar

    Issue with the PHP tests:

    These incorrectly replace multiple consecutive input or output characters (, & .) with just a single putchar / getchar call.

    This is incorrect, as while reading multiple bytes from input would only store the last, it would still consume multiple bytes.

    Same goes for the output: the written character would be the same each time, but it would still be written twice.

    (The interpreter in the tests would print Helo, World!, with just a single 'l', for example).

    I could get my solution to pass by implementing the same error, but that is obviously not right.

    Edit: Just noticed hotdonkey already reported this 9 months ago...

  • cplir-c Avatar

    For the C translation, the tests expect us to return a newly-allocated character string, but never frees it. I know it doesn't free it now because the initial solution returns a statically allocated string instead and doesn't break the allocator. A change to the function's interface or the initial solution would allow the tests to free the returned string instead of wasting that memory.

  • Terteria Avatar

    The tip for C: more memory,very more

  • kflippy Avatar

    completely changes the output? i dont even know how it gets the output its getting, i even tried changing compiler to match theres, and it outputs the correct answer

  • mxzi Avatar

    ummmmm Expected: equal to "*p += 4; " Actual: "*p += 4;"

  • jpssj Avatar

    JS fork, please review:

    • use Chai;
    • use lodash and replace Test.randomNumber() % 8 by _.random(0, 7);
    • define and use an assertStrictEqualPretty function (thanks to @Hobovsky) for large inputs (but there's still a problem with the ~1MB input).
  • hotdonkey Avatar

    In description it is told that repeated "." and "," commands must act like repeated commands

    1. . command must be replaced by putchar(*p);\n. Example: ".." -> "putchar(*p);\nputchar(*p);\n"

    However to pass random tests i had to combine them to one. "...." -> "putchar(*p);\n" So there is either missleading description or tests.

  • aloxmar Avatar

    I tried many times to solve this tricky kata in C, but couldn't avoid timeout error.The solution was found in full replacing library functions, such as 'sprintf' and 'strcat'with handmade code and using for output string size not less than 100000000 bytes. For sure, this is not 4 kyu, at least 3 or 2.

  • jpssj Avatar

    JavaScript. Get

    STDERR
    Max Buffer Size Reached (1.5 MiB)
    

    for one single random performance test when I want to log the input. Cannot see which pattern I didn't take account of. My code is certainly flawed somewhere, but that's also an issue with the kata, right ?

    Edit : I understand that the input is too big to be displayed properly. A workaround could be to display a more informative error message (something about the patterns in the input, or something about the expected output, I don't know). The error message in this case is currently useless :

    The translation was failed.: expected false to deeply equal true
    
  • Cplusplusbird Avatar

    I can't pass the performance test . why? how can i reslove it?

  • ValdemaruS Avatar

    What difference does it make if my algorithm, as a result of calculations, replaced this sequence "+<->" with a similar one "<->+" ? Or this "<<--<-<+>--+" (8 lines in C) to the optimized "<<--<--<+>" (7 lines in C) ? Why is kata hardcoded to one C variant, not realizing that other variant (smaller or equal in number of lines) can lead to the same calculation results? O_o

  • stoketeddy Avatar

    Can't stop getting timeouts for the c++ version.

    I'm 99% sure it's not my fault because even when I immediately return std::string() from the function, I still timeout even though my function is doing absolutely no calculation. All the other tests pass perfectly fine and on my machine at home a 10k brainfuck source code computes in like 30 milliseconds

    Simply returning an empty string immediately still times me out.

  • eurydice5717 Avatar

    Shall I suggest that this kata expected result is 'suboptimal' ?? My attempt passes the fixed tests, but the for some random test the expected contains "... *p += 2; *p -= 1;..." where my solution gives "..*p+=1;..." for those same both lines ??

  • Expurple Avatar

    C++ version should probably accept source_code parameter as const std::string& instead of std::string

  • jdx228 Avatar

    why do I have to return '' in basic#1 but i can't return '' in basic#2

  • humblebadasss Avatar

    My code is showing error, even though expected result and results are exactly identical. I dont understand the issue here. Can someone please help me. Issue 1: Expected: "if (*p) do { putchar(*p); *p = getchar(); if (*p) do { p -= 1; if (*p) do { p -= 1; putchar(*p); p += 1; putchar(*p); *p += 1; } while (*p); if (*p) do { if (*p) do { *p -= 1; p -= 1; if (*p) do { putchar(*p); *p += 1; p += 1; } while (*p); } while (*p); *p -= 1; *p = getchar(); } while (*p); putchar(*p); *p += 1; } while (*p); *p = getchar(); if (*p) do { putchar(*p); *p += 1; p -= 1; } while (*p); putchar(*p); if (*p) do { p += 1; putchar(*p); *p -= 1; if (*p) do { *p -= 1; p -= 1; *p = getchar(); putchar(*p); *p = getchar(); *p += 1; } while (*p); p -= 1; if (*p) do { p += 1; *p -= 1; putchar(*p); *p -= 1; p -= 1; *p = getchar(); *p = getchar(); if (*p) do { *p -= 1; if (*p) do { p -= 1; *p += 1; if (*p) do { *p -= 1; if (*p) do { p += 1; *p -= 2; } while (*p); putchar(*p); if (*p) do { *p += 1; p += 1; *p -= 1; } while (*p); } while (*p); *p += 2; putchar(*p); putchar(*p); } while (*p); putchar(*p); *p -= 1; } while (*p); p -= 1; ..." but was "if (*p) do { putchar(*p); *p = getchar(); if (*p) do { p -= 1; if (*p) do { p -= 1; putchar(*p); p += 1; putchar(*p); *p += 1; } while (*p); if (*p) do { if (*p) do { *p -= 1; p -= 1; if (*p) do { putchar(*p); *p += 1; p += 1; } while (*p); } while (*p); *p -= 1; *p = getchar(); } while (*p); putchar(*p); *p += 1; } while (*p); *p = getchar(); if (*p) do { putchar(*p); *p += 1; p -= 1; } while (*p); putchar(*p); if (*p) do { p += 1; putchar(*p); *p -= 1; if (*p) do { *p -= 1; p -= 1; *p = getchar(); putchar(*p); *p = getchar(); *p += 1; } while (*p); p -= 1; if (*p) do { p += 1; *p -= 1; putchar(*p); *p -= 1; p -= 1; *p = getchar(); *p = getchar(); if (*p) do { *p -= 1; if (*p) do { p -= 1; *p += 1; if (*p) do { *p -= 1; if (*p) do { p += 1; *p -= 2; } while (*p); putchar(*p); if (*p) do { *p += 1; p += 1; *p -= 1; } while (*p); } while (*p); *p += 2; putchar(*p); putchar(*p); } while (*p); putchar(*p); *p -= 1; } while (*p); p -= 1; ..."

    Issue 2: Expected: "if (*p) do { putchar(*p); } while (*p); *p -= 1; putchar(*p); if (*p) do { *p -= 1; putchar(*p); putchar(*p); } while (*p); putchar(*p); *p -= 2; *p = getchar(); p -= 1; *p += 2; putchar(*p); p -= 1; *p += 1; if (*p) do { putchar(*p); *p -= 1; putchar(*p); p += 1; *p += 1; *p = getchar(); } while (*p); *p = getchar(); *p = getchar(); if (*p) do { *p -= 1; p -= 1; if (*p) do { *p -= 1; if (*p) do { putchar(*p); p -= 1; putchar(*p); *p -= 1; } while (*p); if (*p) do { p += 2; } while (*p); if (*p) do { *p = getchar(); } while (*p); } while (*p); *p = getchar(); *p = getchar(); *p = getchar(); putchar(*p); } while (*p); putchar(*p); p -= 1; putchar(*p); if (*p) do { *p = getchar(); p += 1; } while (*p); *p += 2; putchar(*p); *p = getchar(); p -= 1; if (*p) do { if (*p) do { p -= 1; *p += 1; } while (*p); *p += 1; } while (*p); if (*p) do { *p = getchar(); *p = getchar(); p += 1; if (*p) do { *p -= 1; putchar(*p); p += 1; putchar(*p); } while (*p); if (*p) do { putchar(*p); if (*p) do { if (*p) do { *p = getchar(); } while (*p); *p -= 1; p += 1; putchar(*p); *p = getchar(); *p = getchar(); *p = getchar(); putchar(*p); p += 1; if (*p) do { *p += 1; *p = getchar(); if (*p) do { p -= 2; *p = getchar(); *p ..." but was "if (*p) do { putchar(*p); } while (*p); *p -= 1; putchar(*p); if (*p) do { *p -= 1; putchar(*p); putchar(*p); } while (*p); putchar(*p); *p -= 2; *p = getchar(); p -= 1; *p += 2; putchar(*p); p -= 1; *p += 1; if (*p) do { putchar(*p); *p -= 1; putchar(*p); p += 1; *p += 1; *p = getchar(); } while (*p); *p = getchar(); *p = getchar(); if (*p) do { *p -= 1; p -= 1; if (*p) do { *p -= 1; if (*p) do { putchar(*p); p -= 1; putchar(*p); *p -= 1; } while (*p); if (*p) do { p += 2; } while (*p); if (*p) do { *p = getchar(); } while (*p); } while (*p); *p = getchar(); *p = getchar(); *p = getchar(); putchar(*p); } while (*p); putchar(*p); p -= 1; putchar(*p); if (*p) do { *p = getchar(); p += 1; } while (*p); *p += 2; putchar(*p); *p = getchar(); p -= 1; if (*p) do { if (*p) do { p -= 1; *p += 1; } while (*p); *p += 1; } while (*p); if (*p) do { *p = getchar(); *p = getchar(); p += 1; if (*p) do { *p -= 1; putchar(*p); p += 1; putchar(*p); } while (*p); if (*p) do { putchar(*p); if (*p) do { if (*p) do { *p = getchar(); } while (*p); *p -= 1; p += 1; putchar(*p); *p = getchar(); *p = getchar(); *p = getchar(); putchar(*p); p += 1; if (*p) do { *p += 1; *p = getchar(); if (*p) do { p -= 2; *p = getchar(); *p ..."

  • jjtfsalgado Avatar

    I'm having an issue in the last performance test. It says "The translation was failed. - Expected: true, instead got: false" and the input it's "[[>?-+<]]..." repeated many times. What would be the expected output in this case? My solution is outputting the following:

    if (*p) do {
      if (*p) do {
        p += 1;
        *p -= 1;
        *p += 1;
        p -= 1;
      } while (*p);
    } while (*p);
    ...
    

    Not sure what I'm missing here.

  • woahnow101 Avatar

    It's unclear from the instructions what a useless sequence is. You say '+-' is useless, but don't specify that '-+' is useless, for example. Am I supposed to learn C and interpret the code to determine what's useless?

  • tonyojuangang Avatar

    I am having an issue :( When I upload my solution I was doing a print of the code at the beginning of the function just to check what the code was and use it to debug my program in case that solucions didnt match, and it appeared that it did work for all the assertions but receiving an sterror due to the print; but when I remove the print statement the execution time requirement pops up. What can be happening?

  • SubtRose Avatar

    Can i see tests from large_input?

  • andre-dasilva Avatar

    i keep getting a timeout error. is performance relevant in this kata? and if so isn't it too hard for a kyu 4?

  • monstaber Avatar

    Random tests in JavaScript are problematic because the user does not get to see the expected answer, meaning even if we log the input and process it line by line, we cannot see what the checker expects. In some cases it is clear the checker is rejecting a demonstrably correct solution.

    That being said, nice kata. To the people saying it requires too much performance for a level 4 kata, I wouldn't agree, you should just try various methods of parsing until you find the most efficient one possible.

  • amalliar Avatar

    Tried two different approaches — both of them pass 53 tests and then timeout (super_input 6 of 6).

    For anyone considering solving this kata: this is NOT A 4kyu KATA! It seems like the only way to solve it and not get a timeout is to somehow transpile the whole thing on the fly in one pass — something that should have been mentioned in the description. :|

    I'm gonna post both of my solutions in comments to this post just in case...

  • jaybruce1998 Avatar

    Should

    +<-+.,,+[,.,,.+[-.]-,<>-,>>>>]<,+-+>.<[]>,-.++++-.-,[+<->.]>>>.-<+>,>++[]<-<<-,.,<<-+-+>[<.]-,[]
    

    translate into

    +<.,,+[,.,,.+[-.]-,-,>>>>]<,+>.<>,-.+++.-,[+<->.]>>>.-<+>,>++<-<<-,.,<[<.]-,
    

    or is the problem something else?

  • wizzerd Avatar

    This comment has been hidden.

  • akvptp Avatar

    Kotlin translation, would someone mind reviewing this please.

  • java-abhinav07 Avatar

    This comment has been hidden.

  • inyoot Avatar

    Mine passed the static test but then gave Max Buffer Size Reached (1.5 MiB) Any suggestion?

    Time: 10556ms Passed: 2 Failed: 0 Exit Code: 137 Test Results: static_tests STDERR Max Buffer Size Reached (1.5 MiB)

  • cosarara Avatar

    The python version trims the expected output so much that it's impossible to debug.

  • RobertsN Avatar

    My C++ solution is failing with the following status (it passes basic sample tests and is indicating "General Tests" in green as well:

    Time: 2038 ms Passed: 1 Failed: 0 Exit Code: 139

    and an error which reads:

    test: ../../libcxx/include/string:1164: void std::__1::__basic_string_common::__throw_length_error() const: Assertion `!"basic_string length_error"' failed.

    It would appear that either I'm trying to use negative length strings (already checked there's no chance) or that the maximum C++ string size has been exceeded.

    Can someone enlighten me on the possible causes of the error?

    I'd also appreciate if someone could point me to where I can find a reference to what Exit Code: 139 means.

  • Lezh1k Avatar

    Need more info in description :)

    For example : what's result of "[[...][,,]]"? As I understand - it should be "[...,,]", but seems that I didn't get it right and only "[]" sequence shoulde be removed (not "][").

  • caobo56 Avatar

    This comment has been hidden.

  • davidxvuong Avatar

    I've passed 14 tests(9 general-tests, 4 general-test2, and 1 large-input) with no failed test cases, however I'm still getting an error "Execution Timed Out (12000 ms)".

    In my function, I print out code before I do any processing to see when I'm getting a new problem, but after the large-input test it doesn't seem like theres any more tests (I might be wrong).

    If it helps, I wrote the translator in C.

    Any pointers about this would help! Thanks

  • CarbonGrid Avatar

    I saw this pattern under expected

    } while (*p); if (*p) do {

    e.g ][ but OP told us to remove it yeah?

    So why is that expected?

  • L Lawliet Avatar

    This comment has been hidden.

  • Jomopipi Avatar

    wouldn't ",," also be a useless command?

  • andrey_mal Avatar

    Is this normal?

    Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 67108872 bytes) in /home/codewarrior/run.php on line 24

    Do I need to optimize my code or something like that? The instruction does not contain any requirements about it. I think one of the tests (all passes) tries to translate very big string.

    Thanks in advance.

  • narwhaler Avatar

    This comment has been hidden.

  • w7f_ Avatar

    Your function has to remove from the source code all useless command sequences such as: '+-', '<>', '[]'

    shouldn't BF code like [[[[...]]]] be optimized to [...] as well?

  • B1ts Avatar

    Wow I completely missed part 6 of the description and spent a good hour debugging it. Passing all fixed tests and no random tests was kind of annoying.

    This kata would definitely benefit from some more complex, deeper nested examples.

    Nevertheless, a great exersize !

  • dan8472 Avatar

    basic tests 1 and 2 succeed but all random cases fail. As "ooflorent" already mentioned, my solution for [+..,,] also outputs:

    if (*p) do {
      *p += 1;
      putchar(*p);
      putchar(*p);
      *p = getchar();
      *p = getchar();
    } while (*p);
    
  • toshabarinov Avatar

    When i press to "attempt" button i get an error exec_failure. What could be a resaon for this and how can i fix it? Sample tests work fine.

  • theDarkBright Avatar

    This comment has been hidden.

  • theDarkBright Avatar

    After a bit of hair-pulling, the answer turned out to be quite easy. Now, to do it in Java!

  • monadius Avatar

    As already mentioned by @ooflorent, the optimization which deletes [] is not correct. I suggest to add a new optimization which transforms [...][..] to [...]. This optimization is correct and it can significantly reduce the translated code size in some cases. It also makes this kata a little bit more interesting.

  • ooflorent Avatar

    Another issue, your solution wrongly translates [+..,,]. It outputs:

    if (*p) do {
      *p += 1;
      putchar(*p);
      *p = getchar();
    } while (*p);
    

    Whereas the expected result is:

    if (*p) do {
      *p += 1;
      putchar(*p);
      putchar(*p);
      *p = getchar();
      *p = getchar();
    } while (*p);
    
  • ooflorent Avatar

    I think there an issue with the expected solution. +[] should translate to:

    *p += 1;
    if (*p) do {
    } while (*p);
    

    The infinite loop is intended!

  • metalim Avatar

    Javascript translation has issue with random tests. All general tests pass fine (in ~100ms), but none of random tests pass. And since they print no expected result, it would be a nightmare to try and guess what they really expect. Please fix it so it prints expected string in case of mismatch, and stops testing (for performance reasons).

  • cyril-lemaire Avatar

    Errr, well that's not actually an issue, but why on Earth did you chose to translate [***] by if (*p) do {***}while(*p); and not while(*p){***}?

  • ZahNarztiK Avatar

    JavaScript Translation and PHP Translation of this kata have been published! Please review and approve! :D

  • NIaa Avatar

    "Your function has to remove from the source code all useless command sequences such as: '+-', '<>', '[]'. Also it must erase all characters except +-<>,.[]." Is this rule recursive? For example:

    "<[+-]>" -> "<[]>"
    "<[+-]>" -> ""
    

    Which one is right? (I guess the former is right, just want to be sure.)

  • bullet1337 Avatar

    Can you increase timeout for Java please? 20 seconds for ~90k of lines isn't enough

  • TheGrandCircuit  Avatar

    every time i try to submit by solution it gives me an network error.

    (code in comments)

  • trangtran11 Avatar

    I attempt but the network errors keep showing up for no reason. It's not timeout, it said network cannot process the request, and this problem only happen in this challenge. Please Help!

  • trangtran11 Avatar

    "This error was caused due to an issue processing the web request, not because of an issue executing your code. You can retry the request." I attempt before and I have error, so I fix my code, I pass all the sample test, but when I attempt, this network error show up everytime. Please help!

  • adrian.castravete Avatar

    @Varveyn , Can you check if there's something wrong with the compiler / tester? I have made a solution which passes the supplied tests. But when I “Attempt” it the only thing I get is this: https://i.imgur.com/iikigTl.png It's an Internal Server Error (500).

  • Rainmaker52 Avatar

    In the Python solution, I keep getting a timeout, saying my code takes over 12 seconds to complete.

    Now, my code may not be optimal, but on my run-of-the-mill desktop machine, even the code sample at https://pastebin.com/SaU5Jfcv (mentioned in the comment from Naiten) completes in under a second.

  • jerryye Avatar

    In performace test, there is a test input string which length is 18 000 000. and the output string's length is 164 000 000. In java, trying to build a string with a length of 164 000 000 will cause out of memory since the return type of test method is string. Thanks for any advice to pass the test? Thank you!

  • Naiten Avatar

    Well, I finally managed to solve the problem, but my code still hits 20000ms limit. Any chance that limit can be set a bit higher? Or can this happen because of high server load? I'm often getting a timeout even on sample test in this exact kata right now.

  • Naiten Avatar

    Here's an example of medium and small tests https://pastebin.com/SaU5Jfcv https://pastebin.com/HaRSwc7n The error message was trimmed and I can't see what's wrong with it. Neither can I see any mistakes or errors in the code. And yet I pass the sample tests and super test... https://i.imgur.com/nuS7etl.jpg

  • Naiten Avatar

    Argh, I passed all the sample tests, but none of the final tests. And I can't even get what is wrong, because both the output and error meccage is trimmed.

  • daddepledge Avatar

    A really enjoyable Kata, thank you. I completed it in python and think that 2 kyu is very generous, I would have thought 3 or 4.

  • Blind4Basics Avatar

    Java translation (super input tests included), please review and approve!

  • donaldsebleung Avatar

    This comment has been hidden.

  • donaldsebleung Avatar

    [C] This only happened once out of 20+ submissions but it suggests that the reference solution is flawed:

    brainfuck_to_c (".4,.<>>,+.[><>--]+,-[.<,[]>+.><>-]>>") = "putchar(*p);
    *p = getchar();
    putchar(*p);
    p += 1;
    *p = getchar();
    *p += 1;
    putchar(*p);
    if (*p) do {
      p += 1;
      *p -= 2;
    } while (*p);
    *p += 1;
    *p = getchar();
    *p -= 1;
    if (*p) do {
      putchar(*p);
      p -= 1;
      *p = getchar();
      p += 1;
      *p += 1;
      putchar(*p);
      p += 1;
      *p -= 1;
    } while (*p);
    p += 2;
    ", expected "*p = getchar();
    putchar(*p);
    p += 1;
    *p = getchar();
    *p += 1;
    putchar(*p);
    if (*p) do {
      p += 1;
      *p -= 2;
    } while (*p);
    *p += 1;
    *p = getchar();
    *p -= 1;
    if (*p) do {
      putchar(*p);
      p -= 1;
      *p = getchar();
      p += 1;
      *p += 1;
      putchar(*p);
      p += 1;
      *p -= 1;
    } while (*p);
    p += 2;
    "
    

    In the "expected" part of the failure message, the first line of C code is *p = getchar(); which corresponds to the , command; however, the input BF program starts with the characters .4,. which means the first line of C code should have been putchar(*p); instead.

  • nomennescio Avatar

    Please approve C translation

  • Blind4Basics Avatar

    Somebody to translate this to other languages (python, java... By any chance...? ;p)?

    (keeping the target output language to C, of course)

  • donaldsebleung Avatar

    When I press the "Attempt" button to run the Submit tests, the test output states that I have successfully passed all six test groups (namely general_tests, general_tests_2, performance_tests, small_input, medium_input and large_input) but for some reason the test suite still timeouts. Does that mean that my solution has a logical error which fails the 7th test group (following the large_input in the random tests, if any) or does it mean that my solution is not efficient enough? If the latter is the case then shouldn't the performance_tests test group display a failure message instead? Looking forward to an explanation, cheers :)

  • nomennescio Avatar

    I can pass the sample tests just fine, but for the past half hour I'm getting continuous timeouts when running the full tests. Error message: -- Request Error: Network Error

    This error was caused due to an issue processing the web request, not because of an issue executing your code. You can retry the request.

    When I run a sample test after that, it will just pass fine again.

    What the heck?

  • rxe Avatar

    This comment has been hidden.

  • Axure Avatar

    Approved :-)

    But I have to say that if the answers to my questions are both "omitted", things would be more interesting and realistic. (Wouldn't be much harder though)

  • Axure Avatar

    This comment has been hidden.

  • lszcs Avatar

    Please fix your test cases.

    I got the error message "Failed to process output. It is possible that too much data was written to STDOUT.". And my solution this time is: (so it's clear that the error is from YOUR code) std::string brainfuck_to_c(std::string source_code) { return ""; } By the way, I solved the problem too, the basic tests are good, and I hope we will be able to fix (somehow) the other tests.

  • muesli4 Avatar

    [] is not a meaningless statement, it depends on the contents of the tape. Either it will leave the loop, or it won't. In general it is undecidable (i.e. halting problem).

  • simonvpe Avatar

    I'm having some problems with the build server. I've optimized in several iterations and have reached a point where I beleive my code is efficient enough, but I keep getting these stupid problems with the builds (only the "Attempt" builds, the "Run Examples" works fine).

    Sometimes it complains about JSON errors, sometimes I get HTTP 500, and currently I'm stuck with "Failed to process output. It is possible that too much data was written to STDOUT." even though I don't write anything at all to stdout.

    I think that the tests might be a bit on the heavy side for the servers, what do you think?