Question

In: Computer Science

Python Q1. def silly(stuff, more_stuff=None): data = [] for i, thing in enumerate(stuff): if thing >=...

Python
Q1.
def silly(stuff, more_stuff=None):
    data = []
    for i, thing in enumerate(stuff):
        if thing >= 0 or i < 2:
            data.append(thing + i)
    print(len(stuff))
    if more_stuff is not None:
        data += more_stuff
    print(data)

Write a Python statement that calls silly and results in the output (from within silly):

4
[5, 3, 1, 5, -3]

stuff = [5, 2, 0, 2]
more_stuff = [-3]
silly(stuff, more_stuff)

Q2.

Function silly2 is defined as:

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")

Write a single Python statement that calls silly2 and results in the output:

2 2
We're dead, Fred

Solutions

Expert Solution

1)

CODE

def silly(stuff, more_stuff=None):

data = []

for i, thing in enumerate(stuff):

if thing >= 0 or i < 2:

data.append(thing + i)

print(len(stuff))

if more_stuff is not None:

data += more_stuff

print(data)

stuff = [5, 2, 0, 2]

more_stuff = [-3]

silly(stuff, more_stuff)


2)

CODE

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")

nums = [1, 2]

indices = [0, 1]

silly2(nums, indices, -3)


Related Solutions

class Board: def __init__(self): self.pieces = [[None for i in range(8)] for j in range(8)] def...
class Board: def __init__(self): self.pieces = [[None for i in range(8)] for j in range(8)] def __str__(self): s = "" for j in range(7, -1, -1): #for each row for i in range(8): # for each column if self.pieces[j][i] is None: # if self.pieces[j][i] is None s += "." # populate the row with '.' val for each column else: s += self.pieces [j][i].get_symbol() s += "\n" #after each row add a new line return s # return after iterating...
PYTHON def find_treasure(mapfile): with open (mapfile, 'r') as f: data = f.readlines() for i, row in...
PYTHON def find_treasure(mapfile): with open (mapfile, 'r') as f: data = f.readlines() for i, row in enumerate(data): for j, column in enumerate(row): if 'S' in column: print((i,j)) return (i,j) #This is what mapfile contains    #AAASAAA #AASSSAA #AAASAAA #Need to find the coordinate of the middle S
Can you convert this code (Python) to C++ def decrypt(message, key): decryptedText = "" for i...
Can you convert this code (Python) to C++ def decrypt(message, key): decryptedText = "" for i in message: M = ord(i) k = int(key, 16) xor = M ^ k decryptedText += chr(xor) return decryptedText def encrypt(message, key): encryptedText = "" for i in message: M = ord(i) k = int(key, 16) xor = M ^ k encryptedText += chr(xor) return encryptedText # main function userText = input("Enter text: ") userKey = str(input("Enter a key: ")) encryptedMessage = encrypt(userText, userKey)...
PYTHON CODE: def square_matrix_multiplication(matrix1,matrix2): C=[[0 for i in range(len(matrix1))] for i in range(len(matrix2))] for i in...
PYTHON CODE: def square_matrix_multiplication(matrix1,matrix2): C=[[0 for i in range(len(matrix1))] for i in range(len(matrix2))] for i in range(len(matrix1)): for j in range(len(matrix2[0])): C[i][j]=0 for k in range(len(matrix2)): C[i][j] += matrix1[i][k]*matrix2[k][j]    return C I use that function in my code. When I type like "return C", It does not work. If I type print(C), then my code works. Why is it happening? I created two user-entered matrices above the function, then I called the function.
I need to write these three functions in Python using turtle module. def line(t, coord1, coord2)...
I need to write these three functions in Python using turtle module. def line(t, coord1, coord2) t is a turtle object passed in coord1 and coord2 are 2-element lists that represent x, y coordinates draws a line from coord1 to cord2 def poly(t, *coords) t is a turtle object passed in *coords is any number of x, y coordinates each as a 2-element list draws a polygon based on coords (will close off polygon by drawing a line from last...
Please I seek assistance Python Programing import os import numpy as np def generate_assignment_data(expected_grade_file_path, std_dev, output_file_path):...
Please I seek assistance Python Programing import os import numpy as np def generate_assignment_data(expected_grade_file_path, std_dev, output_file_path): """ Retrieve list of students and their expected grade from file, generate a sampled test grade for each student drawn from a Gaussian distribution defined by the student expected grade as mean, and the given standard deviation. If the sample is higher than 100, re-sample. If the sample is lower than 0 or 5 standard deviations below mean, re-sample Write the list of student...
How would I setup this dictionary for Python 3? class Student(object): def __init__(self, id, firstName, lastName,...
How would I setup this dictionary for Python 3? class Student(object): def __init__(self, id, firstName, lastName, courses = None): The “id”, “firstName” and “lastName” parameters are to be directly assigned to member variables (ie: self.id = id) The “courses” parameter is handled differently. If it is None, assign dict() to self.courses, otherwise assign courses directly to the member variable. Note: The “courses” dictionary contains key/value pairs where the key is a string that is the course number (like “course1”) and...
Given the python code below, show output: class MyClass: i = ["1010"] def f(self, name): self.i.append(str("470570"))...
Given the python code below, show output: class MyClass: i = ["1010"] def f(self, name): self.i.append(str("470570")) self.name = name return 'CPS' if __name__ == "__main__": x = MyClass()    print(x.f("U"))    print(x.name)    print(x.i)       y = MyClass()   print(y.f("D"))    print(y.name)   print(y.i)
Python Coding Question (Data Science) Hello I am having difficulty writing a code in python to...
Python Coding Question (Data Science) Hello I am having difficulty writing a code in python to do a specific task. I have two text files, Positive.txt and Practice_forhw1.txt. I want to write a script that will check if any words in Practice_forhw1.txt match any of the words in Positive.txt then the word would get replaced with a "+1" in the Practice_forhw1.txt and then print out the Practice_forhw1.txt.  (i.e if the word "happy" is in both Positive.txt and Practice_forhw1.txt then I want...
This is in Python, I am trying to create a new window to print the data...
This is in Python, I am trying to create a new window to print the data to screen, it prints fine in the terminal. I know this is a small segment of my code and its the only portion I am having a problem with. I was wondering if anyone can see what my problem may be. The current issue is NameError: name 'fltVal' is not defined and AttributeError: 'PressureGUI' object has no attribute 'r_count' Any help would be appreciated...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT