This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// given a string of digits and a target integer, insert + and * operators | |
// between the digits until the resultant equation results in the target, | |
// printing the result. if no possible equation exists with the given string, | |
// print "No solution found" | |
// Ex: "123456" and 464 prints 12*34+56 | |
#include <stdio.h> | |
#include <string.h> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define STARS "****************************************************" | |
void print_pyramid(int levels_n) { | |
int max_w = levels_n + levels_n - 1; | |
for (int level = 1; level <= levels_n; ++level) { | |
int stars_n = level + level - 1; | |
int pad = (max_w - stars_n) / 2; | |
printf("%*s%.*s%*s\n", pad, "", stars_n, STARS, pad, ""); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// n must have exactly 2 distinct divisors to be prime | |
int is_prime(int n) { | |
if (n < 0) { | |
n = -n; | |
} | |
if (n <= 100) { // first 25 primes (add more?) | |
switch (n) { | |
case 2: case 3: case 5: case 7: case 11: case 13: case 17: | |
case 19: case 23: case 29: case 31: case 37: case 41: case 43: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int main() { | |
int N = 102301; | |
printf("before:\t%d\n", N); | |
for (int power = 1; power <= N; power *= 10) { // 1, 10, 100, ... | |
int digit = N / power % 10; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
typedef int (*fn)(void); | |
void custom_transaction(fn *dos, fn *undos, size_t n) { | |
for (int i = 0; i < n; ++i) { | |
if (!(dos[i])()) { | |
while (--i >= 0) { undos[i](); } | |
return; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// write async generator to return N random strings per call | |
// add parameter to limit total amount of strings returned by generator | |
// write async mapper that accepts async generator as argument and | |
// a callback that is applied for every generator result | |
// mapper should return all generator results as array | |
async function getLinesTotal() { | |
return 8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const grid = [ | |
[3, 1, 1, 1], | |
[2, 1, 0, 1], | |
[1, 1, 0, 2], | |
[2, 0, 1, 0], | |
] | |
interface Area { | |
value: number | |
points: Set<number> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <string.h> | |
typedef struct graph_node { | |
char *name; | |
struct graph_node *parent; | |
struct graph_node **children; | |
size_t children_size; | |
size_t children_n; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function floodFill( | |
image: number[][], | |
x: number, | |
y: number, | |
newColor: number, | |
): void { | |
const minY = 0 | |
const maxY = image.length - 1 | |
if (maxY < 0) { return } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function promiseAll<T = unknown>(promises: Promise<T>[]): Promise<T[]> { | |
const results: T[] = [] | |
for (let i = 0; i < promises.length; ++i) { | |
results.push(await promises[i]) | |
} | |
return results | |
} |
NewerOlder