Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>We did something <em>similar</em> in Python not too long ago for a <a href="http://www.pyweek.org/e/RegExExpress/" rel="nofollow noreferrer">RegEx game</a> that we wrote. We had the constraint that the regex had to be randomly generated, and the selected words had to be real words. You can download the completed game EXE <a href="http://samwashburn.com/hanclinto/RegExExpress%201.3%20EXE.zip" rel="nofollow noreferrer">here</a>, and the Python source code <a href="http://media.pyweek.org/dl/7/RegExExpress/regex_express-1.3.zip" rel="nofollow noreferrer">here</a>.</p> <p>Here is a snippet:</p> <pre><code>def generate_problem(level): keep_trying = True while(keep_trying): regex = gen_regex(level) # print 'regex = ' + regex counter = 0 match = 0 notmatch = 0 goodwords = [] badwords = [] num_words = 2 + level * 3 if num_words &gt; 18: num_words = 18 max_word_length = level + 4 while (counter &lt; 10000) and ((match &lt; num_words) or (notmatch &lt; num_words)): counter += 1 rand_word = words[random.randint(0,max_word)] if len(rand_word) &gt; max_word_length: continue mo = re.search(regex, rand_word) if mo: match += 1 if len(goodwords) &lt; num_words: goodwords.append(rand_word) else: notmatch += 1 if len(badwords) &lt; num_words: badwords.append(rand_word) if counter &lt; 10000: new_prob = problem.problem() new_prob.title = 'Level ' + str(level) new_prob.explanation = 'This is a level %d puzzle. ' % level new_prob.goodwords = goodwords new_prob.badwords = badwords new_prob.regex = regex keep_trying = False return new_prob </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload