From d25e076b58526098d641923705b37eb23bb15121 Mon Sep 17 00:00:00 2001 From: joycherotich <117736061+joycherotich@users.noreply.github.com> Date: Fri, 27 Jun 2025 17:51:05 +0300 Subject: [PATCH 1/3] python exercise --- main.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/main.py b/main.py index 06ec71c..430de53 100644 --- a/main.py +++ b/main.py @@ -4,6 +4,8 @@ def get_tx_count(tx_list): Input: List of transaction IDs (strings) Output: Integer count """ + return len(tx_list) + def filter_large_txids(tx_list, min_length): """ @@ -11,6 +13,8 @@ def filter_large_txids(tx_list, min_length): Input: List of txids (strings), minimum length (int) Output: Filtered list of strings """ + return [txid for txid in tx_list if len(txid) >= min_length] + def process_txids(tx_list): """ @@ -19,6 +23,13 @@ def process_txids(tx_list): Input: List of txids (strings) Output: List of (int, string) tuples """ + result = [] + for i, txid in enumerate(tx_list): + if not txid.startswith('tx'): + continue + result.append((i, txid)) + return result + def example_while_loop(limit): """ @@ -27,6 +38,15 @@ def example_while_loop(limit): Input: Integer limit Output: List of integers """ + result = [] + i = 0 + while i < limit: + if i == 5: + break + result.append(i) + i += 1 + return result + def unpack_tuple(block_header): """ @@ -34,6 +54,9 @@ def unpack_tuple(block_header): Input: Tuple (int, str, int) Output: Unpacked tuple values """ + height, prev_hash, timestamp = block_header + return height, prev_hash, timestamp + def dict_methods_example(block): """ @@ -42,6 +65,8 @@ def dict_methods_example(block): Input: Dict Output: Tuple of (list, list, list) """ + return list(block.keys()), list(block.values()), list(block.items()) + def multiple_assignment(a, b): """ @@ -49,6 +74,9 @@ def multiple_assignment(a, b): Input: two integers Output: Tuple of two ints (swapped) """ + a, b = b, a + return a, b + def set_example(addresses): """ @@ -57,6 +85,7 @@ def set_example(addresses): Input: List of strings Output: Set of strings """ + return set(addresses) class BitcoinTransaction: @@ -64,28 +93,35 @@ def __init__(self, txid, amount): """ TODO: Initialize transaction with txid (string) and amount (float). """ + self.txid = txid + self.amount = amount def __str__(self): """ TODO: Return string representation: "Tx {txid} of {amount} BTC" """ + return f"Tx {self.txid} of {self.amount} BTC" + class Wallet: def __init__(self): """ TODO: Initialize an empty list to hold transactions. """ + self.transactions = [] def add_tx(self, tx): """ TODO: Add a BitcoinTransaction to the wallet's transaction list. """ + self.transactions.append(tx) def total_balance(self): """ TODO: Calculate and return the sum of amounts of all transactions. Output: Float """ + return sum(tx.amount for tx in self.transactions) def txid_generator(tx_list): @@ -93,6 +129,9 @@ def txid_generator(tx_list): TODO: Yield each txid from the list one by one. Input: List of strings """ + for txid in tx_list: + yield txid + def filter_txids_gen(tx_list, prefix="tx"): """ @@ -100,3 +139,6 @@ def filter_txids_gen(tx_list, prefix="tx"): Default prefix is 'tx'. Input: List of strings, prefix string """ + for txid in tx_list: + if txid.startswith(prefix): + yield txid From f80b1359dbd77a9f03553d0b07216f4707942894 Mon Sep 17 00:00:00 2001 From: joycherotich <117736061+joycherotich@users.noreply.github.com> Date: Fri, 27 Jun 2025 17:55:01 +0300 Subject: [PATCH 2/3] python exercise --- main.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index 430de53..8c34ac6 100644 --- a/main.py +++ b/main.py @@ -133,12 +133,13 @@ def txid_generator(tx_list): yield txid -def filter_txids_gen(tx_list, prefix="tx"): """ TODO: Yield txids from list that start with given prefix. Default prefix is 'tx'. Input: List of strings, prefix string """ +def filter_txids_gen(tx_list, prefix="tx"): + for txid in tx_list: if txid.startswith(prefix): yield txid From 4b875680ee011f40d6cf33753c02af8e15d48609 Mon Sep 17 00:00:00 2001 From: joycherotich <117736061+joycherotich@users.noreply.github.com> Date: Fri, 27 Jun 2025 18:03:45 +0300 Subject: [PATCH 3/3] python exercise --- main.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index 8c34ac6..bef9842 100644 --- a/main.py +++ b/main.py @@ -124,11 +124,13 @@ def total_balance(self): return sum(tx.amount for tx in self.transactions) -def txid_generator(tx_list): + """ TODO: Yield each txid from the list one by one. Input: List of strings """ +def txid_generator(tx_list): + for txid in tx_list: yield txid