<s> import math <EOL> import collections <EOL> import itertools <EOL> import numbers <EOL> infinity = float ( '<STR_LIT>' ) <EOL> infinities = ( infinity , - infinity ) <EOL> def factorial ( x , start = <NUM_LIT:1> ) : <EOL> '''<STR_LIT>''' <EOL> from combi . _python_toolbox import misc_tools <EOL> return misc_tools . general_product ( range ( start , x + <NUM_LIT:1> ) , start = <NUM_LIT:1> ) <EOL> def inverse_factorial ( number , round_up = True ) : <EOL> '''<STR_LIT>''' <EOL> assert number >= <NUM_LIT:0> <EOL> if number == <NUM_LIT:0> : <EOL> return <NUM_LIT:0> <EOL> elif number < <NUM_LIT:1> : <EOL> return int ( round_up ) <EOL> elif number == <NUM_LIT:1> : <EOL> return <NUM_LIT:1> <EOL> else : <EOL> current_number = <NUM_LIT:1> <EOL> for multiplier in itertools . count ( <NUM_LIT:2> ) : <EOL> current_number *= multiplier <EOL> if current_number == number : <EOL> return multiplier <EOL> elif current_number > number : <EOL> return multiplier if round_up else ( multiplier - <NUM_LIT:1> ) <EOL> def from_factoradic ( factoradic_number ) : <EOL> '''<STR_LIT>''' <EOL> from combi . _python_toolbox import sequence_tools <EOL> assert isinstance ( factoradic_number , collections . Iterable ) <EOL> factoradic_number = sequence_tools . ensure_iterable_is_sequence ( factoradic_number ) <EOL> number = <NUM_LIT:0> <EOL> for i , value in enumerate ( reversed ( factoradic_number ) ) : <EOL> assert <NUM_LIT:0> <= value <= i <EOL> number += value * math . factorial ( i ) <EOL> return number <EOL> def to_factoradic ( number , n_digits_pad = <NUM_LIT:0> ) : <EOL> '''<STR_LIT>''' <EOL> assert isinstance ( number , numbers . Integral ) <EOL> assert number >= <NUM_LIT:0> <EOL> assert isinstance ( n_digits_pad , numbers . Integral ) <EOL> n_digits = inverse_factorial ( number , round_up = False ) + <NUM_LIT:1> <EOL> digits = [ None ] * n_digits <EOL> current_number = number <EOL> for i in range ( n_digits ) [ : : - <NUM_LIT:1> ] : <EOL> unit = math . factorial ( i ) <EOL> digits [ n_digits - i - <NUM_LIT:1> ] , current_number = divmod ( current_number , unit ) <EOL> result = tuple ( digits ) <EOL> if ( len ( result ) < n_digits_pad ) : <EOL> return ( ( <NUM_LIT:0> , ) * ( n_digits_pad - len ( result ) ) ) + result <EOL> else : <EOL> return result </s>
