ahj
BAN USERUsing Binomial Coefficient When taking 8 attempts:
Challenge 1: C(3,2) * C(3,2) * C(2,1) = 18
Challenge 2: C(8,5) =56
Since there are 56 ways to score 5 out of 8 in challenge 2 scheme compared to 18 ways to score 5 out of 8 in challenge one scheme, challenge two is a better choice.
A quick way. Let the c++ standard library make the decision.
#include <sstream>
#include <iostream>
#include <string>
void check_if_number(std::string theString)
{
std::stringstream ss;
ss << theString;
ss.exceptions(std::ios::failbit | std::ios::badbit);
try{
int theInt;
ss >> theInt;
std::cout << theInt << std::endl;
}
catch (...)
{
std::cout << "not an integer!" << std::endl;
}
}
Version with handling zero's and negative numbers as well as positives.
#include <vector>
#include <unordered_set>
#include <algorithm>
#include <cmath>
unsigned return_unique_num(const std::vector<int>& input)
{
int zeros = std::count(input.begin(), input.end(), 0);
if (zeros == 1)
return 0;
std::unordered_set<int> unique_input;
for (auto i : input)
unique_input.insert(i);
long long input_mult = 1;
long long unique_input_mult = 1;
std::for_each(input.begin(), input.end(), [&](int i) {if(i) input_mult *= i; });
std::for_each(unique_input.begin(), unique_input.end(), [&](int i) {if(i) unique_input_mult *= i; });
long long cube_unique_input_mult = unique_input_mult * unique_input_mult * unique_input_mult;
int ret = static_cast<int>(sqrt(cube_unique_input_mult / input_mult));
if (std::count(input.begin(), input.end(), ret) == 1)
return ret;
return -ret;
}
if the input numbers are positive integers then the following is a solution in O(n). if negative is involved there can be a bit of search involved at the end.
unsigned return_unique_num(const std::vector<unsigned>& input)
{
std::unordered_set<unsigned> unique_input;
for (auto i : input)
unique_input.insert(i);
long long input_mult = 1;
long long unique_input_mult = 1;
std::for_each(input.begin(), input.end(), [&](unsigned i) {input_mult *= i; });
std::for_each(unique_input.begin(), unique_input.end(), [&](unsigned i) {unique_input_mult *= i; });
long long cube_unique_input_mult = unique_input_mult * unique_input_mult * unique_input_mult;
return static_cast<unsigned>(sqrt(cube_unique_input_mult / input_mult));
}
def sum_hundred(input):
checker_list = [0] * 100
for number in input_list:
checker_list[number] += 1
for index, number in enumerate(checker_list):
while(number):
number -= 1
while(checker_list[100 - index]):
print index, 100 - index
checker_list[100 - index] -= 1
if __name__ == '__main__':
sum_hundred(input_list)
Repbrandysolsen, Area Sales Manager at AMD
I am a Social butterflies who enjoy travel.Have fun doing this job and project an atmosphere that reflects Amazon ...
Using Binomial Coefficient When taking 6 attempts:
- ahj November 13, 2014Challenge 1: C(3,2) * C(3,2) = 9
Challenge 2: C(6,4) =15
Since there are 15 ways to score 4 out of 6 in challenge 2 scheme compared to 9 ways to score 4 out of 6 in challenge one scheme, challenge two is a better choice as it gives the more flexibility to the shot taker.