From 9d8a4d5729c9beeef467b9366b37497b7f115497 Mon Sep 17 00:00:00 2001 From: Pilolo-hub Date: Sat, 26 Oct 2024 04:43:30 +0000 Subject: [PATCH 1/9] Update Minstack.py --- .../Algorithm Solutions/Minstack.py | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/codefest-2022/Algorithm Challenge/Algorithm Solutions/Minstack.py b/codefest-2022/Algorithm Challenge/Algorithm Solutions/Minstack.py index d182b02..6d48978 100644 --- a/codefest-2022/Algorithm Challenge/Algorithm Solutions/Minstack.py +++ b/codefest-2022/Algorithm Challenge/Algorithm Solutions/Minstack.py @@ -24,15 +24,14 @@ def printMe(self): print(self.stack) print(self.minStack) - -newStack = MinStack() -newStack.push(-2) -newStack.push(0) -newStack.push(-3) -newStack.push(-5) -newStack.printMe() -print(newStack.getMin()) -print(newStack.pop()) -print(newStack.getMin()) -newStack.printMe() - +if __name__ == "__main__": + newStack = MinStack() + newStack.push(-2) + newStack.push(0) + newStack.push(-3) + newStack.push(-5) + newStack.printMe() + print(newStack.getMin()) + print(newStack.pop()) + print(newStack.getMin()) + newStack.printMe() From 523fef53af9acbb98e0037b73f6411494cb69049 Mon Sep 17 00:00:00 2001 From: root Date: Sat, 26 Oct 2024 05:23:10 +0000 Subject: [PATCH 2/9] added files --- .../Algorithm Solutions/.gitignore | 4 ++++ .../Algorithm Solutions/Minstack.c | 17 +++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 codefest-2022/Algorithm Challenge/Algorithm Solutions/.gitignore create mode 100644 codefest-2022/Algorithm Challenge/Algorithm Solutions/Minstack.c diff --git a/codefest-2022/Algorithm Challenge/Algorithm Solutions/.gitignore b/codefest-2022/Algorithm Challenge/Algorithm Solutions/.gitignore new file mode 100644 index 0000000..5129e5c --- /dev/null +++ b/codefest-2022/Algorithm Challenge/Algorithm Solutions/.gitignore @@ -0,0 +1,4 @@ +#executable +*.elf +*.exe +*.out \ No newline at end of file diff --git a/codefest-2022/Algorithm Challenge/Algorithm Solutions/Minstack.c b/codefest-2022/Algorithm Challenge/Algorithm Solutions/Minstack.c new file mode 100644 index 0000000..2c0a667 --- /dev/null +++ b/codefest-2022/Algorithm Challenge/Algorithm Solutions/Minstack.c @@ -0,0 +1,17 @@ +#include +#include + +#define INIT_SIZE 5 +/** +* */ +typedef struct _stack +{ + int capacity; + int *stack; + int *top; +}stack; + +int main (void) +{ + return (0); +} \ No newline at end of file From ce7e0364b828b2bf06e480a6b0d72f3ae6fedf79 Mon Sep 17 00:00:00 2001 From: root Date: Sat, 26 Oct 2024 05:49:11 +0000 Subject: [PATCH 3/9] added files --- .../Algorithm Solutions/Minstack.c | 40 +++++++++++++++++-- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/codefest-2022/Algorithm Challenge/Algorithm Solutions/Minstack.c b/codefest-2022/Algorithm Challenge/Algorithm Solutions/Minstack.c index 2c0a667..bf12781 100644 --- a/codefest-2022/Algorithm Challenge/Algorithm Solutions/Minstack.c +++ b/codefest-2022/Algorithm Challenge/Algorithm Solutions/Minstack.c @@ -3,13 +3,45 @@ #define INIT_SIZE 5 /** -* */ -typedef struct _stack +* struct _minstack- stack of integers +* @capacity: capacity of stack +* @size: size of stack +* @arr: pointer to stack array +* @top: top of stack +* @minarr: stack of the minimum value at every point +*/ +typedef struct _minStack { int capacity; - int *stack; + int size; + int *arr; int *top; -}stack; + int *minarr; + +}minStack; + +minStack *MyStack(void) +{ + minStack *s = (minStack *)malloc(sizeof(minStack)); + s->arr = (int *)malloc(sizeof(int) * INIT_SIZE); + s->minarr = (int *)malloc(sizeof(int) * INIT_SIZE); + s->capacity = INIT_SIZE; + s->top = NULL; + s->size = 0; + return (s); +} + +void push(minStack *s, int val) +{ + s->arr[s->size] = val; + if (s->size && val >= s->minarr[s->size - 1]) + s->minarr[s->size] = s->minarr[s->size - 1]; + else + s->minarr[s->size] = val; + s->top = &s->arr[s->size]; + s->size++; +} + int main (void) { From 0a7a7078faba63db2599caf5aa9393f99df775cf Mon Sep 17 00:00:00 2001 From: root Date: Sat, 26 Oct 2024 06:03:59 +0000 Subject: [PATCH 4/9] modified Minstack.c --- .../Algorithm Solutions/Minstack.c | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/codefest-2022/Algorithm Challenge/Algorithm Solutions/Minstack.c b/codefest-2022/Algorithm Challenge/Algorithm Solutions/Minstack.c index bf12781..7ec86da 100644 --- a/codefest-2022/Algorithm Challenge/Algorithm Solutions/Minstack.c +++ b/codefest-2022/Algorithm Challenge/Algorithm Solutions/Minstack.c @@ -2,6 +2,9 @@ #include #define INIT_SIZE 5 + +typedef int len_t; + /** * struct _minstack- stack of integers * @capacity: capacity of stack @@ -13,7 +16,7 @@ typedef struct _minStack { int capacity; - int size; + len_t size; int *arr; int *top; int *minarr; @@ -33,6 +36,13 @@ minStack *MyStack(void) void push(minStack *s, int val) { + if (s->size == s->capacity) + { + s->arr = (int *)realloc(s->arr, sizeof(int) * s->capacity * 2); + s->minarr = (int *)realloc(s->minarr, sizeof(int) * s->capacity * 2); + s->capacity *= 2; + } + s->arr[s->size] = val; if (s->size && val >= s->minarr[s->size - 1]) s->minarr[s->size] = s->minarr[s->size - 1]; @@ -45,5 +55,18 @@ void push(minStack *s, int val) int main (void) { + minStack *s = MyStack(); + push(s, 89); + push(s, 89); + push(s, 89); + push(s, 89); + push(s, 89); + push(s, 89); + push(s, 89); + push(s, 89); + push(s, 89); + + printf("\t%i\n", *s->top); + return (0); } \ No newline at end of file From 1b0df408a959a505989e99391bc3eaf9ed18e4c2 Mon Sep 17 00:00:00 2001 From: root Date: Sat, 26 Oct 2024 06:43:22 +0000 Subject: [PATCH 5/9] modified Minstack.c --- .../Algorithm Solutions/Minstack.c | 64 ++++++++++++++----- 1 file changed, 49 insertions(+), 15 deletions(-) diff --git a/codefest-2022/Algorithm Challenge/Algorithm Solutions/Minstack.c b/codefest-2022/Algorithm Challenge/Algorithm Solutions/Minstack.c index 7ec86da..1094f95 100644 --- a/codefest-2022/Algorithm Challenge/Algorithm Solutions/Minstack.c +++ b/codefest-2022/Algorithm Challenge/Algorithm Solutions/Minstack.c @@ -1,7 +1,10 @@ +// Using Dynamic arrays to Implement minstack +// Linked list or static arrays can also be used but there pros and cons with each Data Structure used + #include #include -#define INIT_SIZE 5 +#define INIT_SIZE 8 typedef int len_t; @@ -15,12 +18,13 @@ typedef int len_t; */ typedef struct _minStack { - int capacity; - len_t size; int *arr; - int *top; int *minarr; + len_t capacity; + len_t size; + len_t top; + }minStack; minStack *MyStack(void) @@ -29,13 +33,14 @@ minStack *MyStack(void) s->arr = (int *)malloc(sizeof(int) * INIT_SIZE); s->minarr = (int *)malloc(sizeof(int) * INIT_SIZE); s->capacity = INIT_SIZE; - s->top = NULL; + s->top = -1; s->size = 0; return (s); } void push(minStack *s, int val) { + //Resize array if needed if (s->size == s->capacity) { s->arr = (int *)realloc(s->arr, sizeof(int) * s->capacity * 2); @@ -48,25 +53,54 @@ void push(minStack *s, int val) s->minarr[s->size] = s->minarr[s->size - 1]; else s->minarr[s->size] = val; - s->top = &s->arr[s->size]; + s->top = s->size; s->size++; } +void pop(minStack *s) +{ + s->size--; + s->top--; + + //resize array if needed + if (s->size <= s->capacity / 4) + { + s->arr = (int *)realloc(s->arr, sizeof(int) * s->capacity / 2); + s->minarr = (int *)realloc(s->minarr, sizeof(int) * s->capacity / 2); + s->capacity /= 2; + } +} + +void printstack(minStack *s) +{ + int top = s->top; + + printf(" \tStack\t \t \tMinstack\n\n"); + printf("top --->|\t%i\t|\ttop --->|\t%i\t|\n", s->arr[top], s->minarr[top]); + for (int i = top - 1; i >= 0; i--) + printf(" |\t%i\t|\t |\t%i\t|\n", s->arr[i], s->minarr[i]); +} + int main (void) { minStack *s = MyStack(); + push(s, 7); push(s, 89); - push(s, 89); - push(s, 89); - push(s, 89); - push(s, 89); - push(s, 89); - push(s, 89); - push(s, 89); - push(s, 89); + push(s, 9); + push(s, 8); + push(s, 59); + push(s, 0); + push(s, 87); + push(s, 77); + push(s, 45); + + pop(s); + pop(s); + + //printf("\t%i\n", s->arr[s->top]); - printf("\t%i\n", *s->top); + printstack(s); return (0); } \ No newline at end of file From 5137956afb0cfa69477676533d8e20059441ecfd Mon Sep 17 00:00:00 2001 From: root Date: Sat, 26 Oct 2024 06:48:18 +0000 Subject: [PATCH 6/9] moidified Minstack --- .../Algorithm Solutions/Minstack.c | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/codefest-2022/Algorithm Challenge/Algorithm Solutions/Minstack.c b/codefest-2022/Algorithm Challenge/Algorithm Solutions/Minstack.c index 1094f95..7fe4eda 100644 --- a/codefest-2022/Algorithm Challenge/Algorithm Solutions/Minstack.c +++ b/codefest-2022/Algorithm Challenge/Algorithm Solutions/Minstack.c @@ -27,6 +27,11 @@ typedef struct _minStack }minStack; +/** +* MyStack- initializes stack +* Return: stack pointer +* +*/ minStack *MyStack(void) { minStack *s = (minStack *)malloc(sizeof(minStack)); @@ -38,6 +43,12 @@ minStack *MyStack(void) return (s); } +/** +* push- pushes to top of stack +* @s: stack pointer +* @val: value to be pushed +* +*/ void push(minStack *s, int val) { //Resize array if needed @@ -57,6 +68,11 @@ void push(minStack *s, int val) s->size++; } +/** +* pop- pops the top of stack +* @s: stack pointer +* +*/ void pop(minStack *s) { s->size--; @@ -71,6 +87,11 @@ void pop(minStack *s) } } +/** +* printstack- prints stack for testing +* @s: stack pointer +* +*/ void printstack(minStack *s) { int top = s->top; From 7c853c9ca2b88da8bbf396f1c5da86442c70ad43 Mon Sep 17 00:00:00 2001 From: root Date: Sat, 26 Oct 2024 06:53:16 +0000 Subject: [PATCH 7/9] added files --- codefest-2022/Algorithm Challenge/Algorithm Solutions/Minstack.c | 1 - 1 file changed, 1 deletion(-) diff --git a/codefest-2022/Algorithm Challenge/Algorithm Solutions/Minstack.c b/codefest-2022/Algorithm Challenge/Algorithm Solutions/Minstack.c index 7fe4eda..728e526 100644 --- a/codefest-2022/Algorithm Challenge/Algorithm Solutions/Minstack.c +++ b/codefest-2022/Algorithm Challenge/Algorithm Solutions/Minstack.c @@ -120,7 +120,6 @@ int main (void) pop(s); //printf("\t%i\n", s->arr[s->top]); - printstack(s); return (0); From 1fa944fa4815030c96569ca9eab4ce102b9060ae Mon Sep 17 00:00:00 2001 From: root Date: Sat, 26 Oct 2024 06:53:46 +0000 Subject: [PATCH 8/9] moidified Minstack --- codefest-2022/Algorithm Challenge/Algorithm Solutions/Minstack.c | 1 - 1 file changed, 1 deletion(-) diff --git a/codefest-2022/Algorithm Challenge/Algorithm Solutions/Minstack.c b/codefest-2022/Algorithm Challenge/Algorithm Solutions/Minstack.c index 728e526..d19171f 100644 --- a/codefest-2022/Algorithm Challenge/Algorithm Solutions/Minstack.c +++ b/codefest-2022/Algorithm Challenge/Algorithm Solutions/Minstack.c @@ -107,7 +107,6 @@ int main (void) { minStack *s = MyStack(); push(s, 7); - push(s, 89); push(s, 9); push(s, 8); push(s, 59); From 77901016805f15efc6f7b21b23ebfe349ab565bc Mon Sep 17 00:00:00 2001 From: root Date: Sun, 27 Oct 2024 01:03:18 +0000 Subject: [PATCH 9/9] modified Minstack.c --- .../Algorithm Solutions/Minstack.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/codefest-2022/Algorithm Challenge/Algorithm Solutions/Minstack.c b/codefest-2022/Algorithm Challenge/Algorithm Solutions/Minstack.c index d19171f..e0aab29 100644 --- a/codefest-2022/Algorithm Challenge/Algorithm Solutions/Minstack.c +++ b/codefest-2022/Algorithm Challenge/Algorithm Solutions/Minstack.c @@ -1,5 +1,5 @@ // Using Dynamic arrays to Implement minstack -// Linked list or static arrays can also be used but there pros and cons with each Data Structure used +// Linked list or static arrays can also be used but there are pros and cons with each Data Structure used #include #include @@ -101,8 +101,11 @@ void printstack(minStack *s) for (int i = top - 1; i >= 0; i--) printf(" |\t%i\t|\t |\t%i\t|\n", s->arr[i], s->minarr[i]); } - - +/** +* main- let's test +* +* Return: 0 on success +*/ int main (void) { minStack *s = MyStack(); @@ -122,4 +125,4 @@ int main (void) printstack(s); return (0); -} \ No newline at end of file +}