Digit Conversion Function X27470


Statement
 

pdf   zip   main.py

html

Write a function conv_dig(test, conv) that returns a digit conversion function. The returned function takes a positive integer n and returns a new integer where each digit d of n that passes the test (test(d) == True) is replaced by its conversion conv(d). Digits of n that do not pass the test are to be left unchanged by the digit conversion function.

Input

Output

Observation

Let us insist that the digit conversion function described is not the required conv_dig proper, but its output.

Sample session
>>> conv_dig_func = conv_dig(lambda d: d % 2 == 0, lambda d: d // 2)
>>> conv_dig_func(21098)
11094
>>> conv_dig_func(1032547698)
1031527394
>>> conv_dig_func = conv_dig(lambda d: d < 5, lambda d: 2)
>>> conv_dig_func(1064592)
2262592
>>> conv_dig_func(9876543210)
9876522222
Information
Author
Jordi Delgado and José Luis Balcázar
Language
English
Official solutions
Python
User solutions
Python