Skip to content

Instantly share code, notes, and snippets.

Largest product in a series

Write a program that, when given a string of digits, can calculate the largest product for a series of consecutive digits of length n.

For example, for the input '0123456789', the largest product for a series of 3 digits is 504 (7 * 8 * 9), and the largest product for a series of 5 digits is 15120 (5 * 6 * 7 * 8 * 9).

For the input '73167176531330624919225119674426574742355349194934', the

Roman Numerals

The Romans were a clever bunch. They conquered most of Europe and ruled it for hundreds of years. They invented concrete and straight roads and even bikinis. One thing they never discovered though was the number zero. This made writing and dating extensive histories of their exploits slightly more challenging, but the system of numbers they came up with is still in use today. For example the BBC uses Roman numerals to date their programmes.

The Romans wrote numbers using letters - I, V, X, L, C, D, M. (notice these letters have lots of straight lines and are hence easy to hack into stone tablets).

Write a function that convert from normal numbers to Roman Numerals: e.g.

| arabic | roman |

class Phone
def initialize(num)
@num = num
end
def number
if @num.gsub!(/[\s+)(-.]/,"")
return @num
elsif @num.length == 11 && @num[0] == "1"
@epoch
epoch / som.rb
Created July 28, 2014 23:32
tdd_sum_of_multples
class SumOfMultiple
def initialize(lowest = 1, highest = 10)
@lowest = lowest
@highest = highest
end
def calc(*multiples)
sum = 0
(@lowest..@highest).each do |num|
@epoch
epoch / grains.md
Last active September 16, 2015 16:10

Grains

There once was a wise servant who saved the life of a prince. The king promised to pay whatever the servant could dream up. Knowing that the king loved chess, the servant told the king he would like to have grains of wheat. One grain on the first square of a chess board. Two grains on the next. Four on the third, and so on.

There are 64 squares on a chessboard.

Write a program that shows

  • how many grains were on each square, and
  • the total number of grains

Queen Attack

In the game of chess, a queen can attack pieces which are on the same row, column, or diagonal.

A chessboard can be represented by an 8 by 8 array.

Write a program that positions two queens on a chess board and indicates whether or not they are positioned so that they can attack each other.

Jim Weirich:

This is how I explain it… Ruby has Procs and Lambdas. Procs are created with Proc.new { }, lambdas are created with lambda {} and ->() {}.

In Ruby 1.8, proc {} creates lambda, and Ruby 1.9 it creates procs (don't ask).

Lambdas use method semantics when handling parameters, procs use assignment semantics when handling parameters.

This means lambdas, like methods, will raise an ArgumentError when called with fewer arguments than they were defined with. Procs will simply assign nil to variables for arguments that were not passed in.

99 Bottles of Beer

Write a program which prints out the lyrics to that beloved classic, that field-trip favorite: 99 Bottles of Beer on the Wall.

Requirements

  • Your file should be called beer.rb
  • The program should be called with Beer.song
  • The program should output to STDOUT using puts
  • Each verse should be separated by a single blank line.
sudo sh -c "echo 'deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main' > /etc/apt/sources.list.d/pgdg.list"
wget --quiet -O - http://apt.postgresql.org/pub/repos/apt/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
sudo apt-get install postgresql-common
sudo apt-get install postgresql-9.3 libpq-dev
sudo -u postgres createuser your_own_ubuntu_login_username -s
/* comments in */
CREATE TABLE students
(
id SERIAL4 PRIMARY KEY,
first VARCHAR(30) NOT NULL,
last VARCHAR(30) NOT NULL,
dob DATE,
gpa FLOAT8
);