Jupon
BAN USER/*
Given n > 0 fair dice with m > 0 "sides", write a function that returns a histogram of the
frequency of the result of dice rolls. For example, for two dices, each with three sides, the
results are:
*/
void combine(map<int,int>& result, const vector<int>& sides){
if(result.empty()){
vector<int>::const_iterator it;
for (it = sides.begin(); it != sides.end(); ++it){
result.insert(pair<int,int>(*it, 1));
}
return;
}
map<int,int> newResult;
map<int,int>::iterator itMap;
vector<int>::const_iterator itVct;
for (itVct = sides.begin(); itVct != sides.end(); ++itVct){
for (itMap = result.begin(); itMap != result.end(); ++itMap){
int num = itMap->first + (*itVct);
map<int,int>::const_iterator it = newResult.find(num);
if (it == newResult.end()){
newResult.insert(pair<int,int>(num, 1));
}
else {
newResult[num]++;
}
}
}
result = newResult;
}
void processDices(map<int,int>& result, int dice, int side){
vector<int> sides;
for (int i=1; i<=side; ++i){
sides.push_back(i);
}
for (int i=0; i<dice; ++i){
combine(result, sides);
}
}
void Question_2(){
map<int,int> testMap;
processDices(testMap, 4, 4);
map<int,int>::iterator it;
for (it = testMap.begin(); it != testMap.end(); ++it){
cout << it->first << " : " << it->second << endl;
}
}
/*
Given n > 0 fair dice with m > 0 "sides", write a function that returns a histogram of the
frequency of the result of dice rolls. For example, for two dices, each with three sides, the
results are:
*/
void combine(map<int,int>& result, const vector<int>& sides){
if(result.empty()){
vector<int>::const_iterator it;
for (it = sides.begin(); it != sides.end(); ++it){
result.insert(pair<int,int>(*it, 1));
}
return;
}
map<int,int> newResult;
map<int,int>::iterator itMap;
vector<int>::const_iterator itVct;
for (itVct = sides.begin(); itVct != sides.end(); ++itVct){
for (itMap = result.begin(); itMap != result.end(); ++itMap){
int num = itMap->first + (*itVct);
map<int,int>::const_iterator it = newResult.find(num);
if (it == newResult.end()){
newResult.insert(pair<int,int>(num, 1));
}
else {
newResult[num]++;
}
}
}
result = newResult;
}
void processDices(map<int,int>& result, int dice, int side){
vector<int> sides;
for (int i=1; i<=side; ++i){
sides.push_back(i);
}
for (int i=0; i<dice; ++i){
combine(result, sides);
}
}
void Question_2(){
map<int,int> testMap;
processDices(testMap, 4, 4);
map<int,int>::iterator it;
for (it = testMap.begin(); it != testMap.end(); ++it){
cout << it->first << " : " << it->second << endl;
}
}
#include <gtest/gtest.h>
#include <gmock/gmock.h>
using namespace testing;
using namespace std;
struct Node
{
int mData;
Node* mNext;
Node ( int data ) :
mData ( data ),
mNext ( NULL )
{
}
};
Node* createLinkedList ( int number )
{
if ( number == 0 )
{
return NULL;
}
Node* head = new Node ( 1 );
Node* saveHead = head;
for ( int i = 1; i < number; ++i )
{
head->mNext = new Node ( i + 1 );
head = head->mNext;
}
return saveHead;
}
void printList ( Node* head )
{
cout << "HEAD --> ";
while ( head != NULL )
{
cout << head->mData << " --> ";
head = head->mNext;
}
cout << "TAIL" << "\n";
}
Node* swapPair ( Node* head )
{
Node* prevNode = head;
Node* nextNode = NULL;
Node* currentNode = head->mNext;
Node* backupTail = NULL;
Node* result = head->mNext;
if ( currentNode == NULL )
{
return head;
}
while ( currentNode != NULL )
{
nextNode = currentNode->mNext;
if ( backupTail != NULL )
backupTail->mNext = currentNode;
currentNode->mNext = prevNode;
prevNode->mNext = nextNode;
backupTail = prevNode;
if ( nextNode != NULL )
{
currentNode = nextNode->mNext;
prevNode = nextNode;
}
else
{
currentNode = NULL;
}
}
return result;
}
/**
* Test swaping linked list.
*/
TEST(LinkedListTest, 123456789)
{
Node* head = createLinkedList ( 9 );
printList ( head );
Node* result = swapPair ( head );
printList ( result );
}
- Jupon November 05, 2017