This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* CC150 17.13 */ | |
// convert a binary search tree (implemented with BiNode) into a doubly linked list | |
// the values should be kept in order and the operation should be performed in place | |
#include <iostream> | |
using namespace std; | |
struct BiNode { | |
BiNode() : data(0), node1(nullptr), node2(nullptr) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* CC150 17.12 */ | |
// find all pairs of integers within an array which sum to a specified value | |
#include <iostream> | |
#include <vector> | |
#include <algorithm> | |
using namespace std; | |
vector<pair<int, int>> findPairs(vector<int> arr, int sum) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* CC150 17.11 */ | |
// implement a method rand7() given rand5() | |
#include <iostream> | |
#include <cstdlib> | |
#include <map> | |
#define N 10000 | |
using namespace std; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* CC150 17.9 */ | |
// find the frequency of occurrences of any given word in a book | |
#include <iostream> | |
#include <string> | |
#include <unordered_map> | |
#include <fstream> | |
using namespace std; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* CC150 17.8 */ | |
// find the contiguous sequence with the largest sum, return the sum | |
// book solution | |
#include <iostream> | |
#include <vector> | |
using namespace std; | |
// what if required to return start and end indices of the sequence?!! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* CC150 17.7 */ | |
// print an English phrase that describes the given integer | |
// http://en.wikipedia.org/wiki/English_numerals | |
#include <iostream> | |
#include <string> | |
using namespace std; | |
string digits[] = { "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* CC150 17.6 */ | |
// find the smallest sequence m through n s.t. by sorting this sequence the entire array would be sorted | |
#include <iostream> | |
#include <vector> | |
using namespace std; | |
pair<int, int> findSeq(vector<int> arr) | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* CC150 17.5 */ | |
// the game of master mind (RGBY) | |
#include <iostream> | |
#define N 4 | |
class Result { | |
public: | |
Result() : hits(0), phits(0) {} | |
Result(int h, int p) : hits(h), phits(p) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* CC150 17.4 */ | |
// find the maximum of two numbers without if-else or any comparison operator | |
#include <iostream> | |
int sign(int n) { return (n >> 31) & 0x1 ^ 1; } // 1 for positive and 0 for negative | |
int maxNumber(int a, int b) | |
{ | |
// if ab < 0, return the positive one, filt = sign(a) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* CC150 17.3 */ | |
// compute the number of tailing zeros in n factorial | |
#include <iostream> | |
int tailZeros(int n) | |
{ | |
int count = 0; | |
for (int fact = 5; fact <= n; fact += 5) { | |
for (int j = fact; j % 5 == 0; j /= 5) |
NewerOlder