Skip to content

Commit 1185769

Browse files
authored
Merge pull request #34 from franciscoerramuspe/leetcode-problems
Leetcode problems
2 parents 9960a1c + fbd4341 commit 1185769

File tree

4 files changed

+213
-0
lines changed

4 files changed

+213
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
"""
2+
Problem Statement
3+
You are implementing a word-guessing game where players try to guess a secret four-letter word. After each guess, the game provides hints about which letters are correct and/or present in the secret word.
4+
5+
Write a function that takes two strings (the secret word and the guess) and returns a string containing hints in the form of 'W' and 'R' characters, where:
6+
- 'W' means the letter is correct and in the correct position
7+
- 'R' means the letter is present in the secret word but in the wrong position
8+
- The hint string should always show 'W's before 'R's
9+
10+
The function must process the hints in two passes:
11+
1. First pass: Mark all exact matches (same letter, same position) with 'W'
12+
2. Second pass: For remaining unmatched letters, mark with 'R' if they appear somewhere else in the secret word
13+
14+
Definition
15+
16+
Class: GuessHint
17+
Method: getHint
18+
Parameters: str, str
19+
Returns: str
20+
Method signature: def getHint(secret: str, guess: str) -> str
21+
22+
Constraints
23+
- Both secret and guess will be exactly 4 characters long
24+
- All characters will be uppercase letters from 'A' to 'Z'
25+
- The same letter can appear multiple times in either string
26+
27+
Examples
28+
0)
29+
30+
Input: secret = "ABCD", guess = "ABCD"
31+
Returns: "WWWW"
32+
All letters match exactly.
33+
34+
1)
35+
36+
Input: secret = "ABCD", guess = "DCBA"
37+
Returns: "RRRR"
38+
All letters exist but in wrong positions.
39+
40+
2)
41+
42+
Input: secret = "ABAA", guess = "BAAA"
43+
Returns: "WWRR"
44+
Two exact matches (positions 2,3) give "WW", then remaining unmatched letters (A,B) exist in wrong positions giving "RR".
45+
46+
3)
47+
48+
Input: secret = "ABCD", guess = "AAAA"
49+
Returns: "W"
50+
Only first A matches exactly. Other A's don't count since A was already matched.
51+
52+
4)
53+
54+
Input: secret = "ABCD", guess = "EFGH"
55+
Returns: ""
56+
No letters match or exist in the secret word.
57+
58+
Notes:
59+
- Each letter in the secret word can only be matched once
60+
- Priority goes to exact matches (W) before wrong position matches (R)
61+
- The result string must always have W's before R's
62+
"""
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import pytest
2+
3+
from guess_hint import GuessHint
4+
5+
def test_1():
6+
solution = GuessHint()
7+
assert "WWWW" == solution.getHint("ABCD", "ABCD")
8+
9+
def test_2():
10+
solution = GuessHint()
11+
assert "RRRR" == solution.getHint("ABCD", "DCBA")
12+
13+
def test_3():
14+
solution = GuessHint()
15+
assert "WWRR" == solution.getHint("ABAA", "BAAA")
16+
17+
def test_4():
18+
solution = GuessHint()
19+
assert "W" == solution.getHint("ABCD", "AAAA")
20+
21+
def test_5():
22+
solution = GuessHint()
23+
assert "" == solution.getHint("ABCD", "EFGH")
24+
25+
def test_6():
26+
solution = GuessHint()
27+
assert "WWR" == solution.getHint("AABB", "AAAB")
28+
29+
def test_7():
30+
solution = GuessHint()
31+
assert "WR" == solution.getHint("ABCD", "ACAA")
32+
33+
def test_8():
34+
solution = GuessHint()
35+
assert "WWRR" == solution.getHint("AAAA", "ABBA")
36+
37+
def test_9():
38+
solution = GuessHint()
39+
assert "RR" == solution.getHint("ABCD", "CDAA")
40+
41+
def test_10():
42+
solution = GuessHint()
43+
assert "WRR" == solution.getHint("ABCD", "ADBC")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""
2+
Problem Statement
3+
You are given a dictionary representing an on-call schedule where each key is a person's name and the value is a tuple of two integers representing their shift's start and end times in minutes.
4+
5+
Write a function that processes this schedule and returns all distinct time segments with their corresponding on-call personnel. A new segment starts or ends whenever someone's shift begins or ends.
6+
7+
For each segment in the schedule, determine all people who are on-call during that entire segment. The function should return a list of tuples, where each tuple contains:
8+
- The start time of the segment
9+
- The end time of the segment
10+
- A list of names of all people who are on-call during that segment
11+
12+
Definition
13+
14+
Class: OnCallSchedule
15+
Method: createOnCallSchedule
16+
Parameters: dict
17+
Returns: list
18+
Method signature: def createOnCallSchedule(onCallDict)
19+
20+
Constraints
21+
- The dictionary will contain between 1 and 100 entries
22+
- All times will be non-negative integers
23+
- For each person, start time will be strictly less than end time
24+
- All times will be between 0 and 1000000, inclusive
25+
26+
Examples
27+
0)
28+
29+
Input: {"Anna": (10, 100), "Juan": (30, 80)}
30+
Returns: [(10, 30, ["Anna"]), (30, 80, ["Anna", "Juan"]), (80, 100, ["Anna"])]
31+
32+
1)
33+
34+
Input: {"Ben": (50, 70), "Carla": (60, 120), "David": (150, 300)}
35+
Returns: [(50, 60, ["Ben"]), (60, 70, ["Ben", "Carla"]), (70, 120, ["Carla"]), (150, 300, ["David"])]
36+
37+
2)
38+
39+
Input: {"Alice": (0, 50), "Bob": (0, 50)}
40+
Returns: [(0, 50, ["Alice", "Bob"])]
41+
42+
Notes:
43+
- A person is considered on-call for their entire interval, including start and end times
44+
- The output should only include segments where at least one person is on-call
45+
- The segments should be in chronological order
46+
- Each segment's personnel list should be sorted alphabetically
47+
"""
48+
class Solution:
49+
def createOnCallSchedule(self, onCallDict):
50+
return []
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import pytest
2+
3+
from on_call_schedule import Solution
4+
5+
def test1():
6+
solution = Solution()
7+
assert [(10, 30, ["Anna"]),
8+
(30, 80, ["Anna", "Juan"]),
9+
(80, 100, ["Anna"])] == solution.createOnCallSchedule({
10+
"Anna": (10, 100),
11+
"Juan": (30, 80)
12+
})
13+
14+
def test2():
15+
solution = Solution()
16+
assert [(50, 60, ["Ben"]),
17+
(60, 70, ["Ben", "Carla"]),
18+
(70, 120, ["Carla"]),
19+
(150, 300, ["David"])] == solution.createOnCallSchedule({
20+
"Ben": (50, 70),
21+
"Carla": (60, 120),
22+
"David": (150, 300)
23+
})
24+
25+
def test3():
26+
solution = Solution()
27+
assert [(0, 50, ["Alice", "Bob"])] == solution.createOnCallSchedule({
28+
"Alice": (0, 50),
29+
"Bob": (0, 50)
30+
})
31+
32+
def test4():
33+
solution = Solution()
34+
assert [(10, 20, ["Alice"]),
35+
(30, 40, ["Bob"]),
36+
(50, 60, ["Charlie"])] == solution.createOnCallSchedule({
37+
"Alice": (10, 20),
38+
"Bob": (30, 40),
39+
"Charlie": (50, 60)
40+
})
41+
42+
def test5():
43+
solution = Solution()
44+
assert [(0, 10, ["Alice"]),
45+
(10, 20, ["Alice", "Bob"]),
46+
(20, 30, ["Alice", "Bob", "Charlie"]),
47+
(30, 40, ["Bob", "Charlie"]),
48+
(40, 50, ["Charlie"])] == solution.createOnCallSchedule({
49+
"Alice": (0, 30),
50+
"Bob": (10, 40),
51+
"Charlie": (20, 50)
52+
})
53+
54+
def test6():
55+
solution = Solution()
56+
assert [(100, 200, ["Alice"])] == solution.createOnCallSchedule({
57+
"Alice": (100, 200)
58+
})

0 commit comments

Comments
 (0)