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
# reddit.com/r/DailyProgrammer | |
# Solution to Challenge #140: Adjacency Matrix | |
nodes, lines = gets.chomp.split(' ').map {|s| s.to_i} | |
mat = Array.new(nodes) {Array.new(nodes) {0}} | |
(1..lines).step 1 do |x| | |
line = gets.chomp.split('->').map {|s| s.split(' ').map {|t| t.to_i}.compact} | |
combos = line.first.product line.last |
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
# reddit.com/r/DailyProgrammer | |
# Solution to Challenge #145: Tree Generation | |
def print_tree(n, trunk, leaves) | |
(1..n).step(2) do |a| | |
side_spaces = " " * ((n - a) / 2) | |
puts side_spaces + leaves * a | |
end | |
puts " " * ((n - 3) / 2) + trunk * 3 | |
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
# reddit.com/r/DailyProgrammer | |
# Solution to Challenge #???: Minimum Spanning Tree | |
vertices = gets.chomp.to_i | |
adjacency = Array.new(vertices) { gets.chomp.split(',').map {|n| n.to_i } }.transpose # matrix input one liner | |
traversed_vertices = [0] | |
edges = [] | |
alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' |
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
/* | |
reddit.com/r/DailyProgrammer | |
Solution to Challenge #130: Roll the Dies | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <time.h> | |
int main(int argc, char **argv) |
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
/* | |
reddit.com/r/DailyProgrammer | |
Solution to Challenge #131: Who Tests the Tests? | |
*/ | |
#include <stdio.h> | |
#include <string.h> | |
#define UPPER(X) ((X) >= 97 && (X) <= 122 ? (X) - 32 : (X)) | |
#define BUFFER_LENGTH 1024 |
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
# reddit.com/r/DailyProgrammer | |
# Solution to Challenge #???: Pascal's Pyramid | |
def fac(n) | |
n < 2 ? 1 : n * fac(n - 1) | |
end | |
def tri(n) | |
(0..n).to_a | |
.map {|x| fac(n) / (fac(x) * fac(n - x))} |
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
# reddit.com/r/DailyProgrammer | |
# Solution to Challenge #???: Intersecting Rectangles | |
class Rectangle | |
attr_accessor :top, :left, :width, :height | |
def initialize(p1, p2) | |
x_coords = [p1[0], p2[0]].sort | |
y_coords = [p1[1], p2[1]].sort | |
@left = x_coords[0]; @width = x_coords[1] - @left |
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
# reddit.com/r/DailyProgrammer | |
# Solution to Challenge #158 (Intermediate): The ASCII Architect, pt. 1 | |
# I herd u liek spaghetti | |
input, output, longest = gets.chomp.split(''), [], 0 | |
while input.any? | |
output.unshift input[0] =~ /\d/ ? | |
(' ' * input.shift.to_i + '++__***---'[0, input.shift.ord - 'a'.ord + 1]) : | |
'++__***--- '[0, input.shift.ord - 'a'.ord + 1] | |
longest = output[0].length if output[0].length > longest |
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 decompress(chunks, dict) | |
delimiter, next_delimiter = '', ' ' | |
output = '' | |
chunks.each do |ch| | |
case ch | |
when /[0-9]\^/ | |
output += delimiter + dict[ch.to_i].capitalize | |
when /[0-9]!/ | |
output += delimiter + dict[ch.to_i].upcase | |
when /[0-9]/ |
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 |
OlderNewer