-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
114 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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 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,62 @@ | ||
{-# LANGUAGE DeriveGeneric, NamedFieldPuns, OverloadedStrings #-} | ||
module Caide.Parsers.LeetCodeContest( | ||
contestParser | ||
) where | ||
|
||
import Control.Monad.Extended (liftIO) | ||
import qualified Data.ByteString.Lazy as LBS | ||
import Data.Either.Util (mapLeft) | ||
import qualified Data.Text as T | ||
import qualified Data.Text.Encoding as T | ||
import Data.Text (Text) | ||
|
||
import GHC.Generics (Generic) | ||
import Network.URI (parseURI, pathSegments, URI(uriPath, uriQuery, uriFragment)) | ||
|
||
import qualified Data.Aeson as Aeson | ||
|
||
import Caide.Commands.ParseProblem (parseProblems) | ||
import Caide.Parsers.Common (URL, ContestParser(..), isHostOneOf) | ||
import Caide.Types | ||
import Caide.Util (downloadDocument, tshow) | ||
|
||
|
||
isLeetCodeUrl :: URL -> Bool | ||
isLeetCodeUrl = isHostOneOf ["leetcode.com", "www.leetcode.com", "leetcode-cn.com", "www.leetcode-cn.com"] | ||
|
||
|
||
contestParser :: ContestParser | ||
contestParser = ContestParser | ||
{ contestUrlMatches = isLeetCodeUrl | ||
, parseContest = doParseContest | ||
} | ||
|
||
newtype ProblemInContest = ProblemInContest { title_slug :: Text } | ||
deriving (Show, Generic) | ||
instance Aeson.FromJSON ProblemInContest | ||
|
||
newtype Contest = Contest { questions :: [ProblemInContest] } | ||
deriving (Show, Generic) | ||
instance Aeson.FromJSON Contest | ||
|
||
|
||
eitherDecodeText' :: Aeson.FromJSON a => Text -> Either Text a | ||
eitherDecodeText' = mapLeft T.pack . Aeson.eitherDecode' . LBS.fromStrict . T.encodeUtf8 | ||
|
||
doParseContest :: URL -> CaideIO () | ||
doParseContest url = case (mbUri, mbPathSegments) of | ||
(Just uri, Just seg) | length seg >= 2 && seg !! (length seg - 2) == "contest" -> do | ||
let contestId = last seg | ||
apiUri = uri{uriPath = "/contest/api/info/" <> contestId <> "/", uriQuery="", uriFragment=""} | ||
probUrlPrefix = tshow $ apiUri{uriPath="/problems/"} | ||
mbDoc <- liftIO $ downloadDocument $ tshow apiUri | ||
case mbDoc >>= eitherDecodeText' of | ||
Left err -> throw err | ||
Right (Contest{questions}) -> parseProblems 3 $ | ||
[ probUrlPrefix <> (title_slug prob) | prob <- questions ] | ||
|
||
_ -> throw "Invalid contest url" | ||
where | ||
mbUri = parseURI (T.unpack url) | ||
mbPathSegments = pathSegments <$> mbUri | ||
|
This file contains 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,6 @@ | ||
#!/bin/bash | ||
|
||
basic_init | ||
"$CAIDE" contest https://leetcode.com/contest/weekly-contest-291 | ||
compare_with after-parse {total-appeal-of-a-string,k-divisible-elements-subarrays,minimum-consecutive-cards-to-pick-up,remove-digit-from-number-to-maximize-result}/{problem.ini,case{1,2}.{in,out}} | ||
|
Empty file.
1 change: 1 addition & 0 deletions
1
libcaide/tests/leetcode-contest/after-parse/k-divisible-elements-subarrays/case1.out
This file contains 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 @@ | ||
11 |
3 changes: 3 additions & 0 deletions
3
libcaide/tests/leetcode-contest/after-parse/k-divisible-elements-subarrays/case2.in
This file contains 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,3 @@ | ||
[1,2,3,4] | ||
4 | ||
1 |
1 change: 1 addition & 0 deletions
1
libcaide/tests/leetcode-contest/after-parse/k-divisible-elements-subarrays/case2.out
This file contains 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 @@ | ||
10 |
5 changes: 5 additions & 0 deletions
5
libcaide/tests/leetcode-contest/after-parse/k-divisible-elements-subarrays/problem.ini
This file contains 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,5 @@ | ||
[problem] | ||
double_precision: 0.000001 | ||
name: K Divisible Elements Subarrays | ||
type: leetcode,countDistinct:int,nums:vint,k:int,p:int | ||
|
1 change: 1 addition & 0 deletions
1
libcaide/tests/leetcode-contest/after-parse/minimum-consecutive-cards-to-pick-up/case1.in
This file contains 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 @@ | ||
[3,4,2,3,4,7] |
1 change: 1 addition & 0 deletions
1
libcaide/tests/leetcode-contest/after-parse/minimum-consecutive-cards-to-pick-up/case1.out
This file contains 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 @@ | ||
4 |
1 change: 1 addition & 0 deletions
1
libcaide/tests/leetcode-contest/after-parse/minimum-consecutive-cards-to-pick-up/case2.in
This file contains 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 @@ | ||
[1,0,5,3] |
1 change: 1 addition & 0 deletions
1
libcaide/tests/leetcode-contest/after-parse/minimum-consecutive-cards-to-pick-up/case2.out
This file contains 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 @@ | ||
-1 |
5 changes: 5 additions & 0 deletions
5
libcaide/tests/leetcode-contest/after-parse/minimum-consecutive-cards-to-pick-up/problem.ini
This file contains 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,5 @@ | ||
[problem] | ||
double_precision: 0.000001 | ||
name: Minimum Consecutive Cards to Pick Up | ||
type: leetcode,minimumCardPickup:int,cards:vint | ||
|
2 changes: 2 additions & 0 deletions
2
...e/tests/leetcode-contest/after-parse/remove-digit-from-number-to-maximize-result/case1.in
This file contains 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,2 @@ | ||
"123" | ||
"3" |
1 change: 1 addition & 0 deletions
1
.../tests/leetcode-contest/after-parse/remove-digit-from-number-to-maximize-result/case1.out
This file contains 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 @@ | ||
"12" |
2 changes: 2 additions & 0 deletions
2
...e/tests/leetcode-contest/after-parse/remove-digit-from-number-to-maximize-result/case2.in
This file contains 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,2 @@ | ||
"1231" | ||
"1" |
1 change: 1 addition & 0 deletions
1
.../tests/leetcode-contest/after-parse/remove-digit-from-number-to-maximize-result/case2.out
This file contains 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 @@ | ||
"231" |
2 changes: 2 additions & 0 deletions
2
...e/tests/leetcode-contest/after-parse/remove-digit-from-number-to-maximize-result/case3.in
This file contains 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,2 @@ | ||
"551" | ||
"5" |
1 change: 1 addition & 0 deletions
1
.../tests/leetcode-contest/after-parse/remove-digit-from-number-to-maximize-result/case3.out
This file contains 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 @@ | ||
"51" |
5 changes: 5 additions & 0 deletions
5
...ests/leetcode-contest/after-parse/remove-digit-from-number-to-maximize-result/problem.ini
This file contains 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,5 @@ | ||
[problem] | ||
double_precision: 0.000001 | ||
name: Remove Digit From Number to Maximize Result | ||
type: leetcode,removeDigit:String,number:String,digit:character | ||
|
1 change: 1 addition & 0 deletions
1
libcaide/tests/leetcode-contest/after-parse/total-appeal-of-a-string/case1.in
This file contains 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 @@ | ||
"abbca" |
1 change: 1 addition & 0 deletions
1
libcaide/tests/leetcode-contest/after-parse/total-appeal-of-a-string/case1.out
This file contains 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 |
1 change: 1 addition & 0 deletions
1
libcaide/tests/leetcode-contest/after-parse/total-appeal-of-a-string/case2.in
This file contains 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 @@ | ||
"code" |
1 change: 1 addition & 0 deletions
1
libcaide/tests/leetcode-contest/after-parse/total-appeal-of-a-string/case2.out
This file contains 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 @@ | ||
20 |
5 changes: 5 additions & 0 deletions
5
libcaide/tests/leetcode-contest/after-parse/total-appeal-of-a-string/problem.ini
This file contains 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,5 @@ | ||
[problem] | ||
double_precision: 0.000001 | ||
name: Total Appeal of A String | ||
type: leetcode,appealSum:long,s:String | ||
|