by generallyfalse · 8y ago · 0 comments
This kind of problems are designed to test you can define a grammar and apply recursion. Another problem with the same flavor could be to evaluate string expressions like "3+25" using a grammar. My one-hour solution (not checked). But I think I have done similar problems.
def pat(s,p):
...: if s == "":
...: if len(p)>1 and p[1] in "?*":
...: return pat(s,p[2:])
...: if p == "": return s == p
...: if len(p) == 1:
...: if p == "*": return True
...: return s == p
...: if len(p) == 2:
...: if p[1] == "?":
...: return s == p[0] or s==""
...: if p[1] == "*":
...: if s[0] != p[0]: return False
...: if s == p[0]: return True
...: return pat(s[1:],p)
...: if p[1] != "?" and p[1] != "*":
...: return s[0] == p[0] and pat(s[1:],p[1:])
...: if p[1] == "?":
...: if (s[0] != p[0]):
...: return pat(s,p[2:]))
...: if (s[0] == p[0]):
...: return pat(s[1:],p[2:]) or pat(s,p[2:])
...: if p[1] =="*":
...: if (s[0] != p[0]): return pat(s,p[2:])
...: return pat(s[1:],p) or pat(s,p[2:])
...:
...:On the other hand, using iterators in python is nice. You didn't use iterators to compute the sum of cubes, many problems can be solved in one line using iterators. https://www.datacamp.com/community/tutorials/python-iterator...
Perhaps your code should try to go from the easy cases to the complex cases in pattern matching.
As a hobby programmer I program without stress, if I was to make a living by programming I think I shouldn't enjoy so much.
I think you should read norvig post: http://norvig.com/21-days.html, also to learn to program in python: https://github.com/norvig/pytudes
I believe that in a year or two you could be prepared to judge your progress