Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Hello, I'm learning C++ programming and I do like to solve any types of challenges/exercises, and since I have huge desire to become better I decided to post some of my solved challenges here, so that I can find out how my coding style is.
- Is it understandable, how can the code be improved? And anything you think that may be helpful.
- [Here][1] is an example of challenge I try to solve.
- >Edit: Thanks for advices how to make better description of the challenge.
- >I hope that way things seems clearer.
- >The all code is available [here][2].
- [1]: https://www.codeeval.com/open_challenges/42/
- [2]: http://pastebin.com/9m1He65f
- How code works ?
- When get the input for example "123". Find how many different possible way it can subtract.
- size_t perm = pow(2,static_cast<double>(input.size() - 1) );
- Originally there are 4 ways for our example. {123} {1 23} {12 3} {1 2 3}
- which is the value **perm** hold.
- >*makeBinary* convert all values from 0 to **perm** into binary.
- > vector<string> makeBinary(size_t perm)
- {
- vector<string> output;
- /*Since <bitset> will give me string with length 32
- * but all zeros left than first '1' we met does not participate
- in calculation we just erase them. */
- size_t eraseLength = bitset<32>(perm).to_string().find_first_of('1');
- while(perm--)
- {
- string binary = bitset<32>(perm).to_string();
- binary.erase(binary.begin(), binary.begin() + eraseLength);
- output.push_back(binary);
- }
- return output;
- }
- >The logic behind making **perm** into binary is.
- >Every '1' is actually the space
- >between input
- >>0 = 00 -> 123
- >>1 = 01 -> 12 3
- >>2 = 10 -> 1 23
- >>3 = 11 -> 1 2 3
- And that is the function of *getPartitions* do, it will divide input according to **vector binarySet**, which hold all possible binary values.
- vector<string> getPartitions(const vector<string>& binarySet,
- const string& input)
- {
- vector<string> binOperator;
- for(size_t idx = 0; idx < binarySet.size(); idx++)
- {
- string str(input);
- for(size_t pos = 0,opCount = 1;
- (pos = binarySet[idx].find('1',pos) )!= string::npos;
- pos++,opCount++)
- {
- str.insert(pos+opCount, " ");
- }
- binOperator.push_back(str);
- }
- return binOperator;
- }
- Then there is simple function that convert strings to integers, so we can add/subtract.
- vector<int> makePartitionsToNum(const string& str)
- {
- vector<int> numbers;
- stringstream split(str);
- string buf;
- while(split >> buf)
- {
- int value;
- istringstream toNum(buf);
- toNum >> value;
- numbers.push_back(value);
- }
- return numbers;
- }
- This function return numbers that are ready to check if they are **ugly**.
- void getReadyNumbers(vector<int>* readyNumbers,
- const vector<int> &numbers)
- {
- /*If there is only one number example "123"
- *there is no binary operation so just add the number*/
- if(numbers.size() == 1)
- {
- (*readyNumbers).push_back(numbers[0]);
- }
- /*This is also easy case when we have only two numbers
- *{1,23} or {12,3}*/
- else if(numbers.size() == 2)
- {
- (*readyNumbers).push_back(numbers[0] + numbers[1]);
- (*readyNumbers).push_back(numbers[0] - numbers[1]);
- }
- else
- {
- /*And the third case with more than 2 numbers to add/sub
- *The logic is similar, the only difference is now '0' means subtract
- *and '1' means additions the number who gets here is {1,2,3}
- *with total size(3) it have possiblePerm of 4.
- * 0 = 00 -> 1-2-3 = -6
- * 1 = 01 -> 1-2+3 = 2
- * 2 = 10 -> 1+2-3 = 0
- * 3 = 11 -> 1+2+3 = 6
- */
- size_t possiblePerm = pow(2,static_cast<double>(numbers.size()-1));
- vector<string> binSet(makeBinary(possiblePerm));
- for(size_t i=0; i<binSet.size(); i++)
- {
- int result = numbers[0];
- for(size_t binCounter=1;
- binCounter<numbers.size();
- binCounter++)
- {
- if(binSet[i][binCounter - 1] == '1')
- {
- result += numbers[binCounter];
- }
- else
- {
- result -= numbers[binCounter];
- }
- }
- (*readyNumbers).push_back(result);
- }
- }
- }
- And the final step is to find total ugly's count of given input.
- >Ugly number is divisible by any of the one-digit primes (2, 3, 5 or 7) or equal to '0'
- const int one_prime[4] = {2,3,5,7};
- bool isUgly(int number)
- {
- if(number == 0) return true;
- for(int i=0; i<4; i++)
- {
- if(number % one_prime[i] == 0)
- return true;
- }
- return false;
- }
- I do really hope this code now look easier to understand, and I'm very excited to get some feedback. Thank you guys :)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement