From Newsgroup: comp.lang.python
HenHanna <
HenHanna@gmail.com> writes:
Could you share a short, VERY Readable Pythonic code that solves this?
Here is my brute force solution. It prints the list [42] which means
that the 3-digit answer is 042.
def check(n: int) -> bool:
"""return true iff n satisfies all the constraints. n is a 3 digit number
possibly with leading zeros."""
a,b,c = n//100, (n//10)%10, n%10
# 682 -- one number correct and well placed
k1 = (a==6 or b==8 or c==2) and len({a,b,c} & {6,8,2})==1
# 614 -- one number correct but wrongly placed
k2 = a != 6 and b != 1 and c != 4 and \
len({a,b,c} & {6,1,4})==1
# 206 -- two numbers correct but wrongly placed
k3 = len({a,b,c} & {2,0,6})==2 and a!=2 and b!=0 and c!=6
# 738 -- nothing correct
k4 = len({a,b,c} & {7,3,8}) == 0
# 780 -- one number correct but wrongly placed
k5 = len({a,b,c} & {7,8,0})==1 and a!=7 and b!=8 and c!=1
return all([k1,k2,k3,k4,k5])
print(list(filter(check, range(0,1000))))
--- Synchronet 3.20a-Linux NewsLink 1.114