def lambda_planets_aggregate_by_population(): """Lambda function #5.""" data = get_planets_data() known_population = list( filter(lambda a: a['population'] is not 'unknown', data)) unknown_population = list( filter(lambda a: a['population'] is 'unknown', data)) planet_with_max_population = max(known_population, key=lambda a: int(a['population'])) max_population = int(planet_with_max_population['population']) x = y = 1 while x < max_population: x *= 10 y += 1 aggregate_population_ranges = [{'from': 0, 'to': 10}] l = 10 for i in range(y - 1): k = l l = 10**(i + 2) r = {'from': k, 'to': l} aggregate_population_ranges.append(r) aggregate_planets_by_population = {} for rg in aggregate_population_ranges: aggregate_planets_by_population[str(rg['from']) + '-' + str( rg['to'])] = list( filter(lambda a: rg['from'] <= int(a['population']) < rg['to'], known_population)) aggregate_planets_by_population['unknown'] = unknown_population pp(aggregate_planets_by_population)
def lambda_planets_rotation_period(): """Lambda function #1.""" data = get_planets_data() sorted_by_rotation_period = sorted(data, key=lambda a: a['rotation_period'], reverse=False) pp(sorted_by_rotation_period)
def lambda_planets_diameter_to_orbital_period(): """Lambda function #6.""" data = get_planets_data() current_data = list( filter( lambda a: a['diameter'] is not 'unknown' and a['diameter'] is not '0' and a['orbital_period'] is not 'unknown' and a['orbital_period'] is not '0', data)) min_planet = min(current_data, key=lambda a: float(a['diameter'])/float(a['orbital_period'])) print('PLANET') print('-' * 30) pp(min_planet)
def lambda_planets_orbital_period(): """Lambda function #3.""" data = get_planets_data() current_data = list( filter( lambda a: a['surface_water'] is not 'unknown' and a[ 'orbital_period'] is not 'unknown', data)) min_orbital_period = min(current_data, key=lambda a: a['orbital_period']) max_orbital_period = max(current_data, key=lambda a: a['orbital_period']) print('MIN ORBITAL PERIOD') print('-' * 30) pp(min_orbital_period) print('MAX ORBITAL PERIOD') print('-' * 30) pp(max_orbital_period)
def lambda_planets_smallest_and_biggest(): """Lambda function #4.""" data = get_planets_data() current_data = list( filter( lambda a: a['diameter'] is not 'unknown' and a['diameter'] is not '0', data)) smallest_planet = min(current_data, key=lambda a: 4 * math.pi * (float(a['diameter']) / 2)**2) biggest_planet = max(current_data, key=lambda a: 4 * math.pi * (float(a['diameter']) / 2)**2) print('SMALLEST PLANET') print('-' * 30) pp(smallest_planet) print('BIGGEST PLANET') print('-' * 30) pp(biggest_planet)
def lambda_planets_no_water(): """Lambda function #2.""" data = get_planets_data() no_water = list(filter(lambda a: a['surface_water'] == '0', data)) pp(no_water)