This is the multi-page printable view of this section. Click here to print.

Return to the regular view of this page.

PP. Programming Practice

Practice coding problems in Python

Software Engineers need to be expert in their tools. You can’t Google or AI everything and call yourself an expert. Your job interviews will not entail these resources anyway.

1 - Week 2

Refresher on important 131 concepts.

For all of the following, write both the function and some code that calls and tests the function.

Complete the following without using ANY outside aid. If necessary, refer only to the python help() command or the official documentation at https://python.org.

Use your preferred programming environment (IDLE, Pycharm, Visual Studio Code, etc.)

  1. Write a function with two parameters: a string and an integer n. Using a for loop, print the string n times.
  2. Write a function with one parameter: a list. Print each element of the list using a for loop.
  3. Write a function with two parameters: a list and a target. Using a for loop, count the number of times target appears in the list and print the total.
  4. Write a function with two parameters: a list and a target. Using a while or for loop, print the first index in the list where target appears. Print not present if it does not exist in the list.
  5. Write a function with one parameter: a list of integers. Using a loop, count the number of even and odd integers in the list. Print the total count of evens and odds.

Solutions

There are a few ways to solve each problem. Here are a few: week2-soln.py

Submission

Submit your .py file to Canvas for a check. You are not graded on completeness or correctness – this is for learning and feedback.

Key Skills

  • Function definition with parameters.
  • Function calling.
  • Selection using if-else.
  • Iteration using a for loop and working with the list index.
  • Combining iteration and selection.

2 - Week 3

Reinforcing selection, iteration, and function calling.

Instructions

For all of the following, write both the function and some code that calls and tests the function.

Complete the following without using ANY outside aid. If necessary, refer only to the python help() command or the official documentation at https://python.org.

Use PyCharm to complete this task. I know that PyCharm has a built-in AI Assistant. I strongly recommend that you disable it. You are going to be quizzed on these skills in class, and you will not be allowed to use any outside assistance.

  1. Write a function named multiply() with two parameters, a and b, that returns the result.
    • Verify that both a and b are integers.
    • Return nothing if either a or b is not an integer.
  2. Write a function named divide() with two parameters, a and b, that returns the result of a / b.
    • Verify that both a and b are either integers.
    • Return nothing if either a or b is not a number.
    • Return nothing if b equals 0.
  3. Write a calculator function
    • The function must contain an “infinite” while loop that does the following until the user chooses to ’exit'.
    • Prompt the user to make a choice of either multiply, divide, or exit.
    • Do not allow or handle an invalid choice.
    • If the user picks exit, the program must end.
    • Prompt the user to enter two values, a and b.
    • Based on their choice, call either your multiply() or divide() function.
    • If either multiply() or divide() returns nothing, print an error message.
    • Otherwise, print the result in the format, e.g, 4 * 5 = 20 or 2.4 / 1.2 = 2.0.
    • Round the divide result to the tenths place using the built-in round() function when printing.

Submission

Submit your .py file to Canvas for a check. You are not graded on completeness or correctness – this is for learning and feedback.

Key Skills

  • Function definition: parameters and returns.
  • Logical selection using if.
  • Type checking using isinstance().
  • Functions calling functions.