CS 105 Exam 3

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

What is the output of the code below? Don't forget quotes. def theProblem(): word = "Rhode Island" for i in range(len(word)): if word[i] == "l": return word[i:-1] return "" print(theProblem())

'lan'

What variable names does a function have access to?

(a) Parameters that are passed to the function and any variables created in the function. Any scope has access to variables created in that scope. In addition, functions have access to the parameters it's caller passed it.

What value does the following code print? def f(s, v): x = 0 for l in s: if v in l: x += 1 print(x) f(['platypus', 'lion', 'llama'], 'o')

1

A Python while implements a definite loop.

False

The file on disk is updated immediately when a file.write operation is executed in Python.

False Disk writes are very slow compared to the time to execute most Python statements. To get good performance, Pythonbufferswrites, meaning that values to write to files are stored in memory and then periodically copied to files as a group. You can force writes to occur by either using theflush() function orclose()ing the file.

def f(x): for val in x: print(val)

Print every element of a list, each on their own line. print list contents, one element per line output each element of a list on their own line

def f(x): for val in range(x, 0, -1): print(val)

Prints a countdown from a given number down to 1 outputs the integers from x to 1, each on their own line. prints natural numbers descending from x

def f(x, y): for i in range(len(x)): x[i] = y

Replace all elements of a given list with a given value set all entries of list x to be the value y substitute y as the value for every element of x

def f(x, y): z = 0 for val in range(x, y + 1): z += val return z

Returns the sums of integers from x to y, inclusive. Adds up the integers starting at x and going up to y. Counts from one number to another, and returns the sum of those numbers

Computers are universal processing machines.

True

In HTML, elements can be nested inside other elements.

True

In Python, the all() function returns a Boolean (e.g., either True or False).

True

What is the value of the variable x after the following code executes? x = 'Lorem ipsum dolor sit amet.'.split()

['Lorem', 'ipsum', 'dolor', 'sit', 'amet.']

input a letter for string v such that the code prints 0 after the code executes.v = def f(s, v): x = 0 for l in s: if v in l: x += 1 print(x) f(['hedgehog'], v)

any letter not in hedgehog 'a' works

Input a value for the variable x such that the output of the code below is: "imm" x = word = "Persimmon" if (x < 90): print(word[4:7]) elif (x > 90 and x < 100): print(word[-4:-3]) else: print(word[5:-3])

anything less than 90 89 works

Write a statement that takes a variable named file_obj that contains a file object and reads the next 21 characters into a variable named command.

command = file_obj.read(21)

The function below takes a single parameter, a list of numbers called number_list. Complete the function to return a string of the provided numbers as a series of comma separate values (CSV). For example, if the function was provided the argument [22, 33, 44], the function should return '22,33,44'. Hint: in order to use the join function you need to first convert the numbers into strings, which you can do by looping over the number list to create a new list (via append) of strings of each number.

def convert_to_csv(number_list): l = [] for num in number_list: l.append(str(num)) return(','.join(l))

The function below takes one parameter: a list of integers (my_list). Complete the function to count how many of the numbers in the list are even. As discussed in lecture, this question uses the 'visit every element of a collection' pattern, the 'is a number even' pattern, and the 'count' pattern.

def count_even_integers(num_list): even_count = 0 for num in num_list: if num % 2 == 0: even_count += 1 return even_count

Define a function below, even_number_count, which takes a single parameter: a list of integers. Complete the function to count how many of the numbers in the list are even.

def even_number_count(num_list): even_count = 0 for num in num_list: if num % 2 == 0: even_count += 1 return even_count

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 any strings and only numbers (i.e. ints or floats) which are not multiples of the number argument. For example, if the input list is ["Spartacus", 4.0, 5, 2] and input number is 2, then the output should be ["Spartacus", 5]. It is okay to return an empty list if the filter keeps no elements.

def filter_list(num, lst): new_list = [ for var in lst: if type(var) == int and (var % num == 0): new_list.append(var) elif type(var) == float and (var % num == 0.0): new_list.append(var) return new_list ---- DOES NOT WORK

The function below takes one parameter: a string called uid_string that contains a series of user IDs in Comma Separated Values (CSV) format. Complete the function to return a CSV of their illinois email addresses instead. For example, given "sample5, test3", your function should return "[email protected], [email protected]".

def filter_negatives(info_list): new = [] for num in info_list: if num < 0: new.append(num) return newdef filter_negatives(info_list): new = [] for num in info_list: if num < 0: new.append(num) return new

Define a function below, get_netid, which takes a string argument (an email address) as an argument. Email addresses will be passed in using the format '[email protected]'. Complete the function to return the netid part of the email address (i.e., the part before the @ symbol) as a string. For example, your code should return 'xxxx' if given the email address above. Hint: to find the end point for slicing either use the findfunction or use negative indexing.

def get_netid(email_address): get_netid = email_address[:-13] return get_netid

Write a statement that takes a variable named file_output that contains a writeable file object and writes into it the contents of the variable value.

file_output.write(value)

A function can modify the value of a parameter only if it's

mutable

Write a Python function called print_nested_list that takes a single parameter, a list of lists of strings. Complete the function as follows: Each list of strings should be printed with a single space between each string, all on the same line Each list of strings should be printed on its own line Your code shouldn't return a value. For example, the list[["Test"], ["Example", "Online"]should print: Test Example Online

def print_nested_list(lst): for sub_list in lst: print(*sub_list) lst = [["Test"], ["Example", "Online"]] print_nested_list(lst)

Write a statement that creates a single string from an existing list of strings called state_strings by inserting the string ' and ' between every pair of strings in the list. The resulting string should be put into a variable called output_str.

output_str = ' and '.join(state_strings)

The function below takes one parameter: a list of numbers (num_list). Complete the function to return a new list containing only the negative numbers from the original list. For this question, you'll first want to create an empty list. Then, you'll want to visit every element of the original list checking to see if it is negative. For the negative elements, you'll append them to your new list. Finally, return the new list.

def select_negative_numbers(num_list): new_list = [] for i in num_list: if i <0: new_list.append(i) return new_list

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): total = 0 for x in input_list: if x % 2 == 0: total += x return total

If you have a list consisting of just numbers, then you can add all of the values in the list using the sum()function. If your list consists of some numbers and some values of other types (e.g., lists, strings, sets), the sum()function will fail. In this question, we're asking you to write a function that will sum all of the numbers in a list, ignoring the non-numbers. The function below takes one parameter: a list of values (value_list) of various types. The recommended approach for this: (1) create a variable to hold the current sum and initialize it to zero, (2) use a for loop to process each element of the list, (3) test each element to see if it is an integer or a float, and, if so, add its value to the current sum, (4) return the sum at the end.

def sum_lengths(value_list): total = 0 for n in value_list: if type(n) == int or type(n) == float: total += n return total

Write a function called sum_up_to_n that takes a single positive integer parameter. The function should also return a positive integer. In particular, it should return the sum of the positive integers from 1 to the value passed into the function. For example, if 5 is passed into the function, it should return 15 because that is the number you get when you add 1 + 2 + 3 + 4 + 5.

def sum_up_to_n(num): sum = 0 for num in range(0, num + 1) sum +=num return sum

Write a statement that sorts in reverse order (from largest to smallest) the list responses in place.

responses.sort(reverse = True)

Which of the following would return the characters from string s without the first and last characters?

s[1:-1]

Write a statement that slices 10 characters of the string headline starting 8 characters from the beginning. Put this value into a variable named selection. For example, if the headline variable is bound to the string 'abcdefghijklmnopqrstuv', then selectionshould be assigned the value 'ijklmnopqr'.

selection = headline[8:18]

Write a statement that slices a substring out of the string quote and puts it into a variable named selection. If given the string 'Dreaming, after all, is a form of planning.', selection should contain 'after all, i' after your statement executes.

selection = quote[10:22]

Write a statement that inserts the value 'exercise' into the list todo so that it becomes the element at index 7.

todo.insert(7, 'exercise')


संबंधित स्टडी सेट्स

Module 6: Business Ethics and Corporate Social Responsibility

View Set

Module/Unit tests/Pre-A - Project Management C722

View Set

POLS 4750 EXAM 2- Intersectionality & Group Conflict

View Set

Section U: India Between 1905-1915

View Set