What is the time and space complexity of this algorithm and can it be improved (how)? It generates random 4 digit even number and the adjacent 2 digits must be different.

def random_digits():
    import random
    num = ''
    for i in range(0, 4):
        curr = random.randint(0,9)
        print('curr', curr)
        if (i != (i+1)):
            if (i == 3 and (curr % 2 != 0)):
                curr = curr - 1
            num += str(curr)

closed as unclear what you're asking by πάντα ῥεῖ, Graipher, alecxe, 200_success Jun 6 '17 at 21:37

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    \$\begingroup\$ I don't know python, but what is the meaning of if (i != (i+1))? It looks like you're wondering if 3!=4, which is always true. I don't see the 'adjacent 2 digits must be different' part anywhere. \$\endgroup\$ – oerkelens Jun 6 '17 at 19:12
  • 2
    \$\begingroup\$ This question has been downvoted and closed for multiple reasons: (a) possibly broken code, (b) primarily seeking an explanation of code, (c) unclear what you are asking. \$\endgroup\$ – 200_success Jun 6 '17 at 21:38

Not an answer regarding your question:

  • Imports should be at the top;

  • You don't need to import all of random just for randint: Use from random import randint

  • You don't need parentheses in if (i != (i+1)) and if (i == 3 and (curr % 2 != 0))

  • Instead of curr = curr - 1, use curr -= 1

  • If range() is called with a single parameter n, the first value will automatically be 0, so there's no need to explicitly call range(0, n).


Not the answer you're looking for? Browse other questions tagged or ask your own question.