Python Overiew - Department of Computer Science ...

Python Overiew

1. In the boxes below, fill in the missing code that will make the function definition match its description.

def every_nth_character(s, n): """ (str, int) -> str

Precondition: n > 0

Return a string that contains every nth character from s, starting at index 0.

>>> every_nth_character('Computer Science', 3) 'CpeSee'

"""

result = '' i=0

while

:

result = result + s[i]

i=

return result

2. Complete the examples in the docstring and then the function body.

def count_uppercase(s): """ (str) -> int

Return the number of uppercase letters in s.

"""

Python Overiew

3. In math, the Collatz conjecture states that starting from any positive integer, you will eventually reach the number 1 by repeatedly applying the following two rules: ? if the number is even, divide it by 2 to get the next number in the sequence ? if the number is odd, multiply by 3 and add 1 to get the next number in the sequence Repeatedly applying the rules generates a sequence of numbers. The Collatz step count is the number of applications of the rules required before the sequence reaches 1. For example, there are 8 Collatz steps in the Collatz sequence: n = 6 -> n = 3 -> n = 10 -> n = 5 -> n = 16 -> n = 8 -> n = 4 -> n = 2 -> n = 1 Complete this function to count the Collatz steps for a particular number n.

def count_collatz_steps(n): """ (int) -> int

Precondition: n >= 1

Return the number of steps it takes to reach 1 by applying the two rules of the Collatz conjecture beginning from the positive integer n.

>>> count_collatz_steps(6) 8 """

Python Overiew

4. Complete the function body. def greatest_difference(nums1, nums2): """ (list of number, list of number) -> number Precondition: len(nums1) == len(nums2) and nums1 != [] Return the greatest absolute difference between numbers at corresponding positions in nums1 and nums2. >>> greatest_difference([1, 2, 3], [6, 8, 10]) 7 >>> greatest_difference([1, -2, 3], [-6, 8, 10]) 10 """

5. Complete the function body. def can_pay_with_two_coins(denoms, amount): """ (list of int, int) -> bool Return True if and only if it is possible to form amount, which is a number of cents, using exactly two coins, which can be of any of the denominations in denoms. >>> can_pay_with_two_coins([1, 5, 10, 25], 35) True >>> can_pay_with_two_coins([1, 5, 10, 25], 20) True >>> can_pay_with_two_coins([1, 5, 10, 25], 12) False """

Python Overiew

6. The express checkout is for grocery orders with 8 or fewer items. Complete the function body. def express_checkout(product_to_quantity): """ (dict of {str: int}) -> bool

Return True iff the grocery order in product_to_quantity qualifies for the express checkout. product_to_quantity maps products to the numbers of those items in the grocery order.

>>> express_checkout({'banana': 3, 'soy milk': 1, 'peanut butter': 1}) True >>> express_checkout({'banana': 3, 'soy milk': 1, 'twinkie': 5}) False """

Python Overiew

7. The Event class can be used to keep track of event related information, such name and start and end times. Implement the missing method bodies .

class Event: """A new calendar event."""

def __init__(self, start_time, end_time, event_name): """ (Event, int, int, str) -> NoneType

Precondition: 0 > e = Event(12, 13, 'Lunch') >>> e.start_time 12 >>> e.end_time 13 >>> e.name 'Lunch' """

def duration(self): """ (Event) -> int

Return the duration of this event. >>> e = Event(10, 11, 'Lecture') >>> e.duration() 1 """

Python Overiew

def __str__(self): """ (Event) -> str Return a string representation of this event. >>> e = Event(6, 7, 'Run') >>> str(e) 'Run: from 6 to 7' """

def __eq__(self, other): """ (Event, Event) -> bool Return True iff this event has the same start time, end time, and name as other. >>> e1 = Event(6, 7, 'Run') >>> >>>

"""

def overlaps(self, other): """ (Event, Event) -> bool Return True iff this event overlaps with event other. >>> e1 = Event(6, 7, 'Run') >>> e2 = Event(0, 7, 'Sleep') >>>

"""

Python Overiew

8. The Day class can be used to keep track of a set of events scheduled in a given date. Implement the missing method bodies .

import event

class Day: """A calendar day and its events."""

def __init__(self, day, month, year): """ (Day, int, str, int) -> NoneType

Initialize a day on the calendar with day, month and year, and no events.

>>> d = Day(30, 'November', 2016) >>> d.day 30 >>> d.month 'November' >>> d.year 2016 >>> d.events [] """

Python Overiew

def schedule_event(self, new_event): """ (Day, Event) -> bool

Attempts to schedule new_event on this day. If the event overlaps with an existing event return False without appending event. Otherwise, return True and append new_event.

>>> d = Day(1, 'December', 2016) >>> e = event.Event(11, 13, 'Meeting') >>> d.schedule_event(e) True >>> d.events[0] == e True >>> e2 = event.Event(11, 12, 'Lunch') >>> d.schedule_event(e2) False >>> e2 in d.events == False True """

def __str__(self): """ (Day) -> str

Return a string representation of this day.

>>> d = Day(29, 'November', 2016) >>> d.schedule_event(event.Event(15, 16, 'Submit A3 work')) True >>> d.schedule_event(event.Event(16, 23, 'Celebrate!')) True >>> print(d) 29 November 2016: - Submit A3 work: from 15 to 16 - Celebrate!: from 16 to 23 """

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download