-
Notifications
You must be signed in to change notification settings - Fork 363
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
Add data conversion functions #341
Closed
beardbytes
wants to merge
8
commits into
ProAlgos:master
from
beardbytes:test_data_validation_branch
Closed
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3ecb7aa
Data validation utility function
be5bc1c
Tests for the data validation function
e532cb2
Modified the code with the given changes
5eff677
Added executables to run uit tests on data_conversion.hpp
0426695
Changed the name of the file from data_validation to data_conversion
ab2f0b4
Added new line at the end of the file
0c2d9bc
Added and modeified test cases
a106f3f
Added newline at the eof
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 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,64 @@ | ||
/* | ||
Data validation | ||
--------------- | ||
This utility function contains two sub-funtions : | ||
|
||
1) make_upper_case() : converts the characters in a string to upper case . | ||
|
||
2) make_lower_case() : converts the characters in a string to lower case . | ||
|
||
Time complexity | ||
--------------- | ||
O(n), where n is the length of the string . | ||
*/ | ||
|
||
#ifndef DATA_VALIDATION_HPP | ||
#define DATA_VALIDATION_HPP | ||
|
||
#include <string> | ||
|
||
using std::string; | ||
|
||
/* | ||
make_upper_case() | ||
----------------- | ||
This function returns a given string into upper case . | ||
Loop invariant : Selects each character and converts it to upper case . | ||
|
||
Return value | ||
------------ | ||
string having all the characters in upper case . | ||
*/ | ||
|
||
string make_upper_case(string str) | ||
{ | ||
//converting each character to upper case using for loop | ||
for (auto &c : str) | ||
{ | ||
c = std::toupper(c); // using the function toupper() for conversion | ||
} | ||
return str; // returning the converted string | ||
} | ||
|
||
/* | ||
make_lower_case() | ||
----------------- | ||
This function returns a give string into lower case . | ||
Loop invariant : Selects each character and converts it to lower case . | ||
|
||
Return value | ||
------------ | ||
string having all the characters in lower case . | ||
*/ | ||
|
||
string make_lower_case(string str) | ||
{ | ||
// converting each character to lower case using for loop | ||
for (auto &c : str) | ||
{ | ||
c = std::tolower(c); //using the function tolower() for conversion | ||
} | ||
return str; //returning the converted string | ||
} | ||
|
||
#endif // DATA_VALIDATION_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,20 @@ | ||
#include "third_party/catch.hpp" | ||
#include "utils/data_validation.hpp" | ||
|
||
TEST_CASE("Base cases", "[string][data_validation]") | ||
{ | ||
REQUIRE(make_upper_case("cats") == "CATS"); | ||
REQUIRE(make_upper_case("dogs") == "DOGS"); | ||
REQUIRE(make_lower_case("CATS") == "cats"); | ||
REQUIRE(make_lower_case("dogs") == "DOGS"); | ||
} | ||
|
||
TEST_CASE("Mixed cases", "[string][data-validation]") | ||
{ | ||
REQUIRE(make_upper_case("cAts") == "CATS"); | ||
REQUIRE(make_upper_case("dOgs") == "DOGS"); | ||
REQUIRE(make_upper_case("dOGS") == "DOGS"); | ||
REQUIRE(make_lower_case("CaTS") == "cats"); | ||
REQUIRE(make_lower_case("DoGS") == "dogs"); | ||
REQUIRE(make_lower_case("dogS") == "DOGS"); | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than just using
std::toupper()
andstd::tolower()
, I think it would be better to implement this manually (as done here, though there are lots of ways to do it). Obviously in "real life" you'd just use thestd
function, but the point of this repo is (generally) to implement things ourselves (i.e., sorting a list manually rather than just callingstd::sort()
). More good info here.So, please implement these utilities without using the
std
functions. Let me know if you have any questions!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay...I will dive right into it !