← Back to Chapter 7
Chapter 7 Practical Exam MEDIUM-HARD
30:00
Problems
3
Time Limit
30 min
Solved
0/3
Score
0%
1
File Analyzer
Not Started
2
Log Parser
Not Started
3
Config Merger
Not Started

Problem 1: File Analyzer

Difficulty: Medium | Topic: Reading & Processing Files

Description

Write a function analyze_file(content) that takes a string representing file content and returns a dictionary with file statistics.

Requirements

Your function should return a dictionary with these keys:

  • "lines": Total number of lines
  • "words": Total word count
  • "chars": Total character count (excluding newlines)
  • "avg_line_length": Average characters per line (rounded to 1 decimal)

Example

Input Content:
Hello World This is a test file Python is great
Output:
{"lines": 3, "words": 10, "chars": 41, "avg_line_length": 13.7}

Notes

  • Words are separated by whitespace
  • Empty lines count as lines but have 0 words
  • Handle empty input (return zeros)
solution.py
Hint

Use split('\n') to get lines, then split() on each line for words. For average, divide total chars by number of lines and use round(value, 1).

Output
Run your code to see output...

Problem 2: Log Parser

Difficulty: Medium-Hard | Topic: Parsing & Filtering

Description

Write a function parse_logs(log_content, level) that parses log file content and returns entries matching a specific log level.

Log Format

Each log line follows this format:

[TIMESTAMP] LEVEL: Message

Example:

[2024-01-15 10:30:45] ERROR: Connection failed [2024-01-15 10:31:02] INFO: Retry successful [2024-01-15 10:31:15] WARNING: High memory usage

Requirements

  • Return a list of dictionaries with keys: timestamp, level, message
  • Filter by the specified level (case-insensitive)
  • Skip malformed lines
  • Return empty list if no matches

Example

Input:
parse_logs(logs, "ERROR")
Output:
[{"timestamp": "2024-01-15 10:30:45", "level": "ERROR", "message": "Connection failed"}]
solution.py
Hint

Split by newlines, then for each line: extract timestamp between brackets, find level before the colon, and the message after the colon. Use .upper() for case-insensitive comparison.

Output
Run your code to see output...

Problem 3: Config Merger

Difficulty: Hard | Topic: Multi-file Processing

Description

Write a function merge_configs(base_config, override_config) that merges two configuration file contents. The override config takes precedence for duplicate keys.

Config Format

Each config is in key=value format, one per line:

database_host=localhost database_port=5432 debug_mode=false
  • Lines starting with # are comments (ignore)
  • Empty lines should be ignored
  • Keys are case-sensitive

Requirements

  • Return a dictionary with merged key-value pairs
  • Override values take precedence over base values
  • Include all keys from both configs
  • Strip whitespace from keys and values

Example

Base Config:
host=localhost port=8080 debug=false
Override Config:
# Production settings port=443 ssl=true
Output:
{"host": "localhost", "port": "443", "debug": "false", "ssl": "true"}
solution.py
Hint

Create a helper function to parse a single config into a dict. Use line.startswith('#') to detect comments. Parse base first, then update with override using dict.update().

Output
Run your code to see output...