Skip to content

Instantly share code, notes, and snippets.

View jubishop's full-sized avatar
🎼
Making a podcast app.

Justin Bishop jubishop

🎼
Making a podcast app.
View GitHub Profile
def expn(a, b)
a = a.to_r
b = b.to_i
d = 1.to_r
while(b > 0)
d *= a if (b % 2 == 1)
b /= 2
a *= a if (b > 0)
d = d.round(100)
require 'irb/completion'
require 'utility_belt'
require 'interactive_editor'
require 'awesome_print'
AwesomePrint.irb!
class Object
def interesting_methods
@jubishop
jubishop / Factors.rb
Last active September 29, 2015 04:34
# This program is a demo of how to find factors for numbers
# Print message to ask the user for a number
# "puts" is shorthand for "put string".
# It's how we output text to the user.
puts "What number shall we find factors for? "
# We want to get input from the user and then store that input
# in a "variable" so that we can access it again later.
# We've decided to name our variable "number_to_factor".
puts 'What number shall we find factors for? '
number_to_factor = gets.to_i
puts "Factors for #{number_to_factor} are:"
1.upto(number_to_factor) { |number_to_test|
if (number_to_factor % number_to_test == 0)
puts number_to_test
end
@jubishop
jubishop / Factors.rb
Created September 29, 2015 04:06
Factors
# This program is a demo of how to find factors for numbers
# Print message to ask the user for a number
puts 'What number shall we find factors for? '
# We want to get input from user and store in a variable.
# We've decided to name our variable number_to_factor.
# 'gets' is the name of the function that takes input from the user.
# By default 'gets' returns a variable that's called a "string" which means its just text.
# But we actually need our variable to be a number for calculations, so adding
@jubishop
jubishop / Factors.rb
Created September 29, 2015 03:58
Factors Example
# This program is a demo of how to find factors for numbers
# But it's highly inefficient. Can you think of how to make it faster?
# Print message to ask the user for a number
puts 'What number shall we find factors for? '
# Get input from user and store in a variable we've decided
# to name number_to_factor.
# 'gets' is the name of the function that takes input from the user.
# By default 'gets' returns a variable that's called a "string" which means its just text
@jubishop
jubishop / Dollar.rb
Created September 16, 2015 02:59
Dollar class for dealing with dollars and cents without damnable floating points.
def Dollar(val)
Dollar.createRoundedDollar(val.kind_of?(Dollar) ? val.raw : val)
end
def getDollar
input = gets.chomp
return nil if (input.empty?)
Dollar.createRoundedDollar(input)
end
@jubishop
jubishop / probs.rb
Created September 4, 2012 18:47
Girl Probability
girl = 0
boy = 1
at_least_one_girl = 0
all_girls = 0
1000000.times{
one_girl = false
num_girls = 0
3.times {
$me = PUT_YOUR_UID_HERE;
$friends = user_get_reciprocal_friends($me);
$friends_info = user_multiget_basic_info($friends);
$male_friends = array();
$female_friends = array();
foreach ($friends_info as $uid => $friend_info) {
if (gender_is_male($friend_info['gender'])) {
$male_friends[] = $uid;
// $weights = array of weights
// $num = number of result desired.
// returns array of indices chosen from $weights.
function pluck($weights, $num) {
$chosen = array();
while (count($chosen) < $num) {
$sum = array_sum($weights);
$plucked = rand(1, $sum);
$count = 0;
for ($i = 0;