mirror of
https://github.com/harivansh-afk/veet-code.git
synced 2026-04-17 14:01:25 +00:00
problems
This commit is contained in:
parent
76062021e9
commit
c548626d7f
13 changed files with 570 additions and 1 deletions
33
problems/medium/group-transactions/solution.py
Normal file
33
problems/medium/group-transactions/solution.py
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
"""
|
||||
Transaction Grouper
|
||||
|
||||
You're building a financial dashboard for a budgeting app. Users want
|
||||
to see their spending grouped by category with totals, so they can
|
||||
understand where their money is going each month.
|
||||
|
||||
Example 1:
|
||||
Input: transactions = [
|
||||
{"amount": 50, "category": "food", "date": "2024-01-01"},
|
||||
{"amount": 30, "category": "food", "date": "2024-01-02"},
|
||||
{"amount": 100, "category": "transport", "date": "2024-01-01"}
|
||||
]
|
||||
Output: {"food": 80, "transport": 100}
|
||||
Explanation: food: 50+30=80, transport: 100
|
||||
|
||||
Example 2:
|
||||
Input: transactions = []
|
||||
Output: {}
|
||||
Explanation: No transactions means empty result
|
||||
|
||||
Constraints:
|
||||
- Each transaction has "amount" (positive int), "category" (str), "date" (str)
|
||||
- Categories are case-sensitive
|
||||
- Return categories in any order
|
||||
- Amount is always positive
|
||||
"""
|
||||
|
||||
|
||||
def group_transactions(transactions: list[dict]) -> dict[str, int]:
|
||||
"""Return dictionary mapping each category to its total amount."""
|
||||
pass # Your implementation here
|
||||
|
||||
58
problems/medium/group-transactions/tests.py
Normal file
58
problems/medium/group-transactions/tests.py
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
"""Tests for group-transactions."""
|
||||
import pytest
|
||||
from solution import group_transactions
|
||||
|
||||
|
||||
class TestBasicCases:
|
||||
"""Test basic functionality with typical inputs."""
|
||||
|
||||
def test_multiple_categories(self):
|
||||
"""Test grouping across different categories."""
|
||||
txns = [
|
||||
{"amount": 50, "category": "food", "date": "2024-01-01"},
|
||||
{"amount": 30, "category": "food", "date": "2024-01-02"},
|
||||
{"amount": 100, "category": "transport", "date": "2024-01-01"}
|
||||
]
|
||||
assert group_transactions(txns) == {"food": 80, "transport": 100}
|
||||
|
||||
def test_single_category(self):
|
||||
"""Test all transactions in one category."""
|
||||
txns = [
|
||||
{"amount": 10, "category": "food", "date": "2024-01-01"},
|
||||
{"amount": 20, "category": "food", "date": "2024-01-02"},
|
||||
{"amount": 30, "category": "food", "date": "2024-01-03"}
|
||||
]
|
||||
assert group_transactions(txns) == {"food": 60}
|
||||
|
||||
def test_single_transaction(self):
|
||||
"""Test with just one transaction."""
|
||||
txns = [{"amount": 25, "category": "entertainment", "date": "2024-01-01"}]
|
||||
assert group_transactions(txns) == {"entertainment": 25}
|
||||
|
||||
|
||||
class TestEdgeCases:
|
||||
"""Test edge cases and boundary conditions."""
|
||||
|
||||
def test_empty_list(self):
|
||||
"""Test with no transactions."""
|
||||
assert group_transactions([]) == {}
|
||||
|
||||
def test_case_sensitive_categories(self):
|
||||
"""Test that categories are case-sensitive."""
|
||||
txns = [
|
||||
{"amount": 10, "category": "Food", "date": "2024-01-01"},
|
||||
{"amount": 20, "category": "food", "date": "2024-01-02"}
|
||||
]
|
||||
result = group_transactions(txns)
|
||||
assert result == {"Food": 10, "food": 20}
|
||||
|
||||
def test_many_categories(self):
|
||||
"""Test with many different categories."""
|
||||
txns = [
|
||||
{"amount": 1, "category": "a", "date": "2024-01-01"},
|
||||
{"amount": 2, "category": "b", "date": "2024-01-01"},
|
||||
{"amount": 3, "category": "c", "date": "2024-01-01"},
|
||||
{"amount": 4, "category": "d", "date": "2024-01-01"}
|
||||
]
|
||||
assert group_transactions(txns) == {"a": 1, "b": 2, "c": 3, "d": 4}
|
||||
|
||||
32
problems/medium/word-frequency/solution.py
Normal file
32
problems/medium/word-frequency/solution.py
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
"""
|
||||
Word Frequency Counter
|
||||
|
||||
You're building a text analytics tool for a content marketing team.
|
||||
Given a block of text, return the top N most frequently used words,
|
||||
sorted by frequency (highest first), then alphabetically for ties.
|
||||
|
||||
Example 1:
|
||||
Input: text = "the quick brown fox jumps over the lazy dog the fox"
|
||||
n = 2
|
||||
Output: [("the", 3), ("fox", 2)]
|
||||
Explanation: "the" appears 3 times, "fox" appears 2 times
|
||||
|
||||
Example 2:
|
||||
Input: text = "hello world hello"
|
||||
n = 5
|
||||
Output: [("hello", 2), ("world", 1)]
|
||||
Explanation: Only 2 unique words, return all of them
|
||||
|
||||
Constraints:
|
||||
- Words are separated by whitespace
|
||||
- Case-insensitive (convert to lowercase)
|
||||
- Ignore punctuation attached to words
|
||||
- n >= 1
|
||||
- If fewer than n unique words exist, return all of them
|
||||
"""
|
||||
|
||||
|
||||
def top_words(text: str, n: int) -> list[tuple[str, int]]:
|
||||
"""Return top n words by frequency as list of (word, count) tuples."""
|
||||
pass # Your implementation here
|
||||
|
||||
52
problems/medium/word-frequency/tests.py
Normal file
52
problems/medium/word-frequency/tests.py
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
"""Tests for word-frequency."""
|
||||
import pytest
|
||||
from solution import top_words
|
||||
|
||||
|
||||
class TestBasicCases:
|
||||
"""Test basic functionality with typical inputs."""
|
||||
|
||||
def test_basic_frequency(self):
|
||||
"""Test basic word counting."""
|
||||
result = top_words("the quick brown fox jumps over the lazy dog the fox", 2)
|
||||
assert result == [("the", 3), ("fox", 2)]
|
||||
|
||||
def test_all_unique(self):
|
||||
"""Test when all words are unique."""
|
||||
result = top_words("one two three", 2)
|
||||
assert result == [("one", 1), ("three", 1)] or result == [("one", 1), ("two", 1)]
|
||||
|
||||
def test_single_word_repeated(self):
|
||||
"""Test with one word repeated."""
|
||||
result = top_words("hello hello hello", 1)
|
||||
assert result == [("hello", 3)]
|
||||
|
||||
|
||||
class TestEdgeCases:
|
||||
"""Test edge cases and boundary conditions."""
|
||||
|
||||
def test_empty_string(self):
|
||||
"""Test with empty input."""
|
||||
result = top_words("", 5)
|
||||
assert result == []
|
||||
|
||||
def test_n_greater_than_unique_words(self):
|
||||
"""Test when n exceeds unique word count."""
|
||||
result = top_words("hello world", 10)
|
||||
assert len(result) == 2
|
||||
|
||||
def test_case_insensitive(self):
|
||||
"""Test that counting is case-insensitive."""
|
||||
result = top_words("Hello HELLO hello", 1)
|
||||
assert result == [("hello", 3)]
|
||||
|
||||
def test_punctuation_ignored(self):
|
||||
"""Test that punctuation is stripped."""
|
||||
result = top_words("hello, world! hello.", 1)
|
||||
assert result == [("hello", 2)]
|
||||
|
||||
def test_alphabetical_tiebreaker(self):
|
||||
"""Test alphabetical ordering for same frequency."""
|
||||
result = top_words("cat bat ant", 3)
|
||||
assert result == [("ant", 1), ("bat", 1), ("cat", 1)]
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue