Skip to content
The Chat Archive
command-a-03-2025· Cohere LMArena Human Preference 140K

take an intermediate python test of myself

  1. User

    take an intermediate python test of myself

  2. 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

    1. List Manipulation
      Write a function merge_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].

    2. Dictionary Operations
      Write a function count_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}.

    3. Lambda and Sorting
      Given a list of tuples (name, age), write a one-liner using sorted() 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)

    1. Class and Methods
      Create a class BankAccount with attributes account_number and balance. Implement methods:

      • deposit(amount) to add money to the account.
      • withdraw(amount) to subtract money, but raise a ValueError if the balance is insufficient.
      • get_balance() to return the current balance.
    2. Inheritance
      Create a class SavingsAccount that inherits from BankAccount. Add an attribute interest_rate and a method add_interest() that adds interest to the balance based on the interest rate.


    Section 3: Error Handling and File I/O

    1. Error Handling
      Write a function safe_divide(a, b) that divides a by b and returns the result. If b is zero, raise a ZeroDivisionError with a custom error message. If a or b is not a number, raise a TypeError.

    2. File I/O
      Write a function read_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 a FileNotFoundError.


    Section 4: Advanced Topics

    1. 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].

    2. Generators
      Write a generator function fibonacci(n) that yields the first n Fibonacci numbers.
      Example:
      list(fibonacci(5)) should return [0, 1, 1, 2, 3].

    3. Decorators
      Write a decorator timer that prints the execution time of a function. Use it to decorate a function that calculates the factorial of a number.


    Submission Instructions

    1. Write your code for each question in a Python file or notebook.
    2. Test your functions thoroughly with different inputs.
    3. 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.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.