<s> import math <EOL> def percentile ( values , percent ) : <EOL> """<STR_LIT>""" <EOL> if not values : <EOL> return None <EOL> values . sort ( ) <EOL> k = ( len ( values ) - <NUM_LIT:1> ) * percent <EOL> f = math . floor ( k ) <EOL> c = math . ceil ( k ) <EOL> if f == c : <EOL> return values [ int ( k ) ] <EOL> d0 = values [ int ( f ) ] * ( c - k ) <EOL> d1 = values [ int ( c ) ] * ( k - f ) <EOL> return ( d0 + d1 ) <EOL> class GraphZipper ( object ) : <EOL> def __init__ ( self , base_size , zipped_size = <NUM_LIT:1000> ) : <EOL> """<STR_LIT>""" <EOL> self . base_size = base_size <EOL> self . zipped_size = zipped_size <EOL> if self . base_size >= self . zipped_size : <EOL> self . compression_ratio = self . base_size / float ( self . zipped_size ) <EOL> else : <EOL> self . compression_ratio = <NUM_LIT:1> <EOL> self . point_order = <NUM_LIT:0> <EOL> self . cached_ratios_sum = <NUM_LIT:0> <EOL> self . ratio_value_points = [ ] <EOL> self . zipped_graph = [ ] <EOL> def _get_zipped_point ( self ) : <EOL> if self . point_order - self . compression_ratio <= <NUM_LIT:1> : <EOL> order = <NUM_LIT:1> <EOL> elif self . point_order == self . base_size : <EOL> order = self . base_size <EOL> else : <EOL> order = self . point_order - int ( self . compression_ratio / <NUM_LIT> ) <EOL> value = ( <EOL> sum ( p [ <NUM_LIT:0> ] * p [ <NUM_LIT:1> ] for p in self . ratio_value_points ) / <EOL> self . compression_ratio <EOL> ) <EOL> return [ order , value ] <EOL> def add_point ( self , value ) : <EOL> self . point_order += <NUM_LIT:1> <EOL> if self . point_order > self . base_size : <EOL> raise RuntimeError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if not isinstance ( value , ( int , float ) ) : <EOL> value = <NUM_LIT:0> <EOL> if self . compression_ratio <= <NUM_LIT:1> : <EOL> self . zipped_graph . append ( [ self . point_order , value ] ) <EOL> elif self . cached_ratios_sum + <NUM_LIT:1> < self . compression_ratio : <EOL> self . cached_ratios_sum += <NUM_LIT:1> <EOL> self . ratio_value_points . append ( [ <NUM_LIT:1> , value ] ) <EOL> else : <EOL> rest = self . compression_ratio - self . cached_ratios_sum <EOL> self . ratio_value_points . append ( [ rest , value ] ) <EOL> self . zipped_graph . append ( self . _get_zipped_point ( ) ) <EOL> self . ratio_value_points = [ [ <NUM_LIT:1> - rest , value ] ] <EOL> self . cached_ratios_sum = self . ratio_value_points [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> def get_zipped_graph ( self ) : <EOL> return self . zipped_graph </s>
