HackerRank CODING
#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();
char** split_string(char*);
char* kangaroo(int x1, int v1, int x2, int v2) {
}
int main()
{
FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w");
char** x1V1X2V2 = split_string(readline());
char* x1_endptr;
char* x1_str = x1V1X2V2[0];
int x1 = strtol(x1_str, &x1_endptr, 10);
if (x1_endptr == x1_str || *x1_endptr != '\0') { exit(EXIT_FAILURE); }
char* v1_endptr;
char* v1_str = x1V1X2V2[1];
int v1 = strtol(v1_str, &v1_endptr, 10);
if (v1_endptr == v1_str || *v1_endptr != '\0') { exit(EXIT_FAILURE); }
char* x2_endptr;
char* x2_str = x1V1X2V2[2];
int x2 = strtol(x2_str, &x2_endptr, 10);
if (x2_endptr == x2_str || *x2_endptr != '\0') { exit(EXIT_FAILURE); }
char* v2_endptr;
char* v2_str = x1V1X2V2[3];
int v2 = strtol(v2_str, &v2_endptr, 10);
if (v2_endptr == v2_str || *v2_endptr != '\0') { exit(EXIT_FAILURE); }
char* result = kangaroo(x1, v1, x2, v2);
fprintf(fptr, "%s\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;
}
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;
}
APPLE AND ORANGE https://www.hackerrank.com/challenges/apple-and-orange/problem #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(); char** split_string(char*); // Complete the countApplesAndOranges function below. void countApplesAndOranges(int s, int t, int a, int b, int apples_count, int* apples, int oranges_count, int* oranges) { } int main() { char** st = split_string(readline()); char* s_endptr; char* s_str = st[0]; int s = strtol(s_str, &s_endptr, 10); if (s_endptr == s_str || *s_endptr != '\0') { exit(EXIT_FAILURE); } char* t_endptr; char* t_str = st[1]; int t = strtol(t_str, &t_endptr, 10); if (t_endptr == t_str || *t_endptr != '\0') { exit(EXIT_FAILURE); } char** ab = split_string(readline()); char* a_endptr; char* a_str = ab[0]; int a = strtol(a_str, &a_endptr, 10); if (a_endptr == a_str || *a_endptr != '\0') { exit(EXIT_FAILURE); } char* b_endptr; char* b_str = ab[1]; int b = strtol(b_str, &b_endptr, 10); if (b_endptr == b_str || *b_endptr != '\0') { exit(EXIT_FAILURE); } char** mn = split_string(readline()); char* m_endptr; char* m_str = mn[0]; int m = strtol(m_str, &m_endptr, 10); if (m_endptr == m_str || *m_endptr != '\0') { exit(EXIT_FAILURE); } char* n_endptr; char* n_str = mn[1]; int n = strtol(n_str, &n_endptr, 10); if (n_endptr == n_str || *n_endptr != '\0') { exit(EXIT_FAILURE); } char** apples_temp = split_string(readline()); int* apples = malloc(m * sizeof(int)); for (int i = 0; i < m; i++) { char* apples_item_endptr; char* apples_item_str = *(apples_temp + i); int apples_item = strtol(apples_item_str, &apples_item_endptr, 10); if (apples_item_endptr == apples_item_str || *apples_item_endptr != '\0') { exit(EXIT_FAILURE); } *(apples + i) = apples_item; } int apples_count = m; char** oranges_temp = split_string(readline()); int* oranges = malloc(n * sizeof(int)); for (int i = 0; i < n; i++) { char* oranges_item_endptr; char* oranges_item_str = *(oranges_temp + i); int oranges_item = strtol(oranges_item_str, &oranges_item_endptr, 10); if (oranges_item_endptr == oranges_item_str || *oranges_item_endptr != '\0') { exit(EXIT_FAILURE); } *(oranges + i) = oranges_item; } int oranges_count = n; countApplesAndOranges(s, t, a, b, apples_count, apples, oranges_count, oranges); 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; } 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; } POINTERS IN C #include <stdio.h> void update(int *a,int *b) { // Complete this function } int main() { int a, b; int *pa = &a, *pb = &b; scanf("%d %d", &a, &b); update(pa, pb); printf("%d\n%d", a, b); return 0; } NEXT PROBLEM https://www.hackerrank.com/challenges/electronics-shop/problem ELECTRONICS SHOP #include <assert.h> #include <limits.h> #include <math.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> char* readline(); char** split_string(char*); /* * Complete the getMoneySpent function below. */ int getMoneySpent(int keyboards_count, int* keyboards, int drives_count, int* drives, int b) { /* * Write your code here. */ } int main() { FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w"); char** bnm = split_string(readline()); char* b_endptr; char* b_str = bnm[0]; int b = strtol(b_str, &b_endptr, 10); if (b_endptr == b_str || *b_endptr != '\0') { exit(EXIT_FAILURE); } char* n_endptr; char* n_str = bnm[1]; int n = strtol(n_str, &n_endptr, 10); if (n_endptr == n_str || *n_endptr != '\0') { exit(EXIT_FAILURE); } char* m_endptr; char* m_str = bnm[2]; int m = strtol(m_str, &m_endptr, 10); if (m_endptr == m_str || *m_endptr != '\0') { exit(EXIT_FAILURE); } char** keyboards_temp = split_string(readline()); int* keyboards = malloc(n * sizeof(int)); for (int keyboards_itr = 0; keyboards_itr < n; keyboards_itr++) { char* keyboards_item_endptr; char* keyboards_item_str = *(keyboards_temp + keyboards_itr); int keyboards_item = strtol(keyboards_item_str, &keyboards_item_endptr, 10); if (keyboards_item_endptr == keyboards_item_str || *keyboards_item_endptr != '\0') { exit(EXIT_FAILURE); } *(keyboards + keyboards_itr) = keyboards_item; } int keyboards_count = n; char** drives_temp = split_string(readline()); int* drives = malloc(m * sizeof(int)); for (int drives_itr = 0; drives_itr < m; drives_itr++) { char* drives_item_endptr; char* drives_item_str = *(drives_temp + drives_itr); int drives_item = strtol(drives_item_str, &drives_item_endptr, 10); if (drives_item_endptr == drives_item_str || *drives_item_endptr != '\0') { exit(EXIT_FAILURE); } *(drives + drives_itr) = drives_item; } int drives_count = m; /* * The maximum amount of money she can spend on a keyboard and USB drive, or -1 if she can't purchase both items */ int moneySpent = getMoneySpent(keyboards_count, keyboards, drives_count, drives, b); fprintf(fptr, "%d\n", moneySpent); 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; } 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; } |
No comments:
Post a Comment