Page 1 of 3 123 LastLast
Results 1 to 10 of 28

Thread: Beginners programming challenge #27

  1. #1
    Join Date
    Sep 2009
    Location
    Canada, Montreal QC
    Beans
    1,809
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Beginners programming challenge #27

    Welcome to the 27th Beginners programming challenge.
    Because I have a bit of free time, I decided to post this programming challenge early.
    This means it will be a bit more complicated and will probably take more work to achieve.

    Task
    You will have to implement a small regular expression engine that imitates a small subset of the Perl style regular expressions. It will only have to do matching, no need for replace or other complicated features.
    You will then use it in a program that will take input and print the text that was first matched by the regular expression. Hopefully, this will introduce you to regular expressions and understand how such an engine works.
    These are the operators you will have to implement: * . $ ^ ? + and of course normal letters.

    Cookie points

    Cookie points will be awarded for the following extras:

    1. Implementing the | (or) operator.
    2. Implementing character classes (annotated with []).
    3. Take input from stdin if a file is not supplied so piping is possible on the command line.
    4. Printing every match and not only the first one.

    Disqualified Entries:

    Any overly obfuscated code will be immediately disqualified without account for programmers skill. Please remember that these challenges are for beginners and therefore the code should be easily readable and well commented.

    Any non-beginner entries will not be judged. Please use common sense when posting code examples. Please do not give beginners a copy paste solution before they have had a chance to try this for themselves.


    Assistance:

    If you require any help with this challenge please do not hesitate to come and chat to the development focus group. We have a channel on irc.freenode.net #ubuntu-beginners-dev
    Or you can pm me

    Have fun,
    Happy coding

    cgroza
    Last edited by cgroza; July 8th, 2012 at 11:38 PM.
    I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones.
    Freedom is measured in Stallmans.
    Projects: gEcrit

  2. #2
    Join Date
    May 2008
    Location
    UK
    Beans
    1,451
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: Beginners programming challenge #27

    Great Challenge - I am not a beginner but I might have a go at this, and see how i get on. Any submission from me will be in Python though - and i promise not to use the re module.
    Tony - Happy to try to help.
    Unless otherwise stated - all code posted by me is untested. Remember to Mark the Thread as Solved.
    Ubuntu user number # 24044 Projects : TimeWarp - on the fly Backups

  3. #3
    Join Date
    Sep 2009
    Location
    Canada, Montreal QC
    Beans
    1,809
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: Beginners programming challenge #27

    Quote Originally Posted by Tony Flury View Post
    Great Challenge - I am not a beginner but I might have a go at this, and see how i get on. Any submission from me will be in Python though - and i promise not to use the re module.
    If you think you are not a beginner and you think it would be unfair for other users, you could post your solution after the winner is announced. And yes, the users are not allowed to use libraries that are directly related with regular expression engines, the goal is to implement it themselves .
    I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones.
    Freedom is measured in Stallmans.
    Projects: gEcrit

  4. #4
    Join Date
    May 2008
    Location
    UK
    Beans
    1,451
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: Beginners programming challenge #27

    No intention of posting my solution until the winner is announced - although right now I am stuck - so unless i can solve that - i wont be able to post anything.
    Tony - Happy to try to help.
    Unless otherwise stated - all code posted by me is untested. Remember to Mark the Thread as Solved.
    Ubuntu user number # 24044 Projects : TimeWarp - on the fly Backups

  5. #5
    Join Date
    Aug 2005
    Location
    The Local Group
    Beans
    631

    Re: Beginners programming challenge #27

    In my opinion, the level of this problem is more intermediate than beginner. I'd love to be pleasantly surprised by a bunch of beginners rising to the challenge, though.

  6. #6
    Join Date
    Aug 2006
    Location
    60°27'48"N 24°48'18"E
    Beans
    3,458

    Re: Beginners programming challenge #27

    Yeah, this really is more intermediate, although some undergrad CS education on grammars, formal languages and automata should provide one with references to literature where this is handled. Any ad hoc solutions are almost certainly wrong.
    LambdaGrok. | #ubuntu-programming on FreeNode

  7. #7
    Join Date
    Jul 2010
    Beans
    96
    Distro
    Lubuntu

    Re: Beginners programming challenge #27

    Just to make sure I got the meaning of "it will print the text that was first matched by the regular expression".

    1) If the regex is ".*", should it always match anything and print nothing?

    2) If the regex is ".*at", and the string in input is "Matt is at the gym", it should match "Mat", right?
    Last edited by Odexios; July 10th, 2012 at 09:05 AM.
    - What do you get if you multiply six by nine?
    - 42!

  8. #8
    Join Date
    May 2008
    Location
    UK
    Beans
    1,451
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: Beginners programming challenge #27

    Quote Originally Posted by Odexios View Post
    Just to make sure I got the meaning of "it will print the text that was first matched by the regular expression".

    1) If the regex is ".*", should it always match anything and print nothing?
    This should print the whole input string, as the whole string matches the regex.

    Quote Originally Posted by Odexios View Post
    2) If the regex is ".*at", and the string in input is "Matt is at the gym", it should match "Mat", right?
    I would have assumed that all quantifiers are greedy so in you example the matching string would be : "Matt is at" (as that is longer that "Mat"
    Tony - Happy to try to help.
    Unless otherwise stated - all code posted by me is untested. Remember to Mark the Thread as Solved.
    Ubuntu user number # 24044 Projects : TimeWarp - on the fly Backups

  9. #9
    Join Date
    Jul 2010
    Beans
    96
    Distro
    Lubuntu

    Re: Beginners programming challenge #27

    Quote Originally Posted by Tony Flury View Post
    This should print the whole input string, as the whole string matches the regex.


    I would have assumed that all quantifiers are greedy so in you example the matching string would be : "Matt is at" (as that is longer that "Mat"
    Ok; so I guess that printing "the text that was first matched" means that when more than one line is found, the engine should work only on the first line with a match.
    - What do you get if you multiply six by nine?
    - 42!

  10. #10
    Join Date
    May 2007
    Location
    East Yorkshire, England
    Beans
    Hidden!

    Re: Beginners programming challenge #27

    Quote Originally Posted by Odexios View Post
    Ok; so I guess that printing "the text that was first matched" means that when more than one line is found, the engine should work only on the first line with a match.
    "." does not match newlines, so running .* on "hello\nworld" will have two matches: ["hello", "world"], and so "hello" should be printed.
    Website | Blog | The Arch Hurd Project

    If you want to ask about something I posted, send a PM, as I don't watch many threads

Page 1 of 3 123 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •