Skip to content

Instantly share code, notes, and snippets.

@lerox
Last active December 8, 2018 02:33
Show Gist options
  • Save lerox/e5d7e7a09abefdfb2bce0983f8da07f3 to your computer and use it in GitHub Desktop.
Save lerox/e5d7e7a09abefdfb2bce0983f8da07f3 to your computer and use it in GitHub Desktop.
c-lang-hacker-rank.txt
# https://www.hackerrank.com/challenges/hello-world-c/problem
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main()
{
char s[100];
scanf("%[^\n]%*c", &s);
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
printf("Hello, World!\n");
printf(s);
return 0;
}
# https://www.hackerrank.com/challenges/conditional-statements-in-c/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();
int main()
{
char* n_endptr;
char* n_str = readline();
int n = strtol(n_str, &n_endptr, 10);
if (n_endptr == n_str || *n_endptr != '\0') { exit(EXIT_FAILURE); }
// Write Your Code Here
if (n == 1) {
printf("one");
} else if (n == 2) {
printf("two");
} else if (n == 3) {
printf("three");
} else if (n == 4) {
printf("four");
} else if (n == 5) {
printf("five");
} else if (n == 6) {
printf("six");
} else if (n == 7) {
printf("seven");
} else if (n == 8) {
printf("eight");
} else if (n == 9) {
printf("nine");
} else {
printf("Greater than 9");
}
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;
}
# https://www.hackerrank.com/challenges/playing-with-characters/problem
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main()
{
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
char c;
scanf("%c", &c);
printf("%c", c);
scanf("\n");
printf("\n");
char s[500];
scanf("%[^\n]%*c", s);
printf(s);
scanf("\n");
printf("\n");
char sent[500];
scanf("%[^\n]%*c", sent);
printf(sent);
return 0;
}
# https://www.hackerrank.com/challenges/for-loop-in-c/problem
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main()
{
int a, b;
scanf("%d\n%d", &a, &b);
for (int i = a; i <= b; i++) {
printProperly(i);
}
return 0;
}
void printProperly(int n) {
if (n == 1) {
printf("one\n");
} else if (n == 2) {
printf("two\n");
} else if (n == 3) {
printf("three\n");
} else if (n == 4) {
printf("four\n");
} else if (n == 5) {
printf("five\n");
} else if (n == 6) {
printf("six\n");
} else if (n == 7) {
printf("seven\n");
} else if (n == 8) {
printf("eight\n");
} else if (n == 9) {
printf("nine\n");
} else if (n % 2 == 0) {
printf("even\n");
} else {
printf("odd\n");
}
}
# https://www.hackerrank.com/challenges/sum-of-digits-of-a-five-digit-number/problem
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int getIntLen(int number);
int main() {
int n;
scanf("%d", &n);
//Complete the code to calculate the sum of the five digits on n.
int inputLen = getIntLen(n);
int counter = 0;
int divisor = 10;
for (int i = 0; i < inputLen; i++) {
int number = ((n % divisor) - counter) / (divisor/10);
printf("%d\n", number);
counter += number;
divisor *= 10;
}
printf("%d\n", counter);
return 0;
}
int getIntLen(int number) {
int count = 0;
while (number > 0) {
number /= 10;
count++;
}
return count;
}
# https://www.hackerrank.com/challenges/bitwise-operators-in-c/problem
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
//Complete the following function.
void calculate_the_maximum(int n, int k) {
//Write your code here.
int maxAndLessThenK = 0;
int maxOrLessThenK = 0;
int maxXorLessThenK = 0;
for (int i = 1; i < n; i++) {
for (int j = i + 1; j <= n; j++) {
int aAndB = i & j;
int aOrB = i | j;
int aXorB = i ^ j;
if (aAndB < k && aAndB > maxAndLessThenK) {
maxAndLessThenK = aAndB;
}
if (aOrB < k && aOrB > maxOrLessThenK) {
maxOrLessThenK = aOrB;
}
if (aXorB < k && aXorB > maxXorLessThenK) {
maxXorLessThenK = aXorB;
}
}
}
printf("%d\n", maxAndLessThenK);
printf("%d\n", maxOrLessThenK);
printf("%d\n", maxXorLessThenK);
}
int main() {
int n, k;
scanf("%d %d", &n, &k);
calculate_the_maximum(n, k);
return 0;
}
# https://www.hackerrank.com/challenges/printing-pattern-2/problem
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main()
{
int n;
scanf("%d", &n);
// Complete the code to print the pattern.
int sizeOfSquare = n * 2 - 1;
// y
// y
// y
// y
// yx x x x x
// 3 3 3 3 3
// 3 2 2 2 3
// 3 2 1 2 3
// 3 2 2 2 3
// 3 3 3 3 3
for (int yAxis = 0; yAxis < sizeOfSquare; yAxis++) {
for (int xAxis = 0; xAxis < sizeOfSquare; xAxis++) {
int valueToPrint = n;
// 1st 2nd
// 3rd 4th
// 1st little square
if (xAxis <= floor(sizeOfSquare/2) && yAxis <= floor(sizeOfSquare/2)) {
if (xAxis > yAxis) {
valueToPrint -= yAxis;
} else {
valueToPrint -= xAxis;
}
}
// 2nd little square
if (xAxis > floor(sizeOfSquare/2) && yAxis <= floor(sizeOfSquare/2)) {
if (yAxis < sizeOfSquare - xAxis) {
valueToPrint -= yAxis;
} else {
valueToPrint -= sizeOfSquare - 1 - xAxis;
}
}
// 3rd little square
if (xAxis <= floor(sizeOfSquare / 2) && yAxis > floor(sizeOfSquare/2)) {
if (xAxis < sizeOfSquare - yAxis) {
valueToPrint -= xAxis;
} else {
valueToPrint -= sizeOfSquare - 1 - yAxis;
}
}
// 4th little square
if (xAxis > floor(sizeOfSquare/2) && yAxis > floor(sizeOfSquare/2)) {
if (xAxis > yAxis) {
valueToPrint -= sizeOfSquare - 1 - xAxis;
} else {
valueToPrint -= sizeOfSquare - 1 - yAxis;
}
}
printf("%d ", valueToPrint);
}
printf("\n");
}
return 0;
}
# https://www.hackerrank.com/challenges/1d-arrays-in-c/problem
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n;
scanf("%d", &n);
int sum = 0;
// An array is a container object that holds a fixed number of values of a single type.
// To create an array in C, we can do int arr[n];. Here, arr, is a variable array which
// holds up to integers. The above array is a static array that has memory allocated at
// compile time. A dynamic array can be created in C, using the malloc function and the
// memory is allocated on the heap at runtime. To create an integer array, of size ,
// int *arr = (int*)malloc(n * sizeof(int)), where points to the base address of the array.
// In this challenge, you have to create an array of size dynamically, input the elements of
// the array, sum them and print the sum of the elements in a new line.
// // static array that has memory allocated at compile time at the stack
// int arr[n];
// allocates memory on runtime on the heap
int *arr = (int*)malloc(n * sizeof(int));
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
for (int j = 0; j < n; j++) {
sum += arr[j];
}
printf("%d", sum);
return 0;
}
# https://www.hackerrank.com/challenges/reverse-array-c/problem
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num, *arr, i;
scanf("%d", &num);
arr = (int*) malloc(num * sizeof(int));
for(i = 0; i < num; i++) {
scanf("%d", arr + i);
}
/* Write the logic to reverse the array. */
for(i = num - 1; i >= 0; i--)
printf("%d ", *(arr + i));
return 0;
}
# https://www.hackerrank.com/challenges/printing-tokens-/forum
# v1
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char *s;
s = malloc(1024 * sizeof(char));
scanf("%[^\n]", s);
s = realloc(s, strlen(s) + 1);
//Write your logic to print the tokens of the sentence here.
int last = 0;
for (int i = 0; i < strlen(s); i++) {
if (s[i] == ' ' || i == strlen(s) - 1) {
char str[strlen(s)];
memset(&str, 0, strlen(s));
int jPointer = last;
for (int j = 0; j <= i - last; j++) {
if (s[jPointer] != ' ') {
str[j] = s[jPointer];
}
jPointer++;
}
last = i + 1;
if (str[0] != 0) {
printf("%s\n", str);
}
}
}
return 0;
}
# v2
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char *s;
s = malloc(1024 * sizeof(char));
scanf("%[^\n]", s);
s = realloc(s, strlen(s) + 1);
//Write your logic to print the tokens of the sentence here.
char *p = s;
int lastWasSpace = 0;
while (*p != '\0') {
if (*p != ' ') {
printf("%c", *p);
lastWasSpace = 0;
} else if (!lastWasSpace) {
printf("\n");
lastWasSpace = 0;
}
p++;
}
return 0;
}
# v3
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char *s;
s = malloc(1024 * sizeof(char));
scanf("%[^\n]", s);
s = realloc(s, strlen(s) + 1);
//Write your logic to print the tokens of the sentence here.
for (char * j = s; *j != '\0'; j++) {
if (*j == ' ') {
*j = '\n';
}
}
printf("%s", s);
return 0;
}
# next one
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment