Skip to main content
Code Review

Return to Question

Tweeted twitter.com/StackCodeReview/status/1571740882210013185
added 1 character in body; edited tags
Source Link
200_success
  • 145.5k
  • 22
  • 190
  • 478

Description

  • Splits a string into a vector of strings that has consecutive same characters
    • Examples in test cases below ("5500025" --> ["55", "000", "2", "5"])
  • Only ASCII needs to be supported
  • In case of style/formatting issues, I am using https://zed0.co.uk/clang-format-configurator/

Compile

  • gcc -std=c++17 file.cpp
  • I'm not committed to C++17 (using it for std::string_view), so if C++20 or some other version makes it significantly easier, I can switch to that

Test cases

  • Provided in main()
    • "AAABBCDDDDAAEE5" --> ["AAA", "BB", "C", "DDDD", "AA", "EE", "5"]
    • "" --> []

Code

file.cpp (tio.run)

#include <cstddef>
#include <iostream>
#include <string>
#include <vector>
std::vector<std::string>
group_string_components(std::string_view input_string) {
 std::vector<std::string> grouping;
 if (input_string.length() == 0) {
 return grouping;
 }
 std::size_t pos = 0;
 while (pos != std::string::npos) {
 std::size_t next_pos =
 input_string.find_first_not_of(input_string[pos], pos + 1);
 std::size_t repeat = next_pos - pos;
 if (next_pos == std::string::npos) {
 repeat = input_string.length() - pos;
 }
 grouping.push_back(std::string(repeat, input_string[pos]));
 pos = next_pos;
 }
 return grouping;
}
void run_grouping_test(std::string_view test_case_name,
 std::string_view input_string,
 const std::vector<std::string> &expected_grouping) {
 std::vector<std::string> grouping = group_string_components(input_string);
 std::cout << test_case_name << " passed: " << std::boolalpha
 << (grouping == expected_grouping) << "\n";
}
int main() {
 run_grouping_test("General case", "AAABBCDDDDAAEE5",
 {"AAA", "BB", "C", "DDDD", "AA", "EE", "5"});
 run_grouping_test("Empty case", "", {});
 return 0;
}
```

Description

  • Splits a string into a vector of strings that has consecutive same characters
    • Examples in test cases below ("5500025" --> ["55", "000", "2", "5"])
  • Only ASCII needs to be supported
  • In case of style/formatting issues, I am using https://zed0.co.uk/clang-format-configurator/

Compile

  • gcc -std=c++17 file.cpp
  • I'm not committed to C++17 (using it for std::string_view), so if C++20 or some other version makes it significantly easier, I can switch to that

Test cases

  • Provided in main()
    • "AAABBCDDDDAAEE5" --> ["AAA", "BB", "C", "DDDD", "AA", "EE", "5"]
    • "" --> []

Code

file.cpp (tio.run)

#include <cstddef>
#include <iostream>
#include <string>
#include <vector>
std::vector<std::string>
group_string_components(std::string_view input_string) {
 std::vector<std::string> grouping;
 if (input_string.length() == 0) {
 return grouping;
 }
 std::size_t pos = 0;
 while (pos != std::string::npos) {
 std::size_t next_pos =
 input_string.find_first_not_of(input_string[pos], pos + 1);
 std::size_t repeat = next_pos - pos;
 if (next_pos == std::string::npos) {
 repeat = input_string.length() - pos;
 }
 grouping.push_back(std::string(repeat, input_string[pos]));
 pos = next_pos;
 }
 return grouping;
}
void run_grouping_test(std::string_view test_case_name,
 std::string_view input_string,
 const std::vector<std::string> &expected_grouping) {
 std::vector<std::string> grouping = group_string_components(input_string);
 std::cout << test_case_name << " passed: " << std::boolalpha
 << (grouping == expected_grouping) << "\n";
}
int main() {
 run_grouping_test("General case", "AAABBCDDDDAAEE5",
 {"AAA", "BB", "C", "DDDD", "AA", "EE", "5"});
 run_grouping_test("Empty case", "", {});
 return 0;
}
```

Description

  • Splits a string into a vector of strings that has consecutive same characters
    • Examples in test cases below ("5500025" --> ["55", "000", "2", "5"])
  • Only ASCII needs to be supported
  • In case of style/formatting issues, I am using https://zed0.co.uk/clang-format-configurator/

Compile

  • gcc -std=c++17 file.cpp
  • I'm not committed to C++17 (using it for std::string_view), so if C++20 or some other version makes it significantly easier, I can switch to that

Test cases

  • Provided in main()
    • "AAABBCDDDDAAEE5" --> ["AAA", "BB", "C", "DDDD", "AA", "EE", "5"]
    • "" --> []

Code

file.cpp (tio.run)

#include <cstddef>
#include <iostream>
#include <string>
#include <vector>
std::vector<std::string>
group_string_components(std::string_view input_string) {
 std::vector<std::string> grouping;
 if (input_string.length() == 0) {
 return grouping;
 }
 std::size_t pos = 0;
 while (pos != std::string::npos) {
 std::size_t next_pos =
 input_string.find_first_not_of(input_string[pos], pos + 1);
 std::size_t repeat = next_pos - pos;
 if (next_pos == std::string::npos) {
 repeat = input_string.length() - pos;
 }
 grouping.push_back(std::string(repeat, input_string[pos]));
 pos = next_pos;
 }
 return grouping;
}
void run_grouping_test(std::string_view test_case_name,
 std::string_view input_string,
 const std::vector<std::string> &expected_grouping) {
 std::vector<std::string> grouping = group_string_components(input_string);
 std::cout << test_case_name << " passed: " << std::boolalpha
 << (grouping == expected_grouping) << "\n";
}
int main() {
 run_grouping_test("General case", "AAABBCDDDDAAEE5",
 {"AAA", "BB", "C", "DDDD", "AA", "EE", "5"});
 run_grouping_test("Empty case", "", {});
 return 0;
}
Became Hot Network Question
added 78 characters in body
Source Link

Description

  • Splits a string into a vector of strings that has consecutive same characters
    • Examples in test cases below ("5500025" --> ["55", "000", "2", "5"])
  • Only ASCII needs to be supported
  • In case of style/formatting issues, I am using https://zed0.co.uk/clang-format-configurator/

Compile

  • gcc -std=c++17 file.cpp
  • I'm not committed to C++17 (using it for std::string_view), so if C++20 or some other version makes it significantly easier, I can switch to that

Test cases

  • Provided in main()
    • "AAABBCDDDDAAEE5" --> ["AAA", "BB", "C", "DDDD", "AA", "EE", "5"]
    • "" --> []

Code

file.cpp (tio.run)

#include <cstddef>
#include <iostream>
#include <string>
#include <vector>
std::vector<std::string>
group_string_components(std::string_view input_string) {
 std::vector<std::string> grouping;
 if (input_string.length() == 0) {
 return grouping;
 }
 std::size_t pos = 0;
 while (pos != std::string::npos) {
 std::size_t next_pos =
 input_string.find_first_not_of(input_string[pos], pos + 1);
 std::size_t repeat = next_pos - pos;
 if (next_pos == std::string::npos) {
 repeat = input_string.length() - pos;
 }
 grouping.push_back(std::string(repeat, input_string[pos]));
 pos = next_pos;
 }
 return grouping;
}
void run_grouping_test(std::string_view test_case_name,
 std::string_view input_string,
 const std::vector<std::string> &expected_grouping) {
 std::vector<std::string> grouping = group_string_components(input_string);
 std::cout << test_case_name << " passed: " << std::boolalpha
 << (grouping == expected_grouping) << "\n";
}
int main() {
 run_grouping_test("General case", "AAABBCDDDDAAEE5",
 {"AAA", "BB", "C", "DDDD", "AA", "EE", "5"});
 run_grouping_test("Empty case", "", {});
 return 0;
}
```

Description

Compile

  • gcc -std=c++17 file.cpp
  • I'm not committed to C++17 (using it for std::string_view), so if C++20 or some other version makes it significantly easier, I can switch to that

Test cases

  • Provided in main()
    • "AAABBCDDDDAAEE5" --> ["AAA", "BB", "C", "DDDD", "AA", "EE", "5"]
    • "" --> []

Code

file.cpp (tio.run)

#include <cstddef>
#include <iostream>
#include <string>
#include <vector>
std::vector<std::string>
group_string_components(std::string_view input_string) {
 std::vector<std::string> grouping;
 if (input_string.length() == 0) {
 return grouping;
 }
 std::size_t pos = 0;
 while (pos != std::string::npos) {
 std::size_t next_pos =
 input_string.find_first_not_of(input_string[pos], pos + 1);
 std::size_t repeat = next_pos - pos;
 if (next_pos == std::string::npos) {
 repeat = input_string.length() - pos;
 }
 grouping.push_back(std::string(repeat, input_string[pos]));
 pos = next_pos;
 }
 return grouping;
}
void run_grouping_test(std::string_view test_case_name,
 std::string_view input_string,
 const std::vector<std::string> &expected_grouping) {
 std::vector<std::string> grouping = group_string_components(input_string);
 std::cout << test_case_name << " passed: " << std::boolalpha
 << (grouping == expected_grouping) << "\n";
}
int main() {
 run_grouping_test("General case", "AAABBCDDDDAAEE5",
 {"AAA", "BB", "C", "DDDD", "AA", "EE", "5"});
 run_grouping_test("Empty case", "", {});
 return 0;
}
```

Description

  • Splits a string into a vector of strings that has consecutive same characters
    • Examples in test cases below ("5500025" --> ["55", "000", "2", "5"])
  • Only ASCII needs to be supported
  • In case of style/formatting issues, I am using https://zed0.co.uk/clang-format-configurator/

Compile

  • gcc -std=c++17 file.cpp
  • I'm not committed to C++17 (using it for std::string_view), so if C++20 or some other version makes it significantly easier, I can switch to that

Test cases

  • Provided in main()
    • "AAABBCDDDDAAEE5" --> ["AAA", "BB", "C", "DDDD", "AA", "EE", "5"]
    • "" --> []

Code

file.cpp (tio.run)

#include <cstddef>
#include <iostream>
#include <string>
#include <vector>
std::vector<std::string>
group_string_components(std::string_view input_string) {
 std::vector<std::string> grouping;
 if (input_string.length() == 0) {
 return grouping;
 }
 std::size_t pos = 0;
 while (pos != std::string::npos) {
 std::size_t next_pos =
 input_string.find_first_not_of(input_string[pos], pos + 1);
 std::size_t repeat = next_pos - pos;
 if (next_pos == std::string::npos) {
 repeat = input_string.length() - pos;
 }
 grouping.push_back(std::string(repeat, input_string[pos]));
 pos = next_pos;
 }
 return grouping;
}
void run_grouping_test(std::string_view test_case_name,
 std::string_view input_string,
 const std::vector<std::string> &expected_grouping) {
 std::vector<std::string> grouping = group_string_components(input_string);
 std::cout << test_case_name << " passed: " << std::boolalpha
 << (grouping == expected_grouping) << "\n";
}
int main() {
 run_grouping_test("General case", "AAABBCDDDDAAEE5",
 {"AAA", "BB", "C", "DDDD", "AA", "EE", "5"});
 run_grouping_test("Empty case", "", {});
 return 0;
}
```
Source Link

Split string into same consecutive characters

Description

Compile

  • gcc -std=c++17 file.cpp
  • I'm not committed to C++17 (using it for std::string_view), so if C++20 or some other version makes it significantly easier, I can switch to that

Test cases

  • Provided in main()
    • "AAABBCDDDDAAEE5" --> ["AAA", "BB", "C", "DDDD", "AA", "EE", "5"]
    • "" --> []

Code

file.cpp (tio.run)

#include <cstddef>
#include <iostream>
#include <string>
#include <vector>
std::vector<std::string>
group_string_components(std::string_view input_string) {
 std::vector<std::string> grouping;
 if (input_string.length() == 0) {
 return grouping;
 }
 std::size_t pos = 0;
 while (pos != std::string::npos) {
 std::size_t next_pos =
 input_string.find_first_not_of(input_string[pos], pos + 1);
 std::size_t repeat = next_pos - pos;
 if (next_pos == std::string::npos) {
 repeat = input_string.length() - pos;
 }
 grouping.push_back(std::string(repeat, input_string[pos]));
 pos = next_pos;
 }
 return grouping;
}
void run_grouping_test(std::string_view test_case_name,
 std::string_view input_string,
 const std::vector<std::string> &expected_grouping) {
 std::vector<std::string> grouping = group_string_components(input_string);
 std::cout << test_case_name << " passed: " << std::boolalpha
 << (grouping == expected_grouping) << "\n";
}
int main() {
 run_grouping_test("General case", "AAABBCDDDDAAEE5",
 {"AAA", "BB", "C", "DDDD", "AA", "EE", "5"});
 run_grouping_test("Empty case", "", {});
 return 0;
}
```
lang-cpp

AltStyle によって変換されたページ (->オリジナル) /