patternpythonMinor
N-Bonnaci sequence calculator
Viewed 0 times
bonnacicalculatorsequence
Problem
The code calculates a n-bonnaci sequence to a certain number, based on the user's input. It then can print it to a file and/or print it to the console.
The
My questions are:
This is one of my first Python programs, so I want to know what I might be coding 'wrong'.
```
def calculateSequence(n, amntToCalc):
sqnceItratr = 1
sequence = [0] * numbrsToCalc
#Add 1's to the array(Duonacci is 1, 1, 2... Tribonacci is 1, 1, 1, 3...)
while sqnceItratr 0:
#This is to calculate the actual value of a number
#in the sequence
crrntVlue += sequence[sqnceItratr - i]
i -= 1
#Count Down for the number to add to crrntValue(n-bonnacci)
sequence[sqnceItratr] = crrntVlue
# Add crrntValue to the array
sqnceItratr += 1
#Continue to the next value in the array
return sequence
def convertInput(inputToConvert):
inputToConvert = inputToConvert.upper()
if inputToConvert == 'Y':
return True
elif inputToConvert == 'YES':
return True
else:
return False
N = int(input("What do you want to n in N-bonnacci to be? \n"))
numbrsToCalc = int(input("How many digits do you want to calculate? \n This should probably be bigger than n \n"))
sequence = calculateSequence(N, numbrsToCalc)
userInput1 = convertInput(input("Do you want to write it to a file?
The
calculateSequence function calculates the sequence. The first loop fills the sequence with ones (0, 1, 1, 2...). Next, it calculates the value of the current number, using crrntVlue as a placeholder, and using i as the location of the value to add (a number is the sum of the preceding two numbers). sqnceItratr is location of the number that is being calculated.My questions are:
- Is there anything immediately wrong that you see in the code?
- How can I make the code more readable/better variable names, as it was hard to understand the code while coding it?
- How can I restructure the program to make it better?
This is one of my first Python programs, so I want to know what I might be coding 'wrong'.
```
def calculateSequence(n, amntToCalc):
sqnceItratr = 1
sequence = [0] * numbrsToCalc
#Add 1's to the array(Duonacci is 1, 1, 2... Tribonacci is 1, 1, 1, 3...)
while sqnceItratr 0:
#This is to calculate the actual value of a number
#in the sequence
crrntVlue += sequence[sqnceItratr - i]
i -= 1
#Count Down for the number to add to crrntValue(n-bonnacci)
sequence[sqnceItratr] = crrntVlue
# Add crrntValue to the array
sqnceItratr += 1
#Continue to the next value in the array
return sequence
def convertInput(inputToConvert):
inputToConvert = inputToConvert.upper()
if inputToConvert == 'Y':
return True
elif inputToConvert == 'YES':
return True
else:
return False
N = int(input("What do you want to n in N-bonnacci to be? \n"))
numbrsToCalc = int(input("How many digits do you want to calculate? \n This should probably be bigger than n \n"))
sequence = calculateSequence(N, numbrsToCalc)
userInput1 = convertInput(input("Do you want to write it to a file?
Solution
-
Algorithm
Looks like you are doing too much. The recurrence can be greatly simplified:
\$a_{n} = a_{n-1} + a_{n-2} + \cdot\cdot\cdot + a_{n-k+1} + a_{n-k}\$, and
\$a_{n+1} = a_{n} + a_{n-1} + \cdot\cdot\cdot + a_{n-k+1}\$, so
\$a_{n+1} = a_{n} + (a_{n-1} + \cdot\cdot\cdot + a_{n-k+1}) = a_{n} + (a_{n} - a_{n-k})\$
meaning that the next
-
Naming
Honestly, names like
-
Separation of concerns
Algorithm
Looks like you are doing too much. The recurrence can be greatly simplified:
\$a_{n} = a_{n-1} + a_{n-2} + \cdot\cdot\cdot + a_{n-k+1} + a_{n-k}\$, and
\$a_{n+1} = a_{n} + a_{n-1} + \cdot\cdot\cdot + a_{n-k+1}\$, so
\$a_{n+1} = a_{n} + (a_{n-1} + \cdot\cdot\cdot + a_{n-k+1}) = a_{n} + (a_{n} - a_{n-k})\$
meaning that the next
k-bonacchi number is justsequence[n+1] = 2*sequence[n] + sequence[n-k]-
Naming
Honestly, names like
sqnceItratr and crrntVlue are more than unreadable. Don't abbreviate so aggressively. In this particular case I'd say that n fits much better than sqnceItratr.-
Separation of concerns
calculateSequence initialize the sequence and produces the rest of it in a single call. It doesn't look right. I recommend to split the solution in initialize, and compute_next calls, along the lines ofsequence = initialize(N)
for n in range(N, limit):
value = compute_next(sequence, N)
sequence.append(value)Code Snippets
sequence[n+1] = 2*sequence[n] + sequence[n-k]sequence = initialize(N)
for n in range(N, limit):
value = compute_next(sequence, N)
sequence.append(value)Context
StackExchange Code Review Q#119239, answer score: 4
Revisions (0)
No revisions yet.