#🔒 How can I write a generator function from N to N^3?

43 messages · Page 1 of 1 (latest)

hybrid lantern
#

I'm kinda stuck tbh, I know it's possible mathematically speaking, but actually implementing it is hard

regal irisBOT
#

@hybrid lantern

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

hybrid lantern
#

I tried making it go like
(1,2,3)
(1,2,4)
(1,3,4)
(2,3,4)
(1,2,5)
(1,3,5)
(1,4,5)
(2,3,5)
(3,4,5)
(1,2,6)
...

#

So increasing the highest number only when neccesary

#

But I don't really see how

ruby walrus
#

I don't really get it, you want a cartesian product?

hybrid lantern
#

Ye

ruby walrus
#

!d itertools.product

regal irisBOT
#

itertools.product(*iterables, repeat=1)```
Cartesian product of input iterables.

Roughly equivalent to nested for-loops in a generator expression. For example, `product(A, B)` returns the same as `((x,y) for x in A for y in B)`.

The nested loops cycle like an odometer with the rightmost element advancing on every iteration. This pattern creates a lexicographic ordering so that if the input’s iterables are sorted, the product tuples are emitted in sorted order.

To compute the product of an iterable with itself, specify the number of repetitions with the optional *repeat* keyword argument. For example, `product(A, repeat=4)` means the same as `product(A, A, A, A)`.
hybrid lantern
#

But with a < b < c for triplet (a,b,c)

#

Oh nice

#

itertools is so handy

ruby walrus
#

Can do this and filter them

#

Or write one yourself to already incorporate the constraint

hybrid lantern
#

Well, I want it a bit faster than having to get rid of them all, is there a way I get the original function and edit it to remove the extra ones?

ruby walrus
#

!e

for x in range(1, 7):
  for y in range(x, 7):
    for z in range(y, 7):
      print(x, y, z)
regal irisBOT
hybrid lantern
#

Well that'll stop after 7^3 numbers

ruby walrus
#

You wanted it to stop after N^3 right?

hybrid lantern
#

No, I want it to go indefinitely

#

The bijection is from N to N^3, or as you said, the cartesian product of N^3

#

N is just the natural numbers, not an actual number like 7

ruby walrus
#

In what order would you do it?

#

If it is an infinite series

ruby walrus
#
import itertools.count

for x in itertools.count(start=3):
  for y in range(2, x):
    for z in range(1, y):
      print(z, y, x)
#

Something like this?

#

Edited*

hybrid lantern
#

Yes, that works

#

How does it work?

#

Well, I can find that out myself

#

Thanks you so much!

ruby walrus
#

itertools.count is an infinite range

#

The rest should be understandable I hope

hybrid lantern
#

ye

#

Thanks!

#

+close

ruby walrus
#

!close

hybrid lantern
#

!close

regal irisBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.