How to fix tribonacci series function when using generatorsHow to flush output of print function?function default value not definedCalling gen.send() with a new generator in Python 3.3+?Python TypeError: non-empty format string passed to object.__format__ on example 24 from learn python the hard way bookGenerator and list return different resultspython3 send() function in generatorsHow to call multiple functions as arguments inside another function?Setting Dataframe loc result in SQL string - Tuples ErrorPython: queue method wait_for predicate with argumentsHow to create a function which take an iterable and a number “n” as an argument and return tuples with values
London underground zone 1-2 train ticket
Only charge capacitor when button pushed then turn on LED momentarily with capacitor when button released
Does a humanoid possessed by a ghost register as undead to a paladin's Divine Sense?
Can this rough design show the required message?
Why do cheap flights with a layover get more expensive when you split them up into separate flights?
What could prevent players from leaving an island?
Getting Lost in the Caves of Chaos
Our group keeps dying during the Lost Mine of Phandelver campaign. What are we doing wrong?
Repeated! Factorials!
How to switch an 80286 from protected to real mode?
Do some languages mention the top limit of a range first?
Is there a way to prevent the production team from messing up my paper?
Why do dragons like shiny stuff?
Make a living as a math programming freelancer?
What was the role of Commodore-West Germany?
Did silent film actors actually say their lines or did they simply improvise “dialogue” while being filmed?
Why should I "believe in" weak solutions to PDEs?
Based on what criteria do you add/not add icons to labels within a toolbar?
What is the German idiom or expression for when someone is being hypocritical against their own teachings?
Why do proponents of guns oppose gun competency tests?
Could an areostationary satellite help locate asteroids?
A verb for when some rights are not violated?
How do I get the =LEFT function in excel, to also take the number zero as the first number?
The meaning of "scale" in "because diversions scale so easily wealth becomes concentrated"
How to fix tribonacci series function when using generators
How to flush output of print function?function default value not definedCalling gen.send() with a new generator in Python 3.3+?Python TypeError: non-empty format string passed to object.__format__ on example 24 from learn python the hard way bookGenerator and list return different resultspython3 send() function in generatorsHow to call multiple functions as arguments inside another function?Setting Dataframe loc result in SQL string - Tuples ErrorPython: queue method wait_for predicate with argumentsHow to create a function which take an iterable and a number “n” as an argument and return tuples with values
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Following is my approach to return the n element in Tribonacci series
def tri(n,seq = [1, 1, 1]):
for i in range(n-2):
seq = seq[1:] + [sum(seq)]
return seq[-1]
I get the correct result when passing argument through print().
print(tri(10))
Output : 193
However, when using generator(using repl.it), I get error of can only concatenate tuple (not"list") to tuple
I am using below for generator
def tri_generator():
for i in range(1000):
yield (i, (1, 1, 1))
yield (i, (1, 0, 1))
yield (i, (1, 2, 3))
Not sure what I am missing? Any help is appreciated.
python-3.x
add a comment |
Following is my approach to return the n element in Tribonacci series
def tri(n,seq = [1, 1, 1]):
for i in range(n-2):
seq = seq[1:] + [sum(seq)]
return seq[-1]
I get the correct result when passing argument through print().
print(tri(10))
Output : 193
However, when using generator(using repl.it), I get error of can only concatenate tuple (not"list") to tuple
I am using below for generator
def tri_generator():
for i in range(1000):
yield (i, (1, 1, 1))
yield (i, (1, 0, 1))
yield (i, (1, 2, 3))
Not sure what I am missing? Any help is appreciated.
python-3.x
add a comment |
Following is my approach to return the n element in Tribonacci series
def tri(n,seq = [1, 1, 1]):
for i in range(n-2):
seq = seq[1:] + [sum(seq)]
return seq[-1]
I get the correct result when passing argument through print().
print(tri(10))
Output : 193
However, when using generator(using repl.it), I get error of can only concatenate tuple (not"list") to tuple
I am using below for generator
def tri_generator():
for i in range(1000):
yield (i, (1, 1, 1))
yield (i, (1, 0, 1))
yield (i, (1, 2, 3))
Not sure what I am missing? Any help is appreciated.
python-3.x
Following is my approach to return the n element in Tribonacci series
def tri(n,seq = [1, 1, 1]):
for i in range(n-2):
seq = seq[1:] + [sum(seq)]
return seq[-1]
I get the correct result when passing argument through print().
print(tri(10))
Output : 193
However, when using generator(using repl.it), I get error of can only concatenate tuple (not"list") to tuple
I am using below for generator
def tri_generator():
for i in range(1000):
yield (i, (1, 1, 1))
yield (i, (1, 0, 1))
yield (i, (1, 2, 3))
Not sure what I am missing? Any help is appreciated.
python-3.x
python-3.x
edited Mar 27 at 3:58
N997
asked Mar 27 at 3:50
N997N997
655 bronze badges
655 bronze badges
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Here's a simple generator (you can clean up the code as you may like):
def tri_generator():
i = 0
seq = [1, 1, 1]
while True:
seq = [seq[1], seq[2], seq[0] + seq[1] + seq[2]]
yield i, seq
i += 1
n = 10
xx = tri_generator()
for i in range(n - 2):
print(next(xx))
## Output:
## (0, [1, 1, 3])
## (1, [1, 3, 5])
## (2, [3, 5, 9])
## (3, [5, 9, 17])
## (4, [9, 17, 31])
## (5, [17, 31, 57])
## (6, [31, 57, 105])
## (7, [57, 105, 193])
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55369515%2fhow-to-fix-tribonacci-series-function-when-using-generators%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Here's a simple generator (you can clean up the code as you may like):
def tri_generator():
i = 0
seq = [1, 1, 1]
while True:
seq = [seq[1], seq[2], seq[0] + seq[1] + seq[2]]
yield i, seq
i += 1
n = 10
xx = tri_generator()
for i in range(n - 2):
print(next(xx))
## Output:
## (0, [1, 1, 3])
## (1, [1, 3, 5])
## (2, [3, 5, 9])
## (3, [5, 9, 17])
## (4, [9, 17, 31])
## (5, [17, 31, 57])
## (6, [31, 57, 105])
## (7, [57, 105, 193])
add a comment |
Here's a simple generator (you can clean up the code as you may like):
def tri_generator():
i = 0
seq = [1, 1, 1]
while True:
seq = [seq[1], seq[2], seq[0] + seq[1] + seq[2]]
yield i, seq
i += 1
n = 10
xx = tri_generator()
for i in range(n - 2):
print(next(xx))
## Output:
## (0, [1, 1, 3])
## (1, [1, 3, 5])
## (2, [3, 5, 9])
## (3, [5, 9, 17])
## (4, [9, 17, 31])
## (5, [17, 31, 57])
## (6, [31, 57, 105])
## (7, [57, 105, 193])
add a comment |
Here's a simple generator (you can clean up the code as you may like):
def tri_generator():
i = 0
seq = [1, 1, 1]
while True:
seq = [seq[1], seq[2], seq[0] + seq[1] + seq[2]]
yield i, seq
i += 1
n = 10
xx = tri_generator()
for i in range(n - 2):
print(next(xx))
## Output:
## (0, [1, 1, 3])
## (1, [1, 3, 5])
## (2, [3, 5, 9])
## (3, [5, 9, 17])
## (4, [9, 17, 31])
## (5, [17, 31, 57])
## (6, [31, 57, 105])
## (7, [57, 105, 193])
Here's a simple generator (you can clean up the code as you may like):
def tri_generator():
i = 0
seq = [1, 1, 1]
while True:
seq = [seq[1], seq[2], seq[0] + seq[1] + seq[2]]
yield i, seq
i += 1
n = 10
xx = tri_generator()
for i in range(n - 2):
print(next(xx))
## Output:
## (0, [1, 1, 3])
## (1, [1, 3, 5])
## (2, [3, 5, 9])
## (3, [5, 9, 17])
## (4, [9, 17, 31])
## (5, [17, 31, 57])
## (6, [31, 57, 105])
## (7, [57, 105, 193])
answered Mar 27 at 6:12
SanVSanV
5391 silver badge11 bronze badges
5391 silver badge11 bronze badges
add a comment |
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55369515%2fhow-to-fix-tribonacci-series-function-when-using-generators%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown