Skip to content
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

Data conversion #396

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ build/
.idea/
.vscode/
*.swp

cmake-*
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@ handle it from there. :smile:
* [Shunting yard](cpp/include/algorithm/string/shunting_yard.hpp) :white_check_mark:
* Permutation
* [Heap's Algorithm](cpp/include/algorithm/string/heaps_algorithm.hpp) :white_check_mark:

* Utils
* [Data Conversion](cpp/include/utils/data_conversion.hpp)
* Convert to upper case :white_check_mark:
* Convert to lower case :white_check_mark:
### Data structures

* Linked List
Expand Down
7 changes: 7 additions & 0 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,10 @@ add_executable(fenwick_tree
test/data_structure/tree/fenwick_tree.cpp)
target_link_libraries(fenwick_tree test_runner)

# ============================================================================
# Utilities
# ============================================================================

add_executable(data_conversion
test/utils/data_conversion.cpp)
target_link_libraries(data_conversion test_runner)
58 changes: 58 additions & 0 deletions cpp/include/utils/data_conversion.hpp
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(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
31 changes: 31 additions & 0 deletions cpp/test/utils/data_conversion.cpp
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!");
}