Skip to content

Instantly share code, notes, and snippets.

View marsgpl's full-sized avatar
🔴
Recording

Iurii Belobeev marsgpl

🔴
Recording
View GitHub Profile
// 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>
#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, "");
// 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:
@marsgpl
marsgpl / replace-0-with-1.c
Created October 17, 2024 08:52
Write a Program to Replace all 0’s with 1’s in a Number
#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;
#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;
}
@marsgpl
marsgpl / asyncMapper.ts
Last active December 7, 2023 00:43
asyncMapper.ts
// 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
@marsgpl
marsgpl / findLargestArea.ts
Created December 5, 2023 23:25
findLargestArea.ts
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>
@marsgpl
marsgpl / graph.c
Last active December 5, 2023 06:07
graph.c
#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;
@marsgpl
marsgpl / floodFill.ts
Last active December 5, 2023 02:33
floodFill.ts
function floodFill(
image: number[][],
x: number,
y: number,
newColor: number,
): void {
const minY = 0
const maxY = image.length - 1
if (maxY < 0) { return }
@marsgpl
marsgpl / promiseAll.ts
Last active April 28, 2024 22:48
promiseAll.ts
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
}