how to generate a list of 3-digit numbers?

蹲街弑〆低调 提交于 2019-12-24 20:16:06

问题


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))

回答1:


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]



回答2:


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!




回答3:


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



回答4:



## 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



回答5:


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]


来源:https://stackoverflow.com/questions/48769342/how-to-generate-a-list-of-3-digit-numbers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!