Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6146945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:01:59+00:00 2026-05-23T19:01:59+00:00

Take a look at this programming challenge. https://gist.github.com/1083219 We’ve built a new communication protocol

  • 0

Take a look at this programming challenge.

https://gist.github.com/1083219

We’ve built a new communication protocol that sends messages with a restricted syntax.
We need to write a function which determines whether a given message is syntactically valid or not.

Here are the rules:

  1. There are 15 valid characters in the protocol: the lower-case characters ‘a’ through ‘j’
    and the uppercase characters ‘Z’, ‘M’, ‘K’, ‘P’, and ‘Q’.
  2. Every lower-case character in isolation is a valid message, e.g., ‘a’ is a valid message.
  3. If σ is a valid message then so is Zσ.
  4. If σ and τ are valid messages then so are Mστ , Kστ , Pστ , and Qστ .
  5. All other messages are invalid.

Write a function in the language of your choosing to check whether messages are valid.
The input consists of a series of potential messages separated by whitespace and containing
only the 15 characters above.

The output consists of one line per potential messages, followed by ‘VALID’ if the message is valid and ‘INVALID’ if it is invalid.

Below is some example output.

Input    Output
Qa Zj    Qa INVALID
MZca     Zj VALID
Khfa     MZca VALID
         Khfa INVALID

There are many ways to solve it including regex, parsing character by character. I am wondering what others think of how to go about solving the problem.

I think a simple parser might be the best way to go. First write the grammar and then implement the parser.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T19:01:59+00:00Added an answer on May 23, 2026 at 7:01 pm

    I think you should be able to do this with a fairly simple recursive string function f:

    1. Look at the first character of the string
    2. If it is a lower case letter, return the rest of the string
    3. If it is Z, return f(the rest of the string)
    4. If it is M, K, P, or Q, then return f(f(the rest of the string))
    5. Otherwise, return the entire string

    Then wrap this in a function g that returns true if f returns the empty string and false otherwise. Do this for each message.

    This works because the grammar you described is a simple tree structure: Any node with two children is M, K, P, or Q, any node with one child is Z, and any leaf is a lowercase letter. f always consumes exactly one node on the tree and returns the rest of the tree (including siblings), and returns a nonempty string on a malformed tree.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Take a look at this snazy plugin: http://remysharp.com/2007/12/28/jquery-tag-suggestion/ ** it's real small Source: http://remysharp.com/downloads/tag.js
Take a look at this page: http://pearl.tinderfields.com/ In IE6 the menu's background image loads
Take a look at this: WebClient client = new WebClient(); WebRequestSettings wrs = new
Take a look at this example: http://www.extjs.com/deploy/dev/examples/multiselect/multiselect-demo.html On it, there are components rendered against
please take a quick look at this function that I have found on the
take a look of this: http://jsfiddle.net/PFQke/ I want to add some icons to that
take a look at this example code: public class Comment { private Comment() {
Take a look at this html: <head> <title>Test page</title> <script type=text/javascript> function submitForm() {
Take a look at this code: public class Test { public static void main(String...
Take a look at this table: <table cellpadding=0 cellspacing=0 class=order_form> <tr> <th>Amount</th> <th>Desc</th> <th>Price</th>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.