Technical test for a job interview.
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

initial commit

vylion 1afd93d5

+441
+16
.vscode/c_cpp_properties.json
··· 1 + { 2 + "configurations": [ 3 + { 4 + "name": "Linux", 5 + "includePath": [ 6 + "${workspaceFolder}/**" 7 + ], 8 + "defines": [], 9 + "compilerPath": "/usr/bin/clang", 10 + "cStandard": "c11", 11 + "cppStandard": "c++11", 12 + "intelliSenseMode": "clang-x64" 13 + } 14 + ], 15 + "version": 4 16 + }
+26
.vscode/launch.json
··· 1 + { 2 + "version": "0.2.0", 3 + "configurations": [ 4 + { 5 + "name": "Debug", 6 + "type": "cppdbg", 7 + "request": "launch", 8 + "program": "${workspaceFolder}/bin/main", 9 + "args": [], 10 + "stopAtEntry": false, 11 + "cwd": "${workspaceRoot}", 12 + "environment": [], 13 + "externalConsole": true, 14 + "preLaunchTask": "build", 15 + "linux": { 16 + "MIMode": "gdb" 17 + }, 18 + "osx": { 19 + "MIMode": "lldb" 20 + }, 21 + "windows": { 22 + "MIMode": "gdb" 23 + } 24 + } 25 + ] 26 + }
+64
.vscode/settings.json
··· 1 + { 2 + "files.associations": { 3 + "cctype": "cpp", 4 + "clocale": "cpp", 5 + "cmath": "cpp", 6 + "csignal": "cpp", 7 + "cstdarg": "cpp", 8 + "cstddef": "cpp", 9 + "cstdio": "cpp", 10 + "cstdlib": "cpp", 11 + "cstring": "cpp", 12 + "ctime": "cpp", 13 + "cwchar": "cpp", 14 + "cwctype": "cpp", 15 + "array": "cpp", 16 + "atomic": "cpp", 17 + "hash_map": "cpp", 18 + "bit": "cpp", 19 + "*.tcc": "cpp", 20 + "chrono": "cpp", 21 + "cinttypes": "cpp", 22 + "complex": "cpp", 23 + "condition_variable": "cpp", 24 + "cstdint": "cpp", 25 + "deque": "cpp", 26 + "list": "cpp", 27 + "map": "cpp", 28 + "set": "cpp", 29 + "unordered_map": "cpp", 30 + "vector": "cpp", 31 + "exception": "cpp", 32 + "algorithm": "cpp", 33 + "functional": "cpp", 34 + "iterator": "cpp", 35 + "memory": "cpp", 36 + "memory_resource": "cpp", 37 + "numeric": "cpp", 38 + "optional": "cpp", 39 + "random": "cpp", 40 + "ratio": "cpp", 41 + "string": "cpp", 42 + "string_view": "cpp", 43 + "system_error": "cpp", 44 + "tuple": "cpp", 45 + "type_traits": "cpp", 46 + "utility": "cpp", 47 + "fstream": "cpp", 48 + "initializer_list": "cpp", 49 + "iosfwd": "cpp", 50 + "iostream": "cpp", 51 + "istream": "cpp", 52 + "limits": "cpp", 53 + "mutex": "cpp", 54 + "new": "cpp", 55 + "ostream": "cpp", 56 + "sstream": "cpp", 57 + "stdexcept": "cpp", 58 + "streambuf": "cpp", 59 + "thread": "cpp", 60 + "typeinfo": "cpp", 61 + "valarray": "cpp", 62 + "variant": "cpp" 63 + } 64 + }
+64
.vscode/tasks.json
··· 1 + { 2 + "version": "2.0.0", 3 + "tasks": [ 4 + { 5 + "label": "build", 6 + "type": "shell", 7 + "group": { 8 + "kind": "build", 9 + "isDefault": true 10 + }, 11 + "windows": { 12 + "command": "powershell" 13 + }, 14 + "linux": { 15 + "command": "bash" 16 + }, 17 + "osx": { 18 + "command": "bash" 19 + }, 20 + "args": [ 21 + "-c", 22 + "make" 23 + ] 24 + }, 25 + { 26 + "label": "build & run", 27 + "type": "shell", 28 + "group": { 29 + "kind": "test", 30 + "isDefault": true 31 + }, 32 + "windows": { 33 + "command": "powershell" 34 + }, 35 + "linux": { 36 + "command": "bash" 37 + }, 38 + "osx": { 39 + "command": "bash" 40 + }, 41 + "args": [ 42 + "-c", 43 + "'make run'" 44 + ] 45 + }, 46 + { 47 + "label": "clean", 48 + "type": "shell", 49 + "windows": { 50 + "command": "powershell" 51 + }, 52 + "linux": { 53 + "command": "bash" 54 + }, 55 + "osx": { 56 + "command": "bash" 57 + }, 58 + "args": [ 59 + "-c", 60 + "'make clean'" 61 + ] 62 + } 63 + ] 64 + }
+26
Makefile
··· 1 + CC := g++ 2 + C_FLAGS := -std=c++11 -Wall -Wextra -g 3 + 4 + BIN := bin 5 + SRC := src 6 + INCLUDE := include 7 + LIB := lib 8 + 9 + LIBRARIES := 10 + 11 + ifeq ($(OS),Windows_NT) 12 + EXECUTABLE := main.exe 13 + else 14 + EXECUTABLE := main 15 + endif 16 + 17 + all: $(EXECUTABLE) 18 + 19 + clean: 20 + $(RM) $(BIN)/$(EXECUTABLE) 21 + 22 + run: all 23 + ./$(BIN)/$(EXECUTABLE) 24 + 25 + $(EXECUTABLE): $(SRC)/*.cpp 26 + $(CC) $(C_FLAGS) -I$(INCLUDE) -L$(LIB) $^ -o $@ $(LIBRARIES)
+16
include/numeral_index.hpp
··· 1 + 2 + #ifndef NUMERAL_INDEX_HPP 3 + #define NUMERAL_INDEX_HPP 4 + 5 + #include <string> 6 + #include <unordered_map> 7 + 8 + class NumeralIndex { 9 + public: 10 + bool add(std::string word, int value); 11 + 12 + private: 13 + std::unordered_map<std::string, int> index(); 14 + }; 15 + 16 + #endif // !NUMERAL_INDEX_HPP
+21
include/numeral_modifier.hpp
··· 1 + 2 + #ifndef NUMERAL_MODIFIER_HPP 3 + #define NUMERAL_MODIFIER_HPP 4 + 5 + #include "numeral_word.hpp" 6 + 7 + class NumeralModifier : public virtual NumeralWord { 8 + public: 9 + enum Join {ADD, SUBSTRACT}; 10 + 11 + NumeralModifier(std::string word, Join joiner); 12 + int evaluate(); 13 + 14 + private: 15 + Join _joiner; 16 + NumeralWord *_left, *_right; 17 + 18 + int evaluate(NumeralWord* child); 19 + }; 20 + 21 + #endif // !NUMERAL_MODIFIER_HPP
+17
include/numeral_number.hpp
··· 1 + 2 + #ifndef NUMERAL_NUMBER_HPP 3 + #define NUMERAL_NUMBER_HPP 4 + 5 + #include "numeral_word.hpp" 6 + 7 + class NumeralNumber : public virtual NumeralWord { 8 + public: 9 + NumeralNumber(std::string word); 10 + int evaluate(); 11 + 12 + private: 13 + int _value; 14 + NumeralNumber* _power; 15 + }; 16 + 17 + #endif // !NUMERAL_NUMBER_HPP
+17
include/numeral_word.hpp
··· 1 + 2 + #ifndef NUMERAL_WORD_HPP 3 + #define NUMERAL_WORD_HPP 4 + 5 + #include <string> 6 + 7 + class NumeralWord { 8 + public: 9 + NumeralWord(std::string word) : _word(word) {}; 10 + virtual ~NumeralWord(); 11 + virtual int evaluate(); 12 + 13 + protected: 14 + std::string _word; 15 + }; 16 + 17 + #endif // !NUMERAL_WORD_HPP
+109
include/trie.hpp
··· 1 + 2 + #ifndef TRIE_HPP 3 + #define TRIE_HPP 4 + 5 + #include<string> 6 + #include<unordered_map> 7 + 8 + class Trie { 9 + public: 10 + 11 + private: 12 + struct TrieNode 13 + { 14 + bool is_leaf; 15 + std::unordered_map<char, TrieNode*> children; 16 + 17 + TrieNode() : is_leaf(false) {}; 18 + 19 + TrieNode* next(char c) { 20 + if (children.find(c) != children.end()) { 21 + return children[c]; 22 + } 23 + 24 + TrieNode* new_child = new TrieNode; 25 + children[c] = new_child; 26 + 27 + return new_child; 28 + } 29 + 30 + bool insert(const std::string &word, int &pos) { 31 + if (pos == word.length()) { 32 + if (!is_leaf) { 33 + is_leaf = true; 34 + return true; 35 + } 36 + return false; 37 + } 38 + else { 39 + char c = word[pos]; 40 + TrieNode* next_node = next(c); 41 + pos++; 42 + 43 + return next_node->insert(word, pos); 44 + } 45 + } 46 + 47 + bool hasChildren() { 48 + // get iterator to first element. If value is non-empty 49 + // => there is at least 1 value => has children. 50 + // faster than looking at map size 51 + for(auto it : children) { 52 + if(it.second != nullptr) { 53 + return true; 54 + } 55 + } 56 + 57 + return false; 58 + } 59 + 60 + bool remove(const std::string &word, int &pos) { 61 + // base case: end of string 62 + if (pos == word.length() && is_leaf) { 63 + is_leaf = false; 64 + return !hasChildren(); //to be deleted if no children 65 + } 66 + 67 + // otherwise keep going 68 + char c = word[pos]; 69 + pos++; 70 + TrieNode* child = children[c]; 71 + 72 + // found nothing 73 + if (child == nullptr) { 74 + return false; 75 + } 76 + 77 + // if child has the following node, gets marked for removal, 78 + // and child itself is not a leaf 79 + if (child->children.find(c) != child->children.end() && 80 + child->remove(word, pos) && 81 + !child->is_leaf) { 82 + delete child; 83 + return true; 84 + } 85 + } 86 + 87 + bool search(const std::string &word, int &pos) { 88 + if(pos == word.length()) { 89 + return is_leaf; 90 + } 91 + 92 + char c = word[pos]; 93 + pos++; 94 + TrieNode* child = children[c]; 95 + 96 + if (child == nullptr) { 97 + return false; 98 + } 99 + 100 + return child->search(word, pos); 101 + } 102 + }; 103 + 104 + TrieNode* root; 105 + 106 + bool insert(std::string word); 107 + }; 108 + 109 + #endif // !TRIE_HPP
+5
src/main.cpp
··· 1 + #include <iostream> 2 + 3 + int main(int argc, char *argv[]) { 4 + std::cout << "Hello Easy C++ project!" << std::endl; 5 + }
+40
src/numeral_modifier.cpp
··· 1 + 2 + #include "numeral_modifier.hpp" 3 + 4 + NumeralModifier::NumeralModifier(std::string word, Join joiner) : NumeralWord(word), _joiner(joiner) {}; 5 + 6 + NumeralModifier::~NumeralModifier() { 7 + if (_left != nullptr) { 8 + delete _left; 9 + } 10 + 11 + if (_right != nullptr) { 12 + delete _right; 13 + } 14 + } 15 + 16 + int NumeralModifier::evaluate(NumeralWord* child) { 17 + if (child == nullptr) { 18 + return 0; 19 + } 20 + 21 + return child->evaluate(); 22 + } 23 + 24 + int NumeralModifier::evaluate() { 25 + switch (_joiner) 26 + { 27 + case ADD: 28 + return evaluate(_left) + evaluate(_right); 29 + //break; 30 + 31 + case SUBSTRACT: 32 + return evaluate(_left) - evaluate(_right); 33 + //break; 34 + 35 + default: 36 + return 0; 37 + //break; 38 + } 39 + } 40 +
+18
src/numeral_number.cpp
··· 1 + 2 + #include "numeral_number.hpp" 3 + 4 + NumeralNumber::NumeralNumber(std::string word) : NumeralWord(word) {}; 5 + 6 + NumeralNumber::~NumeralNumber() { 7 + if (_power != nullptr) { 8 + delete _power; 9 + } 10 + } 11 + 12 + int NumeralNumber::evaluate() { 13 + if (_power != nullptr) { 14 + return _value * _power->evaluate(); 15 + } 16 + 17 + return _value; 18 + }
+2
src/trie.cpp
··· 1 + 2 + #include "trie.hpp"