forked from ProAlgos/ProAlgos-Cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implementing data conversion (ProAlgos#99)
- Make upper case using bit manipulation with unit tests - Make lower case using bit manipulation with unit tests
- Loading branch information
Showing
3 changed files
with
96 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
Data Conversion | ||
------------------- | ||
Upper/Lower case algorithm using bit manipulation. | ||
Time complexity | ||
--------------- | ||
O(N), where N is the length of the input. | ||
Space complexity | ||
---------------- | ||
O(N), where N is the result. | ||
*/ | ||
|
||
#ifndef DATA_CONVERSION_HPP | ||
#define DATA_CONVERSION_HPP | ||
|
||
#include <string> | ||
|
||
#define UPPER_CASE_BIT_MASK 0b11011111 | ||
#define LOWER_CASE_BIT_MASK 0b00100000 | ||
|
||
bool is_letter(const char& c) { | ||
char lower_cased_char = (char)(c | LOWER_CASE_BIT_MASK); | ||
return lower_cased_char >= 'a' && lower_cased_char <= 'z'; | ||
} | ||
|
||
std::string make_upper_case(const std::string& input) { | ||
std::string result; | ||
for (auto& c: input) { | ||
|
||
if (is_letter(c)) { | ||
result += c & UPPER_CASE_BIT_MASK; | ||
} | ||
else { | ||
result += c; | ||
} | ||
|
||
} | ||
return result; | ||
} | ||
|
||
std::string make_lower_case(const std::string& input) { | ||
std::string result; | ||
for (auto& current_char: input) { | ||
|
||
if (is_letter(current_char)) { | ||
result += current_char | LOWER_CASE_BIT_MASK; | ||
} | ||
else { | ||
result += current_char; | ||
} | ||
|
||
} | ||
return result; | ||
} | ||
|
||
#endif //DATA_CONVERSION_HPP |
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,31 @@ | ||
#include "third_party/catch.hpp" | ||
#include "utils/data_conversion.hpp" | ||
|
||
#define NAME_SPACE "[utils][data-conversion][make_upper_case]" | ||
|
||
TEST_CASE("Base cases", NAME_SPACE) { | ||
REQUIRE(make_upper_case(" ") == " "); | ||
REQUIRE(make_upper_case("1234567890") == "1234567890"); | ||
REQUIRE(make_upper_case("!@#$%^&*()_+±~<>.,|':;") == "!@#$%^&*()_+±~<>.,|':;"); | ||
REQUIRE(make_lower_case(" ") == " "); | ||
REQUIRE(make_lower_case("1234567890") == "1234567890"); | ||
REQUIRE(make_lower_case("!@#$%^&*()_+±~<>.,|':;") == "!@#$%^&*()_+±~<>.,|':;"); | ||
} | ||
|
||
TEST_CASE("Convert to upper case", NAME_SPACE) { | ||
REQUIRE(make_upper_case("hello") == "HELLO"); | ||
REQUIRE(make_upper_case("world") == "WORLD"); | ||
REQUIRE(make_upper_case("GITHUB") == "GITHUB"); | ||
REQUIRE(make_upper_case("Cpp") == "CPP"); | ||
REQUIRE(make_upper_case("Hello World!") == "HELLO WORLD!"); | ||
REQUIRE(make_upper_case("Hello\nWorld!") == "HELLO\nWORLD!"); | ||
} | ||
|
||
TEST_CASE("Convert to lower case", NAME_SPACE) { | ||
REQUIRE(make_lower_case("HELLO") == "hello"); | ||
REQUIRE(make_lower_case("WORLD") == "world"); | ||
REQUIRE(make_lower_case("github") == "github"); | ||
REQUIRE(make_lower_case("Cpp") == "cpp"); | ||
REQUIRE(make_lower_case("Hello World!") == "hello world!"); | ||
REQUIRE(make_lower_case("Hello\nWorld!") == "hello\nworld!"); | ||
} |