Posts

Showing posts with the label program excercise

Program excercise : Write a function that, using the Star Wars API (swapi.co), takes a character name, and returns a list with the names of the films that character was in.

import requests, json def get_film_details_by_url(film_url):     resp = requests.get(film_url)     return_dict = None     if resp.status_code == 200:         return_dict = json.loads(resp.text)     return return_dict       def get_film_details_by_char_name(char_name):     swapi_base_url = "https://swapi.co/api/"     get_people_url = swapi_base_url+"people"     film_url_array = []     film_name_array = []     to_continue = True     while to_continue:         resp = requests.get(get_people_url)         if resp.status_code == 200:             resp_dict =  json.loads(resp.text)             for ppl in resp_dict["results"]:                 if ppl["name"].lower() == char_name.lower():   ...

Program excercise : Write a function that takes two lists of strings as input, and returns a single list of unique strings common to both lists.

def common_strings(str_list1, str_list2):     return [str_value for str_value in str_list1 if str_value in str_list2] Another way, def get_common_strs(list1,list2):     return list(set(list1).intersection(set(list2)))

Program excercise : Write a function that takes a dictionary with numeric values and returns the keys of the top 3 highest values.

def get_top3_values_indices(input_dict, col_range=3):     all_values = list(set(input_dict.values()))     all_values.sort(reverse=True)     top_values = all_values[:col_range]     return [k for k in input_dict.keys() if input_dict[k] in top_values]