Add solutions to challenge 4

This commit is contained in:
2024-07-31 13:19:47 +02:00
parent d3423779dc
commit cc1a51c298
4 changed files with 362 additions and 2 deletions

View File

@@ -1,3 +1,6 @@
import * as fs from 'node:fs';
import { Encoding } from 'node:crypto';
// @ts-ignore
export const range = (from: number, to: number) => [...Array(to - from + 1).keys()].map(i => i + from);
@@ -10,3 +13,11 @@ export const englishFreqs = {
117: 0.02758, 118: 0.00978, 119: 0.02360, 120: 0.00150,
121: 0.01974, 122: 0.00074, 32: 0.13000,
};
type FileToBuff = (fName: string, encoding: Encoding, shouldSplit?: boolean) => Buffer[];
export const fileToBuff: FileToBuff = (fName, encoding, shouldSplit = true) => {
const data = fs.readFileSync(fName, 'utf-8');
const dataLines = shouldSplit ? data.split('\n').map(s => s.trim()) : [data.trim()];
return dataLines.map(x => Buffer.from(x, encoding));
};