1
1
mirror of https://github.com/theoludwig/programming-challenges.git synced 2025-05-18 12:02:53 +02:00

feat(cli): add --all option to run test command

This commit is contained in:
Divlo
2021-10-13 21:43:45 +02:00
parent aeff95e691
commit 0a27b9e1a1
47 changed files with 1657 additions and 1773 deletions

View File

@ -9,7 +9,7 @@ void character_append(char* string, char character) {
string[length + 1] = '\0';
}
const char character_to_upper(const char character) {
char character_to_upper(char character) {
char ascii_a = 'a';
char ascii_A = 'A';
char ascii_z = 'z';

View File

@ -1,5 +1,5 @@
#ifndef CHARACTER_H
#define CHARACTER_H
#ifndef __CHARACTER__
#define __CHARACTER__
/**
* @brief Append a character to a string, assuming string points to an array
@ -14,8 +14,8 @@ void character_append(char* string, char character);
* @brief Converts the character to uppercase.
*
* @param character
* @return const char
* @return char
*/
const char character_to_upper(const char character);
char character_to_upper(char character);
#endif

View File

@ -1,5 +1,5 @@
#ifndef INPUT_H
#define INPUT_H
#ifndef __INPUT__
#define __INPUT__
/**
* @brief Read a line from stdin.

View File

@ -1,5 +1,5 @@
#ifndef STRING_H
#define STRING_H
#ifndef __STRING__
#define __STRING__
/**
* @brief Converts all the alphabetic characters in a string to uppercase.

View File

@ -29,7 +29,7 @@ char* string_caesar_cipher(char* string, int shift) {
for (size_t index = 0; index < string_length; index++) {
char letter = string[index];
if (letter != ' ') {
for (char index_alphabet = 0; index_alphabet < ALPHABET_LENGTH; index_alphabet++) {
for (int index_alphabet = 0; index_alphabet < ALPHABET_LENGTH; index_alphabet++) {
char current_letter = 'A' + index_alphabet;
if (string[index] == current_letter) {
letter = shifted_alphabet[index_alphabet];

View File

@ -9,7 +9,7 @@ void character_append(char* string, char character) {
string[length + 1] = '\0';
}
const char character_to_upper(const char character) {
char character_to_upper(char character) {
char ascii_a = 'a';
char ascii_A = 'A';
char ascii_z = 'z';

View File

@ -14,8 +14,8 @@ void character_append(char* string, char character);
* @brief Converts the character to uppercase.
*
* @param character
* @return const char
* @return char
*/
const char character_to_upper(const char character);
char character_to_upper(char character);
#endif

View File

@ -42,8 +42,8 @@ int main() {
if (is_last_number) {
break;
}
if (numbers[index] + couple_index == numbers[index + couple_index]) {
consecutive[consecutive_length] = numbers[index] + couple_index;
if ((int)(numbers[index] + couple_index) == numbers[index + couple_index]) {
consecutive[consecutive_length] = (int)(numbers[index] + couple_index);
consecutive_length++;
}
bool is_consecutive = consecutive_length == couple_length;

View File

@ -0,0 +1,10 @@
#include "character.h"
#include <stdlib.h>
#include <string.h>
void character_append(char* string, char character) {
size_t length = strlen(string);
string[length] = character;
string[length + 1] = '\0';
}

View File

@ -0,0 +1,13 @@
#ifndef __CHARACTER__
#define __CHARACTER__
/**
* @brief Append a character to a string, assuming string points to an array
* with enough space.
*
* @param string
* @param character
*/
void character_append(char* string, char character);
#endif

View File

@ -0,0 +1,19 @@
#include "input.h"
#include <stdio.h>
#include <stdlib.h>
#include "character.h"
char* input() {
char character;
size_t length = 1;
char* string = malloc(length * sizeof(char));
*string = '\0';
while ((character = getchar()) != '\n' && character != EOF) {
length++;
string = realloc(string, length * sizeof(char));
character_append(string, character);
}
return string;
}

View File

@ -0,0 +1,11 @@
#ifndef __INPUT__
#define __INPUT__
/**
* @brief Read a line from stdin.
*
* @return char*
*/
char* input();
#endif

View File

@ -1,10 +1,10 @@
#include <stdio.h>
#include <stdlib.h>
#include "input.h"
int main() {
char input[1024];
while (scanf("%s", &input) != EOF) {
printf("Hello, %s!", input);
}
char *string = input();
printf("Hello, %s!\n", string);
return EXIT_SUCCESS;
}

View File

@ -9,7 +9,7 @@ void character_append(char* string, char character) {
string[length + 1] = '\0';
}
const char character_to_upper(const char character) {
char character_to_upper(char character) {
char ascii_a = 'a';
char ascii_A = 'A';
char ascii_z = 'z';

View File

@ -1,5 +1,5 @@
#ifndef CHARACTER_H
#define CHARACTER_H
#ifndef __CHARACTER__
#define __CHARACTER__
/**
* @brief Append a character to a string, assuming string points to an array
@ -14,8 +14,8 @@ void character_append(char* string, char character);
* @brief Converts the character to uppercase.
*
* @param character
* @return const char
* @return char
*/
const char character_to_upper(const char character);
char character_to_upper(char character);
#endif

View File

@ -1,6 +1,7 @@
#include "input.h"
#include <stdio.h>
#include <stdlib.h>
#include "character.h"

View File

@ -1,5 +1,5 @@
#ifndef INPUT_H
#define INPUT_H
#ifndef __INPUT__
#define __INPUT__
/**
* @brief Read a line from stdin.

View File

@ -18,7 +18,7 @@ char* string_to_upper(const char* string) {
char* string_reverse(const char* string) {
size_t string_length = strlen(string);
char* result = malloc(sizeof(char) * (string_length + 1));
for (size_t index = string_length - 1; index != -1; index--) {
for (int index = string_length - 1; index != -1; index--) {
character_append(result, string[index]);
}
return result;

View File

@ -1,5 +1,5 @@
#ifndef STRING_H
#define STRING_H
#ifndef __STRING__
#define __STRING__
#include <stdbool.h>

View File

@ -10,7 +10,7 @@ void character_append(char* string, char character) {
string[length + 1] = '\0';
}
void character_print(char* character, int number_of_times) {
void character_print(char* character, size_t number_of_times) {
for (size_t iteration = 0; iteration < number_of_times; iteration++) {
printf("%s", character);
}

View File

@ -1,5 +1,7 @@
#ifndef CHARACTER_H
#define CHARACTER_H
#ifndef __CHARACTER__
#define __CHARACTER__
#include <stdlib.h>
/**
* @brief Append a character to a string, assuming string points to an array
@ -10,6 +12,6 @@
*/
void character_append(char* string, char character);
void character_print(char* character, int number_of_times);
void character_print(char* character, size_t number_of_times);
#endif

View File

@ -1,5 +1,5 @@
#ifndef INPUT_H
#define INPUT_H
#ifndef __INPUT__
#define __INPUT__
/**
* @brief Read a line from stdin.

View File

@ -12,10 +12,10 @@ int main() {
int step = strcmp(type, "normal") == 0 ? 1 : height;
while ((strcmp(type, "normal") == 0 && step <= height) || (strcmp(type, "reverse") == 0 && step != 0)) {
int numberOfStars = (step * 2) - 1;
int totalNumberOfLocations = (height * 2) - 1;
int totalNumberOfSpaces = totalNumberOfLocations - numberOfStars;
int numberOfSpacesOnEachSide = totalNumberOfSpaces / 2;
size_t numberOfStars = (step * 2) - 1;
size_t totalNumberOfLocations = (height * 2) - 1;
size_t totalNumberOfSpaces = totalNumberOfLocations - numberOfStars;
size_t numberOfSpacesOnEachSide = totalNumberOfSpaces / 2;
character_print(" ", numberOfSpacesOnEachSide);
character_print("*", numberOfStars);
character_print(" ", numberOfSpacesOnEachSide);

View File

@ -1,5 +1,5 @@
#ifndef BUBBLE_SORT_H
#define BUBBLE_SORT_H
#ifndef __BUBBLE_SORT__
#define __BUBBLE_SORT__
void swap_numbers(int *value_1, int *value_2);

View File

@ -5,7 +5,7 @@
int main() {
int current_number;
int length = scanf("%d", &current_number);
scanf("%d", &current_number);
int *numbers = malloc(current_number * sizeof(int));
int index_input = 0;
while (scanf("%d", &current_number) != EOF) {

View File

@ -1,5 +1,5 @@
#ifndef INSERTION_SORT_H
#define INSERTION_SORT_H
#ifndef __INSERTION_SORT__
#define __INSERTION_SORT__
void insertion_sort(int *numbers, const int length);

View File

@ -5,7 +5,7 @@
int main() {
int current_number;
int length = scanf("%d", &current_number);
scanf("%d", &current_number);
int *numbers = malloc(current_number * sizeof(int));
int index_input = 0;
while (scanf("%d", &current_number) != EOF) {

View File

@ -1,5 +1,5 @@
#ifndef MERGE_SORT_H
#define MERGE_SORT_H
#ifndef __MERGE_SORT__
#define __MERGE_SORT__
void merge(int numbers[], int left_index, int middle_index, int right_index);

View File

@ -5,7 +5,7 @@
int main() {
int current_number;
int length = scanf("%d", &current_number);
scanf("%d", &current_number);
int *numbers = malloc(current_number * sizeof(int));
int index_input = 0;
while (scanf("%d", &current_number) != EOF) {

View File

@ -5,15 +5,12 @@ char* triangle_type(int triangle_sides[3]) {
if ((triangle_sides[0] + triangle_sides[1] < triangle_sides[2]) || (triangle_sides[2] + triangle_sides[0] < triangle_sides[1]) || (triangle_sides[2] + triangle_sides[1] < triangle_sides[0])) {
return "impossible";
}
if (triangle_sides[0] == triangle_sides[1] && triangle_sides[1] == triangle_sides[2]) {
return "equilateral";
}
if (triangle_sides[0] == triangle_sides[1] || triangle_sides[1] == triangle_sides[2] || triangle_sides[2] == triangle_sides[0]) {
return "isosceles";
}
return "scalene";
}