Skip to content

Instantly share code, notes, and snippets.

@tom-galvin
tom-galvin / c162h.rb
Last active August 29, 2015 14:01
DailyProgrammer Challenge #162h Solution (Novel Compression, pt. 3: Putting it all together)
def tokenize(word, dict)
return word[0] if ['-', 'R*'].include? word
token = (dict.find_index word.downcase.gsub(/[.,\?!;:]/, '')).to_s
(dict << word.downcase.gsub(/[.,\?!;:]/, '');
token = (dict.length - 1).to_s) unless token.length > 0
case word
when /^[a-z]*[.,\?!;:]?$/; # nothing
when /^[A-Z][a-z]*[.,\?!;:]?$/; token += '^'
when /^[A-Z]*[.,\?!;:]?$/; token += '!'
else; puts "Error! Invalid case or punctuation in word #{word}."; abort
@tom-galvin
tom-galvin / c164bh.rb
Created June 15, 2014 16:33
DailyProgrammer Challenge #164bh Solution (A Day in the Life of a Network Router)
alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
vertices = gets.chomp.to_i
adjacency = Array.new(vertices) { gets.chomp.split(',').map {|n| n.to_i } }.transpose
source, sink = *(gets.chomp.split(' ').map {|c| alphabet.index c})
dist = Array.new(vertices, -1)
dist[source] = 0
traversed = []
until traversed.length == vertices
active_vertex = (0...vertices)
.reject {|v| dist[v] == -1}
@tom-galvin
tom-galvin / c164bi.rb
Created June 15, 2014 16:34
DailyProgrammer Challenge #164bi Solution (Prime Factor Trees]
# center a string, padded by spaces
def tcenter(n, str)
side = (n - str.length).to_f / 2.0
return (' ' * side.ceil) + str + (' ' * side.floor)
end
# factorise a factor tree further
# end points (primes) are stored as :type => :atom
# branch points are stored as :type => :branch, with branches :a and :b
# numbers are factorised top-down
@tom-galvin
tom-galvin / c164be.rb
Created June 15, 2014 16:35
DailyProgrammer Challenge #164be Solution (Planetary Gravity Calculator)
# gravitational constant
G = 6.67e-11
class Planet
attr_reader :radius, :density
def initialize(radius, density)
@radius = radius
@density = density
end
@tom-galvin
tom-galvin / graphgen.rb
Created June 15, 2014 16:37
Graph Adjacency Matrix Generator
# Adjacency Matrix graph generator
# for http://www.reddit.com/r/dailyprogrammer/comments/287jxh/
def input(q)
print q
$stdout.flush
gets.chomp
end
def rand_point
@tom-galvin
tom-galvin / c167i.rb
Created June 18, 2014 16:02
DailyProgrammer Challenge #167i Solution (Final Grades)
def line(fn, ln, *grades)
sorted_grades = grades.map {|g| g.to_i}.sort {|g, h| g <=> h}
final = (sorted_grades.inject(:+) / 5.0).round
grade = ['F', 'D', 'C', 'B', 'A', 'A'][[0, final / 10 - 5].max]
grade += (final % 20 < 3 && grade != 'F' ? '-' :
(final % 20 > 16 && !(['A', 'F'].include? grade) ? '+' : ' '))
{:last => ln.gsub('_', ' '), :first => fn.gsub('_', ' '), :grade => final, :string =>
"(#{final.to_s.rjust(2, '0')}%) (#{grade}): #{sorted_grades.map {|g| '%2d' % g}.join ' '}"}
end
@tom-galvin
tom-galvin / c167h.rb
Created June 20, 2014 18:05
DailyProgrammer Challenge #167h Solution (Park Ranger)
# Vertex order
def ord(adj, n)
return adj[n].select {|m| m != -1}.length
end
# Get shortest path length
def dijkstra(adj, source, sink)
vertices = adj.length
dist = Array.new(vertices, -1)
dist[source] = 0
@tom-galvin
tom-galvin / bristol-nn.rb
Created June 26, 2014 22:14
Bristol Challenge NN Algorithm source
# nearest neighbour
# coded in the ugliest way possible :) ouch, mapreduce!
vertices = 91
puts 'Enter distances'
distances = Array.new(vertices) { gets.chomp.split(';').map {|n| n.to_i } }
puts 'Enter durations'
durations = Array.new(vertices) { gets.chomp.split(';').map {|n| n.to_i } }
puts 'Enter place names'
placenames = Array.new(vertices) { gets.chomp.split(';') }
@tom-galvin
tom-galvin / c173i.cs
Last active August 29, 2015 14:04
DailyProgrammer Challenge #173i Solution (Advanced Langton's Ant)
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
namespace AdvancedAnt
{
class Program
{
static void Main(string[] args)
@tom-galvin
tom-galvin / prim.clj
Created August 10, 2014 12:45
Implement's Prim's algorithm in Clojure
; Input via stdin
; Formatted as described in reddit.com/r/dailyprogrammer/comments/20cydp/14042014_challenge_152_hard_minimum_spanning_tree/
(defn with-index [coll]
(map-indexed vector coll))
(defn get-adjacency []
(with-index (map with-index
(repeatedly (read-string (read-line))
#(map read-string (clojure.string/split (read-line) #", *"))))))