-
Notifications
You must be signed in to change notification settings - Fork 0
13 parse readme to collect test values #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
7f0766d
feat(readme_parser): draft of readme_parser
dfayd0 483ea75
test(readme_parser): first tests for the readme_parser
dfayd0 53289d6
refactor(cargo.toml): renaming binary from leetcode_cli to leetcode-cli
dfayd0 7eff113
feat(readme_parser): support for extracting primitive forms of input …
dfayd0 d745b06
test(readme_parser): providing tests support by matching against real…
dfayd0 d97c980
fix(ux): user now can skip specifying --lang in cli and uses the defa…
dfayd0 3130f5e
fix(main): proper fix for using default_language
dfayd0 ea8a316
refactor(config-api-parser): making clippy satisfied
dfayd0 6d046a0
test(cli-utils): fixing tests
dfayd0 15be84d
refactor(readme_parser): improving readability
dfayd0 abaca54
test(readme_parser): improving the readme_parser test suite by adding…
dfayd0 521f9d6
feat(readme_parser): removing variable name and = from inputs, suppor…
dfayd0 1e52bb8
refactor(readme_parser): changed dynamic error to statically defined …
guilhem-sante 0df4156
style(*): style config not yet shared :(
dfayd0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| pub mod cli; | ||
| pub mod config; | ||
| pub mod leetcode_api_runner; | ||
| pub mod readme_parser; | ||
| pub mod utils; | ||
|
|
||
| pub use cli::{ | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| use regex::Regex; | ||
| use thiserror; | ||
|
|
||
| pub struct LeetcodeReadmeParser { | ||
| pub raw: String, | ||
| } | ||
|
|
||
| pub struct ProblemTestData { | ||
| pub example_count: usize, | ||
| pub inputs: Vec<String>, | ||
| pub outputs: Vec<String>, | ||
| } | ||
|
|
||
| #[derive(thiserror::Error, Debug, Clone, Copy, PartialEq, Eq)] | ||
| pub enum LeetcodeReadmeParserError { | ||
| #[error("can't parse empty readme")] | ||
| EmptyReadme, | ||
| } | ||
|
|
||
| impl LeetcodeReadmeParser { | ||
| pub fn new(readme: &str) -> Self { | ||
| LeetcodeReadmeParser { | ||
| raw: readme.to_string(), | ||
| } | ||
| } | ||
|
|
||
| pub fn parse(&self) -> Result<ProblemTestData, LeetcodeReadmeParserError> { | ||
| if self.raw.is_empty() { | ||
| return Err(LeetcodeReadmeParserError::EmptyReadme); | ||
| } | ||
| Ok(ProblemTestData { | ||
| example_count: self.count_examples(), | ||
| inputs: self.extract_inputs(), | ||
| outputs: self.extract_outputs(), | ||
| }) | ||
| } | ||
|
|
||
| fn count_examples(&self) -> usize { | ||
| self.raw | ||
| .lines() | ||
| .filter(|line| line.starts_with("**Example")) | ||
| .count() | ||
| } | ||
|
|
||
| fn extract_inputs(&self) -> Vec<String> { | ||
| self.extract_from_pattern(r"(?m)^\s*\*?\*?Input:\*?\*?\s*(.*)$") | ||
| } | ||
|
|
||
| fn extract_outputs(&self) -> Vec<String> { | ||
| self.extract_from_pattern(r"(?m)^\s*\*?\*?Output:\*?\*?\s*(.*)$") | ||
| } | ||
|
|
||
| fn extract_from_pattern(&self, pattern: &str) -> Vec<String> { | ||
| let re = Regex::new(pattern).unwrap(); | ||
|
|
||
| let mut result = Vec::new(); | ||
| for capture in re.captures_iter(&self.raw) { | ||
| if let Some(matched) = capture.get(1) { | ||
| let input = matched | ||
| .as_str() | ||
| .replace(['\n', '\t'], " ") | ||
| .trim() | ||
| .to_string(); | ||
|
|
||
| let trimmed = if input.contains('=') { | ||
| input | ||
| .split(',') | ||
| .filter_map(|part| { | ||
| if let Some(eq_pos) = part.find('=') { | ||
| Some(part[eq_pos + 1..].trim()) | ||
| } else { | ||
| // Handle continuation of previous array/value | ||
| let trimmed_part = part.trim(); | ||
| (!trimmed_part.is_empty()) | ||
| .then_some(trimmed_part) | ||
| } | ||
| }) | ||
| .collect::<Vec<_>>() | ||
| .join(",") | ||
| } else { | ||
| input.to_string() | ||
| }; | ||
|
|
||
| result.push(trimmed); | ||
| } | ||
| } | ||
| result | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| # Problem 1004: Max_Consecutive_Ones_III | ||
|
|
||
| Given a binary array `nums` and an integer `k`, return *the maximum number of consecutive* `1`*'s in the array if you can flip at most* `k` `0`'s. | ||
|
|
||
| **Example 1:** | ||
|
|
||
| ``` | ||
| Input: nums = [1,1,1,0,0,0,1,1,1,1,0], k = 2 | ||
| Output: 6 | ||
| Explanation: [1,1,1,0,0,1,1,1,1,1,1] | ||
| Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. | ||
| ``` | ||
|
|
||
| **Example 2:** | ||
|
|
||
| ``` | ||
| Input: nums = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], k = 3 | ||
| Output: 10 | ||
| Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] | ||
| Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. | ||
|
|
||
| ``` | ||
|
|
||
| **Constraints:** | ||
|
|
||
| * `1 <= nums.length <= 10<sup>5</sup>` | ||
| * `nums[i]` is either `0` or `1`. | ||
| * `0 <= k <= nums.length` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| # Problem 1768: Merge_Strings_Alternately | ||
|
|
||
| You are given two strings `word1` and `word2`. Merge the strings by adding letters in alternating order, starting with `word1`. If a string is longer than the other, append the additional letters onto the end of the merged string. | ||
|
|
||
| Return *the merged string.* | ||
|
|
||
| **Example 1:** | ||
|
|
||
| ``` | ||
| Input: word1 = "abc", word2 = "pqr" | ||
| Output: "apbqcr" | ||
| Explanation: The merged string will be merged as so: | ||
| word1: a b c | ||
| word2: p q r | ||
| merged: a p b q c r | ||
|
|
||
| ``` | ||
|
|
||
| **Example 2:** | ||
|
|
||
| ``` | ||
| Input: word1 = "ab", word2 = "pqrs" | ||
| Output: "apbqrs" | ||
| Explanation: Notice that as word2 is longer, "rs" is appended to the end. | ||
| word1: a b | ||
| word2: p q r s | ||
| merged: a p b q r s | ||
|
|
||
| ``` | ||
|
|
||
| **Example 3:** | ||
|
|
||
| ``` | ||
| Input: word1 = "abcd", word2 = "pq" | ||
| Output: "apbqcd" | ||
| Explanation: Notice that as word1 is longer, "cd" is appended to the end. | ||
| word1: a b c d | ||
| word2: p q | ||
| merged: a p b q c d | ||
|
|
||
| ``` | ||
|
|
||
| **Constraints:** | ||
|
|
||
| * `1 <= word1.length, word2.length <= 100` | ||
| * `word1` and `word2` consist of lowercase English letters. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| # Problem 221: Maximal_Square | ||
|
|
||
| Given an `m x n` binary `matrix` filled with `0`'s and `1`'s, *find the largest square containing only* `1`'s *and return its area*. | ||
|
|
||
| **Example 1:** | ||
|
|
||
|  | ||
|
|
||
| ``` | ||
| Input: matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]] | ||
| Output: 4 | ||
|
|
||
| ``` | ||
|
|
||
| **Example 2:** | ||
|
|
||
|  | ||
|
|
||
| ``` | ||
| Input: matrix = [["0","1"],["1","0"]] | ||
| Output: 1 | ||
|
|
||
| ``` | ||
|
|
||
| **Example 3:** | ||
|
|
||
| ``` | ||
| Input: matrix = [["0"]] | ||
| Output: 0 | ||
|
|
||
| ``` | ||
|
|
||
| **Constraints:** | ||
|
|
||
| * `m == matrix.length` | ||
| * `n == matrix[i].length` | ||
| * `1 <= m, n <= 300` | ||
| * `matrix[i][j]` is `'0'` or `'1'`. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.