Migrated from private repo
This commit is contained in:
BIN
string_reversal/StringReversal.mp4
Normal file
BIN
string_reversal/StringReversal.mp4
Normal file
Binary file not shown.
30
string_reversal/string_rev.c
Normal file
30
string_reversal/string_rev.c
Normal 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;
|
||||
}
|
96
string_reversal/string_rev.py
Normal file
96
string_reversal/string_rev.py
Normal 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)
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user