Last active
May 5, 2019 02:26
-
-
Save pedryvo/89544a72ccec3bcb1539b5cfcf26ff56 to your computer and use it in GitHub Desktop.
speedio test
This file contains hidden or 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
def fibonacci(n) | |
return 0 if n == 0 | |
return 1 if n > 0 && n <= 2 | |
first = 1 | |
second = 1 | |
until n < 3 | |
fibonacci = first + second | |
first = second | |
second = fibonacci | |
n -= 1 | |
end | |
fibonacci | |
end | |
def print_fibo(n) | |
n.times do |n| | |
print "#{fibonacci(n)} " | |
end | |
end | |
print_fibo(rand(1..15)) | |
puts |
This file contains hidden or 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
def count_occurrence(string) | |
string = string.split('') | |
string_uniq = string.uniq | |
result = [] | |
for i in (0...string_uniq.size) | |
result.push([string_uniq[i-1], string.count(string_uniq[i-1])]) | |
end | |
result = Hash[*result.flatten] | |
result = result.sort_by {|_key, value| value} | |
print result.to_h | |
puts | |
end | |
def get_random_string(length=rand(10..20)) | |
source=("a".."c").to_a | |
key="" | |
length.times{ key += source[rand(source.size)].to_s } | |
return key | |
end | |
a = get_random_string | |
puts a | |
count_occurrence(a) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment