Answer
def solve(n=0, digit=0):
if digit==4:
print(n)
else:
solve(n+1*(10**digit), digit+1)
if digit<=2:
solve(n+5*(10**digit), digit+1)
Work Step by Step
def solve(n=0, digit=0):
if digit==4:
print(n)
else:
solve(n+1*(10**digit), digit+1)
if digit<=2:
solve(n+5*(10**digit), digit+1)
The given algorithm creates the numbers digit by digit. Initially the number is 0, then the all possibilities for first digit is added and function is recursively called to add next digits.