---
# System prepended metadata

title: 'Weight for weight [5 kyu]'
tags: [C++, CodeWars]

---

# Weight for weight [5 kyu]

[Weight for weight](https://www.codewars.com/kata/55c6126177c9441a570000cc)
5 kyu

## Solution
```cpp=
/**
*** Author: R-CO
*** E-Mail: daniel1820kobe@gmail.com
*** Date: 2020-08-06
**/
#include <cstdlib>
#include <iostream>
using std::cout;
using std::endl;
#include <regex>
#include <string>
#include <vector>

struct TestCaseStruct {
  std::string input;
  std::string expected_output;
};

struct Weight {
  int transformed_weight;
  std::string original_weight;
};

class WeightSort {
 public:
  static std::string orderWeight(const std::string &strng) {
    std::string output;

    std::vector<Weight> weight_vector;
    const std::regex whitespace_re("\\s+");
    std::sregex_token_iterator token_itor(strng.begin(), strng.end(),
                                          whitespace_re, -1);
    const std::sregex_token_iterator end;
    while (token_itor != end) {
      Weight weight;
      weight.original_weight = *token_itor++;
      weight.transformed_weight = SumOfDigits(weight.original_weight);
      weight_vector.push_back(weight);
    }

    auto CompareOriginalWeight = [](const Weight &v1, const Weight &v2) {
      return v1.original_weight < v2.original_weight;
    };
    std::stable_sort(weight_vector.begin(), weight_vector.end(),
                     [](const Weight &v1, const Weight &v2) -> bool {
                       return v1.original_weight < v2.original_weight;
                     });
    std::stable_sort(weight_vector.begin(), weight_vector.end(),
                     [](const Weight &v1, const Weight &v2) -> bool {
                       return v1.transformed_weight < v2.transformed_weight;
                     });

    auto itor = weight_vector.begin();
    if (itor != weight_vector.end()) {
      output += itor->original_weight;
      ++itor;
    }
    while (itor != weight_vector.end()) {
      output += " " + itor->original_weight;
      ++itor;
    }

    return output;
  }

 private:
  static std::vector<std::string> splitTokens(const std::string &strng) {
    std::vector<std::string> tokens;

    const std::regex whitespace_re("\\s+");
    std::sregex_token_iterator token_itor(strng.begin(), strng.end(),
                                          whitespace_re, -1);
    const std::sregex_token_iterator end;
    while (token_itor != end) {
      tokens.push_back(*token_itor++);
    }

    return tokens;
  }

  static int SumOfDigits(const std::string &weight) {
    int sum = 0;
    for (const auto c : weight) {
      sum += (c - '0');
    }
    return sum;
  }
};

int main(int argc, char *argv[]) {
  /*
    It(Fixed_Tests)
    {
      dotest("103 123 4444 99 2000", "2000 103 123 4444 99");
      dotest("2000 10003 1234000 44444444 9999 11 11 22 123", "11 11 2000 10003
    22 123 1234000 44444444 9999"); dotest("", ""); dotest("103 123 4444 99 2000
    ", "2000 103 123 4444 99");
    }
   */
  TestCaseStruct test_cases[] = {
      {"103 123 4444 99 2000", "2000 103 123 4444 99"},
      {"2000 10003 1234000 44444444 9999 11 11 22 123",
       "11 11 2000 10003 22 123 1234000 44444444 9999"},
      {"", ""},
      {"103 123 4444 99 2000 ", "2000 103 123 4444 99"}};

  WeightSort solution;
  for (const auto &test_case : test_cases) {
    if (solution.orderWeight(test_case.input) == test_case.expected_output) {
      cout << "test case \"" << test_case.input << "\" is pass." << std::endl;
    } else {
      cout << "test case \"" << test_case.input << "\" is fail." << std::endl;
    }
  }

  return EXIT_SUCCESS;
}

```

## Result

![](https://i.imgur.com/gGozbpg.png)



###### tags: `CodeWars` `C++`
