diff --git a/semester1/pset12/1-3-INCORRECT-GetVowel.c b/semester1/pset12/1-3-INCORRECT-GetVowel.c index d562652..7670d77 100644 --- a/semester1/pset12/1-3-INCORRECT-GetVowel.c +++ b/semester1/pset12/1-3-INCORRECT-GetVowel.c @@ -1,3 +1,4 @@ +// https://bbc3502.hopto.org/c2021/index.php/home-17/590-3 /* ⚠*** This code is not perfect *** */ #include @@ -22,10 +23,10 @@ int main() { #include #pragma GCC optimize("Ofast,inline") int getVowel(char str[], char vowel[]) { - char vowelstr[] = {'a', 'e', 'i', 'o', 'u'}; + char vowelstr[] = {'a', 'e', 'i', 'o', 'u', '\0'}; int len = 0; for (int i = 0, n = strlen(str); i < n; i++) { - for (int j = 0; j < 5; j++) { + for (int j = 0, m = strlen(vowelstr); j < m; j++) { if (vowelstr[j] == str[i]) { vowel[len++] = str[i]; } diff --git a/semester1/pset12/2-1-paren.c b/semester1/pset12/2-1-paren.c new file mode 100644 index 0000000..0a86695 --- /dev/null +++ b/semester1/pset12/2-1-paren.c @@ -0,0 +1,29 @@ +// https://bbc3502.hopto.org/c2021/index.php/home-17/591-1 +// This code should get 100 points but due to the shitty pintia, it gets 90. +#include +#include +#define MAXLEN 101 + +int main(void) { + char str[MAXLEN]; + // fgets is not gets + fgets(str, MAXLEN, stdin); + + // Process string + int count = 0; + for (int i = 0, n = strlen(str); i < n; i++) { + if (str[i] == '(') { + count++; + } else if (str[i] == ')') { + count--; + } + } + + // Validate + if (count != 0) { + printf("parentheses do not match!\n"); + } else { + printf("parentheses match!\n"); + } + return 0; +} diff --git a/semester1/pset12/2-2-IDnumber.c b/semester1/pset12/2-2-IDnumber.c new file mode 100644 index 0000000..c17ee9a --- /dev/null +++ b/semester1/pset12/2-2-IDnumber.c @@ -0,0 +1,43 @@ +#include +#include +#pragma GCC optimize("Ofast,inline") +#define MAXLEN 18 + +// returns 1 if valid, otherwise 0 +int validate(char *id); + +int main(void) { + char *id = malloc(sizeof(char) * MAXLEN); + int lines; + + scanf("%d", &lines); + + for (int i = 0; i < lines; i++) { + scanf("%s", id); + if (validate(id)) { + printf("right\n"); + } else { + printf("wrong\n"); + } + } + + free(id); + return 0; +} + +int validate(char *id) { + char lasttable[] = "10X98765432"; + int factor[] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2}; + int last = 0; + + for (int i = 0; i < 17; i++) { + last += (id[i] - '0') * factor[i]; + } + + last = last % 11; + if (lasttable[last] == id[17]) { + return 1; + } else { + return 0; + } +} diff --git a/semester1/pset12/2-3-analyze.c b/semester1/pset12/2-3-analyze.c new file mode 100644 index 0000000..a78a2ac --- /dev/null +++ b/semester1/pset12/2-3-analyze.c @@ -0,0 +1,33 @@ +#include +#include +#pragma GCC optimize("Ofast,inline") +#define MAXLEN 100 + +int main(void) { + char string[MAXLEN]; + scanf("%s", string); + + char hashA[26] = {}; + char hasha[26] = {}; + + // Store values to hash table + for (int i = 0, j = strlen(string); i < j; i++) { + if (string[i] >= 'A' && string[i] <= 'Z') { + hashA[string[i] - 'A']++; + } else if (string[i] >= 'a' && string[i] <= 'z') { + hasha[string[i] - 'a']++; + } + } + + for (int i = 0; i < 26; i++) { + if (hashA[i]) { + printf("The character %c has presented %i times.\n", i + 'A', hashA[i]); + } + } + for (int i = 0; i < 26; i++) { + if (hasha[i]) { + printf("The character %c has presented %i times.\n", i + 'a', hasha[i]); + } + } + return 0; +} diff --git a/semester1/pset12/2-4-OrderedPrint.c b/semester1/pset12/2-4-OrderedPrint.c new file mode 100644 index 0000000..c0c23b6 --- /dev/null +++ b/semester1/pset12/2-4-OrderedPrint.c @@ -0,0 +1,31 @@ +#include +#include +#pragma GCC optimize("Ofast,inline") +#define MAXLEN 31 + +int main(void) { + int lines; + scanf("%d", &lines); + + // Fancy way of allocating memory to 2d array + char *arr[lines]; + for (int i = 0; i < lines; i++) { + arr[i] = malloc(sizeof(char) * MAXLEN); + } + + // Populate the 2d array + for (int i = 0; i < lines; i++) { + scanf("%s", arr[i]); + } + + // Populate the order array + int order[lines]; + for (int i = 0; i < lines; i++) { + scanf("%d", &order[i]); + } + + for (int i = 0; i < lines; i++) { + printf("%s\n", arr[order[i]]); + } + return 0; +}