Write a single Python statement that calls silly2 and results in the output: 2 2 We're dead, Fred
def silly2(nums, indices, extra=None):
print(len(nums), len(indices))
new_indices = []
for i in indices:
if i >= 0 and i < len(nums):
new_indices.append(i)
if extra:
new_indices.append(extra)
try:
for index in new_indices:
if index < len(nums):
print(nums[index])
except IndexError:
print("We're dead, Fred")
silly2([1, 2], [0, -1])
Answer:
silly2( [1,2] , [-2, -1] , -3 )
Explanation:
Since we need 2 2 at the output and we can find a statement print(len(nums), len(indices)), it is obvious that the size/length of both list we pass to the function should be 2 such that len(nums) = 2 and len(indices) = 2 .
Now the question is what should be the nums list and indices list.
First consider nums list. Since we are not using any values in nums list, it doesn't matter which values we give to that list. (Note that there is a print statement which prints the values of nums, but we don't want that statement to execute as per given output. That print statement is controlled by other aspects which we will consider below)
Now what should be the indices list.
For this, we should have a greater understanding of what the program does.
The program basically selects some indices from the indices list such that these indices are less than the size/length of nums list and greater than 0. (Alternatively we are selecting some valid indices for nums list).
These indices are then added to a new list( new_indices ). An extra is also added if present. The program then prints the values present in nums list at valid positions specified by new_indices. (ie.., for each index value in new_indices , the program print value present index position of nums list if index is less than size of nums list)
Now, from output specification, we can find that we don't want to print any values of nums list, but wants an exception Indexerror which happens when a invalid index is tried with a list. ie.., the new_indices must not contain any valid indices for nums list and must contain at least one invalid index such that it is less that size/length of nums. Now since, when adding elements from from indices to new_indices, we are checking both conditions (i>=0 and i< len(nums) ), only valid indices will get added to new_indices. But we don't want that (we don't want to print any values in numslist). Therefore indices must contains two elements such that the are not valid (either less than 0 or greater than or equal to length of nums) for nums list. Therefore -2,-1 which are less than 0.
Now the new_indices must contain an invalid index which is less than length of nums list. The only way of adding this is through extra. Instead of taking default value for extra, we need to give some value that satisfies our condition. So which value for extra.
We can give any invalid index value less than length of nums list. Since python allows to index using negative number (eg -1 for last element, -2 for second last element ) we need to give to give a negative number such that nums list gets out of bound. ie..., a negative value less than negative of number of elements in nums list ( < - len(nums) ) and hence choose -3.
Now -3 will be added to new_indices and since it is less than size/length of nums list, it will try to print nums[-3] which results in a index error and it will output: We're dead, Fred. Therefore after combining previous output, it will print 2 2 We're dead, Fred.
Write a single Python statement that calls silly2 and results in the output: 2 2 We're...
What are the outputs for each question PYTHON Question 6 bools = [True, 0>-1, not not True, 2%2 == 0, True and False] index = 0 while bools[index]: index += 1 print(index) a) 0 b) 1 c) 2 d) 3 e) none of the above Question 7 for i in range(2): for j in range(1): print(i, j, end = " ") a) no output b) 0 0 1 0 2 0 c) 0 0 0 1 1 0 1 1...
aList = [1, 'Mercy', 20, 'Cyber', 300]. a) Write a single line of Python statement to print the value 20 from aList. b) Write a single line of Python statement to print the character b from aList. c) Write a single line of Python statement to print erc from aList. d) Write a Python segment that print a sequence number starting from 1 followed by ")" and an element for each element of the list. Use a for loop and...
need help on all parts please and thank you
(e) What is the correct output of this code? (2 pts) >>> def func(x): >>> res = 0 >>> for i in range (len (x)): res i >>> return res >>> print (func(4)) ( 4 5 ) 6 ()7 ( What could be described as an immutable list? (2 pts) () a dimple ( ) a tuple ( ) a truffle () a dictionary (g) What is the result of the...
Write a python program write a function numDigits(num) which counts the digits in int num. Answer code is given in the Answer tab (and starting code), which converts num to a str, then uses the len() function to count and return the number of digits. Note that this does NOT work correctly for num < 0. (Try it and see.) Fix this in two different ways, by creating new functions numDigits2(num) and numDigits3(num) that each return the correct number of...
Starting out with Python 4th edition I need help I followed a tutorial and have messed up also how does one include IOError and IndexError exception handling? I'm getting errors: Traceback (most recent call last): File, line 42, in <module> main() File , line 37, in main winning_times = years_won(user_winning_team_name, winning_teams_list) File , line 17, in years_won for winning_team_Index in range(len(list_of_teams)): TypeError: object of type '_io.TextIOWrapper' has no len() Write program champions.py to solve problem 7.10. Include IOError and IndexError...
Python: P2 Write a function that reverses a python list using recursion (Chapter 6-3 in our textbook) E.g.: Input: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Constraints: - List elements are integers - The function should return a reversed array, not print its elements You may use the following code as template: def reverse(my_list, index = None): # your code here
IN PYTHON ''' Helper func 3: checksum_ISBN Input: a list with 9 single digit number in it (first 9 digit in ISBN) Output: print out: "The correct checksum digit is:__. Now we have a legit ISBN: _____" Hint: just loop through 0-9, test every one with helper func1 to find out the one checksum that forms a legit ISBN with the correct ISBN in lis (10 numbers), call helper func2 to format it correctly. Then print the final result. '''...
Python Program Only: Write the function definition only of the "get(self, index)" function. Plug your method in the code file shared with you and test it in the main to get the element at index 3 of mylist2 . class Node: def __init__(self, e): self.element = e self.next = None class LinkedList: def __init__(self): self.head = None self.tail = None self.size = 0 def addFirst(self, e): newNode = Node(e) newNode.next = self.head self.head = newNode self.size = self.size + 1...
PYTHON. Continues off another code. I don't understand this. Someone please help! Comment the lines please so I can understand LinkedList ADT: class myLinkedList: def __init__(self): self.__head = None self.__tail = None self.__size = 0 def insert(self, i, data): if self.isEmpty(): self.__head = listNode(data) self.__tail = self.__head elif i <= 0: self.__head = listNode(data, self.__head) elif i >= self.__size: self.__tail.setNext(listNode(data)) self.__tail = self.__tail.getNext() else: current = self.__getIthNode(i - 1) current.setNext(listNode(data,...
Task 4: for Loops (PROGRAM IS IN PYTHON) SHOW INPUT AND OUTPUT SCREEN SHOTS #!/usr/bin/python3 # Measure some strings: words = ['cat', 'window', 'defenestrate'] for w in words: print(w, len(w)) #!/usr/bin/python3 # Measure some strings: words = ['cat', 'window', 'defenestrate'] for w in words: print(w, len(w)) forvar in list(range(5)): print (var) for letter in 'Python': # traversal of a string sequence print ('Current Letter :', letter) print() fruits = ['banana', 'apple', 'mango'] for fruit in fruits: # traversal of List...