Hsien Chang Peng
BAN USER#include <ctime>
#include <iostream>
#include <list>
#include <string>
void sumNumber(std::string randomNumberString) {
int sum;
bool status;
size_t preIndexOfSharpSign, postIndexOfSharpSign;
std::list<int> listOfInteger;
sum = 0;
status = true;
preIndexOfSharpSign = 0;
postIndexOfSharpSign = 0;
for (int i = 1; postIndexOfSharpSign != std::string::npos; i++) {
for (int j = 0; j < i; j++) {
postIndexOfSharpSign = randomNumberString.find('#', preIndexOfSharpSign);
if (postIndexOfSharpSign == std::string::npos) {
listOfInteger.push_back(std::stoi(randomNumberString.substr(preIndexOfSharpSign, randomNumberString.length() - preIndexOfSharpSign)));
break;
} else {
listOfInteger.push_back(std::stoi(randomNumberString.substr(preIndexOfSharpSign, postIndexOfSharpSign - preIndexOfSharpSign)));
}
preIndexOfSharpSign = postIndexOfSharpSign + 1;
}
for (std::list<int>::iterator listOfIntegerOfIterator = listOfInteger.begin(); listOfIntegerOfIterator != listOfInteger.end(); listOfIntegerOfIterator++) {
std::cout << *listOfIntegerOfIterator << " ";
}
std::cout << std::endl;
if (listOfInteger.size() == i) {
listOfInteger.sort(std::greater<int>());
sum += listOfInteger.front();
listOfInteger.clear();
} else {
listOfInteger.clear();
status = false;
break;
}
}
if (status) {
std::cout << "Sum: " << sum << std::endl;
} else {
std::cout << "Invalid" << std::endl;
}
}
void makeRandomNumberString(std::string & randomNumberString) {
int randomNumber;
std::srand((unsigned int)std::time(nullptr));
randomNumber = std::rand() % 500 + 1;
for (int i = 0; i < randomNumber; i++) {
randomNumberString.append(std::to_string(std::rand() % 10));
if (i < randomNumber - 1) {
randomNumberString.append(1, '#');
}
}
std::cout << randomNumberString << std::endl;
}
int main(int argc, const char * argv[]) {
std::string randomNumberString;
makeRandomNumberString(randomNumberString);
sumNumber(randomNumberString);
return 0;
}
#include <iostream>
#include <list>
#include <stack>
class Graph {
private:
int vertices;
std::list<int> * adjacencies;
private:
void runTopologicalSortUtility(int vertex, bool * visitedVertices, std::stack<int> & stack);
public:
Graph(int vertices);
~Graph();
void addEdge(int from, int to);
void runTopologicalSort();
};
Graph::Graph(int vertices) {
this->vertices = vertices;
this->adjacencies = new std::list<int>[vertices];
}
Graph::~Graph() {
delete [] adjacencies;
}
void Graph::addEdge(int from, int to) {
adjacencies[from].push_back(to);
}
void Graph::runTopologicalSortUtility(int vertex, bool * visitedVertices, std::stack<int> & stack) {
std::reverse_iterator<std::list<int>::iterator> reverseListIterator;
visitedVertices[vertex] = true;
for (reverseListIterator = adjacencies[vertex].rbegin(); reverseListIterator != adjacencies[vertex].rend(); reverseListIterator++) {
if (visitedVertices[*reverseListIterator] == false) {
runTopologicalSortUtility(*reverseListIterator, visitedVertices, stack);
}
}
stack.push(vertex);
}
void Graph::runTopologicalSort() {
bool * visitedVertices;
std::stack<int> stack;
visitedVertices = new bool[vertices];
for (int i = 0; i < vertices; i++) {
visitedVertices[i] = false;
}
for (int vertex = 1; vertex < vertices; vertex++) {
if (visitedVertices[vertex] == false) {
runTopologicalSortUtility(vertex, visitedVertices, stack);
}
}
while (stack.empty() == false) {
std::cout << stack.top() << " ";
stack.pop();
}
std::cout << std::endl;
}
int main(int argc, const char * argv[]) {
Graph graph(6);
graph.addEdge(1, 2);
graph.addEdge(1, 3);
graph.addEdge(1, 4);
graph.addEdge(3, 5);
graph.addEdge(2, 5);
graph.addEdge(4, 5);
std::cout << "Following is a Topological Sort of the given graph" << std::endl;
graph.runTopologicalSort();
return 0;
}
#include <iostream>
#include <string>
std::string findShortestSubString(std::string longString, std::string * shortStrings, int lengthOfShortStrings) {
std::string::size_type start, end;
start = longString.length() - 1;
end = 0;
for (int i = 0; i < lengthOfShortStrings; i++) {
if (longString.find(shortStrings[i]) != std::string::npos) {
if (longString.find(shortStrings[i]) < start) {
start = longString.find(shortStrings[i]);
}
if (longString.find(shortStrings[i]) + shortStrings[i].length() - 1 > end) {
end = longString.find(shortStrings[i]) + shortStrings[i].length() - 1;
}
} else {
start = std::string::npos;
end = std::string::npos;
break;
}
}
if (start != std::string::npos && end != std::string::npos) {
return longString.substr(start, end - start + 1);
} else {
return std::string("There is no sub string containing all short strings.");
}
}
int main(int argc, const char * argv[]) {
std::string longString;
std::string shortStrings[3];
longString = std::string("Mississippi");
shortStrings[0] = std::string("is");
shortStrings[1] = std::string("si");
shortStrings[2] = std::string("ssi");
std::cout << findShortestSubString(longString, shortStrings, sizeof(shortStrings) / sizeof(std::string)) << std::endl;
return 0;
}
package practice;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
public class Practice {
public static void main(String[] args) {
Random random;
int[] firstArray, secondArray;
random = new Random();
firstArray = new int[random.nextInt(10) + 1];
secondArray = new int[random.nextInt(10) + 1];
for (int i = 0; i < firstArray.length; i++) {
firstArray[i] = random.nextInt(10);
}
for (int i = 0; i < secondArray.length; i++) {
secondArray[i] = random.nextInt(10);
}
System.out.println(Arrays.toString(firstArray));
System.out.println(Arrays.toString(secondArray));
System.out.println(Arrays.toString(sumTwoArrays(firstArray, secondArray)));
}
public static int[] sumTwoArrays(int[] firstArray, int[] secondArray) {
int indexOfFirstArray, indexOfSecondArray;
int[] returnedArray;
ArrayList<Integer> integerArrayList;
if (firstArray == null || firstArray.length == 0 || secondArray == null || secondArray.length == 0) {
return null;
}
indexOfFirstArray = firstArray.length - 1;
indexOfSecondArray = secondArray.length - 1;
integerArrayList = new ArrayList();
integerArrayList.add(0, 0);
do {
if (indexOfFirstArray >= 0 && indexOfSecondArray >= 0) {
integerArrayList.add(1, firstArray[indexOfFirstArray] + secondArray[indexOfSecondArray]);
} else if (indexOfFirstArray >= 0 && indexOfSecondArray < 0) {
integerArrayList.add(1, firstArray[indexOfFirstArray]);
} else {
integerArrayList.add(1, secondArray[indexOfSecondArray]);
}
if (indexOfFirstArray >= 0) {
indexOfFirstArray--;
}
if (indexOfSecondArray >= 0) {
indexOfSecondArray--;
}
} while (indexOfFirstArray >= 0 || indexOfSecondArray >= 0);
for (int i = integerArrayList.size() - 1; i >= 0; i--) {
if (integerArrayList.get(i) >= 10) {
integerArrayList.set(i, integerArrayList.get(i) % 10);
integerArrayList.set(i - 1, integerArrayList.get(i - 1) + 1);
}
}
if (integerArrayList.get(0) == 0) {
integerArrayList.remove(0);
}
returnedArray = new int[integerArrayList.size()];
for (int i = 0; i < integerArrayList.size(); i++) {
returnedArray[i] = integerArrayList.get(i);
}
return returnedArray;
}
}
#include <iostream>
#include <string>
std::string findLongestPalindrome(std::string s) {
int low, high, start, end;
bool palindromic;
std::string palindrome;
low = 0;
high = 0;
start = 0;
end = 0;
palindromic = false;
if (s.length() <= 1) {
return s;
}
for (int i = 1; i < s.length(); i++) {
low = i - 1;
high = i;
while (low >= 0 && high < s.length() && s[low] == s[high]) {
if ((high - low) > (end - start)) {
start = low;
end = high;
palindromic = true;
}
--low;
++high;
}
low = i - 1;
high = i + 1;
while (low >= 0 && high < s.length() && s[low] == s[high]) {
if ((high - low) > (end - start)) {
start = low;
end = high;
palindromic = true;
}
--low;
++high;
}
}
if (palindromic == true) {
palindrome = s.substr(start, end - start + 1);
}
return palindrome;
}
int main(int argc, const char * argv[]) {
std::cout << "Longest palindrome string is: " << findLongestPalindrome("abadd") << std::endl;
return 0;
}
#include <iostream>
#include <map>
struct Node {
int value;
Node * leftNode;
Node * rightNode;
};
void addMap(std::map<int, bool> * nodeMap, Node * node) {
if (node != nullptr) {
(*nodeMap)[node->value] = true;
addMap(nodeMap, node->leftNode);
addMap(nodeMap, node->rightNode);
}
}
bool checkRepetitiveNumber(std::map<int, bool> * nodeMap, Node * node) {
if (node != nullptr) {
auto search = nodeMap->find(node->value);
if (search == nodeMap->end()) {
return false || checkRepetitiveNumber(nodeMap, node->leftNode) || checkRepetitiveNumber(nodeMap, node->rightNode);
} else {
return true;
}
} else {
return false;
}
}
int main(int argc, const char * argv[]) {
bool isRepetitive;
Node * firstBinaryTree, * secondBinaryTree;
std::map<int, bool> nodeMap;
isRepetitive = false;
firstBinaryTree = new Node{5, nullptr, nullptr};
firstBinaryTree->leftNode = new Node{1, nullptr, nullptr};
firstBinaryTree->rightNode = new Node{6, nullptr, nullptr};
firstBinaryTree->leftNode->leftNode = new Node{5, nullptr, nullptr};
firstBinaryTree->leftNode->rightNode = new Node{4, nullptr, nullptr};
firstBinaryTree->rightNode->leftNode = new Node{3, nullptr, nullptr};
firstBinaryTree->rightNode->rightNode = new Node{6, nullptr, nullptr};
secondBinaryTree = new Node{12, nullptr, nullptr};
secondBinaryTree->leftNode = new Node{33, nullptr, nullptr};
secondBinaryTree->rightNode = new Node{44, nullptr, nullptr};
secondBinaryTree->leftNode->leftNode = new Node{64, nullptr, nullptr};
secondBinaryTree->leftNode->rightNode = new Node{5, nullptr, nullptr};
addMap(&nodeMap, firstBinaryTree);
isRepetitive = checkRepetitiveNumber(&nodeMap, secondBinaryTree);
if (isRepetitive == true) {
std::cout << "There is a repetitive number." << std::endl;
} else {
std::cout << "There isn't a repetitive number." << std::endl;
}
}
#include <iostream>
#include <queue>
struct Node {
int value;
Node * leftNode;
Node * rightNode;
};
void printNodeAtTheSameLevel(Node * node) {
std::queue<Node *> nodeQueue;
if (node != nullptr) {
nodeQueue.push(node);
do {
std::cout << nodeQueue.front()->value << std::endl;
if (nodeQueue.front()->leftNode != nullptr) {
nodeQueue.push(nodeQueue.front()->leftNode);
}
if (nodeQueue.front()->rightNode != nullptr) {
nodeQueue.push(nodeQueue.front()->rightNode);
}
nodeQueue.pop();
} while (nodeQueue.empty() == false);
}
}
int main(int argc, const char * argv[]) {
Node * binaryTree;
binaryTree = new Node{1, nullptr, nullptr};
binaryTree->leftNode = new Node{2, nullptr, nullptr};
binaryTree->rightNode = new Node{3, nullptr, nullptr};
binaryTree->leftNode->rightNode = new Node{4, nullptr, nullptr};
binaryTree->rightNode->leftNode = new Node{5, nullptr, nullptr};
binaryTree->rightNode->rightNode = new Node{6, nullptr, nullptr};
binaryTree->rightNode->leftNode->leftNode = new Node{7, nullptr, nullptr};
binaryTree->rightNode->leftNode->leftNode->rightNode = new Node{8, nullptr, nullptr};
printNodeAtTheSameLevel(binaryTree);
return 0;
}
#include <iostream>
#include <list>
#include <map>
int removeDuplicate(std::list<int> & integerList) {
std::list<int>::iterator iteratorOfIntegerList;
std::map<int, bool> integerMap;
iteratorOfIntegerList = integerList.begin();
while (iteratorOfIntegerList != integerList.end()) {
if (integerMap[*iteratorOfIntegerList] == false) {
integerMap[*iteratorOfIntegerList] = true;
} else {
integerList.erase(iteratorOfIntegerList);
}
iteratorOfIntegerList++;
}
return (int)integerList.size();
}
int main(int argc, const char * argv[]) {
std::list<int> integerList;
integerList.push_back(21);
integerList.push_back(10);
integerList.push_back(24);
integerList.push_back(2);
integerList.push_back(21);
std::cout << "Before removing duplicates" << std::endl;
for (std::list<int>::iterator iteratorOfIntegerList = integerList.begin(); iteratorOfIntegerList != integerList.end(); iteratorOfIntegerList++) {
std::cout << *iteratorOfIntegerList << ' ';
}
std::cout << std::endl;
std::cout << "The size of removed duplicate integer list is " << removeDuplicate(integerList) << std::endl;
std::cout << "After removing duplicates" << std::endl;
for (std::list<int>::iterator iteratorOfIntegerList = integerList.begin(); iteratorOfIntegerList != integerList.end(); iteratorOfIntegerList++) {
std::cout << *iteratorOfIntegerList << ' ';
}
std::cout << std::endl;
return 0;
}
- Hsien Chang Peng December 28, 2015