This file contains 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
# FizzBuzz extended to be more complicated, in order to | |
# demonstrate the extensibility of this approach | |
# Jack Hooper 2014 | |
def fizzbuzz(n) | |
return n if n.modulo(10).zero? | |
output = "" | |
output.concat("Fizz") if n.modulo(3).zero? | |
output.concat("Buzz") if n.modulo(5).zero? |
This file contains 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
# Jack Hooper 2014 | |
def fizzbuzz(n) | |
output = "" | |
output.concat("Fizz") if n.modulo(3).zero? | |
output.concat("Buzz") if n.modulo(5).zero? | |
return n if output.empty? | |
output | |
end |
This file contains 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
# Jack Hooper 2014 | |
def fizzbuzz(n) | |
return "FizzBuzz" if n.modulo(15).zero? | |
return "Fizz" if n.modulo(3).zero? | |
return "Buzz" if n.modulo(5).zero? | |
n | |
end | |
1.upto(100) { |i| puts fizzbuzz(i) } |
This file contains 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
# Returns True or False depending on whether a given number (n) is prime | |
# Translated from Ruby version | |
# Jack Hooper 2014 | |
def is_prime(n): | |
if n <= 1: | |
return False | |
if n == 2: | |
return True |
This file contains 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
# Returns true or false indicating whether or not a given number is prime | |
# Quick to reject non-primes, reasonably quick to confirm small primes, kinda slow to confirm big primes | |
# Jack Hooper 2014 | |
def is_prime?(n) | |
return false if n <= 1 | |
return true if n == 2 | |
(2...n).each do |i| | |
return false if n.modulo(i).zero? |
This file contains 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
; Calculates factorial of n | |
; Jack Hooper 2014 | |
(defn factorial [n] | |
(reduce * (range (inc n)))) |
This file contains 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
# Factorial calculator thingimajig | |
# Jack Hooper 2014 | |
def factorial(n) | |
(1..n).inject(:*) | |
end |
This file contains 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
# Recursive quicksort algorithm implemented in Python | |
# FP-style, with no data mutation whatsoever | |
# One major advantage of not mutating the data is that if you throw an immutable | |
# data structure at it - such as a tuple or a string - it won't blow up in your face. | |
# Jack Hooper 2014 | |
def quicksort(array): | |
if len(array) <= 1: |
This file contains 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
# Quicksort in Ruby | |
# Functional Programming style, with no data mutation. | |
# Jack Hooper 2014 | |
def quicksort(list) | |
return list if list.length <= 1 | |
higher, lower = list[1..-1].partition { |i| i > list.first } | |
quicksort(lower) + [list.first] + quicksort(higher) | |
end |