In this lab assignment, you will be creating a command line application that takes advantage of Python’s built-in capabilities for reading and writing files.
format() Methodmadlib-cli.madlib-cli.madlib.py at the root of the madlib_cli folder, which will contain all of the Python code that you will write relating to your Madlib game.The resolution to that dilemma is to break down your code so that it is more easily testable.
read_template function that takes in a path to a text file and returns a stripped string of the file’s contents.
read_template should raise a FileNotFoundError if the path is invalid.Create and test a parse_template function that takes in a template string and returns a string with language parts removed and a separate tuple of those language parts.
merge function that takes in a “bare” template and a list of user-entered language parts, and returns a string with the language parts inserted into the template.Read all of the following instructions carefully.
data-structures-and-algorithms.array-binary-search.README.md file.README at the root of the repository - with a link to this challenge’s README file.NOTE: This challenge is whiteboard only. Write out code as part of your whiteboard process, but don’t worry about creating external program files.
Write a function called BinarySearch which takes in 2 parameters: a sorted array and the search key. Without utilizing any of the built-in methods available to your language, return the index of the array’s element that is equal to the value of the search key, or -1 if the element is not in the array.
NOTE: The search algorithm used in your function should be a binary search. Check the Resources section for details.
| Input | Output |
|---|---|
| [4, 8, 15, 16, 23, 42], 15 | 2 |
| [-131, -82, 0, 27, 42, 68, 179], 42 | 4 |
| [11, 22, 33, 44, 55, 66, 77], 90 | -1 |
| [1, 2, 3, 5, 6, 7], 4 | -1 |
Bookmark and Review
Statement on why this topic matter as it relates to what I’m studying in this module:
Knowing how to use ‘with’ for managing resources, reading files with ‘read’ and ‘readline,’ and handling exceptions in Python is vital for writing code that’s clean, handles files effectively, and gracefully deals with errors in your programs.
What is the purpose of the ‘with’ statement when opening a file in Python, and how does it help manage resources while reading and writing files?
with statement automatically takes care of closing the file once it leaves the with block, even in cases of error. Use the with statement as much as possible, as it allows for cleaner code and makes handling any unexpected errors easier. By using ‘with,’ you don’t need to explicitly close the file; the ‘with’ statement takes care of it automatically, preventing resource leaks and promoting cleaner and more Pythonic code.Explain the difference between the ‘read()’ and ‘readline()’ methods for file objects in Python. Provide examples of when to use each method.
read(): This reads from the file based on the number of size bytes. If no argument is passed or None or -1 is passed, then the entire file is read. This method is suitable when you want to read the entire file at once.readline(): This method reads a single line from the file. If the size parameter is provided, it reads up to that number of characters or until the end of the line. It returns a string containing the line read. This method is useful when you want to read the file line by line.Briefly describe the concept of exception handling in Python. How can the ‘try’, ‘except’, and ‘finally’ blocks be used to handle exceptions and ensure proper execution of code? Provide a simple example.
try: This block contains the code that might raise an exception.except: If an exception occurs in the try block, the code in the except block is executed to handle the exception.finally: This block is always executed, regardless of whether an exception occurred or not. It is often used for cleanup operations.
try:
a = 5//0
print(a)
except ZeroDivisionError:
print("Can't divide by zero")
finally:
print('This is always executed')
I watched the video and I want to know what you use to decide in how many increments you would breakdown large datasets when using loops to process.
Write a brief reflection on your learning today, or use the prompt below to get started.
“[Adult learners] demand that their learning is relevant to their real life issues, problems, and tasks, and that their education will boost their performance levels and help them live their lives in a better, and more satisfying way.”
In other words, understanding your motivation for learning contributes to the effectiveness of your learning, and your learning journal is a place to explore those thoughts. What are your motivations for learning?
It is time to evaluate who you already know. Who are these people and what do they do professionally? There are lots of tech roles in all industries, so find out who in your network can connect you to an opportunity.
Now, take the time to identify who in your current network would be great for a 1:1 informational coffee. Also, find 3 different local meetups that you want to target to grow your network.
Assignment Submit the names of who you’d like to reach out to for 1:1 meetings, and the events you want to attend.