Back to General discussions forum
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()))
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
:
l3.append(l2.index(l2[i]))
- shouldn't that simply be l3.append(i)
?break
in the else
branch? Do you actually need the else
branch?return print(*l3)
: do you want to print l3
, or return something?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
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.
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()))
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 ...if len(l3) == len(l1): break
is not required.Thanks so much! Now the code works.