diff --git a/.gitignore b/.gitignore index 9eef975..20970fe 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,12 @@ # Created by https://www.toptal.com/developers/gitignore/api/visualstudiocode,c # Edit at https://www.toptal.com/developers/gitignore?templates=visualstudiocode,c +### CMake ### +bin/ +build/ +lib/ +Testing/ + ### C ### # Prerequisites *.d @@ -58,11 +64,6 @@ dkms.conf ### VisualStudioCode ### .vscode/* -!.vscode/settings.json -!.vscode/tasks.json -!.vscode/launch.json -!.vscode/extensions.json -!.vscode/*.code-snippets # Local History for Visual Studio Code .history/ @@ -75,4 +76,4 @@ dkms.conf .history .ionide -# End of https://www.toptal.com/developers/gitignore/api/visualstudiocode,c +# End of https://www.toptal.com/developers/gitignore/api/visualstudiocode,c \ No newline at end of file diff --git a/06_lists/src/CMakeLists.txt b/06_lists/src/CMakeLists.txt new file mode 100644 index 0000000..6e8e516 --- /dev/null +++ b/06_lists/src/CMakeLists.txt @@ -0,0 +1,20 @@ +cmake_minimum_required(VERSION 3.10) +project(Lists) + +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../bin) +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../lib) +set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../lib) + +add_library(List list.c) +add_library(circularList circularList.c) + +add_executable(sortedList sortedList.c) +target_link_libraries(sortedList List) + +add_executable(josephus josephusProblem.c) +target_link_libraries(josephus circularList) + +add_compile_options(-Wall -Wextra -pedantic) + +enable_testing() +add_test(NAME tests COMMAND sortedList --test) diff --git a/06_lists/src/circularList.c b/06_lists/src/circularList.c new file mode 100644 index 0000000..a7b4c25 --- /dev/null +++ b/06_lists/src/circularList.c @@ -0,0 +1,204 @@ +#include "circularList.h" + +#include +#include +#include + +typedef struct Node { + int data; + struct Node* next; +} Node; + +typedef struct List { + Node* head; +} List; + +List* newList() +{ + List* newList = calloc(1, sizeof(List)); + return newList; +} + +Node* createNode(int data) +{ + Node* newNode = calloc(1, sizeof(Node)); + + if (newNode == NULL) { + return NULL; + } + + newNode->data = data; + newNode->next = NULL; + + return newNode; +} + +bool insert(List* list, int data, unsigned index) +{ + if (list == NULL) { + return false; + } + + Node* newNode = createNode(data); + + if (newNode == NULL) { + return false; + } + + if (list->head == NULL) { + if (index == 0) { + newNode->next = newNode; + list->head = newNode; + return true; + } + free(newNode); + return false; + } + + if (index == 0) { + newNode->next = list->head; + Node* current = list->head; + + while (current->next != list->head) { + current = current->next; + } + + current->next = newNode; + list->head = newNode; + return true; + } + + Node* before = list->head; + + for (unsigned i = 1; i < index; i++) { + before = before->next; + + if (before == list->head) { + free(newNode); + return false; + } + } + + Node* ahead = before->next; + before->next = newNode; + newNode->next = ahead; + + return true; +} + +bool del(List* list, unsigned index) +{ + if (list == NULL || list->head == NULL) { + return false; + } + + Node* before = list->head; + + if (index == 0) { + if (before->next == before) { + free(before); + list->head = NULL; + return true; + } + + Node* current = before; + + while (current->next != before) { + current = current->next; + } + current->next = before->next; + list->head = before->next; + free(before); + return true; + } + + for (unsigned i = 1; i < index; i++) { + before = before->next; + + if (before == list->head) { + return false; + } + } + + Node* current = before->next; + if (current == NULL || current == list->head) { + return false; + } + + before->next = current->next; + free(current); + return true; +} + +int isEmpty(List* list) +{ + if (list == NULL) { + return 2; + } + + return list->head == NULL; +} + +bool deleteList(List* list) +{ + if (list == NULL) { + return true; + } + + if (list->head != NULL) { + Node* current = list->head->next; + while (current != list->head) { + Node* next = current->next; + free(current); + current = next; + } + free(list->head); + } + + free(list); + return true; +} + +bool len(List* list, unsigned* res) +{ + unsigned len = 0; + + if (list == NULL || res == NULL) { + return false; + } + + if (list->head == NULL) { + *res = 0; + return true; + } + + Node* current = list->head; + len = 1; + + while (current->next != list->head) { + len++; + current = current->next; + } + + *res = len; + return true; +} + +bool find(List* list, unsigned index, int* res) +{ + if (list == NULL || list->head == NULL || res == NULL) { + return false; + } + + Node* current = list->head; + for (unsigned i = 0; i < index; i++) { + current = current->next; + + if (current == list->head) { + return false; + } + } + + *res = current->data; + return true; +} diff --git a/06_lists/src/circularList.h b/06_lists/src/circularList.h new file mode 100644 index 0000000..049fcf6 --- /dev/null +++ b/06_lists/src/circularList.h @@ -0,0 +1,14 @@ +#pragma once +#include + +typedef struct Node Node; +typedef struct List List; + +List* newList(); +Node* createNode(int data); +bool insert(List* list, int data, unsigned index); +bool del(List* list, unsigned index); +int isEmpty(List* list); +bool deleteList(List* list); +bool len(List* list, unsigned* res); +bool find(List* list, unsigned index, int* res); \ No newline at end of file diff --git a/06_lists/src/josephusProblem.c b/06_lists/src/josephusProblem.c new file mode 100644 index 0000000..e7ae1d8 --- /dev/null +++ b/06_lists/src/josephusProblem.c @@ -0,0 +1,53 @@ +#include "circularList.h" +#include + +bool josephus(int n, int m, int* res) +{ + if (n <= 0 || m <= 0) { + return false; + } + + List* list = newList(); + int listLen = n; + + for (int i = 1; i <= n; i++) { + if (!insert(list, i, i - 1)) { + deleteList(list); + return false; + } + } + + int currentIndex = (m - 1) % n; + while (listLen > 1) { + if (!del(list, currentIndex)) { + deleteList(list); + return false; + } + listLen--; + currentIndex = (currentIndex + m - 1) % listLen; + } + + int last = -1; + if (!find(list, 0, &last)) { + deleteList(list); + return false; + } + + deleteList(list); + *res = last; + return true; +} + +int main(void) +{ + int n = 0, m = 0; + scanf("%d%d", &n, &m); + + int res = -1; + if (!josephus(n, m, &res)) { + return 1; + } + + printf("%d", res); + return 0; +} \ No newline at end of file diff --git a/06_lists/src/list.c b/06_lists/src/list.c new file mode 100644 index 0000000..821db69 --- /dev/null +++ b/06_lists/src/list.c @@ -0,0 +1,229 @@ +#include "list.h" + +#include +#include +#include + +typedef struct Node { + int data; + struct Node* next; +} Node; + +typedef struct List { + Node* head; +} List; + +List* newList() +{ + List* newList = calloc(1, sizeof(List)); + return newList; +} + +Node* createNode(int data) +{ + Node* newNode = calloc(1, sizeof(Node)); + + if (newNode == NULL) { + return NULL; + } + + newNode->data = data; + newNode->next = NULL; + + return newNode; +} + +bool insert(List* list, int data, unsigned index) +{ + if (list == NULL) { + return false; + } + + Node* newNode = createNode(data); + + if (newNode == NULL) { + return false; + } + + if (index == 0) { + newNode->next = list->head; + list->head = newNode; + return true; + } + + if (list->head == NULL) { + free(newNode); + return false; + } + + Node* before = list->head; + + for (unsigned i = 1; i < index; i++) { + before = before->next; + + if (before == NULL) { + free(newNode); + return false; + } + } + + Node* ahead = before->next; + before->next = newNode; + newNode->next = ahead; + + return true; +} + +bool pop(List* list, unsigned index, int* res) +{ + if (list == NULL || list->head == NULL || res == NULL) { + return false; + } + + Node* before = list->head; + + if (index == 0) { + *res = before->data; + list->head = before->next; + free(before); + return true; + } + + for (unsigned i = 1; i < index; i++) { + before = before->next; + + if (before == NULL) { + return false; + } + } + + Node* current = before->next; + if (current == NULL) { + return false; + } + + *res = current->data; + before->next = current->next; + + free(current); + return true; +} + +bool find(List* list, unsigned index, int* res) +{ + if (list == NULL || list->head == NULL || res == NULL) { + return false; + } + + Node* current = list->head; + + for (unsigned i = 0; i < index; i++) { + current = current->next; + + if (current == NULL) { + return false; + } + } + + *res = current->data; + return true; +} + +int indexOf(List* list, int data, unsigned* res) +{ + if (list == NULL || res == NULL) { + return 2; + } + + if (list->head == NULL) { + *res = 0; + return 1; + } + + Node* current = list->head; + + unsigned i = 0; + while (current != NULL && current->data < data) { + i++; + current = current->next; + } + + *res = i; + if (current == NULL || current->data != data) { + return 1; + } + + return 0; +} + +int isEmpty(List* list) +{ + if (list == NULL) { + return 2; + } + + return list->head == NULL; +} + +bool deleteList(List* list) +{ + if (list == NULL) { + return true; + } + + Node* current = list->head; + + while (current != NULL) { + Node* next = current->next; + free(current); + current = next; + } + + free(list); + return true; +} + +bool len(List* list, unsigned* res) +{ + unsigned len = 0; + + if (list == NULL || res == NULL) { + return false; + } + + Node* current = list->head; + + while (current != NULL) { + len++; + current = current->next; + } + + *res = len; + return true; +} + +bool printList(List* list, unsigned startIndex, unsigned endIndex) +{ + if (list == NULL || list->head == NULL || startIndex >= endIndex) { + return false; + } + + Node* current = list->head; + for (unsigned i = 0; i < startIndex; i++) { + current = current->next; + if (current == NULL) { + return false; + } + } + + for (unsigned i = startIndex; i < endIndex; i++) { + printf("%d ", current->data); + current = current->next; + if (current == NULL) { + break; + } + } + printf("\n"); + + return true; +} diff --git a/06_lists/src/list.h b/06_lists/src/list.h new file mode 100644 index 0000000..708d0c5 --- /dev/null +++ b/06_lists/src/list.h @@ -0,0 +1,16 @@ +#pragma once +#include + +typedef struct Node Node; +typedef struct List List; + +List* newList(); +Node* createNode(int data); +bool insert(List* list, int data, unsigned index); +bool pop(List* list, unsigned index, int* res); +bool find(List* list, unsigned index, int* res); +int indexOf(List* list, int data, unsigned* res); +int isEmpty(List* list); +bool deleteList(List* list); +bool len(List* list, unsigned* res); +bool printList(List* list, unsigned startIndex, unsigned endIndex); diff --git a/06_lists/src/sortedList.c b/06_lists/src/sortedList.c new file mode 100644 index 0000000..c9a5d55 --- /dev/null +++ b/06_lists/src/sortedList.c @@ -0,0 +1,82 @@ +#include +#include + +#include "list.h" + +bool add(List* list, int val) +{ + unsigned index = 0; + if (indexOf(list, val, &index) != 2) { + insert(list, val, index); + return true; + } + + return false; +} + +bool rm(List* list, int val) +{ + unsigned index = -1; + int res = indexOf(list, val, &index); + + if (!res) { + pop(list, index, &val); + } else if (res == 2) { + if (index != 0) { + return false; + } + } + + return true; +} + +#include "../tests/tests.c" + +int main(int argc, char* argv[]) +{ + if (argc == 2 && strcmp(argv[1], "--test") == 0) { + runTests(); + return 0; + } + + int op = 0; + List* list = newList(); + if (list == NULL) { + return 1; + } + + while (scanf("%d", &op) == 1) { + unsigned index = 0; + int val = 0; + + switch (op) { + case 0: + deleteList(list); + return 0; + case 1: + scanf("%d", &val); + + if (!add(list, val)) { + deleteList(list); + return 1; + } + + break; + case 2: + scanf("%d", &val); + + if (!rm(list, val)) { + deleteList(list); + return 1; + } + + break; + case 3: + printList(list, 0, -1); + break; + } + } + + deleteList(list); + return 0; +} diff --git a/06_lists/tests/tests.c b/06_lists/tests/tests.c new file mode 100644 index 0000000..1d31a47 --- /dev/null +++ b/06_lists/tests/tests.c @@ -0,0 +1,182 @@ +#include +#include + +void testAdd() +{ + List* list = newList(); + insert(list, 324, 0); + insert(list, 32423, 1); + assert(add(list, 5555) == true); + + int res = -1; + find(list, 0, &res); + assert(res == 324); + + find(list, 1, &res); + assert(res == 5555); + + find(list, 2, &res); + assert(res == 32423); + + deleteList(list); +} + +void testRm() +{ + List* list = newList(); + insert(list, 324, 0); + insert(list, 5555, 1); + insert(list, 32423, 2); + + assert(rm(list, 5555) == true); + + int res = -1; + find(list, 0, &res); + assert(res == 324); + + find(list, 1, &res); + assert(res == 32423); + + deleteList(list); +} + +void testAddInEmpty() +{ + List* list = newList(); + assert(add(list, 444) == true); + + int res = -1; + find(list, 0, &res); + assert(res == 444); + + deleteList(list); +} + +void testRmInEmpty() +{ + List* list = newList(); + assert(rm(list, 3243241) == true); + + deleteList(list); +} + +void testAddDuplicate() +{ + List* list = newList(); + insert(list, 324, 0); + insert(list, 32423, 1); + assert(add(list, 5555) == true); + assert(add(list, 5555) == true); + + int res = -1; + find(list, 0, &res); + assert(res == 324); + + find(list, 1, &res); + assert(res == 5555); + + find(list, 2, &res); + assert(res == 5555); + + find(list, 3, &res); + assert(res == 32423); + + deleteList(list); +} + +void testRmNonExistent() +{ + List* list = newList(); + add(list, 10); + add(list, 20); + add(list, 30); + + assert(rm(list, 15) == true); + + unsigned length = 0; + len(list, &length); + + assert(length == 3); + + deleteList(list); +} + +void testRmHead() +{ + List* list = newList(); + add(list, 10); + add(list, 20); + + assert(rm(list, 10) == true); + + int res = -1; + + find(list, 0, &res); + assert(res == 20); + + deleteList(list); +} + +void testRmTail() +{ + List* list = newList(); + add(list, 10); + add(list, 20); + + assert(rm(list, 20) == true); + + int res = -1; + + find(list, 0, &res); + assert(res == 10); + + deleteList(list); +} + +void testAddHead() +{ + List* list = newList(); + add(list, 20); + add(list, 10); + + int res = -1; + + find(list, 0, &res); + assert(res == 10); + + find(list, 1, &res); + assert(res == 20); + + deleteList(list); +} + +void testAddTail() +{ + List* list = newList(); + add(list, 10); + add(list, 20); + + int res = -1; + + find(list, 0, &res); + assert(res == 10); + + find(list, 1, &res); + assert(res == 20); + + deleteList(list); +} + +void runTests() +{ + testAdd(); + testRm(); + testAddInEmpty(); + testRmInEmpty(); + testAddDuplicate(); + testRmNonExistent(); + testRmHead(); + testRmTail(); + testAddHead(); + testAddTail(); +} \ No newline at end of file diff --git a/15.09/src/1.c b/15.09/src/1.c new file mode 100644 index 0000000..e5f0148 --- /dev/null +++ b/15.09/src/1.c @@ -0,0 +1,45 @@ +#include +#include +#include +#include + +bool bracketsChecker(char* string) +{ + bool result = true; + int balance = 0; + unsigned long len = strlen(string); + + for (unsigned long i = 0; i < len; i++) { + if (string[i] == '(') { + balance++; + } else if (string[i] == ')') { + balance--; + if (balance < 0) { + result = false; + } + } + } + + if (balance != 0) { + result = false; + } + + return result; +} + +int main(void) +{ + int n = 0; + scanf("%d\n", &n); + + char* input = calloc(n + 1, sizeof(char)); + fgets(input, n + 1, stdin); + + bool result = true; + + result = bracketsChecker(input); + printf("%d\n", result); + + free(input); + return 0; +} \ No newline at end of file diff --git a/15.09/src/2.c b/15.09/src/2.c new file mode 100644 index 0000000..c803d82 --- /dev/null +++ b/15.09/src/2.c @@ -0,0 +1,50 @@ +#include +#include +#include + +#define MIN(a, b) ((a) < (b) ? (a) : (b)) + +bool substrCount(char* s, char* s1, int lenS, int lenS1) +{ + int counter = 0; + for (int i = 0; i < lenS; i++) { + int matchLen = 0; + + for (int o = i; o < MIN(lenS, i + lenS1); o++) { + if (s1[o - i] != s[o]) { + break; + } + matchLen++; + } + + if (matchLen == lenS1) { + counter++; + } + } + + return counter; +} + +int main(void) +{ + int counter = 0; + int lenS = 0; + int lenS1 = 0; + scanf("%d%d\n", &lenS, &lenS1); + + char* s = malloc((lenS + 1) * sizeof(char)); + char* s1 = malloc((lenS1 + 1) * sizeof(char)); + + fgets(s, lenS + 1, stdin); + getchar(); + fgets(s1, lenS1 + 1, stdin); + + counter = substrCount(s, s1, lenS, lenS1); + + printf("%d\n", counter); + + free(s); + free(s1); + + return 0; +} \ No newline at end of file diff --git a/15.09/src/3.c b/15.09/src/3.c new file mode 100644 index 0000000..c0abb49 --- /dev/null +++ b/15.09/src/3.c @@ -0,0 +1,25 @@ +#include +#include + +int main(void) +{ + int n = 0; + int counter = 0; + scanf("%d\n", &n); + + int *array = malloc(n * sizeof(int)); + for (int i = 0; i < n; i++) { + scanf("%d", &array[i]); + } + + for (int i = 0; i < n; i++) { + if (!array[i]) { + counter++; + } + } + + printf("%d\n", counter); + + free(array); + return 0; +} \ No newline at end of file