Migrated from private repo
This commit is contained in:
23
pattern/pattern.c
Normal file
23
pattern/pattern.c
Normal file
@ -0,0 +1,23 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int isMatch(char *pattern, char *string) {
|
||||
if (*pattern == '\0') {
|
||||
return (*string == '\0');
|
||||
}
|
||||
if (pattern[1] == '?') {
|
||||
return (pattern[0] == string[0] && isMatch(pattern + 2, string + 1)) //Call 1
|
||||
|| isMatch(pattern + 2, string); //Call 2
|
||||
}
|
||||
return pattern[0] == string[0] && isMatch(pattern + 1, string + 1); //Call 3
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
char pattern[30], string[30];
|
||||
scanf("%s %s", pattern, string);
|
||||
if (isMatch(pattern, string) == 0) {
|
||||
printf("NO ");
|
||||
}
|
||||
printf("MATCH\n");
|
||||
return 0;
|
||||
}
|
37
pattern/pattern.py
Normal file
37
pattern/pattern.py
Normal file
@ -0,0 +1,37 @@
|
||||
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:])
|
Reference in New Issue
Block a user