Migrated from private repo

This commit is contained in:
Boyan 2023-12-18 21:37:02 +01:00
parent 9ba14b2f44
commit 3f5025d68c
11 changed files with 317 additions and 0 deletions

BIN
factorial/FactorialTree.mp4 Normal file

Binary file not shown.

17
factorial/factorial.c Normal file
View File

@ -0,0 +1,17 @@
#include <stdio.h>
int factorial(int n) {
if (n == 0) {
return 1;
}
return n * factorial(n - 1);
}
int main() {
int n;
scanf("%d", &n);
printf("%d\n", factorial(n));
return 0;
}

71
factorial/factorial.py Normal file
View File

@ -0,0 +1,71 @@
from manim import *
config.background_color = (33, 33, 33)
class FactorialTree(Scene):
# This animation is fundamentally wrong, as it does NOT utilize this type of graph correctly
# Still gets the job done though (I think)
def construct(self):
# Draw the initial node
self.bottom = Text(f"factorial(5)", font_size=24, font="Agave Nerd Font Mono").to_edge(DOWN)
self.play(Write(self.bottom))
self.top_num = 1
self.top = Text(f"result = 0", font_size=24, font="Agave Nerd Font Mono").to_edge(UP).to_edge(LEFT)
self.last_node = None
self.play(Write(self.top))
root = self.create_node("5!", position=ORIGIN).to_edge(UP)
self.play(FadeIn(root))
def drawNode(node, parent, position, right = False):
# Draw a multiplication sign in the center between the left and the right node
self.bottom.become(Text(f"factorial({n})", font_size=24, font="Agave Nerd Font Mono").to_edge(DOWN))
self.play(Indicate(self.bottom))
self.play(GrowArrow(Arrow(parent.get_bottom(), node.get_top()).scale(0.5)))
if final:
self.play(Write(Text("x", font_size=24, font="Agave Nerd Font Mono").move_to(position + DOWN * 0.5)))
self.last_node = node
self.play(FadeIn(node))
self.play(FadeToColor(node, GREEN))
# Recursive function to create tree
def factorialTree(n, parent, position, level=1):
if n <= 0:
# Closing animation
self.play(FadeToColor(parent, GREEN))
self.bottom.become(Text(f"factorial({n})", font_size=24, font="Agave Nerd Font Mono").to_edge(DOWN))
self.play(Indicate(self.bottom))
return
# Left child (n-1)!
left_pos = position + LEFT + level * DOWN
left_child = self.create_node(f"{n-1}!", position=left_pos)
drawNode(left_child, parent, position)
factorial_tree(n-1, left_child, left_pos, level + 0.3)
# Funky updates
self.top_num *= n
self.play(FadeToColor(parent, GREEN))
# Right child (result of n-1 factorial)
right_pos = position + RIGHT + level * DOWN
right_child = self.create_node(str(self.top_num), position=right_pos)
drawNode(right_child, parent, position, final=True, last=True)
self.play(Transform(self.top, Text(f"result = {self.top_num}", font_size=24, font="Agave Nerd Font Mono").to_edge(UP).to_edge(LEFT)))
# Create the tree
factorial_tree(5, root, root.get_center())
self.play(Indicate(self.last_node))
self.wait(1)
def create_node(self, content, position):
node = Circle().scale(0.25)
node_text = Text(content).scale(0.25).move_to(node)
node_group = VGroup(node, node_text).move_to(position)
return node_group

23
pattern/pattern.c Normal file
View 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
View 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:])

Binary file not shown.

1
sierpinski/no_code.md Normal file
View File

@ -0,0 +1 @@
The code is lost unfortunately, but we didn't have this in the slides anyway for obvious reasons though.

42
sierpinski/triangle.c Normal file
View File

@ -0,0 +1,42 @@
#include <stdio.h>
#include <string.h>
#include <math.h>
#define SIZE 32 // Size of the grid
void sierpinski(int x, int y, int size, char grid[SIZE][SIZE]) {
if (size == 1) {
grid[y][x] = '*';
return;
}
int subSize = size / 2;
// Top triangle
sierpinski(x, y, subSize, grid);
// Bottom left triangle
sierpinski(x - subSize, y + subSize, subSize, grid);
// Bottom right triangle
sierpinski(x + subSize, y + subSize, subSize, grid);
}
int main() {
char grid[SIZE][SIZE];
memset(grid, ' ', sizeof(grid));
sierpinski(SIZE / 2, 0, SIZE / 2, grid);
// Calculate the height of the triangle and print only the necessary rows
int height = (int)(0.866 * SIZE / 2);
for (int i = 0; i <= height; i++) {
for (int j = 0; j < SIZE; j++) {
printf("%c", grid[i][j]);
}
printf("\n");
}
return 0;
}

Binary file not shown.

View File

@ -0,0 +1,30 @@
#include <stdio.h>
#include <string.h>
void reverseString(char *str, int start, int end) {
// Base case: if start index is greater or equal to end index
if (start >= end) {
return;
}
// Swap the characters at start and end
char temp = str[start];
str[start] = str[end];
str[end] = temp;
// Recursive call with modified indices
reverseString(str, start + 1, end - 1);
}
int main() {
char str[] = "Hello, World!";
int n = strlen(str);
printf("Original string: %s\n", str);
// Call the reverseString function
reverseString(str, 0, n - 1);
printf("Reversed string: %s\n", str);
return 0;
}

View File

@ -0,0 +1,96 @@
from manim import *
config.background_color = (33, 33, 33)
INPUT = "PFISAWESOME" # Manim does not like spaces unfortunately
class StringReversal(Scene):
def construct(self):
# Big string
self.string = Text(INPUT, font_size=64, font="Agave Nerd Font Mono")
# Intro animation
self.play(Write(self.string))
self.bottom = Text(f"reverse({self.string.text}, start=0, end={len(self.string)-1})", font_size=24, font="Agave Nerd Font Mono")
self.bottom.to_edge(DOWN).to_edge(LEFT)
self.play(Write(self.bottom))
# Call the recursive function
result = self.reverse(INPUT, 0, len(INPUT) - 1)
# Outro animation
self.play(Unwrite(self.bottom))
self.play(FadeToColor(self.string, GREEN))
self.wait(4)
self.play(Unwrite(self.string))
self.wait(1)
def bottom_text(self, text):
# Update the bottom text
self.play(
Transform(
self.bottom,
Text(
text,
font_size=24,
font="Agave Nerd Font Mono"
).to_edge(DOWN).to_edge(LEFT)
)
)
self.wait(1)
# Visual swap
def swap_chars(self, counter):
# Indicate the characters we're about to swap
self.play(
Indicate(self.string[counter]),
Indicate(self.string[len(self.string) - counter - 1])
)
self.wait(.5)
# Swap the characters
self.play(
Swap(
self.string[counter],
self.string[len(self.string) - counter - 1]
)
)
self.wait(1)
def reverse(self, string, start, end, counter=0):
# Color the characters that we're about to swap in bottom text
self.bottom_text(f"reverse({string}, start={start}, end={end})")
# Base case: If we've reached the middle of the string
if start >= end:
self.play(FadeToColor(self.bottom, GREEN))
self.play(Unwrite(self.bottom))
# There must be a way to set defaults for text
success = Text(
"We reached the base case!",
font_size=24,
font="Agave Nerd Font Mono"
).to_edge(DOWN).to_edge(LEFT)
self.play(Write(success))
self.wait(1)
self.play(Unwrite(success))
return string
# Swap first and last chars of string
string = list(string)
string[start], string[end] = string[end], string[start]
string = "".join(string)
self.swap_chars(counter)
counter += 1
# Recursively call reverse on the substring
self.reverse(string, start + 1, end - 1, counter)