Skip to content

Instantly share code, notes, and snippets.

Avatar
💭
Perpetually confused.

Christopher Kruse ballpointcarrot

💭
Perpetually confused.
View GitHub Profile
@ballpointcarrot
ballpointcarrot / sum_squares.rb
Created Apr 20, 2011
A simple solution to finding the difference of the "square of the sums" and the "sum of the squares."
View sum_squares.rb
def sums(n)
(1..n).inject(0){|sum,i| sum + i**2}
end
def squares(n)
(1..n).inject(0){|sum, i| sum + i} ** 2
end
puts squares(100) - sums(100)
@ballpointcarrot
ballpointcarrot / spiral_numbers.rb
Created Apr 19, 2011
Calculating the diagonal sum of a number spiral, with '1' in the center.
View spiral_numbers.rb
#adds the diagonals of a number spiral (where 1 is center)
def spiral(n)
sum = 1
max = n**2
corner = 1
step = 2
while corner < max
4.times do |i|
corner += step
sum += corner
@ballpointcarrot
ballpointcarrot / primes.rb
Created Apr 18, 2011
A comparison between the mathn library in Ruby and a self-built solution for prime generation.
View primes.rb
require 'mathn'
# Generates a list of primes below a given value
def primelist(n)
k = 1
a = [2,3]
while 6*k < n
a.push(6*k-1)
a.push(6*k+1)
k += 1
@ballpointcarrot
ballpointcarrot / fizzbuzz.rb
Created Apr 13, 2011
A FizzBuzz-styled programming question for a job application.
View fizzbuzz.rb
def fizzbuzz
fifteens = temp = 0
0.upto(1000).collect do |x|
fifteens += x if x % 15 == 0
temp += x if ((x % 3 == 0) ^ (x % 5 == 0))
end
puts "Single fifteens: #{temp + fifteens}"
puts "Double fifteens: #{temp + (fifteens * 2)}"
end
@ballpointcarrot
ballpointcarrot / _form.html.haml
Created Mar 3, 2011
Examle Rails locale YAML to customize Form Label names
View _form.html.haml
~ form_for(@race) do |f|
~ if @race.errors.any?
#error_explanation
%h2 #{pluralize(@race.errors.count, "error")} prohibited this race from being saved:
%ul
~@race.errors.full_messages.each do |msg|
%li= msg
.field
=f.label :name
%br
View paintLWP2.java
class PaintEngine extends Engine implements SensorEventListener {
private final Paint[] mCardinalPaints = new Paint[4];
private final Runnable mDrawWP = new Runnable(){
public void run() {
drawFrame();
}
};
private SensorManager mSensMgr =
(SensorManager) getSystemService(SENSOR_SERVICE);
View gist:443139
package com.cornerofseven.android.lwp.paint;
import android.graphics.Paint;
import android.hardware.SensorManager;
import android.service.wallpaper.WallpaperService;
import android.view.SurfaceHolder;
/**
* PaintService
* Basic setup to create an Android Live Wallpaper.
View anagram.rb
def word_to_hash(word)
primes = {
'a' => 2, 'b' => 3, 'c' => 5, 'd' => 7, 'e' => 1, 'f' => 13, 'g' => 17,
'h' => 19, 'i' => 23, 'j' => 29, 'k' => 31, 'l' => 37, 'm' => 41,
'n' => 43, 'o' => 47, 'p' => 51, 'q' => 53, 'r' => 59, 's' => 61,
't' => 67, 'u' => 71, 'v' => 73, 'w' => 79, 'x' => 83, 'y' => 97,
'z' => 101
}
sum = 1
word.split('').each{|ch|