Problem 69 - Fibonacci Divisibility

Back to General discussions forum

CurioFibo     2022-08-28 16:42:09

Hi there folks, I need help with the problem 69. I have test for many times and in different approaches and I the answer dosen't macth.

The code is follewing under:

Thanks so much in advance.

from csv import reader

def stringToInteger(): """ This function open a .csv file and return a list of integer. :return: """ arq = open("problem069.csv") l1 = reader(arq, delimiter=" ") l1 = list(*l1) for i in range(0, len(l1)): l1[i] = int(l1[i]) return l1

def fibonacciSequence(): """ This function return a Fibonacci Sequence until of stipulated limit. :return: """ l2 = [] a, b = 0, 1 p = a + b for i in range(0, 1_000): l2.append(a) a, b = b, p p = a + b return l2

def fibonacciDivisibility(l1, l2): """ Given usual Fibonacci Sequence, starting with 0 and 1:

        0 1 1 2 3 5 8 13 21 34 ...

and some value M you will be asked to find the index of the
first non-zero member of this list, which is evenly divisible
by this M, e.g. if you are given M = 17 the answer is 9 (the
index of the element 34).

Input data in the first line will contain the number of test-cases.
Next line will contain exactly this of divisors M (not exceeding 10000)
for which you should give answers. Answer should contain indices of
members of Fibonacci Sequence, separated by spaces.
:param l1:
:param l2:
:return:
"""
l3 = []
for i in range(1, len(l2)):
    for k in range(0, len(l1)):
        if l2[i] % l1[k] == 0:
            l3.append(l2.index(l2[i]))
            break
        else:
            break
return print(*l3)

fibonacciDivisibility(stringToInteger(), (fibonacciSequence()))

gardengnome     2022-08-28 17:46:22
User avatar

The method fibonacciSequence works fine. General comment: python function names should ideally be lower case, something like fibonacci_sequence.

A number of questions to think about with regard to fibonacciDivisibility:

  • Are the outer and inner loop in the right order?
  • l3.append(l2.index(l2[i])) - shouldn't that simply be l3.append(i)?
  • Why do you break in the else branch? Do you actually need the else branch?
  • return print(*l3): do you want to print l3, or return something?
CurioFibo     2022-08-28 19:38:19

Hello There!

I belive strongelly that the loops are correct order.

I've used l3.append(l2.index(l2[i]) becouse a got many list in the task

If I don't break with else, then answer is bigger than a need. Without break the loop above will runner instead has already have the solution.

And the last question, I could return a list of index fibonacci sequence but for apresentition the resulta I prefer print with return to be more carefully with de code.

One question? Did you have already run the code? It has worked?

Looking forward,

MFGlasner

gardengnome     2022-08-28 20:00:21
User avatar

Yes I got a modified version of your code working. The questions, in particular 1 and 3, give very strong hints. I recommend to think about them again.

CurioFibo     2022-08-29 17:00:55

Hi there, I did some changes in my code as sugested but still dosen't work.

Bellowing is my code refactored.

from csv import reader

def stringToInteger(): """ This function open a .csv file and return a list of integer. :return: """ arq = open("problem069.csv") l1 = reader(arq, delimiter=" ") l1 = list(l1) for i in range(0, len(l1)): l1[i] = int(l1[i]) print(l1) return l1

def fibonacciSequence(): """ This function return a Fibonacci Sequence until of stipulated limit. :return: """ l2 = [] a, b = 0, 1 p = a + b for i in range(0, 1000): l2.append(a) a, b = b, p p = a + b return l2

def fibonacciDivisibility(l1, l2): """ Given usual Fibonacci Sequence, starting with 0 and 1:

        0 1 1 2 3 5 8 13 21 34 ...

and some value M you will be asked to find the index of the
first non-zero member of this list, which is evenly divisible
by this M, e.g. if you are given M = 17 the answer is 9 (the
index of the element 34).

Input data in the first line will contain the number of test-cases.
Next line will contain exactly this of divisors M (not exceeding 10000)
for which you should give answers. Answer should contain indices of
members of Fibonacci Sequence, separated by spaces.
:param l1:
:param l2:
:return:
"""
l3 = []
for i in l2[1:]:
    if len(l3) == len(l1):
        break
    for k in l1:
        if i % k == 0:
            l3.append(l2.index(i))
            break
print(*l3)

fibonacciDivisibility(stringToInteger(), (fibonacciSequence()))

gardengnome     2022-08-29 17:21:00
User avatar
  1. The two loops are still the wrong way around. The outer loop should be over l1 which is your list of inputs. You are trying to do the following: For each input k in l1, find the (index of) the smallest Fibonaacci number i in l2[1:] that ...
  2. The check if len(l3) == len(l1): break is not required.
CurioFibo     2022-08-29 19:11:39

Thanks so much! Now the code works.

Please login and solve 5 problems to be able to post at forum