Skip to content

Instantly share code, notes, and snippets.

View marsgpl's full-sized avatar
🔴
Recording

Iurii Belobeev marsgpl

🔴
Recording
View GitHub Profile
@marsgpl
marsgpl / lru.ts
Created June 19, 2025 17:26
LRU cache
// problems: 1ms resolution
export type LruCacheKey = string | Symbol
export type LruCacheValue = unknown
export interface ILruCache {
set: (this: ILruCache, key: LruCacheKey, value: LruCacheValue) => ILruCache
get: (this: ILruCache, key: LruCacheKey) => LruCacheValue | undefined
delete: (this: ILruCache, key: LruCacheKey) => LruCache
}
@marsgpl
marsgpl / prime.c
Created May 29, 2025 07:02
check whether a number is a prime or not
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
static void error(const char *message, ...) {
va_list args;
fprintf(stderr, "\e[31mError:\e[0m ");
va_start(args, message);
@marsgpl
marsgpl / replace.c
Created May 29, 2025 06:59
Write a Program to Replace all 0’s with 1’s in a Number.
#include <stdio.h>
#include <math.h>
int main() {
const int number = -102301;
printf("before: %d\n", number);
int input = number < 0 ? -number : number;
int output = 0;
@marsgpl
marsgpl / swap.c
Created May 29, 2025 06:58
Write a Program in C to Swap the values of two variables without using any extra variable.
#include <stdio.h>
int main() {
int a = 123123123;
int b = 456456456;
printf("before swap: a = %d b = %d\n", a, b);
// other swap options:
// 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