简体   繁体   中英

how to generate a list of 3-digit numbers?

How can I generate a list of 3-digit numbers for which the sum of their digits equal 17?

For example assuming number XYZ , we would have X+Y+Z=17

This is how far I got:

numbers = list(range(1, 1000))

It appears that you want the sum of the digits to be 17 and not the sum of the numbers as the "but I want the first number + the second number + the third number = 17 ?" implies.

So, take a look at this:

result = [x for x in range(100, 1000) if sum(int(y) for y in str(x))==17]
print(result)  # [179, 188, 197, 269, ..., 962, 971, 980]

You can first generate a list of numbers that matches your condition like,

test_list = [value for value in range(100, 1000, 1) if sum(int(a) for a in str(value)) is 17]

So to make this a random list, you can use random package.

random.shuffle(test_list)

So test_list wil be a completely random list with all the possible three digit numbers in it. So this could be the fastest way to generate a random list matching your condition. ( shuffle )

Hope this helps!

Disclaimer: This method will be random and you can have duplicates. If you want an exhaustive list of unique numbers, it will not work, and you might prefer Ev. Kounis' solution.

Here we go, pick 2 digits which sum is between 8 and 17, then calculate the required 3rd one to add up to 17.

The sum of the first 2 digits needs to be:

  • 8 or more because you can have 9 max for the last digit

  • 17 or less because you can't have negative numbers for the last digit

This way:

import random 

def getAnotherNumber():
    d1 = 0
    d2 = 0
    while 17 < d1 + d2 or  d1 + d2 < 8:
        d1 = random.randrange(1, 10)
        d2 = random.randrange(1, 10)
    d3 = 17-d1-d2
    return  100*d1 + 10* d2 + d3

# Make a list of 10 numbers    
list = [getAnotherNumber() for x in range(10)]

print(list)

outputs something like this:

[773, 746, 980, 179, 269, 179, 755, 485, 755, 476]

If you need more comment, and/or if what I've done is not what you asked (somehow), tell me. :)

#   All numbers having 3 digits.
numbers = [x for x in range(100, 1000)]

newList = []
for number in numbers:
    #   Integer to string.
    str_number = str(number)

    #   Separating digits using strings, and casting result to int.
    real_a = int(str_number[0])
    real_b = int(str_number[1])
    real_c = int(str_number[2])

    # Summing, and checking result.
    digitsSum = real_a + real_b + real_c
    if digitsSum == 17:
        newList.append(number)

print newList
## Sum of digits of individual number
def sum_digits(n):
    s = 0
    while n:
        s += n % 10
        n //= 10
    return s

if __name__ == '__main__':

    ## Declare a list
    alist = list()

    ## Iterate from 100 - 1000
    for x in range(100,1000):
        tmp = sum_digits(x)

        ## Add to list once its sum is equal to 17
        if tmp ==  17:
            alist.append(x)

    print alist

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM