Skip to content

Instantly share code, notes, and snippets.

View tenebrousedge's full-sized avatar

Kaia Leahy tenebrousedge

View GitHub Profile
@tenebrousedge
tenebrousedge / protein_translation.cr
Created February 2, 2020 14:52
Iterable Example -- Exercism "Protein Translation" solution
require "string_scanner"
module ProteinTranslation
class Polypeptide
alias Codon = String
include Iterator(Codon)
CODONS = {
/AUG/ => "Methionine",
/UUU|UUC/ => "Phenylalanine",
@tenebrousedge
tenebrousedge / forth.cr
Created October 13, 2019 03:35
Forth Interpreter written in Crystal-lang
module Forth
alias Stack = Array(Int32)
alias Operation = Proc(Stack, Stack)
end
module Forth
struct Statement
getter text : String
getter?(new_word : Bool) { text.starts_with?(":") }
forward_missing_to @text
@tenebrousedge
tenebrousedge / quads.rb
Created May 23, 2017 23:30
HackerRank - Beautiful Quadruples
#!/bin/ruby
A = gets.strip.split(' ').map(&:to_i)
Array.class_eval do
def valid?
(0..3).all? {|i| 1 <= self[i] && self[i] <= A[i] }
end
end
@tenebrousedge
tenebrousedge / substring.rb
Created May 23, 2017 13:24
HackerRank Challenge - Special Substrings
#!/bin/ruby
require 'pry-byebug'
require 'set'
# require 'trie'
# So this code works correctly, but it's horribly slow. Probably it needs a trie-based approach.
SPECIAL = {}
PREFIX = {}
#!/usr/bin/env ruby
def hangman
tries_left = 12
# only select words less than ten letters long
lines = File.readlines('/usr/share/dict/words').select { |e| e.length < 10 }
# grab a random word
word = lines[rand(lines.length)].chomp.chars
guessed = []
while tries_left > 0
@tenebrousedge
tenebrousedge / Category.php
Created October 19, 2013 23:04
Example of a Hierarchical Model Relation in CakePHP This is what you usually need instead of Tree Behavior
<?php
class Category extends Model {
public $name = 'Category';
public $hasMany = array('Children' => array(
'className' => 'Category',
'foreignKey' => 'parent_id'
));
@tenebrousedge
tenebrousedge / MultiMysql.php
Created April 13, 2013 21:19
CakePHP Mysql Database driver extension for inserting lots of rows at once. Depending on how your data is being generated, this is liable to be extremely memory-inefficient.
//Store it in App/Model/Datasource/Database/MultiMysql.php
<?php
App::uses('Mysql', 'Model/Datasource/Database');
class MultiMysql extends Mysql {
public function insertMulti($table, $fields, $values) {
$table = $this->fullTableName($table);
$holder = implode(',', array_fill(0, count($fields), '?'));
$fields = implode(', ', array_map(array(&$this, 'name'), $fields));
@tenebrousedge
tenebrousedge / example.php
Created December 5, 2012 18:12
Apis Configure information
//database.php
var $apis = array(
'datasource' => 'Apis.ApisSource',
'login' => '',
'password' =>'',
'authMethod' => 'OAuthV2'
);
//some other config file
$config['Apis']['Cloudprint']['oauth'] = array(