python - Customized number system -
input: set of numbers , redefined relationship of digits 0-9 in ascending order.
goal: based on redefined relationship, set of numbers must listed in ascending order.
input format: first line contain the set of numbers. next line contain digits 0-9 in redefined ascending order.
boundary conditions: size of set of numbers 2 100.
output format: set of numbers in ascending order per redefined order of digits separated space.
example 1:
input: 20 50 11 121 9231476058 output: 50 11 20 121 explanation: 121 3 digit number , hence comes first. per redfeined order 2 > 1 > 5. 121 greater others , comes in end. 20 > 11 > 50 , hence in ascending order reversed.
example 2:
input: 319 311 198 420 1948327605 output: 319 311 420 198 explanation: per redfeined order 1 > 4 > 3 among 319 , 311, 1 > 9 hence final ascending order 319 311 420 198.
my solution far : thought of adding first line of inputs list x , zip customization ordering list.
x=[319,311,198,420] y=[1,9,4,8,3,2,7,6,0,5] l=[x (y,x) in sorted(zip(y,x))] print l l produces output : [319,198,420,311]. whereas, expected output : [319,311,420,198].
either approach totally wrong or there fine-tuning required.
please help.
how about:
>>> x = [319,311,198,420] >>> y = [1,9,4,8,3,2,7,6,0,5] >>> d = {str(v): str(i) (i, v) in enumerate(reversed(y))} >>> sorted(x, key=lambda n: int(''.join(d[v] v in str(n)))) [319, 311, 420, 198] >>> this creates look-up dictionary integers given value based on index.
then input sequence sorted value calculated looking value given each integer in dictionary.
Comments
Post a Comment