Skip to content
This repository was archived by the owner on Dec 12, 2023. It is now read-only.

Commit 6a956ca

Browse files
committed
Merge pull request #48 from jamesfzhang/master
A few solutions in ruby
2 parents 39fe30e + 6c537bc commit 6a956ca

File tree

10 files changed

+41
-0
lines changed

10 files changed

+41
-0
lines changed

factorial/factorial.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
def factorial(n)
2+
return 1 if n < 2
3+
n * factorial(n - 1)
4+
end

fibonacci/fib.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def fib(n)
2+
return 0 if n == 0
3+
return 1 if n == 1 || n == 2
4+
fib(n - 2) + fib(n - 1)
5+
end

integer-length/int_length.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
def int_length(num)
2+
return 1 if num.abs < 10
3+
1 + int_length(num / 10)
4+
end
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def multiples_of_three_and_five
2+
(1...1000).to_a.select { |num| num % 3 == 0 || num % 5 == 0 }.reduce(&:+)
3+
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def odd_occuring_element(array)
2+
array.uniq.each do |num|
3+
return num if array.count(num) % 2 != 0
4+
end
5+
end
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def remove_duplicates(str)
2+
str.split("").uniq.join
3+
end
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def reverse_words(array)
2+
array.split(" ").reverse.join(" ")
3+
end
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def shortest_fizz_buzz
2+
(1..100).to_a.each{|i|puts i%3==0&&i%5==0 ? "FizzBuzz": i%3==0 ? "Fizz": i%5==0 ? "Buzz": i}
3+
end

sum-of-array-plus-one/sum_plus_one.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def sum_plus_one(array)
2+
array.reduce(&:+) + array.size
3+
end

word-positions/word_position.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def word_position(text, word)
2+
text = text.split(" ")
3+
positions = []
4+
text.each_index do |i|
5+
positions << i if text[i] == word
6+
end
7+
return positions
8+
end

0 commit comments

Comments
 (0)