CS Homework 1 Solution

$30.00 $24.00

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/5 – (2 votes)

You’ll get a: zip file solution

 

Description

5/5 – (2 votes)
  1. 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

“””

2. 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

“””

1

3. Complete the function body.

def all fluffy(s):

“”” (str) -> bool

Return True iff every character in s is fluffy. Fluffy characters are those that appear in the word ‘fluffy’.

  • all fluffy(‘fullfly’)

True

  • all fluffy(‘firefly’) False

“””

4. Complete the function body.

def digital sum(nums list):

“”” (list of str) -> int

Precondition: s.isdigit() holds for each string s in nums list.

Return the sum of all the digits in all strings in nums list.

  • digital sum([‘64’, ‘128’, ‘256’])

34

  • digital sum([‘12’, ‘3’])

6

“””

  1. 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.

2

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

“””

Before 11:59:59 p.m., Friday, 19 April 2019 (3rd Friday), you must upload a .PY file with all your solutions of the above questions to the course Blackboard assignment for Homework 1.

3

CS Homework 1 Solution
$30.00 $24.00