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
21 changes: 16 additions & 5 deletions .github/workflows/awesome_workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,22 @@ jobs:
- name: Lint modified files
shell: bash
run: python3 scripts/file_linter.py
- name: Commit and push changes
run: |
git diff DIRECTORY.md
git commit -am "clang-format and clang-tidy fixes for ${GITHUB_SHA::8}" || true
git push origin HEAD:$GITHUB_REF || true
- name: Create Pull Request for automated fixes
uses: peter-evans/create-pull-request@v5
with:
commit-message: 'chore: clang-format and clang-tidy fixes for ${{ github.sha }}'
title: 'chore: Automated formatting and linting fixes'
body: |
## Automated Changes
This PR contains automated formatting and linting fixes generated by the CI workflow.

- clang-format applied
- clang-tidy checks fixed
- filename formatting applied
branch: automated-fixes-${{ github.run_number }}
delete-branch: true
author: 'github-actions[bot] <github-actions[bot]@users.noreply.github.com>'
committer: 'github-actions[bot] <github-actions[bot]@users.noreply.github.com>'

build:
name: Compile checks
Expand Down
55 changes: 27 additions & 28 deletions data_structures/trie_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ class trie {
std::array<std::shared_ptr<trie>, NUM_CHARS << 1> arr;
bool isEndofWord = false; ///< identifier if a node is terminal node

/** @brief Check whether the current trie node has any children */
bool hasChildren() const {
for (const auto& child : arr) {
if (child) {
return true;
}
}
return false;
}

/**
* @brief Convert a character to integer for indexing
*
Expand All @@ -55,7 +65,7 @@ class trie {
*/
bool search(const std::shared_ptr<trie>& root, const std::string& str,
int index) {
if (index == str.length()) {
if (index == (int)str.length()) {
if (!root->isEndofWord) {
return false;
}
Expand Down Expand Up @@ -105,7 +115,7 @@ class trie {
* @returns `false` if not found
*/
bool search(const std::string& str, int index) {
if (index == str.length()) {
if (index == (int)str.length()) {
if (!isEndofWord) {
return false;
}
Expand All @@ -132,41 +142,27 @@ class trie {
* @returns `false` if unsuccessful
*/
bool deleteString(const std::string& str, int index) {
if (index == str.length()) {
if (index == (int)str.length()) {
if (!isEndofWord) {
return false;
}
isEndofWord = false;
// following lines - possible source of error?
// for (int i = 0; i < NUM_CHARS; i++)
// if (!arr[i])
// return false;
return true;
}
int j = char_to_int(str[index]);
if (!arr[j]) {
return false;
}
bool var = deleteString(str, index + 1);
if (var) {
bool deleted = arr[j]->deleteString(str, index + 1);
if (!deleted) {
return false;
}

if (!arr[j]->isEndofWord && !arr[j]->hasChildren()) {
arr[j].reset();
if (isEndofWord) {
return false;
} else {
int i = 0;
for (i = 0; i < NUM_CHARS; i++) {
if (arr[i]) {
return false;
}
}
return true;
}
}

/* should not return here */
std::cout << __func__ << ":" << __LINE__
<< "Should not reach this line\n";
return false;
return true;
}
};
} // namespace data_structures
Expand All @@ -192,10 +188,13 @@ static void test() {
assert(root.search("World", 0));
std::cout << "World - " << root.search("World", 0) << "\n";

// Following lines of code give erroneous output
// root.deleteString("hello", 0);
// assert(!root.search("hello", 0));
// std::cout << "hello - " << root.search("world", 0) << "\n";
root.insert("Hell");
assert(root.search("Hell", 0));
assert(root.search("Hello", 0));

assert(root.deleteString("Hello", 0));
assert(!root.search("Hello", 0));
assert(root.search("Hell", 0));
}

/**
Expand Down
53 changes: 53 additions & 0 deletions search/recursive_binary_search.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* @file recursive_binary_search.cpp
* @brief Recursive Binary Search implementation
*/

#include <cassert>
#include <iostream>
#include <vector>

namespace search {

namespace recursive_binary_search {

int binarySearch(const std::vector<int>& arr,
int left,
int right,
int target) {
if (left > right) {
return -1;
}

int mid = left + (right - left) / 2;

if (arr[mid] == target) {
return mid;
}

if (arr[mid] > target) {
return binarySearch(arr, left, mid - 1, target);
}

return binarySearch(arr, mid + 1, right, target);
}

} // namespace recursive_binary_search

} // namespace search

static void test() {
std::vector<int> arr = {1, 2, 3, 4, 5, 6, 7};

int result = search::recursive_binary_search::binarySearch(
arr, 0, arr.size() - 1, 5);

assert(result == 4);

std::cout << "Test passed!\n";
}

int main() {
test();
return 0;
}