This commit is contained in:
Harivansh Rathi 2025-12-14 17:15:37 -05:00
parent 76062021e9
commit c548626d7f
13 changed files with 570 additions and 1 deletions

View file

@ -0,0 +1,33 @@
"""
Palindrome Checker
You're building a word game that awards bonus points for palindromes.
Given a string, determine if it reads the same forwards and backwards,
ignoring case and non-alphanumeric characters.
Example 1:
Input: text = "A man, a plan, a canal: Panama"
Output: True
Explanation: "amanaplanacanalpanama" is a palindrome
Example 2:
Input: text = "race a car"
Output: False
Explanation: "raceacar" is not a palindrome
Example 3:
Input: text = " "
Output: True
Explanation: Empty after removing non-alphanumeric
Constraints:
- Input is always a string
- Ignore spaces, punctuation, and case
- Empty string is considered a palindrome
"""
def is_palindrome(text: str) -> bool:
"""Return True if text is a palindrome, False otherwise."""
pass # Your implementation here

View file

@ -0,0 +1,44 @@
"""Tests for palindrome-checker."""
import pytest
from solution import is_palindrome
class TestBasicCases:
"""Test basic functionality with typical inputs."""
def test_simple_palindrome(self):
"""Test basic palindrome word."""
assert is_palindrome("racecar") == True
def test_sentence_palindrome(self):
"""Test palindrome with spaces and punctuation."""
assert is_palindrome("A man, a plan, a canal: Panama") == True
def test_not_palindrome(self):
"""Test non-palindrome string."""
assert is_palindrome("hello") == False
class TestEdgeCases:
"""Test edge cases and boundary conditions."""
def test_empty_string(self):
"""Test with empty input."""
assert is_palindrome("") == True
def test_single_character(self):
"""Test with single character."""
assert is_palindrome("a") == True
def test_only_spaces(self):
"""Test with only whitespace."""
assert is_palindrome(" ") == True
def test_mixed_case(self):
"""Test case insensitivity."""
assert is_palindrome("RaceCar") == True
def test_numbers_in_string(self):
"""Test with numbers."""
assert is_palindrome("12321") == True

View file

@ -0,0 +1,31 @@
"""
Two Sum
You are a cashier at a busy store. A customer wants to pay exactly
a target amount using exactly two items from their basket.
Given a list of item prices and a target total, find the indices
of the two items that add up to the target.
Example 1:
Input: prices = [2, 7, 11, 15], target = 9
Output: [0, 1]
Explanation: prices[0] + prices[1] = 2 + 7 = 9
Example 2:
Input: prices = [3, 2, 4], target = 6
Output: [1, 2]
Constraints:
- 2 <= len(prices) <= 10^4
- Each price is a positive integer
- Exactly one solution exists
"""
def two_sum(prices: list[int], target: int) -> list[int]:
"""Return indices of two prices that add up to target."""
for i in range(len(prices)):
for j in range(i+1, len(prices)):
if prices[i] + prices[j] == target:
return [i,j]

View file

@ -0,0 +1,35 @@
"""Tests for two-sum."""
import pytest
from solution import two_sum
class TestBasicCases:
"""Test basic functionality with typical inputs."""
def test_example_one(self):
"""Test first example from problem description."""
assert sorted(two_sum([2, 7, 11, 15], 9)) == [0, 1]
def test_example_two(self):
"""Test second example from problem description."""
assert sorted(two_sum([3, 2, 4], 6)) == [1, 2]
def test_adjacent_elements(self):
"""Test when answer elements are adjacent."""
assert sorted(two_sum([1, 2, 3, 4], 7)) == [2, 3]
class TestEdgeCases:
"""Test edge cases and boundary conditions."""
def test_same_value_twice(self):
"""Test with duplicate values that sum to target."""
assert sorted(two_sum([3, 3], 6)) == [0, 1]
def test_first_and_last(self):
"""Test when answer is first and last elements."""
assert sorted(two_sum([2, 4, 6, 8, 10, 7], 9)) == [0, 5]
def test_negative_numbers(self):
"""Test with negative numbers in the list."""
assert sorted(two_sum([-1, -2, -3, -4, -5], -8)) == [2, 4]