Skip to content

Instantly share code, notes, and snippets.

View frectonz's full-sized avatar
❄️

Fraol Lemecha frectonz

❄️
View GitHub Profile
def island_perimeter(grid):
perimeter = 0
for i in range(len(grid)):
row = grid[i]
for j in range(len(row)):
cell = grid[i][j]
if cell != 1:
continue
@frectonz
frectonz / is_isomorphic.rs
Last active October 18, 2023 10:04
From cassidoo's October 18, 2023 newsletter
fn is_isomorphic(s1: &str, s2: &str) -> bool {
let mask1 = str_mask(s1);
let mask2 = str_mask(s2);
mask1 == mask2
}
fn str_mask(s: &str) -> Vec<usize> {
s.chars()
.fold((None, Vec::<usize>::new()), |(last_c, mut acc), curr_c| {
@frectonz
frectonz / faultyKeeb.ts
Created August 14, 2023 08:43
From cassidoo's August 14, 2023 newsletter
function faultyKeeb(str: string): string {
const out = [];
const vowels = ["a", "e", "i", "o", "u"];
for (const ch of str) {
if (vowels.includes(ch)) {
out.reverse();
} else {
out.push(ch);
}
}
export class LaunchControl {
private serverUrl: string;
private lastSessionState: "start" | "end" | null;
private trackingId: string;
constructor(serverUrl: string, trackingId: string) {
this.serverUrl = serverUrl;
this.lastSessionState = null;
this.trackingId = trackingId;
}
@frectonz
frectonz / pies.ml
Last active June 5, 2023 11:31
From cassidoo's Jun 5, 2023 Newsletter
let sum = List.fold_left (fun acc x -> acc + x) 0
type result = { pies : int; uneaten_pieces : int }
let pies pieces piece_per_pie =
let total_pieces = sum pieces in
let pies =
if total_pieces mod piece_per_pie == 0 then total_pieces / piece_per_pie
else (total_pieces / piece_per_pie) + 1
in
@frectonz
frectonz / oneRow.ml
Last active May 29, 2023 10:12
From cassidoo's May 29, 2023 Newsletter
module KeyToRowMap = Map.Make (Char)
let qwertyKeyToRowMap =
let firstRow = ['q'; 'w'; 'e'; 'r'; 't'; 'y'; 'u'; 'i'; 'o'; 'p'] in
let secondRow = ['a'; 's'; 'd'; 'f'; 'g'; 'h'; 'j'; 'k'; 'l'] in
let thirdRow = ['z'; 'x'; 'c'; 'v'; 'b'; 'n'; 'm'] in
KeyToRowMap.empty
|> (fun m -> List.fold_left (fun acc key -> KeyToRowMap.add key 1 acc) m firstRow)
|> (fun m -> List.fold_left (fun acc key -> KeyToRowMap.add key 2 acc) m secondRow)
|> (fun m -> List.fold_left (fun acc key -> KeyToRowMap.add key 3 acc) m thirdRow)
@frectonz
frectonz / oddSquareSum.ml
Last active May 8, 2023 09:55
From cassido's May 05, 2023 Newsletter
let int_sqrt x = x |> float_of_int |> sqrt |> int_of_float
(**
* [oddSquareNumber n]
* returns the sum of all odd square numbers less than [n]
*
* it does this by going through all the odd numbers from [1] to [sqrt(n)]
* and adding the square of each number to the sum if the square is less
* than [n].
*)
@frectonz
frectonz / maxPointsOnLine.ts
Last active April 17, 2023 09:55
From cassido's April 17, 2023 Newsletter
type Point = [number, number];
const slope = ([x1, y1]: Point, [x2, y2]: Point) => (y2 - y1) / (x2 - x1);
const makeKey = ([x1, y1]: Point, [x2, y2]: Point) =>
`(${x1},${y1}) -> (${x2},${y2})`;
const splitKey = (key: string): [string, string] =>
key.split(" -> ") as [string, string];
function maxPointsOnLine(points: Point[]) {
const slopes = new Map<string, number>();
@frectonz
frectonz / convert_color.rs
Last active March 20, 2023 17:15
From cassido's March 13, 2023 Newsletter
use std::str::FromStr;
#[derive(Debug)]
struct Rgb {
r: u8,
g: u8,
b: u8,
}
impl FromStr for Rgb {
@frectonz
frectonz / fraction_math.rs
Last active March 13, 2023 17:05
From cassido's March 13, 2023 Newsletter
use anyhow::{anyhow, Error, Result};
use std::{fmt::Display, str::FromStr};
#[derive(Debug, PartialEq, Eq)]
struct Fraction {
top: isize,
bottom: isize,
}
impl FromStr for Fraction {