INTRODUCTION – Final Project
You will learn in this module to apply a structural problem-solving framework for challenging projects. Focus on understanding the problem, available solutions, and drawing up a proper plan to meet the challenge’s specifications.
Objectives of Learning:
- Use a framework: Build a structure from startup to completion to attack complex problems systematically.
- Write a Problem Statement: Indicate precisely what the challenge is, and give input and output that should result from a solution.
- Research: Identify all possible options and techniques to address the problem.
- Plan the Solution: Draft an in-depth plan that incorporates all steps necessary in solving the problem.
- Implement the solution: Write and run a very complicated script in order to effectively execute the planned solution.
MODULE 5 GRADED ASSESSMENT
1. A programmer is working on a new project and encounters a complex problem. They are tempted to spend a lot of time trying to solve the problem on their own. However, a colleague advises them to research existing solutions first. What is the colleague’s advice based on?
- The colleague believes that the programmer should not trust their own abilities to solve the problem.
- The colleague believes that the programmer should not waste time reinventing something that already exists. (CORRECT)
- The colleague believes that the programmer should ask someone with more experience to solve this problem.
- The colleague believes that the programmer should not try to be innovative.
Corrrect
2. Based on the following code, what is the correct output?
numbers = [ 4, 6, 2, 7, 1 ]
numbers.sort()
print(numbers)
- 7, 6, 4, 2, 1
- 1, 2, 4, 6, 7 (CORRECT)
- 1, 2, 6, 7, 4
- 4, 6, 2, 7, 1
Correct
3. What is the sort() method used for in Python?
- The sort() method returns the list from the code.
- The sort() method sorts a list without creating a new list. (CORRECT)
- The sort() method creates a new list by sorting the strings or values in the original list.
- The sort() method returns a new list that’s been sorted.
Correct
4. You want to sort the following list in descending order. What is the correct code to use to do this?
numbers = [ 4, 6, 2, 7, 1 ]
- numbers.sort()
- numbers.sort(reverse = False)
- numbers.sort(backup = True)
- numbers.sort(reverse = True) (CORRECT)
Correct
5. You need to sort a list of strings based on their length, what argument would you use to do this?
- key=len (CORRECT)
- sort(len)
- key=length
- sorted(len)
Correct
6. Which of the following statements are true about Python dictionaries? Select all that apply.
- Dictionaries are an ordered collection of key-value pairs.
- Dictionaries are mutable data structures. (CORRECT)
- Dictionaries are used to store data that needs to be accessed quickly and efficiently. (CORRECT)
- Values in a dictionary can be Python objects. (CORRECT)
Correct
7. Which of the following best describes the purpose of an if statement in Python?
- To store data in a temporary variable
- To execute a block of code only if a certain condition is true (CORRECT)
- To repeat a block of code a certain number of times
- To define a function that performs a specific task
Correct
8. A grocery store is developing a loyalty program to reward their customers based on their purchase history. The store wants to assign different reward tiers based on the total amount spent by each customer in the past year. To accomplish this, they need a conditional statement that will execute different blocks of code based on the customer’s spending amount. What conditional statement is most suitable for this task?
- elif statement (CORRECT)
- true statement
- and statement
- if statement
Correct
9. Take a look at the code snippet below. What is the purpose of the generate_report function?
def generate_report(machines):
for machine, users in machines.items():
if len(users) > 0:
user_list = ", ".join(users)
print("{}: {}".format(machine, user_list))
- To create a new dictionary that contains the same information as the machine’s dictionary.
- To sort the machine’s dictionary by the number of users logged into each machine.
- To check if there are any users logged into any machines.
- To generate a report that lists all the machines and the users logged into each machine. (CORRECT)
Correct
10. Why is understanding the problem essential to writing a Python script? Select all that apply.
- Ensure that you are writing a script that is efficient and effective (CORRECT)
- To write code that will solve the problem in a clear and concise way (CORRECT)
- To select which scripting language to use.
- To identify the inputs and outputs of the problem (CORRECT)
Correct
11. You need to sort a list in descending order, which argument should you use in your sort() method to do this?
- reverse = True (CORRECT)
- reverse = False
- backup = True
- backup = False
Correct
12. Based on the following code, what is the correct output?
names = ["Carlos", "Ray", "Alex", "Kelly"]
print(sorted(names, key=len))
- [‘Carlos’, ‘Ray’, ‘Alex’, ‘Kelly’]
- [‘Ray’, ‘Alex’, ‘Kelly’, ‘Carlos’] (CORRECT)
- [‘Carlos’, ‘Kelly’, ‘Alex’, ‘Ray’]
- [‘Alex’, ‘Ray’, ‘Carlos’, ‘Kelly’]
Correct
13. A grocery store is tracking the inventory of its products. For each product, the store needs to keep track of the product name, the product ID, the quantity in stock, and the price.
- A list
- A set
- A dictionary (CORRECT)
- A tuple
Correct
14. A software engineer is tasked with developing a system that monitors network traffic and alerts administrators when suspicious activity is detected. The system receives a continuous stream of network traffic data, which needs to be processed and analyzed in real-time. The engineer is unsure of the best way to handle this high-volume data stream efficiently. What should the programmer do?
- Use a third-party library or tool designed for real-time data processing.
- Create 2 separate functions to process the events and print the associated data to the screen. (CORRECT)
- Create one function to process the events and print the associated data to the screen.
- Increase the size of the database server.
Correct
15. What does the following code snippet do?
if event.type == "login":
machines[event.machine].add(event.user)
- It removes the user from the list of logged-in users for the specified machine.
- It checks if the event type is “logout” and if so, removes the user from the list of logged-in users for the specified machine.
- It checks if the event type is “login” and if so, adds the user to the list of logged-in users for a specified machine. (CORRECT)
- It adds the user to the list of logged-in users for the specified machine.
Correct
16. Based on the following code, what is the correct output?
numbers = [ 3, 5, 4, 8, 1 ]
numbers.sort()
print(numbers)
- 3, 5, 4, 8, 1
- 1, 3, 4, 5, 8 (CORRECT)
- 8, 5, 4, 3, 1
- 3, 1, 5, 4, 8
Correct
17. What is the purpose of the argument key=len in a Python script?
- To sort a list of strings based on their capitalization.
- To sort a list of strings based on importance.
- To sort a list of strings based on their length. (CORRECT)
- To sort a list of strings alphabetically.
Correct
18. A programmer is tasked with developing a program that processes events and prints the associated data to the screen. The programmer is unfamiliar with the most efficient approach to accomplish this task. What should the programmer do?
- Create one function to process the events and print the associated data to the screen.
- Create 2 separate functions to process the events and print the associated data to the screen. (CORRECT)
- Increase the size of the database server.
- Calculate the results of a single function to avoid having to recalculate them for each user.
Correct
19. Which of the following statements accurately describes the purpose of an elif statement in Python?
- To combine multiple conditions into a single statement, allowing for more complex decision-making.
- To repeat a block of code a certain number of times, or until a specific condition is met.
- To store data in a temporary variable, accessible only if later called on within the script.
- To execute a block of code if a specific condition is true, otherwise execute an alternative block of code. (CORRECT)
Correct
20. Take a look at the code snippet below. What parameters does this method take in? Select all that apply.
class Event:
def __init__(self, event_date, event_type, machine_name, user):
self.date = event_date
self.type = event_type
self.machine = machine_name
self.user = user
- event_date (CORRECT)
- event_time (CORRECT)
- machine_name (CORRECT)
- event
Correct
21. Restate the problem statement from the video example.
- Each login event is an instance of the Event class.
- The attributes of the Event class are date, user, machine, and type.
- The company manager wants to know which users are currently connected to which machines.
- We need to process a list of Event objects using their attributes to generate a report that lists all users currently logged in to the machines. (CORRECT)
Right on! This problem statement defines the problem in specific terms and provides a direction and goal.
22. There are two standard methods to sort a list in Python, sort and sorted. What is the difference between these two methods?
- sort returns a new list, while sorted returns the same list reorganized.
- sort lists alphabetically, while sorted lists by word length.
- sorted returns a new list, while sort returns the same list reorganized. (CORRECT)
Awesome! Rightly so! It is very useful though to know the differences between the methods, however, the real key will be the ability to study and utilize the tools that we have got.
23. Having a plan is half the battle. When planning out your program, why is it often a good idea to separate functions?
- A: It simplifies making changes and fixing bugs.
- B: It allows us to use the same function for multiple purposes.
- C: It makes list sorting easier.
- D: Both A and B. (CORRECT)
Great work! The majority benefit from separating functions as this aids in debugging and implementing changes. Tom keeps the functions non-interdependent and uncomplicated, improvised reusability, and adapts a function for different purposes easily.
24. It’s important to know why we’ve written a function. In the example used in the video (shown here), what is the purpose of “if len(users) > 0:” ?
def generate_report(machines):
for machine, users in machines.items():
if len(users) > 0:
user_list = ", ".join(users)
print("{}: {}".format(machine, user_list))
- Ensure that we don’t print any machines where nobody is currently logged in (CORRECT)
- Generates a string of logged-in users for a given machine
- Sorts the list of users
- Iterate over the keys and values in the dictionary
Awesome! The string generated below this check ensures that lists with zero users do not appear.
25. Which line from the program we just wrote combines each logged-in user attribute into one variable?
- events.sort(key=get_event_date)
- self.user = user
- user_list = “, “.join(users) (CORRECT)
- machines[event.machine].add(event.user)
Nice job! python string join method joins user attributes into one single string. If there are several attributes, it will separate them with commas to build that string.
26. A software engineer is tasked with developing a new feature for a popular web application. The feature involves a complex algorithm that requires a significant amount of coding. The engineer is tempted to start writing the code from scratch, but their manager suggests taking a different approach. What is the manager’s recommendation based on?
- The manager believes that the engineer should not try to be creative.
- The manager believes that the engineer should not trust their own abilities to solve the problem.
- The manager believes that the engineer should spend some time researching the problem to identify if a similar problem has been solved before. (CORRECT)
- The manager believes that the software engineer should ask someone with more experience to tackle this problem.
27. A system administrator is managing a network of computers. Each computer has a set of users who are currently logged in. The administrator wants to be able to quickly look up the current users of a given computer. Which data structure is the most efficient for storing the current users of each computer?
- A set
- A dictionary (CORRECT)
- A list
- A tuple
Correct
28. Which method is used to sort a list and keep the original list in tact?
- The sort() method
- The sorted() method with a reverse argument.
- The print() method.
- The sorted() method (CORRECT)
Correct
CONCLUSION – Final Project
Thus, a problem-solving framework can be well learned from modules for completing complex projects effectively. One has also learned how to develop a problem statement in such a way that a clear understanding of the problem is possible from it. It has also trained for extensive research, which equips one with the ability to evaluate options and finally make appropriate decisions.
In fact, as he advances into the planning section, there would already be a very good foundation laid down which would enable one to embark on a complex project with confidence, a sense of strategy, and clarity.