Skip to content
Merged
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
29 changes: 18 additions & 11 deletions Hacktobercpp/count_digits.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
/*Given a number n. Count the number of digits in n which evenly divide n. Return an integer, total number of digits of n which divides n evenly.

Note :- Evenly divides means whether n is divisible by a digit i.e. leaves a remainder 0 when divided.*/
#include <bits/stdc++.h>
#include <iostream>
using namespace std;

/*
Given a number N, count the number of digits in N
which evenly divide N.
Return the count of such digits.
*/

class Solution {
public:
int evenlyDivides(int N) {
int count = 0;
int temp = N;

while (temp > 0) {
int last_digit = temp % 10;
temp = temp / 10;
int digit = temp % 10; // Get last digit
temp /= 10; // Remove last digit

// Check if the last digit is non-zero and divides N
if (last_digit != 0 && N % last_digit == 0) {
// Check if digit is non-zero and divides N
if (digit != 0 && N % digit == 0) {
count++;
}
}
Expand All @@ -24,16 +27,20 @@ class Solution {
}
};

// Driver Code Starts.
// Driver code
int main() {
int t;
cout << "Enter number of test cases: ";
cin >> t;

while (t--) {
int N;
cout << "Enter a number: ";
cin >> N;
Solution ob;
cout << ob.evenlyDivides(N) << endl;

Solution obj;
cout << "Number of digits evenly dividing " << N << " is: "
<< obj.evenlyDivides(N) << endl;
}

return 0;
Expand Down
135 changes: 58 additions & 77 deletions Josephus Circular Linked List/main.cpp
Original file line number Diff line number Diff line change
@@ -1,127 +1,108 @@
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <string>
using namespace std;

// Node structure for circular linked list
struct Node {
string payload;
string name;
Node* next;
};

/**
* constructing a new load with the parameter as a the payload
*/
Node* newNode(string payload) {
// Function to create a new node
Node* createNode(const string& name) {
Node* node = new Node();
node->payload = payload;
node->name = name;
node->next = nullptr;
return node;
}

/**
* loadGame takes an int and a string vector of names and loads a circular
* linked list (of n size), with the names from the vector
*/
Node* loadGame(int n, vector<string> names) {
if(n == 0) {
return nullptr;
}
// Function to load names into a circular linked list
Node* loadGame(int n, const vector<string>& names) {
if (n == 0) return nullptr;
if (n != names.size()) throw runtime_error("Number of names does not match n");

if(n != names.size()) {
throw runtime_error("Mismatch: n does not match number of names provided");
}

Node* head = nullptr;
Node* prev = nullptr;
string name;

for (int i = 0; i < n; ++i) {
name = names.at(i);
Node* newNodePtr = newNode(name);
if (head == nullptr) {
head = newNodePtr; // initialize head specially
} else {
prev->next = newNodePtr;
}
prev = newNodePtr;
Node* node = createNode(names[i]);
if (!head) head = node;
else prev->next = node;
prev = node;
}
prev->next = head; //make circular
prev->next = head; // make it circular

return head;
}

/**
* prints out the data of each node in the linked list
*/
void print(Node* start) { // prints list
// Function to print circular linked list
void printList(Node* start) {
if (!start) return;
Node* curr = start;
while (curr != nullptr) {
cout << curr->payload << endl;
do {
cout << curr->name << " ";
curr = curr->next;
if (curr == start) {
break; // exit circular list
}
}
} while (curr != start);
cout << endl;
}

/**
* Runs the Josephus algorithm on a circular linked list. This algorithm simulates
* a game where every k-th person is eliminated until only one person remains.
* The function takes two parameters, start, which is a node*, pointing to the start, and an integer
* which is how many nodes to iterate past
*/
Node* runGame(Node* start, int k) { // josephus w circular list, k = num skips
if(start == nullptr) {
return nullptr;
}
// Josephus game function
Node* runGame(Node* start, int k) {
if (!start) return nullptr;
if (k <= 0) throw runtime_error("k must be positive");

if(k <= 0) {
throw runtime_error("k must be positive");
}

Node* curr = start;
Node* prev = curr;
while (curr->next != curr) { // exit condition, last person standing
for (int i = 0; i < k; ++i) { // find kth node
Node* prev = nullptr;

while (curr->next != curr) { // more than one person left
// skip k-1 nodes
for (int i = 0; i < k - 1; ++i) {
prev = curr;
curr = curr->next;
}
// eliminate curr
cout << "Eliminated: " << curr->name << endl;
prev->next = curr->next;
Node* temp = curr;
curr = curr->next;
delete temp;
}
return curr; // last person standing
return curr; // last person remaining
}
Comment on lines +50 to 72
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Critical: Null pointer dereference when k=1.

When k=1, the for loop at lines 60-63 never executes, leaving prev as nullptr. Line 66 then dereferences prev, causing a crash.

Additionally, after the first elimination, prev is not updated correctly before the next iteration of the outer loop, which will cause issues.

Apply this fix to properly handle the circular elimination:

 Node* runGame(Node* start, int k) {
     if (!start) return nullptr;
     if (k <= 0) throw runtime_error("k must be positive");
 
     Node* curr = start;
-    Node* prev = nullptr;
 
     while (curr->next != curr) { // more than one person left
+        Node* prev = curr;
         // skip k-1 nodes
         for (int i = 0; i < k - 1; ++i) {
             prev = curr;
             curr = curr->next;
         }
         // eliminate curr
         cout << "Eliminated: " << curr->name << endl;
         prev->next = curr->next;
         Node* temp = curr;
         curr = curr->next;
         delete temp;
     }
     return curr; // last person remaining
 }

The key change: initialize prev = curr at the start of each iteration of the outer loop, ensuring it's always set before the elimination step.

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In Josephus Circular Linked List/main.cpp around lines 50 to 72, the code can
dereference a null prev when k==1 and fails to keep prev correct between
iterations; to fix, inside the outer while loop initialize prev = curr at the
start of each iteration, then run the inner loop k-1 times updating prev = curr;
curr = curr->next, perform elimination via prev->next = curr->next, store temp
for deletion, and set curr = prev->next after deleting temp so prev is always
valid for the next iteration.


/* Driver program to test above functions */
int main() {
int n=1, k=1, max; // n = num names; k = num skips (minus 1)
string name;
vector<string> names;
int n, k;
cout << "Enter number of players: ";
cin >> n;
cout << "Enter step count (k): ";
cin >> k;

// get inputs and check if cin is in a valid state
cin >> n >> k;
if(!cin) { throw runtime_error("Faulty input"); }
if(n <= 0 || k < 0) { throw runtime_error("n must be positive, k must be non-negative"); }
if (n <= 0 || k <= 0) {
cout << "Invalid input: n and k must be positive integers." << endl;
return 1;
}

while (cin >> name && name != ".") { names.push_back(name); } // EOF or . ends input
cout << "Enter names of players (enter '.' to finish):" << endl;
vector<string> names;
string name;
for (int i = 0; i < n; ++i) {
cin >> name;
names.push_back(name);
}
Comment on lines +86 to +92
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Misleading comment about input termination.

The comment states "enter '.' to finish" but the code simply reads exactly n names without checking for '.'. This mismatch could confuse users.

Apply this fix:

-    cout << "Enter names of players (enter '.' to finish):" << endl;
+    cout << "Enter names of " << n << " players:" << endl;
     vector<string> names;
     string name;
     for (int i = 0; i < n; ++i) {
         cin >> name;
         names.push_back(name);
     }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
cout << "Enter names of players (enter '.' to finish):" << endl;
vector<string> names;
string name;
for (int i = 0; i < n; ++i) {
cin >> name;
names.push_back(name);
}
cout << "Enter names of " << n << " players:" << endl;
vector<string> names;
string name;
for (int i = 0; i < n; ++i) {
cin >> name;
names.push_back(name);
}
🤖 Prompt for AI Agents
In Josephus Circular Linked List/main.cpp around lines 86 to 92, the prompt says
"enter '.' to finish" but the code reads exactly n names; update the input loop
to honor the termination token by reading in a loop that breaks if the user
enters "." (or stops when the count reaches n), trim/validate the input before
pushing to names, and keep the prompt consistent with behavior (or alternatively
change the prompt to not mention '.' if you intend to read exactly n names).


// initialize and run game
Node* startPerson = loadGame(n, names);

cout << "\nInitial Circle of Players:" << endl;
printList(startPerson);
cout << endl;
print(startPerson);
cout << endl;
Node* lastPerson = runGame(startPerson, k);

if (lastPerson != nullptr) {
cout << lastPerson->payload << " wins!" << endl;
} else {
cout << "error: null game" << endl;
Node* winner = runGame(startPerson, k);

if (winner) {
cout << "\nWinner is: " << winner->name << "!" << endl;
delete winner; // clean up last node
}

delete lastPerson; // delete the last node remaining in the heap

return 0;
}
105 changes: 75 additions & 30 deletions Tic-Tac-Toe/index.html
Original file line number Diff line number Diff line change
@@ -1,33 +1,78 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Tic-Tac-Toe Game</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="msg-container hide">
<p id="msg">Winner</p>
<button id="new-btn">New Game</button>
</div>
<main>
<h1>Tic Tac Toe</h1>
<div class="container">
<div class="game">
<button class="box"></button>
<button class="box"></button>
<button class="box"></button>
<button class="box"></button>
<button class="box"></button>
<button class="box"></button>
<button class="box"></button>
<button class="box"></button>
<button class="box"></button>
</div>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Tic-Tac-Toe | Hacktoberfest Project</title>
<link rel="stylesheet" href="style.css" />
<style>
/* Quick inline CSS for beginners (optional, you can move to style.css) */
body {
font-family: Arial, sans-serif;
text-align: center;
background-color: #f5f5f5;
}
h1 {
margin-top: 20px;
}
.container {
display: inline-block;
margin: 20px auto;
}
.game {
display: grid;
grid-template-columns: repeat(3, 100px);
grid-gap: 10px;
}
.box {
width: 100px;
height: 100px;
font-size: 2rem;
cursor: pointer;
}
.msg-container {
margin: 20px 0;
}
.hide {
display: none;
}
#reset-btn, #new-btn {
margin-top: 10px;
padding: 10px 20px;
font-size: 1rem;
cursor: pointer;
}
</style>
Comment on lines +8 to +45
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion | 🟠 Major

Remove duplicate inline CSS to maintain single source of truth.

The inline styles duplicate and conflict with style.css. For example, the body background is #f5f5f5 here but #548687 in the external stylesheet. Maintaining styles in two places violates DRY and creates confusion.

Remove the entire inline style block:

   <link rel="stylesheet" href="style.css" />
-  <style>
-    /* Quick inline CSS for beginners (optional, you can move to style.css) */
-    body {
-      font-family: Arial, sans-serif;
-      text-align: center;
-      background-color: #f5f5f5;
-    }
-    h1 {
-      margin-top: 20px;
-    }
-    .container {
-      display: inline-block;
-      margin: 20px auto;
-    }
-    .game {
-      display: grid;
-      grid-template-columns: repeat(3, 100px);
-      grid-gap: 10px;
-    }
-    .box {
-      width: 100px;
-      height: 100px;
-      font-size: 2rem;
-      cursor: pointer;
-    }
-    .msg-container {
-      margin: 20px 0;
-    }
-    .hide {
-      display: none;
-    }
-    #reset-btn, #new-btn {
-      margin-top: 10px;
-      padding: 10px 20px;
-      font-size: 1rem;
-      cursor: pointer;
-    }
-  </style>
 </head>

If additional styles are needed beyond what's in style.css, add them to the external stylesheet instead.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
<style>
/* Quick inline CSS for beginners (optional, you can move to style.css) */
body {
font-family: Arial, sans-serif;
text-align: center;
background-color: #f5f5f5;
}
h1 {
margin-top: 20px;
}
.container {
display: inline-block;
margin: 20px auto;
}
.game {
display: grid;
grid-template-columns: repeat(3, 100px);
grid-gap: 10px;
}
.box {
width: 100px;
height: 100px;
font-size: 2rem;
cursor: pointer;
}
.msg-container {
margin: 20px 0;
}
.hide {
display: none;
}
#reset-btn, #new-btn {
margin-top: 10px;
padding: 10px 20px;
font-size: 1rem;
cursor: pointer;
}
</style>
🤖 Prompt for AI Agents
In Tic-Tac-Toe/index.html around lines 8 to 45, there is a duplicate inline
<style> block that conflicts with style.css (e.g., body background differs) and
violates DRY; remove the entire inline style block from the HTML and move any
necessary, unique rules into style.css instead so all styling lives in the
external stylesheet, ensuring no conflicting selectors or duplicated properties
remain.

</head>
<body>
<!-- Message popup for winner or draw -->
<div class="msg-container hide">
<p id="msg">Winner</p>
<button id="new-btn">Start New Game</button>
</div>

<main>
<h1>Tic Tac Toe</h1>

<div class="container">
<div class="game">
<!-- 9 grid buttons -->
<button class="box"></button>
<button class="box"></button>
<button class="box"></button>
<button class="box"></button>
<button class="box"></button>
<button class="box"></button>
<button class="box"></button>
<button class="box"></button>
<button class="box"></button>
</div>
<button id="reset-btn">Reset Game</button>
</main>
<script src="app.js"></script>
</body>
</html>
</div>

<button id="reset-btn">Reset Game</button>
</main>

<!-- Link to JavaScript file -->
<script src="app.js"></script>
</body>
</html>
Loading
Loading