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
# using first order functions | |
def factorial(n) | |
(1..n).inject(:*) || 1 | |
end | |
factorial(4) | |
=> 24 |
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
Prelude> let factorial n = product [1..n] | |
Prelude> factorial 2 | |
2 | |
Prelude> factorial 3 | |
6 | |
Prelude> factorial 4 | |
24 |
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
punctuation = %w{; = - _ . , ? ! $} | |
letters = [('a'..'z'), ('A'..'Z')].map { |i| i.to_a }.flatten | |
"#{punctuation.sample}#{(0...10).map { letters[rand(letters.length)] }.join}#{rand(1000)}" |
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
# just in case you work in a server were you do not have package groups defined. RHEL / CentOS | |
# yum groupinstall 'Development Tools' | |
yum -y install autoconf automake binutils bison flex gcc gcc-c++ gettext libtool make patch pkgconfig redhat-rpm-config rpm-build rpm-sign |
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
def combinations(array) | |
m = array.length | |
(1...2**m).map do | n | | |
(0...m).select { | i | n[i] == 1 }.map { | i | array[i] } | |
end | |
end | |
def calculate_combinations(comb_array, target_sum=15) | |
combinations(comb_array).map {|a| [a, a.inject(:+)]}.select {|v| v if v.last == target_sum} | |
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
a = 95800 | |
b = 217519 | |
c = 414560 | |
d = 422481 | |
a**4 + b**4 + c**4 == d**4 # => 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
# https://codility.com/demo/results/demoKSSE8R-5B3/ | |
# A small frog wants to get to the other side of the road | |
# two solutions, this one gets 33% of scores: | |
def solution(x, y, d) | |
y % d > 0 ? ((y - x) / d) + 1 : (y - x) / d | |
end | |
# this gets 100% scores: | |
def solution(x, y, d) |
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
def fibonacci(n) | |
n <= 1 ? 1 : fibonacci(n - 1) + fibonacci(n - 2) | |
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
Initial solution, test passing, 0% scores: | |
(1..a.size-2).each {|i| p (a.slice(i,a.size).inject(:+) - a.slice(0, i).inject(:+)) }.min | |
I found this on internet, 100% scores, very long: | |
puts "tape_equilibrium: " | |
return 0 if a.empty? | |
# Example data: N=5, P(1-4), A[3,1,2,4,3] | |
# P1: |
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
(1..100).each {|i| print i; print ' fizz' if i%3 == 0; print ' buzz' if i%5 ==0; puts} | |
# recursive | |
def fizzbuzz(n = 1) | |
if n < 101 | |
print "#{i}: " | |
print "Fizz" if 0 == i % 3 | |
print "Buzz" if 0 == i % 5 | |
print "\n" | |
fizzbuzz(n + 1) |
OlderNewer