Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Challenge: Memory-Efficient Data Pipeline | Writing Memory-Efficient Code
Python Memory Management
Osio 2. Luku 5
single

single

Challenge: Memory-Efficient Data Pipeline

Pyyhkäise näyttääksesi valikon

Tehtävä

Pyyhkäise aloittaaksesi koodauksen

You are building a memory-efficient pipeline that processes a large sequence of transaction records. Your goal is to use the right data structures to minimize memory usage throughout.

You are given the following list of raw transaction tuples:

raw_transactions = [
    (1, 4500.0, "USD"),
    (2, 1200.5, "EUR"),
    (3, 8900.0, "USD"),
    (4, 300.75, "EUR"),
    (5, 15000.0, "USD"),
]
  1. Define a class Transaction with __slots__ = ("transaction_id", "amount", "currency") and an __init__ method that sets all three attributes.
  2. Create a generator expression called transaction_stream that yields a Transaction instance for each tuple in raw_transactions — do not use a list comprehension.
  3. Using array.array with type code "d" (double), create a variable called usd_amounts that contains the amount of every transaction where currency == "USD". Consume transaction_stream to build it.
  4. Print usd_amounts.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 5
single

single

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

some-alt