---
title: "Lazy Solutions"
description: "`If-else` or `match-case` statements are something I try to avoid whenever possible. I'll explain why in upcoming posts. So, what should you write instead?"
canonical_url: "https://otabek.io/blogs/dangasa-yechimlar"
md_url: "https://otabek.io/blogs/dangasa-yechimlar.md"
language: "en"
last_updated: "2024-05-17"
tags: ["Lazy Engineering"]
---

# Lazy Solutions

I try to avoid writing `If-else` or `match-case` statements as much as possible. I'll explain why in future posts. So, what should you write instead?

![](https://telegra.ph/file/444265b97572e2cd4c9ef.jpg)

Of course, it depends on the situation, but for many of the problems I encounter, I’ve solved them using a hashmap. Imagine you have a problem with specific or limited values. You need to solve it without writing `if-else` statements. For example, let’s say you need to check if a number is divisible by 2, and if it is, print "even", otherwise print "odd". What would you do?

Here’s how I would approach it:

```python
hashmap = {
    0: "even"
}

number = int(input("Enter a number: "))
print(hashmap.get(number % 2, "odd"))
```

Now, let's make the problem a bit more complex. You're building a system for currency conversion. The user enters an **amount**, **from** currency, and **to** currency, and you need to calculate the conversion without using `if-else` statements.

For example, here’s the data the user enters:

```bash
Amount: 24
From: USD
To: UZS
```

Now, you need to convert $24 into Uzbek Som (UZS). How would you do it?

Here’s my solution:

```python
# Store the conversion rate for 1$ for each currency
currencies = {
    "USD": 1,
    "UZS": 11300,
    "RUB": 90,
    "EUR": 0.85,
}

# Conversion methods
convert = {
    "TO_USD": lambda amount, rate: amount / rate,
    "FROM_USD": lambda amount, rate: amount * rate,
}

# User input part
amount = float(input("Amount: "))
from_currency = input("From: ").upper()
to_currency = input("To: ").upper()

# Convert the amount to USD first, then convert it to the desired currency
in_usd = convert["TO_USD"](amount, currencies[from_currency])

# Now convert the USD amount to the target currency
result = convert["FROM_USD"](in_usd, currencies[to_currency])

print(result)
```

![](https://media2.giphy.com/media/l4FB49aNstvAcSBQ4/giphy.gif?cid=6c09b9525m3ww55i9mnf6k5zfqgfoyg3mznoov3u6mpyocku&ep=v1_gifs_search&rid=giphy.gif&ct=g)

Think about lazy solutions like this. I'm glad if this was helpful!

---

```quiz
{
  "quiz": {
    "id": "hashmap-solutions-quiz",
    "title": "Hashmap Solutions Quiz",
    "description": "Test your understanding of using hashmaps to replace if-else",
    "questions": [
      {
        "id": "q1",
        "type": "single-choice",
        "question": "Why might you want to avoid if-else statements?",
        "options": [
          { "id": "a", "text": "They can become complex and hard to maintain", "description": "" },
          { "id": "b", "text": "They don't work in Python", "description": "If-else works fine, but hashmaps can be cleaner." },
          { "id": "c", "text": "They are slower than anything else", "description": "It's more about code clarity than raw speed." },
          { "id": "d", "text": "They cause errors", "description": "If-else works fine, but hashmaps offer elegance." }
        ]
      },
      {
        "id": "q2",
        "type": "drag-fill",
        "question": "Complete the hashmap solution for even/odd:",
        "template": "hashmap = { 0: \"even\" }\\nprint(hashmap.{{b1}}(number % 2, {{b2}}))",
        "options": [
          { "id": "opt1", "content": "get" },
          { "id": "opt2", "content": "\"odd\"" }
        ],
        "blanks": [
          { "id": "b1" },
          { "id": "b2" }
        ]
      },
      {
        "id": "q3",
        "type": "single-choice",
        "question": "What does hashmap.get(key, default) return if key doesn't exist?",
        "options": [
          { "id": "a", "text": "The default value", "description": "" },
          { "id": "b", "text": "An error", "description": ".get() returns the default, not an error." },
          { "id": "c", "text": "None always", "description": ".get() returns the specified default value." },
          { "id": "d", "text": "The first value in the hashmap", "description": ".get() returns the default for missing keys." }
        ]
      },
      {
        "id": "q4",
        "type": "single-choice",
        "question": "In the currency converter, what is stored as the value for each currency?",
        "options": [
          { "id": "a", "text": "The conversion rate relative to USD", "description": "" },
          { "id": "b", "text": "The country name", "description": "The example stores exchange rates, not country names." },
          { "id": "c", "text": "A random number", "description": "The rates are meaningful conversion values." },
          { "id": "d", "text": "The currency symbol", "description": "The key is the symbol, the value is the rate." }
        ]
      },
      {
        "id": "q5",
        "type": "single-choice",
        "question": "What can you store as values in a Python dictionary?",
        "options": [
          { "id": "a", "text": "Any type including functions (lambdas)", "description": "" },
          { "id": "b", "text": "Only strings", "description": "Dictionaries can store any type, including functions." },
          { "id": "c", "text": "Only numbers", "description": "Dictionaries can store any type of value." },
          { "id": "d", "text": "Only lists", "description": "Dictionaries are very flexible with value types." }
        ]
      },
      {
        "id": "q6",
        "type": "drag-drop",
        "question": "Arrange the currency conversion steps:",
        "items": [
          { "id": "input", "content": "Get amount, from_currency, to_currency" },
          { "id": "tousd", "content": "Convert amount to USD" },
          { "id": "fromusd", "content": "Convert USD to target currency" },
          { "id": "output", "content": "Return the result" }
        ]
      }
    ]
  },
  "answers": {
    "q1": { "correctOptionIds": ["a"] },
    "q2": { "correctPlacements": { "b1": "opt1", "b2": "opt2" } },
    "q3": { "correctOptionIds": ["a"] },
    "q4": { "correctOptionIds": ["a"] },
    "q5": { "correctOptionIds": ["a"] },
    "q6": { "correctOrder": ["input", "tousd", "fromusd", "output"] }
  }
}
```


## Sitemap

See the full [Markdown sitemap](/sitemap.md) for all pages.
