CS 105 Final(coding Questions)

¡Supera tus tareas y exámenes ahora con Quizwiz!

Create a Sentence class from scratch. Objects of your class should have a data attribute called value. Your class should have an __init__ method that takes a parameter (in addition to self) that lets the value attribute to be set. The default value for value is the empty string.

class Sentence: def __init__(self,value=''): self.value = value

Below is a working start to a Suitcase class that provides a default constructor. Extend the class to include an insert_item method that takes two parameters: the name of an item and a weight (in kilograms) for that item. Your method should add the name of the item to the list of items in the Suitcase and add the weight to the total weight of the items.

class Suitcase: def __init__(self): self.items = [] self.current_weight_in_kg = 0.0 # a float def insert_item(self,name,weight): self.items.append(name) self.current_weight_in_kg += weight

The function below takes one parameter: (exam_score) containing an integer from 0 to 100. Complete the function to return a single capital letter ('A', 'B', 'C', 'D', or 'F'). The letter you return is a function of the parameter as indicated by the table below. A = 90 - 100 B = 80 - 89 C = 70 - 79 D = 60 - 69 F = 0 -59

def assign_grade(exam_score): if (exam_score >= 0) and (exam_score <= 59): return 'F' if (exam_score >= 60) and (exam_score <= 69): return 'D' if (exam_score >= 70) and (exam_score <= 79): return 'C' if (exam_score >= 80) and (exam_score <= 89): return 'B' if (exam_score >= 90) and (exam_score <= 100): return 'A'

The function below takes a single string parameter called sentence. Your function should return True if the sentence contains at least one copy of each of the following vowels: a, e, i, o, and u. Otherwise, return False. The vowels can be either upper case or lower case.

def contains_all_values(sentence): a=0 e=0 I=0 o=0 u=0 for i in sentence: if((i=='a' or i=='A')): a=a+1; elif((i=='e' or i=='E')): e=e+1; elif((i=='i' or i=='I')): i=I+1; elif((i=='o' or i=='O')): o=o+1; elif((i=='u' or i=='U')): u=u+1; if(a!=0 and e!=0 and I!=0 and o!=0 and u!=0): return True else: return False

Define a function below, count_string_vals, which takes a dictionary as a parameter. The dictionary has keys that are strings but can have any kind of value associated with its keys. Complete the function to return a count of how many string values are in the dictionary.

def count_string_vals(dict): count = 0 for key in dict: if type(dict[key]) == str: count += 1 return count

The function below takes two string parameters: sentence is a string containing a series of words separated by whitespace and letter is a string containing a single lower case letter. Complete the function to return a string containing one of the words in sentence that contains letter (in either upper case or lower case). Your code should return the word with its capitalization in the original sentence. If there are multiple words in sentence that contain letter, you can return any of them.

def extract_word_with_given_letter(sentence, letter): x = sentence.split() y = [] z = [] for val in x: if letter.lower() in val.lower(): return val

Define a new function below, filter_list, which takes two arguments: a list and a number. The list can contain numbers and strings. Complete the function to return only numbers (i.e. ints or floats) which are multiples of the number argument. For example, if the input list is [4, 6, "hello", 2.5] and input number is 2, then the output should be [4, 6]. It is okay to return an empty list if the filter keeps no elements.

def filter_list(List,number): l=[] for i in List: if(isinstance(i,int)): if(i%number == 0): l.append(i) return l

The function below takes a list of strings, shopping_cart, and a dictionary with strings as keys and floating points as values, price_lookup, as parameters. Use a loop to iterate over the values in shopping cart to lookup the prices in price_lookup and append them to a new list. Finally, return the new list as if it were a receipt for all the items in the shopping cart.

def get_receipt(shopping_cart, price_lookup): list = [] for values in shopping_cart: list.append(price_lookup.get(values)) return list

The function below takes a string argument sentence. Complete the function to return the second word of the sentence. You can assume that the words of the sentence are separated by whitespace and that there are at least two words in the sentence. You don't need to worry about removing any punctuation.

def return_second_word(sentence): p1 = sentence.split() return p1[1]

The function below takes an argument: a list, input_list. Add all even numbers from the input_list and return that sum

def sum_even_nums(input_list): sum = 0 for num in range(len(input_list)): if (input_list[num] % 2) == 0: sum = sum + input_list[num] return sum

The following function definition takes a single parameter, filename, which contains a path to a TSV (tab separated value) file that contains rows of integers. Complete the function such that it returns a list where the value in the returned list corresponds to the sum of the row in the TSV. Here is an example TSV file: file1.tsv

def tsv_sum_rows(filename): try: f = open(filename) result = [] for line in f: data = line.strip().split() total = 0 for value in data: total += int(value) result.append(total) f.close() return result except IOError: return []

Define a function below called increase_elements_by_x, which takes two arguments - a list of numbers and a single positive number (you might want to call it x). Complete the function such that it returns a copy of the original list where every value is increased by the second argument. For example, given the inputs [1,2,5] and 2, your function should return [3,4,7].

def increase_elements_by_x(nums, x): new_nums = [] for n in nums: new_nums.append(n+x) return new_nums

Your friend just came back from a weekend hike and they excitedly gave you a Python list of birds they saw on their trip. Unfortunately, they were not careful about their notes, having things such as "sparrow" and "SpARROW" written down originally. They want you to write a function which takes this disorganized list of bird names as an input and returns just a list of the unique bird species they saw. Please make all the strings lower case.

def unique_bird_log(exciting_birds): res = [] for x in exciting_birds: if x.lower() not in res: res.append(x.lower()) return res


Conjuntos de estudio relacionados

CH 27 Peds-The child with a condition of the blood.....

View Set

Test 3: Bell's Palsy & Trigeminal Neuralgia

View Set

Aggregate Demand an Aggregate supply

View Set

Computerized Office Final (Study Guide)

View Set

Money and Banking Chapter 15: Tools of Monetary Policy

View Set

Emmanuel Saez and Gabriel Zucman, 2016, "Wealth Inequality in the United States since 1913: Evidence from Capitalized Income Tax Data"

View Set