Skip to content

Instantly share code, notes, and snippets.

@ProProgrammer
Last active December 25, 2015 14:58
Show Gist options
  • Save ProProgrammer/6994472 to your computer and use it in GitHub Desktop.
Save ProProgrammer/6994472 to your computer and use it in GitHub Desktop.
Formulating Regular Expressions 1 (Challenge and their solutions) - Sharing amazing learnings from Udacity's CS262 - Programming Languages - https://www.udacity.com/course/cs262.Strict Advise: Do Attempt Challenge before you see solution
# RE Challenges
# Assign to the variable regexp a Python regular expression that matches single-
# argument mathematical functions.
# The function name is a lowercase word (a-z), the function argument must be a
# number (0-9), and there may optionally be spaces before and/or after the
# argument.
# Hint: You may need to escape the ( and ).
import re
regexp = _________________ (Fill in the blank Space)
# regexp matches:
print re.findall(regexp,"cos(0)") == ["cos(0)"]
#>>> True
print re.findall(regexp,"sqrt( 2 )") == ["sqrt( 2 )"]
#>>> True
# regexp does not match:
print re.findall(regexp,"cos (0)") != ["cos (0)"]
#>>> True
print re.findall(regexp,"sqrt(x)") != ["sqrt(x)"]
#>>> True
regexp = r"[a-z]+\( *[0-9] *\)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment