Stack Overflow uses routes with the format /:id/:title for questions like this one, which I find to be more memorable than simply a terse ID. I’ve decided that I want this for my new project, a rewrite of this blog in Ruby on Rails 5. Small changes to a model and its routes are required […]

Another year, another version. My growth as a programmer strikes me when I compare previous versions with the latest code. Less code which works better. You may compare the last three versions together on GitHub. #!/usr/bin/env zsh # # Functions # # # Check if Photograph is (Approximately) Square # # While it was originally […]

How perfect and clear, hindsight! This was another “oh, duh” moment for me. Stupid means humble means another step closer to enlightenment, so that’s good. In our application a user may favourite content in a manner à la Facebook. When rendering views, the naive way (the Mark way before this post) is to test for […]

This does not generate lexicographic permutations (use Lexicographer for that!); this plucks off one letter from the end and attaches it to the start: rotations = (str) -> Array(str.length).fill(str).map (rotation, index) -> while index-- rotation = rotation[-1..-1] + rotation[0..-2] rotation I liked my solution and wanted to share.

Like, fuck it. After something like three years fucking with this problem, a realization has come upon me that the simplest solutions are the best solutions. The core of my problem was this: I wanted to manage a set of margin and padding sizes from a central point. I want to base sizes all sizes […]

This Codewars problem annoyed the crap out of me. shift = (letter, amount) -> mod = if /[A-Z]/.test letter then 65 else 97 String.fromCharCode mod + (letter.charCodeAt(0) + amount) % mod % 26 The formula to constrain a number to a range is: (range_start + offset_here) % range_start % range_size

A self-referential has_many :through relationship is one where a class interacts with itself through a join table. The classic use are followings: I follow another user. Another user follows me. So a Following is a two-way relationship between the User class and itself. There are a bunch of guides for this that are either outdated […]

Routing concerns have been a thing since Rails 4, but this morning I ran into a real annoyance: the documentation doesn’t adequately explain how to pass a module, in cases where you use separate sub-controllers. More than that, even-what if I want to pass any other options? Here’s what I came up with, using splats […]

MOC <3 background = white default current_time = white default bold directory = white default disabled = white default empty_mixer_bar = white default empty_time_bar = white default enabled = white default bold entry = white default entry_title = white default error = white default bold,underline file = white default filled_mixer_bar = white default reverse filled_time_bar […]

This is a stupid fucking thing, akin to Googling basic syntax for your language, but here we are. My underling and I wasted 40 minutes today re-figuring this out. Migration: class PluralSingular < ActiveRecord::Migration[5.0] create_join_table :plural, :plural end Model: app/model/foo.rb class Foo < ApplicationRecord has_many :plural_singular has_many :plural, through: :plural_singular end Join model: app/model/plural_singular.rb class […]

I finished with one programming job on Friday and I start my next Monday. That means it’s time for some good ol’ retrospection. I used to think I was a special snowflake because I fell into coding, but Stack Overflow says otherwise. I wrote a great sob story about my hard-luck, self-taught career, but I […]

Recursive hash operations were annoying as heck to puzzle out because, you know, functions aren’t just functions in Ruby: You have functions and procs/lambdas, all of which have subtle differences. Like a regular function cannot be passed as a block. Well, whatever, I had a problem where I had to walk, reject-from and then compact […]

Problem #30 was solved on new ground: my first run through Project Euler was in rigid order of first to last ending at #25. The recent round of solutions mostly cover old ground, which works well for me by itself because it shows off how my code has improved since 2012. Also, the run of […]

The new solution for Problem #18 (compared to the old one) is more elegant: I reverse and reduce the triangle in a manner that doesn’t require me to check. The solution for #67 is a bit more longform, so I turned the method I use here into an NPM package: Triangutron. const eighteen = ` […]

I went back and owned the fuck out of Problem 24. I present to you a fully-functional functional lexicographic permutation generator generator. Hail Satan! \m/ #!/usr/bin/env node const lexicographer = require('lexicographer'); const array = [...Array(10).keys()]; const breakpoint = 1000000; let permutations = 0; for (let value of lexicographer.permute(array)) { permutations++; if (permutations === breakpoint) { […]

#24 defeated the fuck out of me first time through: My solution to this was basically stolen elsewhere, and it took me a whole year to come back, sit down, and work through my own solution. The poor wording (I’ve since edited it) of the Wikipedia article didn’t help me. The specific piece of information […]

Once again, it’s more readable than my last solution. I used a generator to get the next triangle. function divisorCount(number) { let end = Math.ceil(Math.sqrt(number)); let divisorCount = 0; for (let i = 1; i < = end; i++) { if (!(number % i)) { divisorCount += 2; } } return divisorCount; } function* triangleGenerator(number) […]

Hackoberfest has made Tic-tac-toe a thing in my life. tictactoe-server opened an issue for a ES6 refactor of the solution checker. I kicked around the lengthy imperative code check and found I wanted to expand the solution, that I wanted to drop in Tic-tac-toe boards of arbitrary size, and to check them in an efficient […]

This solution uses the Sieve of Eratosthenes and the range() functions I detailed a while back. Hurray for function composition! function factors(number) { const primes = sieve(range(2, number >> 1)).filter(n => !(number % n)); return primes.length ? primes : [number]; } The code is simple: Generate a list of primes in range 2-(n / 2) […]

Call me stupid, but the Sieve defeated me for years-I could never quite seem to make it click. TL;DR: Return the first element of the array, joined with the rest of the array with all multiples of the first element removed. A non-zero value is truthy in JavaScript, which is coerced into true, so non-multiples […]

This solution employs the previous solution. With the XOR method explained, you invert the array: Find the unique elements in the array. Concat the new array to the original. This makes the even (even count) numbers odd, and the odd numbers even. Use the XOR method as before. Behold: function evenOccurring(array) { return [].concat(array, array.filter(unique)).reduce(xor); […]

I dug into the problem and discovered that any number XOR’d with itself an odd number of times equals zero, whilst an even number of times equals the number itself: 7 ^ 7 = 0 7 ^ 7 ^ 7 = 7 The trick here is: Numbers which appear in the array an even number […]