Assignment: CP0 - Intro/Review Python

22-23 - HHS - UW CSE163 INT DATA PROG A - T2
2023-01-10T19:59:43Z

Due: Jan 6, 2023 by 11:59pm

Problem 1 : Minute Countdown

Write a program that counts down from a minute in decrements of 10 seconds using a for loop. The output of the program, when hitting the Run button, should be the following:

One minute countdown
60
50
40
30
20
10
0
Done!

Hint: The lines of numbers should all be produced by your for loop while the first and last lines will appear outside the loop (since they only happen once).

Hint: Think carefully about what the inputs to range will be! The parameter values are allowed to be negative! Ask yourself what the sequence of values need to be and what the values you need to pass to range are!

Requirements

Your solution should use a for loop and should not use a while loop.

Your program should use the main-method pattern (we provided the starter code). You do not need to define any other functions for this problem!

Problem 2: Countdown

This problem is very similar to the last practice problem, but now we want you to write a function called countdown that takes a starting number of seconds and starts the countdown from there instead (still counting by 10s). The format of the output will be slightly different to accommodate this starting point.

If the sequence does not evenly end with a 0 (e.g. if the countdown starts from 15 ), then 0 will not be printed. This shown in the example calls below.

The countdown function should take one int parameter for the starting point. You may assume the parameter is an int. If the parameter value is less than 0 , it should instead print Start must be non-negative! .

Here is an example method with four example calls to the function and their output is shown after ( print statements included to space out output).

def sample_run():
    countdown(60)
    print()
    countdown(15)
    print()
    countdown(-4)
    print()
    countdown(0)

Output of sample_run

60 second countdown
60
50
40
30
20
10
0
Done!
15 second countdown
15
5
Done!
Start must be non-negative!
0 second countdown
0
Done!

Hint: The lines of numbers should all be produced by your for loop while the first and last lines will appear outside the loop (since they only happen once).

Requirements

Your solution should use a for loop and should not use a while loop.

Your solution to Problem 2 will be included in the main.py file. Simply include the function named countdown (as described above) above the main method.

Problem 3 : Fibonacci

Write a function named fibonacci to compute the first Fibonacci number larger than a given value n .

The Fibonacci Sequence is the following sequence of numbers: 1, 1, 2, 3, 5, 8, 13, … The sequence starts with 1 and 1. The next number in the sequence is the sum of the previous two.

Your function should return the first Fibonacci number in the sequence that exceeds the given value n. For example, the following snippet that uses your function shows the output in the comments next to each line.

print(fibonacci(3)) # 5
print(fibonacci(6)) # 8
print(fibonacci(-2)) # 1

Hint: You will want to store the previous two numbers in the sequence in variables, one to keep track of the current number and one to keep track of the previous. The next number is the sum of curr and prev.

Requirements

You do not need to define any other function nor create any other file for this problem!

Problem 4 : switch_pairs

Write a function named switch_pairs that accepts a string as a parameter and returns that string with each pair of neighboring letters reversed. If the string has an odd number of letters, the last letter should not be modified. For example, the call switch_pairs("example") should return "xemalpe" and the call switch_pairs("hello there") should return "ehll ohtree".

Note: This function returns a string and does not print anything.

Problem 5 : find_range

Write a function named find_range that accepts a list of integers as a parameter and returns the range of values contained in the list. The range is defined to be one more than the difference between its largest and smallest elements.

For example, consider the list [12, 17, 6]. The largest element in this list is 17 and the smallest is 6, so the range is 12 (one more than 17 - 6). If the largest and smallest values are the same, the range is 1.

Requirements

You may assume that the list contains at least one element (that its length is at least 1).

You should NOT modify the contents of the list.

You should NOT use a “nested-loop” to solve this problem (a loop inside a loop).

Problem 6 : count_votes

Write a function called count_votes that takes a list of numbers indicating votes for candidates 0, 1, or 2 and returns a new list of length 3 showing how many counts each candidate got.

For example:

votes = [1, 0, 1, 1, 2, 0]
result = count_votes(votes)
print(result) # [2, 3, 1]

The returned list has a 2 at index 0 because a 0 appeared twice in the votes.

Requirements

You may assume the input list only contains values that are 0, 1, or 2.

You should not modify the contents of the list.

Problem 1 : Minute Countdown

Write a program that counts down from a minute in decrements of 10 seconds using a for loop. The output of the program, when hitting the Run button, should be the following:

One minute countdown
60
50
40
30
20
10
0
Done!

Hint: The lines of numbers should all be produced by your for loop while the first and last lines will appear outside the loop (since they only happen once).

Hint: Think carefully about what the inputs to range will be! The parameter values are allowed to be negative! Ask yourself what the sequence of values need to be and what the values you need to pass to range are!

Requirements

Your solution should use a for loop and should not use a while loop.

Your program should use the main-method pattern (we provided the starter code). You do not need to define any other functions for this problem!

Problem 2: Countdown

This problem is very similar to the last practice problem, but now we want you to write a function called countdown that takes a starting number of seconds and starts the countdown from there instead (still counting by 10s). The format of the output will be slightly different to accommodate this starting point.

If the sequence does not evenly end with a 0 (e.g. if the countdown starts from 15 ), then 0 will not be printed. This shown in the example calls below.

The countdown function should take one int parameter for the starting point. You may assume the parameter is an int. If the parameter value is less than 0 , it should instead print Start must be non-negative! .

Here is an example method with four example calls to the function and their output is shown after ( print statements included to space out output).

def sample_run():
    countdown(60)
    print()
    countdown(15)
    print()
    countdown(-4)
    print()
    countdown(0)

Output of sample_run

60 second countdown
60
50
40
30
20
10
0
Done!
15 second countdown
15
5
Done!
Start must be non-negative!
0 second countdown
0
Done!

Hint: The lines of numbers should all be produced by your for loop while the first and last lines will appear outside the loop (since they only happen once).

Requirements

Your solution should use a for loop and should not use a while loop.

Your solution to Problem 2 will be included in the main.py file. Simply include the function named countdown (as described above) above the main method.

Problem 3 : Fibonacci

Write a function named fibonacci to compute the first Fibonacci number larger than a given value n .

The Fibonacci Sequence is the following sequence of numbers: 1, 1, 2, 3, 5, 8, 13, … The sequence starts with 1 and 1. The next number in the sequence is the sum of the previous two.

Your function should return the first Fibonacci number in the sequence that exceeds the given value n. For example, the following snippet that uses your function shows the output in the comments next to each line.

print(fibonacci(3)) # 5
print(fibonacci(6)) # 8
print(fibonacci(-2)) # 1

Hint: You will want to store the previous two numbers in the sequence in variables, one to keep track of the current number and one to keep track of the previous. The next number is the sum of curr and prev.

Requirements

You do not need to define any other function nor create any other file for this problem!

Problem 4 : switch_pairs

Write a function named switch_pairs that accepts a string as a parameter and returns that string with each pair of neighboring letters reversed. If the string has an odd number of letters, the last letter should not be modified. For example, the call switch_pairs("example") should return "xemalpe" and the call switch_pairs("hello there") should return "ehll ohtree".

Note: This function returns a string and does not print anything.

Problem 5 : find_range

Write a function named find_range that accepts a list of integers as a parameter and returns the range of values contained in the list. The range is defined to be one more than the difference between its largest and smallest elements.

For example, consider the list [12, 17, 6]. The largest element in this list is 17 and the smallest is 6, so the range is 12 (one more than 17 - 6). If the largest and smallest values are the same, the range is 1.

Requirements

You may assume that the list contains at least one element (that its length is at least 1).

You should NOT modify the contents of the list.

You should NOT use a “nested-loop” to solve this problem (a loop inside a loop).

Problem 6 : count_votes

Write a function called count_votes that takes a list of numbers indicating votes for candidates 0, 1, or 2 and returns a new list of length 3 showing how many counts each candidate got.

For example:

votes = [1, 0, 1, 1, 2, 0]
result = count_votes(votes)
print(result) # [2, 3, 1]

The returned list has a 2 at index 0 because a 0 appeared twice in the votes.

Requirements

You may assume the input list only contains values that are 0, 1, or 2.

You should not modify the contents of the list.