-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2dafa20
commit 37b967d
Showing
13 changed files
with
817 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
#include <assert.h> | ||
#include <ctype.h> | ||
#include <limits.h> | ||
#include <math.h> | ||
#include <stdbool.h> | ||
#include <stddef.h> | ||
#include <stdint.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
char* readline(); | ||
char* ltrim(char*); | ||
char* rtrim(char*); | ||
char** split_string(char*); | ||
|
||
int parse_int(char*); | ||
|
||
/* | ||
* Complete the 'hourglassSum' function below. | ||
* | ||
* The function is expected to return an INTEGER. | ||
* The function accepts 2D_INTEGER_ARRAY arr as parameter. | ||
*/ | ||
|
||
int hourglassSum(int arr_rows, int arr_columns, int** arr) { | ||
int actual, max = -64; | ||
for(int i = 0; i < arr_rows - 2; i++){ | ||
for(int j = 0; j < arr_columns - 2; j++){ | ||
actual = arr[i][j] + arr[i][j+1] + arr[i][j+2] + arr[i+1][j+1] + arr[i+2][j] + arr[i+2][j+1] + arr[i+2][j+2]; | ||
if(actual > max) | ||
max = actual; | ||
} | ||
} | ||
return max; | ||
} | ||
|
||
int main() | ||
{ | ||
FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w"); | ||
|
||
int** arr = malloc(6 * sizeof(int*)); | ||
|
||
for (int i = 0; i < 6; i++) { | ||
*(arr + i) = malloc(6 * (sizeof(int))); | ||
|
||
char** arr_item_temp = split_string(rtrim(readline())); | ||
|
||
for (int j = 0; j < 6; j++) { | ||
int arr_item = parse_int(*(arr_item_temp + j)); | ||
|
||
*(*(arr + i) + j) = arr_item; | ||
} | ||
} | ||
|
||
int result = hourglassSum(6, 6, arr); | ||
|
||
fprintf(fptr, "%d\n", result); | ||
|
||
fclose(fptr); | ||
|
||
return 0; | ||
} | ||
|
||
char* readline() { | ||
size_t alloc_length = 1024; | ||
size_t data_length = 0; | ||
|
||
char* data = malloc(alloc_length); | ||
|
||
while (true) { | ||
char* cursor = data + data_length; | ||
char* line = fgets(cursor, alloc_length - data_length, stdin); | ||
|
||
if (!line) { | ||
break; | ||
} | ||
|
||
data_length += strlen(cursor); | ||
|
||
if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { | ||
break; | ||
} | ||
|
||
alloc_length <<= 1; | ||
|
||
data = realloc(data, alloc_length); | ||
|
||
if (!data) { | ||
data = '\0'; | ||
|
||
break; | ||
} | ||
} | ||
|
||
if (data[data_length - 1] == '\n') { | ||
data[data_length - 1] = '\0'; | ||
|
||
data = realloc(data, data_length); | ||
|
||
if (!data) { | ||
data = '\0'; | ||
} | ||
} else { | ||
data = realloc(data, data_length + 1); | ||
|
||
if (!data) { | ||
data = '\0'; | ||
} else { | ||
data[data_length] = '\0'; | ||
} | ||
} | ||
|
||
return data; | ||
} | ||
|
||
char* ltrim(char* str) { | ||
if (!str) { | ||
return '\0'; | ||
} | ||
|
||
if (!*str) { | ||
return str; | ||
} | ||
|
||
while (*str != '\0' && isspace(*str)) { | ||
str++; | ||
} | ||
|
||
return str; | ||
} | ||
|
||
char* rtrim(char* str) { | ||
if (!str) { | ||
return '\0'; | ||
} | ||
|
||
if (!*str) { | ||
return str; | ||
} | ||
|
||
char* end = str + strlen(str) - 1; | ||
|
||
while (end >= str && isspace(*end)) { | ||
end--; | ||
} | ||
|
||
*(end + 1) = '\0'; | ||
|
||
return str; | ||
} | ||
|
||
char** split_string(char* str) { | ||
char** splits = NULL; | ||
char* token = strtok(str, " "); | ||
|
||
int spaces = 0; | ||
|
||
while (token) { | ||
splits = realloc(splits, sizeof(char*) * ++spaces); | ||
|
||
if (!splits) { | ||
return splits; | ||
} | ||
|
||
splits[spaces - 1] = token; | ||
|
||
token = strtok(NULL, " "); | ||
} | ||
|
||
return splits; | ||
} | ||
|
||
int parse_int(char* str) { | ||
char* endptr; | ||
int value = strtol(str, &endptr, 10); | ||
|
||
if (endptr == str || *endptr != '\0') { | ||
exit(EXIT_FAILURE); | ||
} | ||
|
||
return value; | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
#include <assert.h> | ||
#include <limits.h> | ||
#include <math.h> | ||
#include <stdbool.h> | ||
#include <stddef.h> | ||
#include <stdint.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
char* readline(); | ||
|
||
typedef struct SinglyLinkedListNode SinglyLinkedListNode; | ||
typedef struct SinglyLinkedList SinglyLinkedList; | ||
|
||
struct SinglyLinkedListNode { | ||
int data; | ||
SinglyLinkedListNode* next; | ||
}; | ||
|
||
struct SinglyLinkedList { | ||
SinglyLinkedListNode* head; | ||
SinglyLinkedListNode* tail; | ||
}; | ||
|
||
SinglyLinkedListNode* create_singly_linked_list_node(int node_data) { | ||
SinglyLinkedListNode* node = malloc(sizeof(SinglyLinkedListNode)); | ||
|
||
node->data = node_data; | ||
node->next = NULL; | ||
|
||
return node; | ||
} | ||
|
||
void insert_node_into_singly_linked_list(SinglyLinkedList** singly_linked_list, int node_data) { | ||
SinglyLinkedListNode* node = create_singly_linked_list_node(node_data); | ||
|
||
if (!(*singly_linked_list)->head) { | ||
(*singly_linked_list)->head = node; | ||
} else { | ||
(*singly_linked_list)->tail->next = node; | ||
} | ||
|
||
(*singly_linked_list)->tail = node; | ||
} | ||
|
||
void print_singly_linked_list(SinglyLinkedListNode* node, char* sep, FILE* fptr) { | ||
while (node) { | ||
fprintf(fptr, "%d", node->data); | ||
|
||
node = node->next; | ||
|
||
if (node) { | ||
fprintf(fptr, "%s", sep); | ||
} | ||
} | ||
} | ||
|
||
void free_singly_linked_list(SinglyLinkedListNode* node) { | ||
while (node) { | ||
SinglyLinkedListNode* temp = node; | ||
node = node->next; | ||
|
||
free(temp); | ||
} | ||
} | ||
|
||
/* | ||
* Complete the 'getNode' function below. | ||
* | ||
* The function is expected to return an INTEGER. | ||
* The function accepts following parameters: | ||
* 1. INTEGER_SINGLY_LINKED_LIST llist | ||
* 2. INTEGER positionFromTail | ||
*/ | ||
|
||
/* | ||
* For your reference: | ||
* | ||
* SinglyLinkedListNode { | ||
* int data; | ||
* SinglyLinkedListNode* next; | ||
* }; | ||
* | ||
*/ | ||
|
||
int getNode(SinglyLinkedListNode* llist, int positionFromTail) { | ||
int len; | ||
SinglyLinkedListNode *i; | ||
len ^= len; | ||
for(i = llist; i != NULL; i = i->next) | ||
len++; | ||
positionFromTail = len - (positionFromTail + 1); | ||
len ^= len; | ||
for(i = llist; i != NULL; i = i->next, len++){ | ||
if(len == positionFromTail) | ||
return i->data; | ||
} | ||
return 0; | ||
} | ||
|
||
int main() | ||
{ | ||
FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w"); | ||
|
||
char* tests_endptr; | ||
char* tests_str = readline(); | ||
int tests = strtol(tests_str, &tests_endptr, 10); | ||
|
||
if (tests_endptr == tests_str || *tests_endptr != '\0') { exit(EXIT_FAILURE); } | ||
|
||
for (int tests_itr = 0; tests_itr < tests; tests_itr++) { | ||
SinglyLinkedList* llist = malloc(sizeof(SinglyLinkedList)); | ||
llist->head = NULL; | ||
llist->tail = NULL; | ||
|
||
char* llist_count_endptr; | ||
char* llist_count_str = readline(); | ||
int llist_count = strtol(llist_count_str, &llist_count_endptr, 10); | ||
|
||
if (llist_count_endptr == llist_count_str || *llist_count_endptr != '\0') { exit(EXIT_FAILURE); } | ||
|
||
for (int i = 0; i < llist_count; i++) { | ||
char* llist_item_endptr; | ||
char* llist_item_str = readline(); | ||
int llist_item = strtol(llist_item_str, &llist_item_endptr, 10); | ||
|
||
if (llist_item_endptr == llist_item_str || *llist_item_endptr != '\0') { exit(EXIT_FAILURE); } | ||
|
||
insert_node_into_singly_linked_list(&llist, llist_item); | ||
} | ||
|
||
char* position_endptr; | ||
char* position_str = readline(); | ||
int position = strtol(position_str, &position_endptr, 10); | ||
|
||
if (position_endptr == position_str || *position_endptr != '\0') { exit(EXIT_FAILURE); } | ||
|
||
int result = getNode(llist->head, position); | ||
|
||
fprintf(fptr, "%d\n", result); | ||
} | ||
|
||
fclose(fptr); | ||
|
||
return 0; | ||
} | ||
|
||
char* readline() { | ||
size_t alloc_length = 1024; | ||
size_t data_length = 0; | ||
char* data = malloc(alloc_length); | ||
|
||
while (true) { | ||
char* cursor = data + data_length; | ||
char* line = fgets(cursor, alloc_length - data_length, stdin); | ||
|
||
if (!line) { break; } | ||
|
||
data_length += strlen(cursor); | ||
|
||
if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { break; } | ||
|
||
size_t new_length = alloc_length << 1; | ||
data = realloc(data, new_length); | ||
|
||
if (!data) { break; } | ||
|
||
alloc_length = new_length; | ||
} | ||
|
||
if (data[data_length - 1] == '\n') { | ||
data[data_length - 1] = '\0'; | ||
} | ||
|
||
data = realloc(data, data_length); | ||
|
||
return data; | ||
} |
Binary file added
BIN
+36.7 KB
...uctures/Get_node_value/get-the-value-of-the-node-at-a-specific-position-from-the-tail.pdf
Binary file not shown.
Oops, something went wrong.