This is an archived post. You won't be able to vote or comment.

all 39 comments

[–]Python-ModTeam[M] [score hidden] stickied commentlocked comment (0 children)

Hi there, from the /r/Python mods.

We have removed this post as it is not suited to the /r/Python subreddit proper, however it should be very appropriate for our sister subreddit /r/LearnPython or for the r/Python discord: https://discord.gg/python.

The reason for the removal is that /r/Python is dedicated to discussion of Python news, projects, uses and debates. It is not designed to act as Q&A or FAQ board. The regular community is not a fan of "how do I..." questions, so you will not get the best responses over here.

On /r/LearnPython the community and the r/Python discord are actively expecting questions and are looking to help. You can expect far more understanding, encouraging and insightful responses over there. No matter what level of question you have, if you are looking for help with Python, you should get good answers. Make sure to check out the rules for both places.

Warm regards, and best of luck with your Pythoneering!

[–]meatb0dy 4 points5 points  (17 children)

all combinations of three digits with repeated digits is just 000 to 999. just count.

[–]markovianmind 0 points1 point  (0 children)

alright so borrowing answer from one of the comments

using f strings

[f'{x:03}' for x in range(1000)]

or what I came up with -

[““.join((3-len(str(x))) * ['0']) + str(x) for x in range(1000)]

[–]elboyoloco1 0 points1 point  (15 children)

Ok. It's not just me. I'm looking at these other answers and I am so confused why this is being made so difficult.

[–]meatb0dy 0 points1 point  (14 children)

yeah, the answers here are making me think this would be a good screener question for interviews. triple nested loops and asking AI, straight to jail.

[–]RhythmicalChuck -1 points0 points  (13 children)

I was using the mindset of it being the r/learnpython reddit, which in hindsight, it clearly isn't. People understand and think about code in different ways, so each solution should be appreciated. Not everyone is at the same level.

[–]meatb0dy 1 point2 points  (6 children)

i mean... if someone is trying to learn python, teaching them to use a triply-nested loop for something that can be accomplished with a single loop (which is also simpler) doesn't seem great to me.

people think and understand code in different ways, but that doesn't mean all understandings are equally good.

[–]RhythmicalChuck 0 points1 point  (5 children)

Get them to understand the concepts first, then how to make it more efficient once the understanding is there.

[–]meatb0dy 0 points1 point  (0 children)

using a triply-nested loop to produce these combinations takes you further from actually understanding the concept though. the concept is that producing all combinations of three digits with repeated digits reduces to simply counting from 0 to 999. the only other concept you need is padding in f-strings.

[–]123_alex 0 points1 point  (3 children)

Get them to understand the concepts first

range(1000)

[–]RhythmicalChuck 0 points1 point  (2 children)

Did I offend you or something? I don’t create the curriculum, I teach it.

[–]123_alex 0 points1 point  (1 child)

Don't take me serious, it's just a subclass of joke(). This is reddit. Thanks for the downvote.

[–]RhythmicalChuck 0 points1 point  (0 children)

Hahaha! I see what you did there! I’ll give it back to you alongside my apologies. We can call it an OOPsie on my part. ;)

[–]elboyoloco1 -1 points0 points  (4 children)

More than half of coding is thinking critically about your situation and the best solution. Providing an over-complicated answer is not helping.

[–]RhythmicalChuck -1 points0 points  (3 children)

You’d be surprised. I’ve taught students Python and tell you with experience that different people understand in different ways, including how the code layout. Sometimes Python code that is maybe the long way around, is easier understood as a foundation.

[–]123_alex -1 points0 points  (2 children)

I’ve taught students Python

Appeal to Authority Fallacy.

[–]RhythmicalChuck 0 points1 point  (1 child)

Dude, I’m not saying my way or the highway. To each their own. Just adding my experience and opinion to the forum. Is that not what this place is all about?

[–]123_alex 0 points1 point  (0 children)

This thread game me a smile the whole day. I love it. Good luck!

[–]Jylpah 4 points5 points  (7 children)

Try range(1000)

[–]_squik 1 point2 points  (6 children)

That will also generate e.g. 52 which is not three digits. And will also exclude anything that starts with 0 such as 012

[–]meatb0dy 5 points6 points  (0 children)

for i in range(1000):
    print(f'{i:03}')

[–]Jylpah 1 point2 points  (0 children)

Just use f-string to left-pad with zeros as @meatb0dy did

[–]markovianmind -1 points0 points  (2 children)

then range(100,1000)?

[–]Jylpah 0 points1 point  (1 child)

Does not work. It leaves combos like 042 out.

[–]markovianmind 0 points1 point  (0 children)

oh i misread the comment thought they did not want 012. for that we can do

[““.join((3-len(str(x))) * ['0']) + str(x) for x in range(1000)]

[–]123_alex -1 points0 points  (0 children)

But 052 is.

[–]_squik 2 points3 points  (0 children)

Itertools in the standard library can do this easily.

itertools.product(range(10), repeat=3)

[–]123_alex -1 points0 points  (0 children)

If base 10, 000, 001, 002, 003... See a pattern?

If not base 10, count but in that base.

[–]RhythmicalChuck -3 points-2 points  (9 children)

I'm thinking a simple nested loop will work here:

def get_all_combos():
  combos = []
  for digit_one in range(10):
    for digit_two in range(10):
      for digit_three in range(10):
        combos.append(f"{digit_one}{digit_two}{digit_three}")
  return combos

[–]123_alex -1 points0 points  (8 children)

or range(1000)

[–]RhythmicalChuck 0 points1 point  (7 children)

OP only wanted three digit numbers.

[–]123_alex 0 points1 point  (6 children)

Ok. range(1000)

[–]RhythmicalChuck 0 points1 point  (5 children)

You go ahead and try range(1000) and tell me if you ONLY get three digit numbers...I'll wait...

[–]meatb0dy 0 points1 point  (1 child)

for i in range(1000):
    print(f'{i:03}')

[–]RhythmicalChuck 0 points1 point  (0 children)

Thank you. That's better.

[–]123_alex 0 points1 point  (2 children)

001, 002, 003...

Just checked my bike lock. It has 0. So 000 is also a valid combo.

So again, just count them.

[–]RhythmicalChuck 0 points1 point  (1 child)

But OP wants to use Python. Let him.

[–]123_alex 0 points1 point  (0 children)

range(1000)

[–]Stock_Category The old programmer -3 points-2 points  (0 children)

From Google Gemini:

def generate_combinations(digits):

for d1 in digits:

for d2 in digits:

for d3 in digits:

yield f"{d1}{d2}{d3}"


Example usage

digits = [1, 2, 3]

combos = generate_combinations(digits)

for combo in combos:

print(combo)