This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# gravitational constant | |
G = 6.67e-11 | |
class Planet | |
attr_reader :radius, :density | |
def initialize(radius, density) | |
@radius = radius | |
@density = density | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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(';') } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Drawing; | |
namespace AdvancedAnt | |
{ | |
class Program | |
{ | |
static void Main(string[] args) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 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) #", *")))))) |