Skip to content
TheChatArchiveThe Chat Archive
Metallama-4-maverick-17b-128e-instruct· MetaHugging Face LMArena Human Preference 140K

How to add currency conversion?

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with DeepSeekdeepseek-v3-0324’s answer.
  1. User

    How to add currency conversion?

  2. Meta Assistant

    To add currency conversion, you'll need to introduce a mechanism to store and retrieve conversion rates. Here's an example of how you can extend the Money class to support currency conversion.

    Step 1: Introduce a CurrencyConverter class

    from typing import Dict, Type
    
    class CurrencyConverter:
        def __init__(self) -> None:
            self.conversion_rates: Dict[Type, Dict[Type, float]] = {}
    
        def add_conversion_rate(self, from_currency: Type, to_currency: Type, rate: float) -> None:
            if from_currency not in self.conversion_rates:
                self.conversion_rates[from_currency] = {}
            self.conversion_rates[from_currency][to_currency] = rate
    
        def convert(self, money: 'Money[T]', to_currency: Type) -> 'Money':
            from_currency = type(money)
            if from_currency == to_currency:
                return money
            if from_currency not in self.conversion_rates or to_currency not in self.conversion_rates[from_currency]:
                raise ValueError(f"No conversion rate available from {from_currency.__name__} to {to_currency.__name__}")
            rate = self.conversion_rates[from_currency][to_currency]
            return Money[to_currency](money.amount * rate)
    

    Step 2: Modify the Money class to use the CurrencyConverter

    T = TypeVar('T')
    
    class Money(Generic[T]):
        converter: CurrencyConverter = CurrencyConverter()
    
        def __init__(self, amount: float) -> None:
            self.amount = amount
    
        def __add__(self, other: 'Money[T]') -> 'Money[T]':
            return Money(self.amount + other.amount)
    
        def __sub__(self, other: 'Money[T]') -> 'Money[T]':
            return Money(self.amount - other.amount)
    
        def convert_to(self, to_currency: Type) -> 'Money':
            return self.converter.convert(self, to_currency)
    
        def __repr__(self) -> str:
            return f"Money[{T.__name__}]({self.amount})"
    

    Step 3: Usage example

    # Set up conversion rates
    Money.converter.add_conversion_rate(USD, EUR, 0.88)
    Money.converter.add_conversion_rate(EUR, USD, 1.14)
    
    # Create money instances
    usd = Money[USD](100)
    eur = Money[EUR](70)
    
    # Convert currencies
    usd_in_eur = usd.convert_to(EUR)
    eur_in_usd = eur.convert_to(USD)
    
    print(usd_in_eur)  # Money[EUR](88.0)
    print(eur_in_usd)  # Money[USD](79.8)
    

    Explanation

    • The CurrencyConverter class stores conversion rates between different currencies.
    • The convert method converts a Money instance from one currency to another using the stored conversion rates.
    • The Money class now has a convert_to method that uses the CurrencyConverter to convert the currency.
    • You can add conversion rates using the add_conversion_rate method.

    Optional: Improving the CurrencyConverter

    You can improve the CurrencyConverter by:

    • Using a more robust data structure to store conversion rates (e.g., a graph data structure).
    • Implementing a mechanism to update conversion rates dynamically (e.g., using an API).
    • Adding error handling for cases where conversion rates are not available.

    Optional: Type-safe conversion

    To make the conversion type-safe, you can introduce a new type variable for the target currency:

    U = TypeVar('U')
    
    class Money(Generic[T]):
        # ...
    
        def convert_to(self, to_currency: Type[U]) -> 'Money[U]':
            return self.converter.convert(self, to_currency)
    

    This way, the type checker will ensure that the conversion is done to a specific currency type.

    By using this approach, you can add currency conversion to your Money class while maintaining type safety.

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