Skip to content

Instantly share code, notes, and snippets.

View jackhooper's full-sized avatar

Jack Hooper jackhooper

  • Australia
View GitHub Profile
@jackhooper
jackhooper / fizz-buzz-bazz-bozzle.rb
Created January 19, 2014 07:15
FizzBuzz, extended to be more complicated. Now, if n is a multiple of 7, the program prints out "Bazz", unless it is also a multiple of 14, in which case it prints out "Bozzle". If n is a multiple of 10, it prints out n, no matter what. Inspired by a section of David Fayram's 'FizzBuzz, A Deep Naval to Gaze Into' (http://dave.fayr.am/posts/2012-…
# 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?
@jackhooper
jackhooper / extensible-fizzbuzz.rb
Created January 19, 2014 06:36
Extensible FizzBuzz implementation (based around string concatenation), written in Ruby.
# 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
@jackhooper
jackhooper / fizzbuzz.rb
Last active January 2, 2016 05:09
A very clean implementation of FizzBuzz in Ruby.
# 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) }
@jackhooper
jackhooper / is_prime.py
Last active January 1, 2016 22:19
Translation of the Ruby version. Should work in either Python 2.x or 3.x. Everything I said in the description of the Ruby version applies here, although I will point out that this version is probably faster simply because Python is generally faster than Ruby (assuming we're talking about CPython vs YARV or MRI - JRuby vs Jython may be a differe…
# 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
@jackhooper
jackhooper / is_prime.rb
Last active January 1, 2016 22:18
Figures out whether or not a number is prime. In terms of performance, it generally rejects non-primes quickly, as it doesn't generally have to iterate too many times before it gets a clean division. On the other hand, whilst it confirms small primes reasonably quickly, it takes a few moments to confirm really big ones (it will get there eventua…
# 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?
@jackhooper
jackhooper / factorial.clj
Created January 1, 2014 14:29
Calculates the factorial of n. Written in Clojure
; Calculates factorial of n
; Jack Hooper 2014
(defn factorial [n]
(reduce * (range (inc n))))
@jackhooper
jackhooper / factorial.rb
Last active January 1, 2016 22:09
Calculating the factorial of a given number, written in Ruby.
# Factorial calculator thingimajig
# Jack Hooper 2014
def factorial(n)
(1..n).inject(:*)
end
@jackhooper
jackhooper / quicksort.py
Last active May 8, 2022 03:48
Quicksort in Python. Functional-programming style, zero data mutation.
# 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:
@jackhooper
jackhooper / quicksort.rb
Last active January 1, 2016 22:09
Functional-programming style Quicksort implementation written in Ruby, with no data mutation.
# 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