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...
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...
Python programming problem! Suppose that there is a json file called data from the desktop. I...
Python programming problem! Suppose that there is a json file called data from the desktop. I want to print the value with the key of "year" and "country". Json file below: { "A":{ "year":11, "grade":A, "country":America}, "B":{ "year":18, "grade":C, "country":England}, "C":{ "year":19, "grade":B, "country":China},} I want a code that I can only replace the name of key from year to grade and the code could be work.
Python I am creating a program that allows a user to extract data from a .csv...
Python I am creating a program that allows a user to extract data from a .csv file and print the statistics of a certain column in that file. The statistics include Count, Mean, Standard Deviation, Min, and Max. Here is the code I have so far: import csv import json class POP: """ Extract the data """ def __init__(self, line): self.data = line # get elements self.id = self.data[0].strip() self.geography = self.data[1].strip() self.targetGeoId = self.data[2].strip() self.targetGeoId2 = self.data[3].strip() self.popApr1 =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT