Skip to content

Commit

Permalink
More LeetCode support
Browse files Browse the repository at this point in the history
See #50
  • Loading branch information
slycelote committed Jan 25, 2022
1 parent ae0f591 commit 7d01894
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 11 deletions.
22 changes: 22 additions & 0 deletions libcaide/res/templates/test_template.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,28 @@ public void Serialize(TextWriter output, double val)
}
}

class BoolSerializer : TCSerializer<bool>
{
public bool Deserialize(TextReader input)
{
input.SkipWhile(char.IsWhiteSpace);
StringBuilder s = new StringBuilder();
for (int i = 0; i < 4; ++i)
s.Append(input.ReadChar());
if (s.ToString() == "true")
return true;
else if (s.ToString() == "fals" && input.ReadChar() == 'e')
return false;
else
throw new IOException("true or fales is expected");
}

public void Serialize(TextWriter output, bool val)
{
output.Write(val ? "true" : "false");
}
}

class StringSerializer : TCSerializer<string>
{
public string Deserialize(TextReader input)
Expand Down
27 changes: 27 additions & 0 deletions libcaide/res/templates/topcoder_serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,33 @@ struct Serializer {
}
};

template<>
struct Serializer<bool> {
static void serialize(std::ostream& out, bool val) {
out << (val ? "true" : "false");
}

static bool deserialize(std::istream& in) {
std::string val(4, ' ');
for (int i = 0; i < 4; ++i)
if (!(in >> val[i]))
throw std::invalid_argument(
std::string("Couldn't deserialize a value of type bool"));

if (val == "true")
return true;
else if (val == "fals") {
char c;
in >> c;
if (c == 'e')
return false;
}

throw std::invalid_argument(
std::string("Couldn't deserialize a value of type bool"));
}
};

template<>
struct Serializer<std::string> {
static void serialize(std::ostream& out, const std::string& val) {
Expand Down
1 change: 1 addition & 0 deletions libcaide/src/Caide/CSharp/serializer.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
}}{{#type_is_int}}new Caide.IntSerializer(){{/type_is_int
}}{{#type_is_long}}new Caide.LongSerializer(){{/type_is_long
}}{{#type_is_double}}new Caide.DoubleSerializer(){{/type_is_double
}}{{#type_is_bool}}new Caide.BoolSerializer(){{/type_is_bool
}}{{#type_is_string}}new Caide.StringSerializer(){{/type_is_string
}}{{#dimension_is_1}}){{/dimension_is_1}}
26 changes: 18 additions & 8 deletions libcaide/src/Caide/Parsers/LeetCode.hs
Original file line number Diff line number Diff line change
Expand Up @@ -163,18 +163,28 @@ numTests :: [TestCase] -> (Int, Int)
numTests tests = (length [t | t@(TestCase _ (Just _)) <- tests], length tests)

typeParser :: Parsec.Parsec Text () (TopcoderType, Int)
typeParser = do
typeParser =
let (try, string, many, eof) = (Parsec.try, Parsec.string, Parsec.many, Parsec.eof)
typ <- try (string "integer" $> TCInt)
<|> try (string "string" $> TCString)
<|> try (string "double" $> TCDouble)
arr <- many (string "[]")
eof
return (typ, length arr)
elementTypeParser = try (string "integer" $> TCInt)
<|> try (string "string" $> TCString)
<|> try (string "double" $> TCDouble)
<|> try (string "boolean" $> TCBool)
format1 = do
_ <- try $ string "list<"
typ <- elementTypeParser
_ <- string ">"
eof
return (typ, 1)
format2 = do
typ <- elementTypeParser
arr <- many (string "[]")
eof
return (typ, length arr)
in format1 <|> format2

parseValue :: Param -> Either Text TopcoderValue
parseValue Param{name, type_} = do
(typ, dim) <- mapLeft tshow $ Parsec.parse typeParser "" type_
(typ, dim) <- mapLeft tshow $ Parsec.parse typeParser (T.unpack name) type_
return TopcoderValue{tcValueName = name, tcValueType = typ, tcValueDimension = dim}

parseMethod :: MethodMetaData -> Either Text TopcoderMethod
Expand Down
1 change: 1 addition & 0 deletions libcaide/src/Caide/Problem.hs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ encodeType TCInt = "int"
encodeType TCLong = "long"
encodeType TCDouble = "double"
encodeType TCString = "string"
encodeType TCBool = "bool"

encodeValue :: TopcoderValue -> Aeson.Value
encodeValue TopcoderValue{..} = Aeson.object $
Expand Down
4 changes: 3 additions & 1 deletion libcaide/src/Caide/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ data TopcoderValue = TopcoderValue
, tcValueDimension :: !Int -- ^ Dimension, e.g. 2 for vector<vector<int>>
} deriving (Show, Eq)

data TopcoderType = TCInt | TCLong | TCDouble | TCString
data TopcoderType = TCInt | TCLong | TCDouble | TCString | TCBool
deriving (Show, Eq)

data TopcoderMethod = TopcoderMethod
Expand Down Expand Up @@ -288,12 +288,14 @@ instance Option TopcoderType where
optionToString TCLong = "long"
optionToString TCDouble = "double"
optionToString TCString = "String"
optionToString TCBool = "bool"

optionFromString "int" = Just TCInt
optionFromString "long" = Just TCLong
optionFromString "double" = Just TCDouble
optionFromString "String" = Just TCString
optionFromString "string" = Just TCString
optionFromString "bool" = Just TCBool
optionFromString _ = Nothing

-- name:vvType
Expand Down
10 changes: 10 additions & 0 deletions libcaide/test/ProblemParsers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,16 @@ problemParserTests = TestList
{ problemType = mkProblemType "leetcode,findMedianSortedArrays:double,nums1:vint,nums2:vint" }
[ mkTestCase "[1,3]\n[2]" "2.00000"
, mkTestCase "[1,2]\n[3,4]" "2.50000" ]
, lc "https://leetcode.com/problems/sequential-digits"
(makeProblem "Sequential Digits" "sequential-digits")
{ problemType = mkProblemType "leetcode,sequentialDigits:vint,low:int,high:int" }
[ mkTestCase "100\n300" "[123,234]"
, mkTestCase "1000\n13000" "[1234,2345,3456,4567,5678,6789,12345]" ]
, lc "https://leetcode.com/problems/detect-capital"
(makeProblem "Detect Capital" "detect-capital")
{ problemType = mkProblemType "leetcode,detectCapitalUse:bool,word:String" }
[ mkTestCase "\"USA\"" "true"
, mkTestCase "\"FlaG\"" "false" ]
]
]
where
Expand Down
2 changes: 1 addition & 1 deletion libcaide/tests/csharp.test
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ run_csharp_executable ./prob.exe
"$CSC" -target:library submission.cs
cd ..

"$CAIDE" problem prob3 --lang csharp --type leetcode,methodName:long,i:int,d:double,s:String,vi:vint,vs:vString
"$CAIDE" problem prob3 --lang csharp --type leetcode,methodName:long,i:int,d:double,s:String,b:bool,vi:vint,vs:vString
cd prob3
"$CSC" -out:prob.exe prob*.cs
run_csharp_executable ./prob.exe
Expand Down
2 changes: 1 addition & 1 deletion libcaide/tests/simplecpp.test
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ cxx prob*.cpp -o prob2.exe
cxx -c submission.cpp
cd ..

"$CAIDE" problem prob3 --lang simplecpp --type leetcode,methodName:long,i:int,d:double,s:String,vi:vint,vs:vString
"$CAIDE" problem prob3 --lang simplecpp --type leetcode,methodName:long,i:int,d:double,s:String,b:bool,vi:vint,vs:vString
cd prob3
cxx prob*.cpp -o prob3.exe
./prob3.exe
Expand Down

0 comments on commit 7d01894

Please sign in to comment.