Skip to content

Instantly share code, notes, and snippets.

@FremyCompany
FremyCompany / readme.md
Last active June 15, 2022 06:26 — forked from DigiTec/readme.md

Spoiler pages for the FindNearbyWords challenge

The goal of the exercise is to find an efficient way to take an existing word and lookup all words in a dictionary that differ from the original by only a single character. This is the kernel function for larger algorithmic problems like the Word Ladder, where you try to find the shortest route between two words, by changing one character at a time, and where each intermediate step must be in an intial dictionary of words.

Bruce Force

The simplest algorithm possible will take the input word and then compare it against every word in the dictionary, character by character, and count the differences. If the character differences between the word are just 1, then we add it to our list, otherwise we reject it. JavaScript has the Array.filter method which does precisely what we want and we just supply a function which returns true or false depending on if our criterion are met.

It is possible to optimize differsByOne further to get more benefits, but given t

@FremyCompany
FremyCompany / a.js
Last active November 19, 2021 16:03
Namespaces events without jQuery
// once in the headers
defineMetaEvent('Namespace','Click');
// then when you need it
document.body.onNamespaceClick.add(function(e) { /*...*/ });
document.body.onNamespaceClick.clear();
@FremyCompany
FremyCompany / data.rs
Last active May 27, 2019 14:41
Large Trie rust compiler oom repro
use std::collections::hash_map::HashMap;
use std::hash::Hash;
pub struct Trie<K, V> where K: std::fmt::Debug+Eq+Hash+Clone, V: std::fmt::Debug+Clone {
value: Option<V>,
children: HashMap<K, Trie<K, V>>,
}
impl<K, V> Trie<K,V> where K: std::fmt::Debug+Eq+Hash+Clone, V: std::fmt::Debug+Clone {
pub fn new() -> Trie<K, V> {
@FremyCompany
FremyCompany / map-update.js
Last active February 5, 2019 13:37
Proof of concept of styleMap extension
// ================================================================
// enable to update a set of properties with a single call
// ================================================================
StylePropertyMapReadOnly.prototype.update = function(updaters) {
var oldValues = Object.create(null);
for(var key in updaters) { oldValues[key] = this.get(key); }
for(var key in updaters) { let newValue = updaters[key](oldValues); if (newValue) this.set(key, newValue); }
}
@FremyCompany
FremyCompany / compile.bat
Created February 4, 2019 19:57
Output all colors of an image in a file
# Open a command prompt and type the following lines:
cd C:\type\the\path\to\the\folder\containing\the\cs\file
C:\Windows\Microsoft.NET\Framework\v3.5\csc.exe extract-colors.cs /reference:System.Drawing.dll
extract-colors.exe > result.txt
@FremyCompany
FremyCompany / log-clicks.js
Created June 12, 2017 22:05
Debug WebDriver-generated mouse events
window.onclick = function(e) {
var d = document.createElement("div");
d.style = `position: fixed; top: ${e.clientY}px; left: ${e.clientX}px; outline: 3px solid ${e.ctrlKey ? 'green' : 'red'};`;
document.body.appendChild(d);
}
@FremyCompany
FremyCompany / typescript-jsx-mithril.tsx
Created February 22, 2017 23:45
Small helper to get autocompletion when authoring Mitrhil components in Typescript
function Component<T>(view: (attributes: T, children: Array<any>) => Mithril.VirtualElement): () => Mithril.Component<Mithril.ControllerWithAttributes<T>> & { attributes?: T} {
return () => ({ view(n) { return view(n.attributes, n.children); } } as any);
}
var Abc = Component<{ input:string, value:string }>(a =>
<div>{a.input}</div>
);
var t = <Abc input="true" value="true" />
@FremyCompany
FremyCompany / index.html
Last active January 27, 2017 04:20
Get the line of source code that generated an element for jsfiddle/etc purposes
<!doctype html><script onload="document.scripts[0].remove()">
var html =
`<html>
<head>
<title>Test page</title>
<style>
input:hover {
outline: 3px solid red;
}
</style>
@FremyCompany
FremyCompany / mapping-system.d.ts
Created June 8, 2016 22:51
Node Mapping System
declare var Map: { new(): Map<any,any> };
interface Map<IndexType, ValueType> {
get(index:IndexType): ValueType,
set(index:IndexType, value:ValueType)
}
declare var MappingSystem: MappingSystemConstructor;
interface MappingSystemConstructor {
new(): MappingSystem<any,any,any,any>
NodeMappingSystem: NodeMappingSystemConstructor
@FremyCompany
FremyCompany / height-distribution-ideas.md
Last active April 23, 2016 06:42
Table height distribution -- ideas

Computing the table height

The height of a table is the largest of:

  • The specified height of the table (percentages are resolved against ancestors as usual)
  • The height required for the rows and the vertical border-spacings. For each row, the largest of:
  • The row’s specified height if it is defined in non-percentages units (0 otherwise)
    • Also the eventual rowgroup specified size, using the usual splitting among tracks
  • The height deemed necessary to render all its cells when running the same algorithm that is being used to compute a column min-width, where the min-height of a cell is the largest of:
    • The cell’s specified height if it is defined in non-percentages units (0 otherwise)
    • The height of the cell border and padding plus the scrollHeight of its content layouted as if:
  • the cell height was set to 0px and overflow was set to hidden,