Created
March 13, 2010 15:20
-
-
Save iansheridan/331368 to your computer and use it in GitHub Desktop.
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
# output and array sorted | |
def ian_sort(a) | |
c = false # change | |
a[0...-1].each_with_index { |v, i| | |
if v > a[i+1] then | |
a[i], a[i+1] = a[i+1], a[i] | |
c = true | |
end | |
} | |
ian_sort(a) if c | |
a | |
end | |
# output and array of uniq values | |
def ian_uniq(a) | |
c = false # change | |
a.each_with_index{|v,i| | |
if v == a[i-1] | |
a.delete_at i | |
c = true | |
end | |
} | |
ian_uniq a if c | |
a | |
end | |
## now to test | |
# letters | |
a = %w{s h r s c g u d s f h t w e t y} | |
ian_sort a | |
ian_uniq a | |
# words | |
a = %w{sit her rat sit cat get use die sit fat her too wiz eat too yes} | |
ian_sort a | |
ian_uniq a | |
# numbers | |
a = [1,2,4,6,43,5,3,122,1,2,1,3,45,6,78] | |
ian_sort a | |
ian_uniq a |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment