Bird
Raised Fist0
Prompt Engineering / GenAIml~20 mins

Document loading and parsing in Prompt Engineering / GenAI - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Document Parsing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this document parsing code?
Consider the following code that loads and parses a JSON document string. What will be the value of parsed_data after running this code?
Prompt Engineering / GenAI
import json
json_string = '{"name": "Alice", "age": 30, "skills": ["Python", "ML"]}'
parsed_data = json.loads(json_string)
A{'name': 'Alice', 'age': 30, 'skills': ['Python', 'ML']}
B{"name": "Alice", "age": 30, "skills": ["Python", "ML"]}
C{'name': "Alice", 'age': 30, 'skills': ["Python", "ML"]}
D{"name": 'Alice', "age": 30, "skills": ['Python', 'ML']}
Attempts:
2 left
💡 Hint
Remember that json.loads converts JSON strings into Python dictionaries with single quotes.
🧠 Conceptual
intermediate
1:30remaining
Which format is best for loading structured text documents for ML?
You want to load a large collection of structured text documents for machine learning. Which document format is most suitable for easy parsing and extracting fields?
ABinary files with custom encoding
BPlain text files with no structure
CEncrypted PDF files
DCSV files with columns for each field
Attempts:
2 left
💡 Hint
Think about formats that clearly separate data fields for easy extraction.
Metrics
advanced
1:00remaining
How to measure parsing success rate on a document dataset?
You have a dataset of 1000 documents to parse. Your parser successfully extracts data from 920 documents without errors. What is the parsing success rate?
A100%
B8%
C92%
D90%
Attempts:
2 left
💡 Hint
Success rate = (number of successful parses / total documents) * 100
🔧 Debug
advanced
2:00remaining
Why does this XML parsing code raise an error?
This code tries to parse an XML document but raises an error. What is the cause?
Prompt Engineering / GenAI
import xml.etree.ElementTree as ET
xml_string = '<root><item>Value</item></root>'
root = ET.parse(xml_string)
AET.parse expects a file path or file object, not a string of XML content
BThe XML string is malformed and missing closing tags
CET.parse cannot parse XML with nested tags
DThe xml.etree.ElementTree module is not imported correctly
Attempts:
2 left
💡 Hint
Check the expected input type for ET.parse.
Model Choice
expert
3:00remaining
Which model is best for extracting structured data from scanned document images?
You have scanned images of invoices and want to extract structured fields like date, total, and vendor name. Which AI model type is best suited for this task?
AConvolutional Neural Network (CNN) for image classification
BOptical Character Recognition (OCR) combined with Named Entity Recognition (NER)
CGenerative Adversarial Network (GAN) for image generation
DRecurrent Neural Network (RNN) for time series prediction
Attempts:
2 left
💡 Hint
Think about models that convert images to text and then extract information.

Practice

(1/5)
1. What is the main purpose of document loading in AI projects?
easy
A. To clean the data by removing errors
B. To train the AI model with labeled data
C. To visualize the results of the AI model
D. To read text files so the computer can access their content

Solution

  1. Step 1: Understand document loading

    Document loading means reading text files so the computer can access the content inside.
  2. Step 2: Differentiate from other tasks

    Training models, visualization, and cleaning are different steps after loading the document.
  3. Final Answer:

    To read text files so the computer can access their content -> Option D
  4. Quick Check:

    Document loading = reading files [OK]
Hint: Loading means reading files into the computer [OK]
Common Mistakes:
  • Confusing loading with training the model
  • Thinking loading cleans the data
  • Mixing loading with visualization
2. Which Python code snippet correctly loads a text file named data.txt into a string variable?
easy
A. with open('data.txt', 'x') as file: text = file.read()
B. file = open('data.txt', 'w') text = file.read()
C. with open('data.txt', 'r') as file: text = file.read()
D. text = open('data.txt').write()

Solution

  1. Step 1: Check file mode for reading

    Mode 'r' opens the file for reading, which is needed to load text.
  2. Step 2: Use context manager and read method

    Using with open(...) ensures safe file handling, and file.read() reads all content.
  3. Final Answer:

    with open('data.txt', 'r') as file: text = file.read() -> Option C
  4. Quick Check:

    Open with 'r' and read() = correct loading [OK]
Hint: Use 'r' mode and read() to load text files [OK]
Common Mistakes:
  • Using 'w' mode which is for writing, not reading
  • Calling write() instead of read()
  • Using 'x' mode which is for creating new files
3. What will be the output of this Python code that parses a loaded text?
text = "Hello world! Welcome to AI."
words = text.split()
print(words)
medium
A. ['Hello', 'world', 'Welcome', 'to', 'AI']
B. ['Hello', 'world!', 'Welcome', 'to', 'AI.']
C. ['Hello world! Welcome to AI.']
D. ['H', 'e', 'l', 'l', 'o']

Solution

  1. Step 1: Understand split() method

    The split() method splits the string by spaces into a list of words, keeping punctuation attached.
  2. Step 2: Apply split() to the text

    Splitting "Hello world! Welcome to AI." results in ['Hello', 'world!', 'Welcome', 'to', 'AI.'] including punctuation.
  3. Final Answer:

    ['Hello', 'world!', 'Welcome', 'to', 'AI.'] -> Option B
  4. Quick Check:

    split() by space keeps punctuation attached [OK]
Hint: split() breaks text by spaces, punctuation stays [OK]
Common Mistakes:
  • Expecting punctuation to be removed automatically
  • Thinking split() returns a single string list
  • Confusing split() with list(text) which splits characters
4. Identify the error in this code that tries to parse a document into sentences:
text = "AI is fun. Let's learn it."
sentences = text.split('. ')
print(sentences)
medium
A. The split delimiter '. ' misses the last sentence ending
B. The code should use splitlines() instead of split()
C. The print statement is missing parentheses
D. The variable name 'sentences' is invalid

Solution

  1. Step 1: Analyze split delimiter usage

    Splitting by '. ' splits sentences but leaves the last sentence without a trailing '. ' unseparated.
  2. Step 2: Understand effect on last sentence

    The last sentence "Let's learn it." remains attached with the period, causing inconsistent splitting.
  3. Final Answer:

    The split delimiter '. ' misses the last sentence ending -> Option A
  4. Quick Check:

    Splitting by '. ' misses last sentence split [OK]
Hint: Splitting by '. ' misses last sentence if no trailing space [OK]
Common Mistakes:
  • Thinking splitlines() splits sentences
  • Forgetting print() needs parentheses in Python 3
  • Assuming variable names cause errors
5. You have a text file with multiple paragraphs separated by blank lines. Which approach best loads and parses it into a list of paragraphs for AI processing?
hard
A. Read the file, split text by double newlines '\n\n', then strip whitespace from each paragraph
B. Read the file line by line and treat each line as a paragraph
C. Use split() to split by single spaces to get paragraphs
D. Load the file and convert all text to uppercase without splitting

Solution

  1. Step 1: Understand paragraph separation

    Paragraphs are separated by blank lines, which means two newline characters '\n\n'.
  2. Step 2: Parse paragraphs correctly

    Splitting by '\n\n' divides text into paragraphs; stripping whitespace cleans each paragraph.
  3. Final Answer:

    Read the file, split text by double newlines '\n\n', then strip whitespace from each paragraph -> Option A
  4. Quick Check:

    Split by '\n\n' for paragraphs [OK]
Hint: Paragraphs split by double newlines '\n\n' [OK]
Common Mistakes:
  • Splitting by single spaces splits words, not paragraphs
  • Treating each line as a paragraph loses multi-line paragraphs
  • Ignoring whitespace cleanup after splitting