4 kyu

Simplexer

250 of 750Mihail-K
Description
Loading description...
Strings
Parsing
Algorithms
  • Please sign in or sign up to leave a comment.
  • Mayube Avatar

    (C#) The description makes no mention of handling null values, yet "Test Empty" in the hidden test cases tests with a null value. It's also unclear what "Text Exception" is testing, or what it's passing. It seems to expect false when my otherwise-valid solution returns true. The only valid case in C# for an IEnumerator to throw an exception is if the underlying collection is modified during iteration. Strings are immutable so this is an unreachable scenario.

  • cleverboy1 Avatar

    I enjoyed this kata since I'm a big fan of regular expressions, But it's more suited at 5kyu instead of 4.

  • Bront_in_Hood Avatar

    There is a strange requirement: "true123 is an identifier, as opposed to boolean followed by integer".. Looks like foreign for this task. Actually I ignored it (did not check and maintain this case). And my solvation passed all tests! May be you should remove this requirement from kata description?

  • Goudron Avatar

    To pass "testException" in Java you have to throw an exception in "next()" if the string is empty, which is not obvious. Returning null or new Token(null, null) in "next()" or false in "hasNext()" doesn't work.

  • Awesome A.D. Avatar

    Rust translation ready for review.

  • CoreTOP Avatar

    Failed with just one test

    "Single boolean" test
    ReferenceError: cons is not defined
        at Context.<anonymous> (test.js:119:28)
        at process.processImmediate (node:internal/timers:471:21)
    

    But don't know what "ReferenceError: cons is not defined" could mean under the hood, because in local

    Set the log on input and output and output as expected for test: My output: Token { text: 'true', type: 'boolean' }

    Expected by the test:

     it('Single boolean', function() {
            var lexer = new Simplexer('true');
            assert.strictEqual(cons, true, 'Has next');
            assert.deepEqual(lexer.next(), new Token('true', 'boolean'));
        });
    
  • Awesome A.D. Avatar

    I think this kata is valuable as a precursor to all those other parsing-related kata, which require a lexer as a fundamental part of the solution. However, it is currently in a deplorable state.

    Therefore, I've authored a Fork (in Python) that completely reworks everything. In particular:

    • Completely reworked the description, including clear sections for language specific information
    • Added random tests
    • Cleaned up the spec and added tests to enforce it. Notably:
      • Whitespace is optional where it is not necessary to disambiguate tokens
      • string tokens should match any character between quotes, including whitespace
    • Authored a new, working solution to replace the broken old one.

    The point about strings in particular happens to be a breaking change for most of the current regex-based solutions in Python, although strictly speaking it's the fault of users for not writing proper regexes. Granted, the current tests don't actually enforce anything, but still...

    I would invite users to review the fork, add suggestions in the forks's discourse, and if consensus is achieved that it merits approval, work on adding language specific blocks where appropriate for existing languages. That means that the current fork is not meant for approval in its current state (since it lacks all language specific stuff apart from Python), rather as a template to be worked off of.

  • Madjosz Avatar

    Missing random tests in Java.

  • Madjosz Avatar

    Missing random tests in C#.

  • XoRMiAS Avatar

    JavaScript should use chai for assertions. Fixed here

  • XoRMiAS Avatar

    JavaScript: Insufficient test cases and no random tests. Most solutions (including the sample solution) are incorrect.

    Fixed here.

  • XoRMiAS Avatar

    JavaScript fork(s):

  • trashy_incel Avatar

    Javascript: assertSimilar is deprecated, use assertDeepEquals

  • Root-Exec Avatar

    Java question: I'm a little confused on what to do with an empty string or null... I'm throwing a NoSuchElementException() in the case that it isn't a valid string but it is saying that I'm still failing the test.

  • C4Oc Avatar

    I understand almost nothing from the description of this Kata and the examples given just can't make up for the 75% missing explanation. Can someone please explain me what I even have to do for this Kata?

  • FArekkusu Avatar

    in python, C# and Java, you'll have to manage null/None input too

    This is not true for Python. IMO, this requirement should be removed from other languages if it's actually present there.

  • FArekkusu Avatar

    No random tests.

  • rbahlsten Avatar

    Is there something wrong with the error message? I'm literally returning exactly what I'm supposed to...

    [Token(chars='x', type='identifier')] should equal [Token(chars='x', type='identifier')]

    [Token(chars='true', type='boolean')] should equal [Token(chars='true', type='boolean')]

    [Token(chars='12345', type='integer')] should equal [Token(chars='12345', type='integer')]

    [Token(chars='"String"', type='string')] should equal [Token(chars='"String"', type='string')]

    [Token(chars='break', type='keyword')] should equal [Token(chars='break', type='keyword')]

  • kellda Avatar

    Can someone review my translation in Rust ?

  • kellda Avatar

    Would someone review if I make a translation in Rust ?

  • RealKenshiro Avatar

    Very interesting Kata, thank you.

  • borutflis Avatar

    Did anyone produce any special solution for the keywords not being captured among identifiers? Was it just the word order?

  • crunchCBS Avatar

    This is what I get when I run the skeletal code: expected:<[Token{text=return, type=keyword}, Token{text= , type=whitespace}, Token{text=x, type=identifier}, Token{text= , type=whitespace}, Token{text=+, type=operator}, Token{text= , type=whitespace}, Token{text=1, type=integer}]> but was:<[]>

    Shouldn't the actual portion be <[ Token{text=x, type=identifier}]> ?

  • fenring76 Avatar

    The error output string should be changed. I was recieving a lot of these types of errors when submitting tokens with integer (rather than the expected string) values:

    [Token(chars='12345', type='integer')] should equal [Token(chars='12345', type='integer')]
    

    This made it much more difficult to debug than it needed to be for what I think are obvious reasons.

    Cheers

  • phantamanta44 Avatar

    having to handle the null case seems out-of-scope for this kind of problem. it's more of a tacked-on "gotcha" for those who skim through the spec than any sort of real programming challenge. i would suggest omitting it

  • MDabrowski1990 Avatar

    I highly suggest to rewrite the description. I have read it like 3 times and still no clue what the code should do.

    Example input and example output would be help a lot.

  • allenCao Avatar

    My code can pass the testSingle case in RUN SIMPLES TESTS but why it fails through the ATTEMPT

  • purpleb3ar Avatar

    I get the js heap out of memory error when i run the base code that was already prepared

  • joecastle Avatar
  • higuera Avatar

    This comment has been hidden.

  • siebenschlaefer Avatar

    Python: Could you add tester("forecast", [Token("forecast", "identifier")]) to the tests? Some solutions yield Token(chars='for', type='keyword'), Token(chars='ecast', type='identifier').

  • bouchert Avatar

    At least in the Python version, the way the test is written forces us to use Simplexer as both an iterable and an iterator. Even though it's uncommon to parse a program more than once, it is considered poor practice to have the iterable and iterator be the same object, because it prevents using multiple iterators in parallel, in nested loops over the same iterable, for example. If you want to permit solutions that separate the iterable (a Simplexer class, containing an _iter_ method and returning an iterator) from the iterator, (perhaps called something like a SimplexIterator, containing a _next_ method), the tests need to say something like iterator=iter(Simplexer(test_str)) and then use next(iterator) everywhere you might have used next on the Simplexer object itself instead.

  • lechevalier Avatar

    Python

    Ran (), got: : [Token(chars='(', type='operator'), Token(chars=')', type='operator')] should equal [Token(chars='()', type='operator')]

    Actual and expected results are swapped in random test cases.

  • ice1000 Avatar

    RegEx fun

  • JohanWiltink Avatar

    JavaScript version should not test for null, but for undefined, behaving as an empty string.

  • ice1000 Avatar

    Interesting.

  • JohanWiltink Avatar

    (JavaScript)

    It'll take in an input string

    null is not a String. If you want to pass in an empty string, that would be "".

  • Blind4Basics Avatar

    ok...

    • modified the tests of the python version to have a "more logical" system (somehow)
    • added tests for exceptions in python and java. if Someone could do it in JS, that would be awesome...
    • added on test in python
    • made the tests retrocompatible with previous solutions (even if...)
    • description updated

    Have fun, guys!

  • dinglemouse Avatar

    Fun Kata. Thanks :-)

  • bugman4 Avatar

    Could you give the bigenner the hint about the 'Iterators'?

  • jolaf Avatar

    (Python)

    Tokens are represented by Token objects, which define two properties as strings: text, and type.


    When I try to create a Token object as follows: Token(), I get the following error: TypeError: init() takes exactly 3 arguments (1 given). I suppose I should pass text and type as constructor parameters, but this is never described in the kata description.

  • jolaf Avatar

    (Python)

    If no tokens are available, the next method should throw an exception.


    My method raises AttributeError in that case, that fails the test. It should be specified, what kind of exception is expected.

  • jolaf Avatar

    (Python) Why Simplexer has to be callable? Why calling Simplexer.simplex("x")() instead of just Simplexer.simplex("x")?

  • Darnor Avatar

    "If no tokens are available, the next method should throw an exception": Maybe you could specify what kind of Exception you want thrown? (IllegalArgumentException ?)

    Also due to a bug in CW the empty test passes even if you don't check for null input due to a NullPointerException. https://github.com/Codewars/codewars.com/issues/21

  • ChristianECooper Avatar

    This comment has been hidden.

  • xehpuk Avatar

    What is the lexer supposed to do with unrecognized tokens? What is the expected result for the input "0 ~ 1"?

  • Stuart65 Avatar

    agh! Finally finished this - my answer works, though verbose. My only observation is that failng tests require should state as a minimum what the expected value is.

    Fantastic learning exercise though.

  • Stuart65 Avatar

    Am getting

    testSingle(SimplexerTest) expected: but was:

    Any help here? I have no idea what condition I am failing. All cases of a single 'token' have been covered?

  • mortonfox Avatar

    I got the following error when I submitted my solution:

    Could not write to file #<File Token.java>, because that file already exists.  Perhaps it already contains the setup or test fixture code?
    

    Why did that happen?

  • Balkoth Avatar

    Your description says "If no tokens are available, the next method should throw an exception", but there are no tests for it. You should add one.

  • wthit56 Avatar

    I liked this kata, though it took some superfluous reading to figure out what you actually wanted me to do. Ideally, you should give all necessary instructions in the description, rather than leaving the coder to look at the stub and example fixture to figure out how to do things.

    Another couple of small issues would be nice to fix, also:

    • Because of how Test.assertEquals renders strings, it is very hard to see the whitespace problems. Perhaps you could write a custom function for it, to make it clearer on what to debug? I tend to wrap things in <pre style='display:inline'> tags.

    • And a very minor typo: There a 7 token types should be There are 7 token types.