-
-
Save bbookman/1a98da47a1a6d1ceffdd3bae7be36abe to your computer and use it in GitHub Desktop.
''' | |
Find all of the numbers from 1-1000 that have a 3 in them | |
''' | |
three = [n for n in range(0,1000) if '3' in str(n)] | |
print(three) |
These 3 options work:
x = [i for i in range(1, 1001) if str(i).find("3") != -1]
y = [i for i in range(1, 1001) if str(i).count("3") > 0]
z = [i for i in range(1, 1001) if '3' in str(i)]
print("x ->", x, len(x), "\n")
print("y ->", y, len(y), "\n")
print("z ->", z, len(z))
You could also do it this way:
lst_include3=list(filter(lambda x: '3' in str(x), range(1,1000)))
print(lst_include3)
solution = [a for a in range(1, 1000+1) if '3' in str(a)]
num_3 = [num for num in range(1001) if '3' in str(num)]
print(num_3)
r=[x for x in range(1,1001) if x%10==3 or (x//10)%10==3 or (x//100)%10==3]
print(r)
list=[i for i in range(1,1001) if '3' in str(i) ]
num = print([n for n in range(1,1001) if '3' in str(n)]
Output: [3, 13, 23, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 43, 53, 63, 73, 83, 93, 103, 113, 123, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 143, 153, 163, 173, 183, 193, 203, 213, 223, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 243, 253, 263, 273, 283, 293, 303, 313, 323, 330, 331, 332, 333, 334, 335, 336, 337, 338, 339, 343, 353, 363, 373, 383, 393, 403, 413, 423, 433, 443, 453, 463, 473, 483, 493, 503, 513, 523, 533, 543, 553, 563, 573, 583, 593, 603, 613, 623, 633, 643, 653, 663, 673, 683, 693, 703, 713, 723, 733, 743, 753, 763, 773, 783, 793, 803, 813, 823, 833, 843, 853, 863, 873, 883, 893, 903, 913, 923, 933, 943, 953, 963, 973, 983, 993]
l = [x for x in range(1001) if '3' in str(x)]
print(l)
I think with the string option is better because is the simplest and that's the more legible solution, instead of you can write several conditions to get it without using strings. However, strings are a tool to use to and our goal should be make it easy.