# Фибоначи лайк

Краен срок
29.10.2016 19:00

Срокът за предаване на решения е отминал

# Фибоначи лайк

Дефинирайте функция fibonacci_like?, която приема списък от числа и проверява дали подадената редица "прилича" на фибоначи, т.е. всеки неин член е получен от сумата на предишните два. Нека върнатият резултат е true или false в зависимост от това дали условието е изпълнено или не. Ще подаваме редици с поне 3 числа.

### Пример

fibonacci_like?([2, 6, 8, 14, 22, 36]) # => true
fibonacci_like?([0, 1, 1, 2, 3, 8])    # => false


### Примерни тестове

Написали сме примерни тестове, които може да намерите в хранилището с домашните.

Преди да предадете решение се уверете, че тестовете ви се изпълняват без грешки. Това ще ви гарантира, че не сте сбъркали нещо тривиално. Например име на функция, брой приемани аргументи и подобни.

## Решения

• Коректно
• 4 успешни тест(а)
• 0 неуспешни тест(а)
Здравко Петров
def fibonacci(number, bottom_one, bottom_two)
if number == 1
bottom_one
elsif number == 2
bottom_two
else
fibonacci(number-1, bottom_one, bottom_two) + fibonacci(number-2, bottom_one, bottom_two)
end
end
def fibonacci_like?(arr)
if fibonacci(arr.length, arr, arr) == arr.last
true
else
false
end
end
....

Finished in 0.00277 seconds
4 examples, 0 failures
• Коректно
• 4 успешни тест(а)
• 0 неуспешни тест(а)
Александър Ойнаков
def fibonacci_like?(numbers)
numbers.each_with_index do |item , index|
unless (0..1).include? index
if numbers[index - 1] + numbers[index - 2] != numbers [index]
return false
end
end
end
if (0..1).include? numbers.size
return false
end
return true
end
....

Finished in 0.00277 seconds
4 examples, 0 failures
• Некоректно
• 3 успешни тест(а)
• 1 неуспешни тест(а)
Кузман Белев
def fibonacci_like?(sequence)
sequence.each_cons(3).all? { |a, b, c| (a + b).round(10) == c }
end
...F

Failures:

1) #fibonacci_like? works with bigger numbers
Failure/Error: expect(fibonacci_like?(sequence)).to be true

expected #<TrueClass:20> => true
got #<FalseClass:0> => false

Compared using equal?, which compares object identity,
but expected and actual are not the same object. Use
expect(actual).to eq(expected) if you don't care about
object identity in this example.
# /tmp/d20161030-13689-cnoezg/spec.rb:27:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:7:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:7:in block (2 levels) in <top (required)>'

Finished in 0.00291 seconds
4 examples, 1 failure

Failed examples:

rspec /tmp/d20161030-13689-cnoezg/spec.rb:19 # #fibonacci_like? works with bigger numbers
• Коректно
• 4 успешни тест(а)
• 0 неуспешни тест(а)
Дарин Тодоров
def fib_like_creator(first_element, second_element, len = nil)
new_array = []
while new_array.length < len && len do
new_array << first_element
first_element, second_element = second_element, first_element + second_element
end
new_array
end
def fibonacci_like?(sequence)
new_sequence = fib_like_creator(sequence, sequence, sequence.length)
sequence == new_sequence ? true : false
end
....

Finished in 0.0028 seconds
4 examples, 0 failures
• Некоректно
• 2 успешни тест(а)
• 2 неуспешни тест(а)
Симеон Гергинов
def fibonacci_like?(numbers)
result = true
index = 0
while index + 2 < numbers.size
current = numbers[index] + numbers[index + 1]
result = current != numbers[index + 2] ? false : true
result == false unless result
index += 1
end
result
end
FF..

Failures:

1) #fibonacci_like? works with fibonacci sequence
Failure/Error: expect(fibonacci_like?([0, 1, 1, 2, 3, 6, 8, 14])).to be false

expected #<FalseClass:0> => false
got #<TrueClass:20> => true

Compared using equal?, which compares object identity,
but expected and actual are not the same object. Use
expect(actual).to eq(expected) if you don't care about
object identity in this example.
# /tmp/d20161030-13689-a57jwo/spec.rb:4:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:7:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:7:in block (2 levels) in <top (required)>'

2) #fibonacci_like? handles different starting numbers
Failure/Error: expect(fibonacci_like?([1, 4, 6, 10, 16, 26, 42, 68])).to be false

expected #<FalseClass:0> => false
got #<TrueClass:20> => true

Compared using equal?, which compares object identity,
but expected and actual are not the same object. Use
expect(actual).to eq(expected) if you don't care about
object identity in this example.
# /tmp/d20161030-13689-a57jwo/spec.rb:9:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:7:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:7:in block (2 levels) in <top (required)>'

Finished in 0.00285 seconds
4 examples, 2 failures

Failed examples:

rspec /tmp/d20161030-13689-a57jwo/spec.rb:2 # #fibonacci_like? works with fibonacci sequence
rspec /tmp/d20161030-13689-a57jwo/spec.rb:7 # #fibonacci_like? handles different starting numbers
• Некоректно
• 3 успешни тест(а)
• 1 неуспешни тест(а)
Костадин Самарджиев
def produce_fibnacci(sequence, counter)
next_number = -> (seq) { seq[seq.size - 2] + seq[seq.size - 1] }
fibonacci_seq = sequence
if fibonacci_seq.size == counter
fibonacci_seq
else
fibonacci_seq.push(next_number.call(sequence))
produce_fibnacci(fibonacci_seq, counter)
end
end
def fibonacci_like?(sequence)
fibonacci = produce_fibnacci(sequence[0..2], sequence.size)
fibonacci == sequence ? true : false
end
.F..

Failures:

1) #fibonacci_like? handles different starting numbers
Failure/Error: expect(fibonacci_like?([1, 4, 6, 10, 16, 26, 42, 68])).to be false

expected #<FalseClass:0> => false
got #<TrueClass:20> => true

Compared using equal?, which compares object identity,
but expected and actual are not the same object. Use
expect(actual).to eq(expected) if you don't care about
object identity in this example.
# /tmp/d20161030-13689-bgw0or/spec.rb:9:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:7:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:7:in block (2 levels) in <top (required)>'

Finished in 0.00287 seconds
4 examples, 1 failure

Failed examples:

rspec /tmp/d20161030-13689-bgw0or/spec.rb:7 # #fibonacci_like? handles different starting numbers
• Некоректно
• 0 успешни тест(а)
• 4 неуспешни тест(а)
Малина Демирова
require 'bigdecimal'
require 'bigdecimal/util'
def fibonacci_like?(sequence)
firstElement = sequence.round.abs
secondElement = sequence[-1].round
sequenceLength = sequence.length - 1
if isFibonacci?(firstElement) & isFibonacci?(secondElement)
newSequence = (firstElement..sequenceLength).map(&:fibonacci_sequence.to_proc)
end
if newSequence == sequence
return true
else
sequence.each_with_index do |element, index|
if element == (sequence[index - 1] + sequence[index - 2])
result = false
else
result = true
end
return result
end
end
end
def isFibonacci?(number)
if number == 0
return true
else
number = BigDecimal.new(number)
firstBigNumber = (5 * (number**2) + 4).sqrt(0)
secondBigNumber = (5 * (number**2) - 4).sqrt(0)
return (firstBigNumber == firstBigNumber.round || secondBigNumber == secondBigNumber.round)
end
end
class Fixnum
def fibonacci_sequence
phi = (1 + Math.sqrt(5)) / 2
((phi**self - (-1 / phi)**self) / Math.sqrt(5)).to_i
end
end
FFFF

Failures:

1) #fibonacci_like? works with fibonacci sequence
Failure/Error: expect(fibonacci_like?([0, 1, 1, 2, 3, 6, 8, 14])).to be false

expected #<FalseClass:0> => false
got #<TrueClass:20> => true

Compared using equal?, which compares object identity,
but expected and actual are not the same object. Use
expect(actual).to eq(expected) if you don't care about
object identity in this example.
# /tmp/d20161030-13689-12vafqi/spec.rb:4:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:7:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:7:in block (2 levels) in <top (required)>'

2) #fibonacci_like? handles different starting numbers
Failure/Error: expect(fibonacci_like?([1, 4, 6, 10, 16, 26, 42, 68])).to be false

expected #<FalseClass:0> => false
got #<TrueClass:20> => true

Compared using equal?, which compares object identity,
but expected and actual are not the same object. Use
expect(actual).to eq(expected) if you don't care about
object identity in this example.
# /tmp/d20161030-13689-12vafqi/spec.rb:9:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:7:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:7:in block (2 levels) in <top (required)>'

3) #fibonacci_like? handles sequences with negative numbers
Failure/Error: expect(fibonacci_like?([-1, -2, -3, -5, -9])).to be false

expected #<FalseClass:0> => false
got #<TrueClass:20> => true

Compared using equal?, which compares object identity,
but expected and actual are not the same object. Use
expect(actual).to eq(expected) if you don't care about
object identity in this example.
# /tmp/d20161030-13689-12vafqi/spec.rb:16:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:7:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:7:in block (2 levels) in <top (required)>'

4) #fibonacci_like? works with bigger numbers
Failure/Error: expect(fibonacci_like?(sequence + )).to be false

expected #<FalseClass:0> => false
got #<TrueClass:20> => true

Compared using equal?, which compares object identity,
but expected and actual are not the same object. Use
expect(actual).to eq(expected) if you don't care about
object identity in this example.
# /tmp/d20161030-13689-12vafqi/spec.rb:28:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:7:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:7:in block (2 levels) in <top (required)>'

Finished in 0.00964 seconds
4 examples, 4 failures

Failed examples:

rspec /tmp/d20161030-13689-12vafqi/spec.rb:2 # #fibonacci_like? works with fibonacci sequence
rspec /tmp/d20161030-13689-12vafqi/spec.rb:7 # #fibonacci_like? handles different starting numbers
rspec /tmp/d20161030-13689-12vafqi/spec.rb:14 # #fibonacci_like? handles sequences with negative numbers
rspec /tmp/d20161030-13689-12vafqi/spec.rb:19 # #fibonacci_like? works with bigger numbers
• Некоректно
• 3 успешни тест(а)
• 1 неуспешни тест(а)
Петър Нетовски
def fibonacci_like?(sequence)
sequence.each_cons(3) { |group| return false if (group + group).round(2) != group }
true
end
...F

Failures:

1) #fibonacci_like? works with bigger numbers
Failure/Error: expect(fibonacci_like?(sequence)).to be true

expected #<TrueClass:20> => true
got #<FalseClass:0> => false

Compared using equal?, which compares object identity,
but expected and actual are not the same object. Use
expect(actual).to eq(expected) if you don't care about
object identity in this example.
# /tmp/d20161030-13689-l5oaap/spec.rb:27:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:7:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:7:in block (2 levels) in <top (required)>'

Finished in 0.00291 seconds
4 examples, 1 failure

Failed examples:

rspec /tmp/d20161030-13689-l5oaap/spec.rb:19 # #fibonacci_like? works with bigger numbers
• Коректно
• 4 успешни тест(а)
• 0 неуспешни тест(а)
Ивайло Дончев
def fibonacci_like?(list_of_integers)
list_length = list_of_integers.length
list_is_fibonacci = true
if list_length < 3
list_is_fibonacci = false
end
indexes = Array((2..list_length - 1))
indexes.each do |index|
if list_of_integers[index] != list_of_integers[index - 1] + list_of_integers[index - 2]
list_is_fibonacci = false
end
end
list_is_fibonacci
end
....

Finished in 0.00281 seconds
4 examples, 0 failures
• Коректно
• 4 успешни тест(а)
• 0 неуспешни тест(а)
Момчил Баталов
def fibonacci_like?(input_array)
isTrue = true
if(input_array.size < 3)
return "Wrong input!"
end
i = 2
while (i < input_array.size)
if input_array[i] == input_array[i - 1] + input_array[i - 2]
isTrue = true
else
isTrue = false
break
end
i += 1
end
if isTrue
return true
else
return false
end
end
....

Finished in 0.00299 seconds
4 examples, 0 failures
• Некоректно
• 3 успешни тест(а)
• 1 неуспешни тест(а)
Михаил Здравков
def fibonacci_like?(numbers)
numbers[2..-1].reduce numbers[0..1] do |last_two, current|
return false if last_two.map(&:to_f).reduce(&:+) != current.to_f
[last_two.last, current]
end
true
end
...F

Failures:

1) #fibonacci_like? works with bigger numbers
Failure/Error: expect(fibonacci_like?(sequence)).to be true

expected #<TrueClass:20> => true
got #<FalseClass:0> => false

Compared using equal?, which compares object identity,
but expected and actual are not the same object. Use
expect(actual).to eq(expected) if you don't care about
object identity in this example.
# /tmp/d20161030-13689-ir1pd9/spec.rb:27:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:7:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:7:in block (2 levels) in <top (required)>'

Finished in 0.01707 seconds
4 examples, 1 failure

Failed examples:

rspec /tmp/d20161030-13689-ir1pd9/spec.rb:19 # #fibonacci_like? works with bigger numbers
• Некоректно
• 3 успешни тест(а)
• 1 неуспешни тест(а)
Стамен Драгоев
def fibonacci_like?(arr)
(arr.length - 2).times do |index|
return false if (arr[index] + arr[index + 1]).round(5) != arr[index + 2]
end
true
end
...F

Failures:

1) #fibonacci_like? works with bigger numbers
Failure/Error: expect(fibonacci_like?(sequence)).to be true

expected #<TrueClass:20> => true
got #<FalseClass:0> => false

Compared using equal?, which compares object identity,
but expected and actual are not the same object. Use
expect(actual).to eq(expected) if you don't care about
object identity in this example.
# /tmp/d20161030-13689-1xifuon/spec.rb:27:in block (2 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:7:in block (3 levels) in <top (required)>'
# ./lib/language/ruby/run_with_timeout.rb:7:in block (2 levels) in <top (required)>'

Finished in 0.0029 seconds
4 examples, 1 failure

Failed examples:

rspec /tmp/d20161030-13689-1xifuon/spec.rb:19 # #fibonacci_like? works with bigger numbers
• Коректно
• 4 успешни тест(а)
• 0 неуспешни тест(а)
Васил Чимев
def fibonacci_like?(array)
fib_like = []
array.each_cons(3) do |subarray|
if subarray + subarray == subarray
fib_like.push true
else
fib_like.push false
end
end
fib_like.include?(false) ? false : true
end
....

Finished in 0.00752 seconds
4 examples, 0 failures
• Коректно
• 4 успешни тест(а)
• 0 неуспешни тест(а)
Кристъфър Коруев
def fibonacci_like?(list)
is_fibonacci_like = lambda do |list|
flag = (list + list) == list
return flag if list.length == 3
return is_fibonacci_like.call(list[1, list.length]) if flag
flag
end
is_fibonacci_like.call(list)
end
....

Finished in 0.00281 seconds
4 examples, 0 failures
• Коректно
• 4 успешни тест(а)
• 0 неуспешни тест(а)
Георги Карапетров
def fibonacci_first_triplet?(sequence)
return false if sequence.length < 3
sequence + sequence == sequence
end
def fibonacci_like?(sequence)
return true if sequence.length == 3 and fibonacci_first_triplet?(sequence)
fibonacci_first_triplet?(sequence) && fibonacci_like?(sequence.drop(1))
end
....

Finished in 0.00281 seconds
4 examples, 0 failures
• Коректно
• 4 успешни тест(а)
• 0 неуспешни тест(а)
Станислав Димитров
def fibonacci_like?(arr)
result = []
arr_size = arr.length-2
arr.each_with_index do |_,index|
if index < arr_size
result << ((arr[index] + arr[index+1]).round(4) == arr[index+2].round(4))
end
end
result.include?(false) ? false : true
end
....

Finished in 0.00286 seconds
4 examples, 0 failures
• Коректно
• 4 успешни тест(а)
• 0 неуспешни тест(а)
Красимир Тренчев
# def fibonacci_like?(seq)
# res_index = 2
# while res_index < seq.length do
# first = Complex(seq[res_index - 2])
# second = Complex(seq[res_index - 1])
# result = Complex(seq[res_index])
# return false unless first + second == result
# res_index += 1
# end
# true
# end
def fibonacci_like?(seq)
return true unless seq.length > 2
(head + tail == tail) && fibonacci_like?(tail)
end
....

Finished in 0.00279 seconds
4 examples, 0 failures
• Коректно
• 4 успешни тест(а)
• 0 неуспешни тест(а)
Беатрис Бонева
def fibonacci_like?(numbers)
first, second, third = *numbers[0..2]
is_fibonacci_like_num = (first + second == third)
next_numbers = numbers.drop(1)
if next_numbers.size < 3
is_fibonacci_like_num
else
is_fibonacci_like_num ? fibonacci_like?(next_numbers) : false
end
end
....

Finished in 0.00272 seconds
4 examples, 0 failures`