Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion modules/arrays_and_strings/arrays_algorithms.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ bool allUniqueBruteForce(std::string s);
std::string inverse(const char* str);
bool isPermBruteForce(std::string s1, std::string s2);
bool isPerm(const std::string& s1, const std::string& s2);
bool isPalindrome(const std::string& s);
std::string URLify(const char* s, int n);
bool isPalindromePerm(std::string s);
bool oneAway(std::string s1, std::string s2);
std::string strCompression(const std::string& s);
std::vector<int> rotate(std::vector<int> mat, int n);
std::vector<int> rotateInPlace(std::vector<int> mat, int n);
int numberOfIslands(std::vector<int> v, int N, int M);

void perm(std::string s, int l, int r);
#endif // MODULES_ARRAYS_AND_STRINGS_ARRAYS_ALGORITHMS_H_
13 changes: 13 additions & 0 deletions modules/arrays_and_strings/arrays_alroritms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <utility>
#include <unordered_set>
#include <unordered_map>
#include <cmath>
#include "../../modules/arrays_and_strings/arrays_algorithms.h"

using std::vector;
Expand Down Expand Up @@ -218,3 +219,15 @@ int numberOfIslands(vector<int> v, int N, int M) {

return count;
}

bool isPalindrome(const std::string& s) {
for (size_t i = 0; i < s.size() / 2; ++i) {
if (s[i] != s[s.size() - i - 1])
return false;
}
return true;
}

void perm(const std::string& s) {
std::vector<bool> chosen(s.size());
}
22 changes: 22 additions & 0 deletions modules/arrays_and_strings/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,28 @@ INSTANTIATE_TEST_SUITE_P(
std::make_tuple<vector<int>>({ 0, 0, 0, 0, 1, 0, 0, 0, 0 }, 1),
std::make_tuple<vector<int>>({ 1, 1, 1, 0, 0, 1, 1, 0, 0 }, 2)));

class PalindromeTestFixture : public ::testing::TestWithParam<std::tuple<std::string, bool>> {};

TEST_P(PalindromeTestFixture, correctness_is_palindrome) {
std::string s = std::get<0>(GetParam());
bool expected = std::get<1>(GetParam());
ASSERT_EQ(isPalindrome(s), expected);
}


INSTANTIATE_TEST_SUITE_P(
IsPalindrome,
PalindromeTestFixture,
::testing::Values(
std::make_tuple<std::string>("a", true),
std::make_tuple<std::string>("ab", false),
std::make_tuple<std::string>("aa", true),
std::make_tuple<std::string>("aaaa", true),
std::make_tuple<std::string>("baab", true),
std::make_tuple<std::string>("aabaa", true),
std::make_tuple<std::string>("nolemonnomelon", true),
std::make_tuple<std::string>("nolemoannomelon", false),
std::make_tuple<std::string>("level", true)));

int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
Expand Down
18 changes: 18 additions & 0 deletions modules/linked_lists/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
get_filename_component(ProjectId ${CMAKE_CURRENT_SOURCE_DIR} NAME)

set(ProjectId "${ProjectId}")
project( ${ProjectId} )
message( STATUS "-- " ${ProjectId} )

file(GLOB_RECURSE header_files "*.h")
file(GLOB_RECURSE source_files "*.cpp")
set(PACK_LIB "${ProjectId}_lib")
add_library(${PACK_LIB} STATIC ${header_files} ${source_files})

add_executable(${ProjectId} ${source_files})

target_link_libraries(${ProjectId} ${PACK_LIB})
target_link_libraries(${ProjectId} gtest gtest_main)

enable_testing()
add_test(NAME ${ProjectId} COMMAND ${ProjectId})
232 changes: 232 additions & 0 deletions modules/linked_lists/linked_lists.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
// Copyright 2020 Vikhrev Ivan

#include <initializer_list>
#include <unordered_set>
#include "../../modules/linked_lists/linked_lists.h"

List::List(int n, int d) {
head = new Node;
Node* curr = head;
int i = 0;
while (i < n) {
curr->data = d;
if (i == n - 1) {
curr->next = nullptr;
} else {
curr->next = new Node;
}
curr = curr->next;
++i;
}
}

List::List(const std::initializer_list<int>& lst) {
head = new Node;
Node* curr = head;
size_t i = 0;
for (auto el : lst) {
curr->data = el;
if (i == lst.size() - 1) {
curr->next = nullptr;
} else {
curr->next = new Node;
}
curr = curr->next;
++i;
}
}

List::List(const List& l) {
head = new Node;
Node* curr1 = head;
Node* curr2 = l.head;
while (curr2) {
curr1->data = curr2->data;
if (curr2->next == nullptr)
curr1->next = nullptr;
else
curr1->next = new Node;
curr1 = curr1->next;
curr2 = curr2->next;
}
}

List::~List() {
Node* curr = head;
while (curr) {
curr = head->next;
delete head;
head = curr;
}
}

int List::back() const {
if (head) {
Node* curr = head;
while (curr->next) {
curr = curr->next;
}
return curr->data;
}
return 0;
}

void List::push_back(int d) {
if (head) {
Node* curr = head;
while (curr->next) {
curr = curr->next;
}
curr->next = new Node(d);
} else {
head = new Node(d);
}
}

bool List::operator==(const List& l) const {
Node* curr1 = head;
Node* curr2 = l.head;

while (curr1 && curr2) {
if ((*curr1).data == curr2->data) {
curr1 = curr1->next;
curr2 = curr2->next;
} else {
return false;
}
}

return curr1 == curr2;
}

void List::remove_duplicates1() {
std::unordered_set<int> s;
if (!empty()) {
s.insert(head->data);
Node* prev = head;
Node* curr = head->next;
while (curr) {
if (s.find(curr->data) == s.end()) {
s.insert(curr->data);
prev = prev->next;
} else {
prev->next = curr->next;
}
curr = curr->next;
}
}
}

void List::remove_duplicates2() {
if (!empty()) {
Node* p1 = head, *prev = head;
Node* p2;
while (p1) {
p2 = p1->next;
while (p2) {
if (p1->data == p2->data) {
prev->next = p2->next;
} else {
prev = prev->next;
}
p2 = p2->next;
}
p1 = p1->next;
prev = p1;
}
}
}

// not stable
void List::rearrangeBadImpl(int x) {
Node* start, *end, *curr;
start = end = curr = head;
curr = curr->next;
while (curr) {
Node* tmp = curr;
curr = curr->next;
if (tmp->data < x) {
tmp->next = start;
start = tmp;
} else {
end->next = tmp;
end = tmp;
}
}
end->next = nullptr;
head = start;
}

// stable
void List::rearrange(int x) {
if (head) {
Node *lessHead = nullptr, *lessCurr = nullptr,
*greaterHead = nullptr, *greaterCurr = nullptr,
*curr = nullptr;

curr = head;
while (curr) {
Node* tmp = curr;
curr = curr->next;
if (tmp->data < x) {
if (!lessHead) {
lessHead = tmp;
lessCurr = lessHead;
} else {
lessCurr->next = tmp;
lessCurr = lessCurr->next;
}
lessCurr->next = nullptr;
} else {
if (!greaterHead) {
greaterHead = tmp;
greaterCurr = greaterHead;
} else {
greaterCurr->next = tmp;
greaterCurr = greaterCurr->next;
}
greaterCurr->next = nullptr;
}
}
if (lessHead) {
head = lessHead;
lessCurr->next = greaterHead;
} else {
head = greaterHead;
}
}
}

// assuming that 0 from end is end
int List::find_k_from_end(int k) {
Node* curr = head;
Node* kth = head;
int i = 0;

while (curr) {
if (i > k) {
kth = kth->next;
}
++i;
curr = curr->next;
}

return kth->data;
}
std::ostream& operator<<(std::ostream& os, const List& l) {
Node *curr = l.head;
if (l.head) {
int i = 0;
while (curr) {
if (curr->next == nullptr)
os << curr->data;
else
os << curr->data << "-->";
i++;
curr = curr->next;
}
} else {
os << "empty";
}
return os;
}
44 changes: 44 additions & 0 deletions modules/linked_lists/linked_lists.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2020 Vikhrev Ivan

#include <initializer_list>
#include <iostream>
#ifndef MODULES_LINKED_LISTS_LINKED_LISTS_H_
#define MODULES_LINKED_LISTS_LINKED_LISTS_H_

struct Node {
Node* next;
int data;
Node() : next(nullptr), data(0) {}
explicit Node(int d) : next(nullptr), data(d) {}
Node(Node* n, int d) : next(n), data(d) {}
bool operator==(const Node& n) const { return (data == n.data && next == n.next); }
bool operator!=(const Node& n) const { return !(*this == n); }
};

class List {
private:
Node* head;
public:
List() : head(nullptr) {}
explicit List(int d) {head = new Node(d);}
List(int n, int d);
List(const List& l);
List(const std::initializer_list<int>& lst);
~List();
Node* get_head() const { return head; }
void push_front(int d) { head = new Node(head, d); }
void push_back(int d);
int front() const { return head ? head->data : 0; }
int back() const;
bool empty() const { return !head; }
bool operator==(const List& l) const;
bool operator!=(const List& l) const {return !(*this == l);}
friend std::ostream& operator<<(std::ostream& os, const List& l);
void remove_duplicates1();
void remove_duplicates2();
void rearrangeBadImpl(int x);
void rearrange(int x);
int find_k_from_end(int k);
};

#endif // MODULES_LINKED_LISTS_LINKED_LISTS_H_
Loading