take an intermediate python test of myself
- User
take an intermediate python test of myself
- Assistant
Sure! Below is an Intermediate Python Test designed to assess your skills in Python programming. The test covers a range of topics, including data structures, functions, object-oriented programming, error handling, and file I/O. Take your time, and feel free to ask for hints or explanations if you get stuck.
Intermediate Python Test
Section 1: Data Structures and Functions
-
List Manipulation
Write a functionmerge_sort_lists(list1, list2)that merges two sorted lists into a single sorted list.
Example:
merge_sort_lists([1, 3, 5], [2, 4, 6])should return[1, 2, 3, 4, 5, 6]. -
Dictionary Operations
Write a functioncount_word_frequency(sentence)that takes a sentence (string) and returns a dictionary with word frequencies. Ignore punctuation and convert all words to lowercase.
Example:
count_word_frequency("Hello, world! Hello Python.")should return{"hello": 2, "world": 1, "python": 1}. -
Lambda and Sorting
Given a list of tuples(name, age), write a one-liner usingsorted()and a lambda function to sort the list by age in descending order.
Example:
[("Alice", 25), ("Bob", 30), ("Charlie", 20)]should return[("Bob", 30), ("Alice", 25), ("Charlie", 20)].
Section 2: Object-Oriented Programming (OOP)
-
Class and Methods
Create a classBankAccountwith attributesaccount_numberandbalance. Implement methods:deposit(amount)to add money to the account.withdraw(amount)to subtract money, but raise aValueErrorif the balance is insufficient.get_balance()to return the current balance.
-
Inheritance
Create a classSavingsAccountthat inherits fromBankAccount. Add an attributeinterest_rateand a methodadd_interest()that adds interest to the balance based on the interest rate.
Section 3: Error Handling and File I/O
-
Error Handling
Write a functionsafe_divide(a, b)that dividesabyband returns the result. Ifbis zero, raise aZeroDivisionErrorwith a custom error message. Ifaorbis not a number, raise aTypeError. -
File I/O
Write a functionread_and_process_file(filename)that reads a file, counts the number of lines, and returns the total number of characters in the file. Handle the case where the file does not exist by raising aFileNotFoundError.
Section 4: Advanced Topics
-
List Comprehensions
Given a list of numbers, use a list comprehension to create a new list containing only the even numbers squared.
Example:
[1, 2, 3, 4, 5]should return[4, 16]. -
Generators
Write a generator functionfibonacci(n)that yields the firstnFibonacci numbers.
Example:
list(fibonacci(5))should return[0, 1, 1, 2, 3]. -
Decorators
Write a decoratortimerthat prints the execution time of a function. Use it to decorate a function that calculates the factorial of a number.
Submission Instructions
- Write your code for each question in a Python file or notebook.
- Test your functions thoroughly with different inputs.
- Share your code, and I’ll review it and provide feedback!
Good luck! Let me know if you’d like hints or explanations for any of the questions.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.