2023-12-18 21:37:02 +01:00

37 lines
1.2 KiB
Python

from manim import *
# This one does not work, as I decided that it would be better to do it the old-fashioned way.
# What does that mean?
# 67 slides for the same problem
PATTERN = "PF-is-?n?o?t?-bad"
STRING = "PF-is-bad"
class patternMatching(Scene):
def construct(self):
# Show inputs on top
pattern = Text(PATTERN, font_size=24)
string = Text(STRING, font_size=24)
pattern.to_edge(UP)
string.next_to(pattern, DOWN)
self.patterns = VGroup(pattern, string)
# Write the initial function call
self.play(Write(self.patterns))
self.wait(2)
self.results = []
self.is_match(PATTERN, STRING)
def is_match(self, pattern, string):
self.results.append(Text(f"{pattern}, {string}", font_size=24).to_edge(UP))
# Base case: If either the pattern or string is empty
if not pattern:
return self.callback()
# If the pattern is not empty but the string is
if len(pattern) > 1 and pattern[1] == '?':
return (pattern[0] == string[0] and self.is_match(pattern[2:], string[1:])) or self.is_match(pattern[2:], string)
0
# If the pattern is not empty and the string is not empty
return pattern[0] == string[0] and self.is_match(pattern[1:], string[1:])