Creating a Garage Band with Object-Oriented Programming.
pythonic-garage-band.pythonic-garage-band.Use Python classes to model a Band made up of different kinds of musicians.
Start with Guitarist, Bassist, and Drummer.
Make use of a Musician base class to handle common functionality which particular kinds of musicians will inherit.
Unit tests will be supplied for this lab. Your job is to make them pass. Do NOT modify the supplied tests (except to enable for stretch goals).
name attribute which is a string.members attribute which is a list of instances that inherit from Musician base (or super) class.play_solos method that asks each member musician to play a solo, in the order they were added to the band.__str__ and __repr__ methods.to_list which returns a list of previously created Band instances.__str__ and __repr__ methods.get_instrument method that returns a string.play_solo method that returns a string.Read all of the following instructions carefully.
Ask the candidate to write a function to add up the sum of each row in a matrix of arbitrary size and return an array with the appropriate values.
Familiarize yourself with the grading rubric so you know how to score the interview.
Look for effective problem-solving, efficient use of time, and effective communication with the whiteboard space available.
Every solution might look a little different, but the candidate should be able to at least convince you that their code works to solve the problem.
Assign points for each item on the Rubric, according to how well the candidate executed on that skill.
Add up all the points at the end and record the total at the bottom of the page.
| Input | Output |
|---|---|
| [ [1, 2, 3], [3, 5, 7], [1, 7, 10] ] | [6, 15, 18] |
| [ [0, 1, 5], [-4, 7, 2], [-3, 12, 11] ] | [6, 5, 20] |
The candidate should draw the input and output as a square of integers.
Bookmark and Review
Statement on why this topic matter as it relates to what I’m studying in this module:
Classes and objects are used for structuring code and managing data effectively, while recursion introduces a problem-solving approach. Combining this with an understanding of fixtures and coverage in testing enhances code organization and maintainability.
What are the key differences between classes and objects in Python, and how are they used to create and manage instances of a class?
classes: a template or blueprint that has attributes (variables) and methods (functions). They define the structure and behavior of objects.objects: instances of classes, created from these templates. They encapsulate variables and functions into a single entity, embodying the characteristics defined by the class. myobjectx = MyClass()myobjectx.myvariable) is used to access variables within an object. Functions within an object are invoked similarly (myobjectx.myfunction()).Explain the concept of recursion and provide an example of how it can be used to solve a problem in Python. What are some best practices to follow when implementing a recursive function?
Example from reading:
def factorial_recursive(n):
if n == 1:
return 1
else:
return n * factorial_recursive(n-1)
What is the purpose of pytest fixtures and code coverage in testing Python code? Explain how they can be used together to improve the quality and maintainability of a project.
fixtures: objects or setups shared across multiple tests, enhancing test setup flexibility.
coverage: assesses the thoroughness of testing by identifying which parts of the code are executed during tests.
pytest-cov package to integrate code coverage into pytest.- Run tests with the --cov option, specifying the program to be tested.I’m confused about maintaining state during recursion it seems familiar to JS but I can’t quite picture it.
Write a brief reflection on your learning today, or use the prompt below to get started.
Consider the following quote from the article linked above:
“Adults need to know why to learn something; as soon as they answer this question, they are ready to start.”
The why applies to both large-scale (learning web development as a new profession) and small-scale (learning a particular tool or technique), and your learning journal is an excellent place to identify and contemplate those whys. They are important. So… what is your why?
Algorithm: Initialize an empty list to store the sum of each row. Iterate through each row in the matrix. Initialize a variable row_sum to 0 for the current row. Iterate through each element in the row. If the element is None, treat it as 0. Add the element to row_sum. Append row_sum to the result list. Return the result list containing the sum of each row.
def row_sums(matrix): result = [] for row in matrix: row_sum = 0 for element in row: row_sum += element if element is not None else 0 result.append(row_sum) return result
matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]
matrix = [ [-1, -2, -3], [-4, -5, -6], [-7, -8, -9] ]
matrix = [ [1, 2, 3], [4, None, 6], [7, 8, 9] ]
Row Sums: [1 + 2 + 3 = 6] [4 + 0 + 6 = 10] [7 + 8 + 9 = 24]
Walkthrough: We initialize an empty list (result) to store the row sums. For each row in the matrix, we initialize row_sum to 0. We iterate through each element in the row, treating None as 0 and adding the values to row_sum. After processing each row, we append the row_sum to the result list. Finally, we return the list containing the row sums.
Time Complexity (Big O): Let’s denote the number of rows as m and the number of elements in each row as n. The time complexity is O(m * n) because we iterate through each element in the matrix once.
Space Complexity: The space complexity is O(m), where m is the number of rows, as we only use a single list to store the row sums.