mirror of
https://github.com/Oxbian/CodingGame.git
synced 2025-07-06 20:05:27 +02:00
update codingame challenges
Adding python challenges and sorting the challenges
This commit is contained in:
66
C/Easy_Challenges/ascii_art.c
Executable file
66
C/Easy_Challenges/ascii_art.c
Executable file
@ -0,0 +1,66 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
int L;
|
||||
scanf("%d", &L);
|
||||
int H;
|
||||
scanf("%d", &H); fgetc(stdin);
|
||||
char tab[L][1025];
|
||||
char T[257];
|
||||
scanf("%[^\n]", T); fgetc(stdin);
|
||||
for (int i = 0; i < H; i++) {
|
||||
char ROW[1025];
|
||||
scanf("%[^\n]", ROW); fgetc(stdin);
|
||||
for (int j=0; j<1025; j++)
|
||||
tab[i][j] = ROW[j];
|
||||
}
|
||||
|
||||
int size=0;
|
||||
//On compte la taille du mot
|
||||
for (int i =0; i < 257; i++)
|
||||
{
|
||||
if (T[i] != '\0')
|
||||
{
|
||||
size++;
|
||||
//On convertit notre mot
|
||||
if (T[i] > 96 && T[i] < 123)
|
||||
T[i] -= 32;
|
||||
}
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
||||
//Affichage du mot
|
||||
for (int i = 0; i < H; i++) //On répète ça le nombre de lignes
|
||||
{
|
||||
for (int j = 0; j< size; j++) //Répétition du nombre de lettre pour afficher la ligne i de chaque lettre
|
||||
{
|
||||
int id = T[j] - 65;
|
||||
//fprintf(stderr,"T = %d Id= %d\n",T[j],id);
|
||||
if ((id >= 0 && id < 26))
|
||||
{
|
||||
for (int k =(id*L); k<((id+1)*(L)); k++)
|
||||
{
|
||||
printf("%c",tab[i][k]);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int k =(26*L); k<(27*(L)); k++)
|
||||
{
|
||||
printf("%c",tab[i][k]);
|
||||
}
|
||||
}
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
// Write an answer using printf(). DON'T FORGET THE TRAILING \n
|
||||
// To debug: fprintf(stderr, "Debug messages...\n");
|
||||
|
||||
return 0;
|
||||
}
|
50
C/Easy_Challenges/chuck_norris.c
Executable file
50
C/Easy_Challenges/chuck_norris.c
Executable file
@ -0,0 +1,50 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
//Convert char into binary ascii
|
||||
void charToBin(char c, char *buffer)
|
||||
{
|
||||
for (int i = 6; i >=0; i--)
|
||||
{
|
||||
buffer[i] = '0' + c%2;
|
||||
c/=2;
|
||||
}
|
||||
buffer[7] = '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
char MESSAGE[101], binary[8], binMessage[707], prev;
|
||||
int i = 0, j = 0, count = 0;
|
||||
scanf("%[^\n]", MESSAGE);
|
||||
binMessage[0] = '\0'; //Reset the string
|
||||
|
||||
for (int i = 0; i < strlen(MESSAGE); i++)
|
||||
{
|
||||
charToBin(MESSAGE[i], binary);
|
||||
strncat(binMessage,binary,7); //Adding the binary of each char into the binMessage string
|
||||
}
|
||||
|
||||
for (i =0; binMessage[i] != '\0'; i++)
|
||||
{
|
||||
if (binMessage[i] != prev)
|
||||
{
|
||||
for (j=0; j<count; j++)
|
||||
printf("0");
|
||||
|
||||
if (count != 0)
|
||||
printf(" ");
|
||||
|
||||
printf((binMessage[i] == '0') ? "00 " : "0 ");
|
||||
count =1;
|
||||
}
|
||||
else
|
||||
count++;
|
||||
|
||||
prev = binMessage[i];
|
||||
}
|
||||
for (int j = 0 ; j < count ; j++) //Showing the rest of the binMessage string
|
||||
printf("0");
|
||||
return 0;
|
||||
}
|
BIN
C/Easy_Challenges/defibrillators
Executable file
BIN
C/Easy_Challenges/defibrillators
Executable file
Binary file not shown.
79
C/Easy_Challenges/defibrillators.c
Normal file
79
C/Easy_Challenges/defibrillators.c
Normal file
@ -0,0 +1,79 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
#include <ctype.h>
|
||||
|
||||
double distance (double latA, double longA, double latB, double longB)
|
||||
{
|
||||
fprintf(stderr, "Affichage des valeurs de latA %lf et longA %lf, latB %lf et longB %lf\n",latA, longA, latB, longB);
|
||||
return (sqrt(pow((longB-longA)*cos((latA + latB)/2),2)+pow((latB - latA),2)) * 6371);
|
||||
}
|
||||
|
||||
char *floatFrToUs(const char *textToTransform)
|
||||
{
|
||||
char *token[3] = {NULL};
|
||||
int counter = 0;
|
||||
char *next_token = strtok(textToTransform, ",");
|
||||
while(next_token != NULL )
|
||||
{
|
||||
size_t token_length = strlen(next_token);
|
||||
token[counter] = calloc(token_length, sizeof(char));
|
||||
memcpy(token[counter], next_token, token_length);
|
||||
next_token = strtok(NULL, ",");
|
||||
++counter;
|
||||
}
|
||||
char *result = malloc(strlen(token[0]) + strlen(token[1]) + strlen(".") + 1);
|
||||
memcpy(result, token[0], strlen(token[0]));
|
||||
memcpy(result + strlen(token[0]), ".", strlen(".") + 1);
|
||||
memcpy(result + strlen(token[0]) + strlen("."), token[1], strlen(token[1]) + 1);
|
||||
return result;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
char LON[51], LAT[51], result[257];
|
||||
scanf("%s%s", LON, LAT);
|
||||
fprintf(stderr,"Debug LON: %s, LAT: %s\n",LON,LAT);
|
||||
double lon = 0, lat = 0;
|
||||
sscanf(floatFrToUs(LON), "%lf", &lon);
|
||||
sscanf(floatFrToUs(LAT), "%lf", &lat);
|
||||
fprintf(stderr,"Debug LON: %s, LAT: %s\n",LON,LAT);
|
||||
|
||||
int N, counter = 0;
|
||||
float min = 99999999;
|
||||
scanf("%d", &N); fgetc(stdin);
|
||||
for (int i = 0; i < N; i++) {
|
||||
char DEFIB[257], *token[8] = {NULL};
|
||||
scanf("%[^\n]", DEFIB); fgetc(stdin);
|
||||
char *next_token = strtok(DEFIB, ";");
|
||||
while(next_token != NULL )
|
||||
{
|
||||
size_t token_length = strlen(next_token);
|
||||
token[counter] = calloc(token_length, sizeof(char));
|
||||
memcpy(token[counter], next_token, token_length);
|
||||
next_token = strtok(NULL, ";");
|
||||
++counter;
|
||||
}
|
||||
for (int j =0; j<counter; j++)
|
||||
fprintf(stderr,"%d: %s\n",i,token[j]);
|
||||
|
||||
double dist = 0, entryLon = 0, entryLat = 0;
|
||||
sscanf(floatFrToUs(token[counter-2]), "%lf", &entryLon);
|
||||
sscanf(floatFrToUs(token[counter-1]), "%lf", &entryLat);
|
||||
dist = distance(lat, lon, entryLat, entryLon);
|
||||
fprintf(stderr, "Affichage des valeurs de lon %.4lf et lat %.4lf\n",entryLon, entryLat);
|
||||
fprintf(stderr,"Valeur distance: %f\n",dist);
|
||||
if (dist < min)
|
||||
{
|
||||
fprintf(stderr,"Valeur copier car dist vaut %lf\n",dist);
|
||||
min = dist;
|
||||
strcpy(result,token[1]);
|
||||
}
|
||||
counter = 0;
|
||||
}
|
||||
|
||||
printf("%s\n",result);
|
||||
|
||||
return 0;
|
||||
}
|
49
C/Easy_Challenges/mars_landers_1.c
Executable file
49
C/Easy_Challenges/mars_landers_1.c
Executable file
@ -0,0 +1,49 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
// the number of points used to draw the surface of Mars.
|
||||
int surface_n;
|
||||
scanf("%d", &surface_n);
|
||||
for (int i = 0; i < surface_n; i++) {
|
||||
// X coordinate of a surface point. (0 to 6999)
|
||||
int land_x;
|
||||
// Y coordinate of a surface point. By linking all the points together in a sequential fashion, you form the surface of Mars.
|
||||
int land_y;
|
||||
scanf("%d%d", &land_x, &land_y);
|
||||
}
|
||||
|
||||
// game loop
|
||||
while (1) {
|
||||
int X;
|
||||
int Y;
|
||||
// the horizontal speed (in m/s), can be negative.
|
||||
int h_speed;
|
||||
// the vertical speed (in m/s), can be negative.
|
||||
int v_speed;
|
||||
// the quantity of remaining fuel in liters.
|
||||
int fuel;
|
||||
// the rotation angle in degrees (-90 to 90).
|
||||
int rotate;
|
||||
// the thrust power (0 to 4).
|
||||
int power;
|
||||
scanf("%d%d%d%d%d%d%d", &X, &Y, &h_speed, &v_speed, &fuel, &rotate, &power);
|
||||
|
||||
// Write an action using printf(). DON'T FORGET THE TRAILING \n
|
||||
// To debug: fprintf(stderr, "Debug messages...\n");
|
||||
|
||||
int final_power = 0;
|
||||
// 2 integers: rotate power. rotate is the desired rotation angle (should be 0 for level 1), power is the desired thrust power (0 to 4).
|
||||
if (Y > 1500)
|
||||
final_power = 3;
|
||||
if (Y < 1500 && v_speed < -35)
|
||||
final_power = 4;
|
||||
|
||||
printf("0 %d\n",final_power);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
49
C/Easy_Challenges/power_of_thor_1.c
Executable file
49
C/Easy_Challenges/power_of_thor_1.c
Executable file
@ -0,0 +1,49 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
// the X position of the light of power
|
||||
int light_x;
|
||||
// the Y position of the light of power
|
||||
int light_y;
|
||||
// Thor's starting X position
|
||||
int initial_tx;
|
||||
// Thor's starting Y position
|
||||
int initial_ty;
|
||||
|
||||
scanf("%d%d%d%d", &light_x, &light_y, &initial_tx, &initial_ty);
|
||||
|
||||
// game loop
|
||||
while (1)
|
||||
{
|
||||
// The remaining amount of turns Thor can move. Do not remove this line.
|
||||
int remaining_turns;
|
||||
scanf("%d", &remaining_turns);
|
||||
|
||||
char *dirX = "";
|
||||
char *dirY = "";
|
||||
|
||||
if ((initial_tx >= 0 && initial_tx < 40) && (initial_ty >= 0 && initial_ty < 18))
|
||||
{
|
||||
if (initial_tx > light_x)
|
||||
dirX = "W", initial_tx--;
|
||||
else if (initial_tx < light_x)
|
||||
dirX = "E", initial_tx++;
|
||||
else
|
||||
dirX = "";
|
||||
|
||||
if (initial_ty > light_y)
|
||||
dirY = "N", initial_ty--;
|
||||
else if (initial_ty < light_y)
|
||||
dirY = "S", initial_ty++;
|
||||
else
|
||||
dirY = "";
|
||||
}
|
||||
printf("%s%s\n", dirY, dirX);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
40
C/Easy_Challenges/temperatures.c
Executable file
40
C/Easy_Challenges/temperatures.c
Executable file
@ -0,0 +1,40 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#define TAILLE_MAX 10000
|
||||
|
||||
int main()
|
||||
{
|
||||
// the number of temperatures to analyse
|
||||
int n,min=TAILLE_MAX;
|
||||
scanf("%d", &n);
|
||||
for (int i = 0; i < n; i++) {
|
||||
// a temperature expressed as an integer ranging from -273 to 5526
|
||||
int t;
|
||||
scanf("%d", &t);
|
||||
|
||||
if (abs((0-t)) < abs(0-min))
|
||||
{
|
||||
min = t;
|
||||
}
|
||||
else if (min == t)
|
||||
{
|
||||
min = t;
|
||||
}
|
||||
else if (abs(t) == abs(min))
|
||||
{
|
||||
min = abs(min);
|
||||
}
|
||||
}
|
||||
|
||||
// Write an answer using printf(). DON'T FORGET THE TRAILING \n
|
||||
// To debug: fprintf(stderr, "Debug messages...\n");
|
||||
|
||||
if (min == TAILLE_MAX)
|
||||
printf("0");
|
||||
else
|
||||
printf("%d",min);
|
||||
return 0;
|
||||
}
|
27
C/Easy_Challenges/the_descent.c
Executable file
27
C/Easy_Challenges/the_descent.c
Executable file
@ -0,0 +1,27 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
// game loop
|
||||
while (1) {
|
||||
int mountain_max = 0, mountain_Id = 0;
|
||||
for (int i = 0; i < 8; i++) {
|
||||
// represents the height of one mountain.
|
||||
int mountain_h;
|
||||
scanf("%d", &mountain_h);
|
||||
if (mountain_h > mountain_max)
|
||||
{
|
||||
mountain_max = mountain_h;
|
||||
mountain_Id = i;
|
||||
}
|
||||
}
|
||||
printf("%d\n",mountain_Id);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
42
C/Medium_Challenges/shadow_of_knight_1.c
Executable file
42
C/Medium_Challenges/shadow_of_knight_1.c
Executable file
@ -0,0 +1,42 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int W,H;
|
||||
scanf("%d%d", &W, &H);
|
||||
int N;
|
||||
scanf("%d", &N);
|
||||
int X0,Y0;
|
||||
scanf("%d%d", &X0, &Y0);
|
||||
|
||||
int LX = 0, LY = 0, HX = W - 1, HY = H - 1;
|
||||
// game loop
|
||||
while (1) {
|
||||
// the direction of the bombs from batman's current location (U, UR, R, DR, D, DL, L or UL)
|
||||
char bomb_dir[4];
|
||||
scanf("%s", bomb_dir);
|
||||
fprintf(stderr,"La bombe est dans la direction: %s",bomb_dir);
|
||||
|
||||
if (bomb_dir[0] == 'L' || bomb_dir[1] == 'L')
|
||||
HX = X0 - 1;
|
||||
else if (bomb_dir[0] == 'R' || bomb_dir[1] == 'R')
|
||||
LX = X0 + 1;
|
||||
|
||||
if (bomb_dir[0] == 'U')
|
||||
HY = Y0 - 1;
|
||||
else
|
||||
LY = Y0 + 1;
|
||||
|
||||
X0 = (HX + LX) /2;
|
||||
Y0 = (HY + LY) /2;
|
||||
|
||||
|
||||
// the location of the next window Batman should jump to.
|
||||
printf("%d %d\n",X0,Y0);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
20
C/Medium_Challenges/stock_exchange_losses.c
Executable file
20
C/Medium_Challenges/stock_exchange_losses.c
Executable file
@ -0,0 +1,20 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int n, v, max = 0, max_lost =0, curr_lost = 0;
|
||||
scanf("%d", &n);
|
||||
for (int i = 0; i < n; i++) {
|
||||
scanf("%d", &v);
|
||||
|
||||
curr_lost = v-max;
|
||||
if (curr_lost < max_lost) max_lost = curr_lost;
|
||||
if (v>max) max = v;
|
||||
}
|
||||
printf("%d\n",max_lost);
|
||||
|
||||
return 0;
|
||||
}
|
64
C/Medium_Challenges/there_is_no_spoon_1.c
Executable file
64
C/Medium_Challenges/there_is_no_spoon_1.c
Executable file
@ -0,0 +1,64 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
// the number of cells on the X axis
|
||||
int width;
|
||||
scanf("%d", &width);
|
||||
|
||||
// the number of cells on the Y axis
|
||||
int height;
|
||||
scanf("%d", &height); fgetc(stdin);
|
||||
|
||||
// New grid which will contains all the stuff
|
||||
char grid[30][30];
|
||||
|
||||
//Getting items from input and adding them into the grid
|
||||
for (int i = 0; i < height; i++) {
|
||||
char line[32];
|
||||
scanf("%[^\n]", line); fgetc(stdin);
|
||||
|
||||
for(int j =0;j < 32; j++){
|
||||
grid[i][j] = line[j];
|
||||
}
|
||||
}
|
||||
|
||||
for (int y = 0; y < height; y++)
|
||||
{
|
||||
for (int x = 0; x < width; x++)
|
||||
{
|
||||
//If we found a node, we print it's coordonnate and after check his neighbours
|
||||
if (grid[y][x] == '0')
|
||||
{
|
||||
printf("%d %d ",x,y);
|
||||
short x2, y2, ansX = -1, ansY = -1;
|
||||
for (x2 = x+1; x2 < width; ++x2) //Checking if we found another node on the same line
|
||||
{
|
||||
if (grid[y][x2] == '0') //If node found we add it as neightbours and stop searching
|
||||
{
|
||||
ansX = x2;
|
||||
ansY = y;
|
||||
break;
|
||||
}
|
||||
}
|
||||
printf("%d %d ",ansX, ansY);
|
||||
ansX = -1; ansY = -1;
|
||||
for (y2 = y+1; y2 < height; ++y2) //Checking if we found another node on the same column
|
||||
{
|
||||
if (grid[y2][x] == '0') //If we found we add it as neightbours and stop searching
|
||||
{
|
||||
ansX = x;
|
||||
ansY = y2;
|
||||
break;
|
||||
}
|
||||
}
|
||||
printf("%d %d\n",ansX, ansY);
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
Reference in New Issue
Block a user