-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from alexander-ponomaroff/issue-41
Fix issue #41 – Project Fully Refactored, Files Renamed, CONTRIBUTING.md added
- Loading branch information
Showing
55 changed files
with
344 additions
and
488 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# **How to Contribute** | ||
|
||
Look through Collection_of_Algorithms folder. If you know of a certain C++ algorithm or a problem, however you notice that it is missing from our collection or if it exists but can be improved: | ||
* proceed to file an issue, stating what you want to work on | ||
* create a file (following our naming conventions) to work on new code or edit existing code in existing files | ||
* create a pull request to merge your work | ||
* look out for comments on your pull request that request changes or additions to your code | ||
|
||
## **File Naming Convention** | ||
|
||
### All new files should follow the following naming format: | ||
|
||
**MergeSort.cpp** | ||
|
||
* Capitalize the first letters of every word in the title | ||
* No spaces or special characters between words | ||
* The rest of the letters are lowercase | ||
|
||
### Do not name files in the following format: | ||
|
||
* mergeSort.cpp | ||
* merge_sort.cpp | ||
* merge sort.cpp | ||
* Merge-Sort.cpp | ||
* ... |
File renamed without changes.
244 changes: 122 additions & 122 deletions
244
algos/SudokuSolve.cpp → ...f_Algorithms/Backtracking/SudokuSolve.cpp
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,123 +1,123 @@ | ||
// A Backtracking program in C++ to solve Sudoku problem | ||
#include <iostream> | ||
#define UNASSIGNED 0 | ||
#define N 4 | ||
#define SQN 2 | ||
using namespace std; | ||
// This function finds an entry in grid that is still unassigned | ||
bool Find0Loc(int grid[N][N], int &row, int &col); | ||
|
||
// Checks whether it will be legal to assign num to the given row,col | ||
bool isSafe(int grid[N][N], int row, int col, int num); | ||
|
||
/* Takes a partially filled-in grid and attempts to assign values to | ||
all unassigned locations in such a way to meet the requirements | ||
for Sudoku solution (non-duplication across rows, columns, and boxes) */ | ||
bool SolveSudoku(int grid[N][N]) | ||
{ | ||
int row, col; | ||
if (!Find0Loc(grid, row, col)) | ||
return true; // success! | ||
|
||
// consider digits 1 to 9 | ||
for (int num = 1; num <= N; num++) | ||
{ | ||
// if looks promising | ||
if (isSafe(grid, row, col, num)) | ||
{ | ||
// make tentative assignment | ||
grid[row][col] = num; | ||
if (SolveSudoku(grid)) | ||
return true; //Done! | ||
// failure, unmake & try again | ||
grid[row][col] = UNASSIGNED; | ||
} | ||
} | ||
return false; // this triggers backtracking | ||
} | ||
|
||
/* Searches the grid to find an entry that is still unassigned. If | ||
found, the reference parameters row, col will be set the location | ||
that is unassigned, and true is returned. If no unassigned entries | ||
remain, false is returned. */ | ||
bool Find0Loc(int grid[N][N], int &row, int &col) | ||
{ | ||
for (row = 0; row < N; row++) | ||
for (col = 0; col < N; col++) | ||
if (grid[row][col] == UNASSIGNED) | ||
return true; | ||
return false; | ||
} | ||
|
||
/* Returns a boolean which indicates whether any assigned entry | ||
in the specified row matches the given number. */ | ||
bool UsedInRow(int grid[N][N], int row, int num) | ||
{ | ||
for (int col = 0; col < N; col++) | ||
if (grid[row][col] == num) | ||
return true; | ||
return false; | ||
} | ||
|
||
/* Returns a boolean which indicates whether any assigned entry | ||
in the specified column matches the given number. */ | ||
bool UsedInCol(int grid[N][N], int col, int num) | ||
{ | ||
for (int row = 0; row < N; row++) | ||
if (grid[row][col] == num) | ||
return true; | ||
return false; | ||
} | ||
|
||
/* Returns a boolean which indicates whether any assigned entry | ||
within the specified 3x3 box matches the given number. */ | ||
bool UsedInBox(int grid[N][N], int boxStartRow, int boxStartCol, int num) | ||
{ | ||
for (int row = 0; row < SQN; row++) | ||
for (int col = 0; col < SQN; col++) | ||
if (grid[row+boxStartRow][col+boxStartCol] == num) | ||
return true; | ||
return false; | ||
} | ||
|
||
/* Returns a boolean which indicates whether it will be legal to assign | ||
num to the given row,col location. */ | ||
bool isSafe(int grid[N][N], int row, int col, int num) | ||
{ | ||
/* Check if 'num' is not already placed in current row, | ||
current column and current 3x3 box */ | ||
return !UsedInRow(grid, row, num) && | ||
!UsedInCol(grid, col, num) && | ||
!UsedInBox(grid, row - row%SQN , col - col%SQN, num); | ||
} | ||
|
||
/* A utility function to print grid */ | ||
void printGrid(int grid[N][N]) | ||
{ | ||
for (int row = 0; row < N; row++) | ||
{ | ||
for (int col = 0; col < N; col++) | ||
printf("%2d", grid[row][col]); | ||
printf("\n"); | ||
} | ||
} | ||
|
||
/* Driver Program to test above functions */ | ||
int main() | ||
{ | ||
// 0 means unassigned cells | ||
int grid[N][N]; | ||
for(int i =0; i<N; i++){ | ||
for(int j=0; j<N; j++){ | ||
cout<<i<<j; | ||
cin>>grid[i][j]; | ||
} | ||
} | ||
|
||
if (SolveSudoku(grid) == true) | ||
printGrid(grid); | ||
else | ||
printf("No solution exists"); | ||
|
||
return 0; | ||
// A Backtracking program in C++ to solve Sudoku problem | ||
#include <iostream> | ||
#define UNASSIGNED 0 | ||
#define N 4 | ||
#define SQN 2 | ||
using namespace std; | ||
// This function finds an entry in grid that is still unassigned | ||
bool Find0Loc(int grid[N][N], int &row, int &col); | ||
|
||
// Checks whether it will be legal to assign num to the given row,col | ||
bool isSafe(int grid[N][N], int row, int col, int num); | ||
|
||
/* Takes a partially filled-in grid and attempts to assign values to | ||
all unassigned locations in such a way to meet the requirements | ||
for Sudoku solution (non-duplication across rows, columns, and boxes) */ | ||
bool SolveSudoku(int grid[N][N]) | ||
{ | ||
int row, col; | ||
if (!Find0Loc(grid, row, col)) | ||
return true; // success! | ||
|
||
// consider digits 1 to 9 | ||
for (int num = 1; num <= N; num++) | ||
{ | ||
// if looks promising | ||
if (isSafe(grid, row, col, num)) | ||
{ | ||
// make tentative assignment | ||
grid[row][col] = num; | ||
if (SolveSudoku(grid)) | ||
return true; //Done! | ||
// failure, unmake & try again | ||
grid[row][col] = UNASSIGNED; | ||
} | ||
} | ||
return false; // this triggers backtracking | ||
} | ||
|
||
/* Searches the grid to find an entry that is still unassigned. If | ||
found, the reference parameters row, col will be set the location | ||
that is unassigned, and true is returned. If no unassigned entries | ||
remain, false is returned. */ | ||
bool Find0Loc(int grid[N][N], int &row, int &col) | ||
{ | ||
for (row = 0; row < N; row++) | ||
for (col = 0; col < N; col++) | ||
if (grid[row][col] == UNASSIGNED) | ||
return true; | ||
return false; | ||
} | ||
|
||
/* Returns a boolean which indicates whether any assigned entry | ||
in the specified row matches the given number. */ | ||
bool UsedInRow(int grid[N][N], int row, int num) | ||
{ | ||
for (int col = 0; col < N; col++) | ||
if (grid[row][col] == num) | ||
return true; | ||
return false; | ||
} | ||
|
||
/* Returns a boolean which indicates whether any assigned entry | ||
in the specified column matches the given number. */ | ||
bool UsedInCol(int grid[N][N], int col, int num) | ||
{ | ||
for (int row = 0; row < N; row++) | ||
if (grid[row][col] == num) | ||
return true; | ||
return false; | ||
} | ||
|
||
/* Returns a boolean which indicates whether any assigned entry | ||
within the specified 3x3 box matches the given number. */ | ||
bool UsedInBox(int grid[N][N], int boxStartRow, int boxStartCol, int num) | ||
{ | ||
for (int row = 0; row < SQN; row++) | ||
for (int col = 0; col < SQN; col++) | ||
if (grid[row+boxStartRow][col+boxStartCol] == num) | ||
return true; | ||
return false; | ||
} | ||
|
||
/* Returns a boolean which indicates whether it will be legal to assign | ||
num to the given row,col location. */ | ||
bool isSafe(int grid[N][N], int row, int col, int num) | ||
{ | ||
/* Check if 'num' is not already placed in current row, | ||
current column and current 3x3 box */ | ||
return !UsedInRow(grid, row, num) && | ||
!UsedInCol(grid, col, num) && | ||
!UsedInBox(grid, row - row%SQN , col - col%SQN, num); | ||
} | ||
|
||
/* A utility function to print grid */ | ||
void printGrid(int grid[N][N]) | ||
{ | ||
for (int row = 0; row < N; row++) | ||
{ | ||
for (int col = 0; col < N; col++) | ||
printf("%2d", grid[row][col]); | ||
printf("\n"); | ||
} | ||
} | ||
|
||
/* Driver Program to test above functions */ | ||
int main() | ||
{ | ||
// 0 means unassigned cells | ||
int grid[N][N]; | ||
for(int i =0; i<N; i++){ | ||
for(int j=0; j<N; j++){ | ||
cout<<i<<j; | ||
cin>>grid[i][j]; | ||
} | ||
} | ||
|
||
if (SolveSudoku(grid) == true) | ||
printGrid(grid); | ||
else | ||
printf("No solution exists"); | ||
|
||
return 0; | ||
} |
File renamed without changes.
58 changes: 29 additions & 29 deletions
58
algos/Dynamic Programming/coinChange.cpp → ...rithms/Dynamic_Programming/CoinChange.cpp
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,29 @@ | ||
//Author:: ayushjain02 | ||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
int main(){ | ||
//you can increase the no of denominations here. | ||
int den[5]={0,1,2,5,10}; | ||
int value; | ||
cout<<"Enter the amount\n"; | ||
cin>>value; | ||
int mem[5][value+1]; | ||
mem[0][0]=0; | ||
for(int i=1;i<=4;i++) | ||
mem[i][0]=0; | ||
for(int i=1;i<=value;i++) | ||
mem[0][i]=value+1; | ||
for(int i=1;i<5;i++){ | ||
for(int j=1;j<value+1;j++) | ||
mem[i][j]=min(1+mem[i][j-den[i]],mem[i-1][j]); | ||
} | ||
for(int i=0;i<5;i++){ | ||
for(int j=0;j<value+1;j++) | ||
cout<<mem[i][j]<<" "; | ||
cout<<"\n"; | ||
} | ||
cout<<"Minimum no of coins required are: "<<mem[4][value]; | ||
return 0; | ||
} | ||
//Author:: ayushjain02 | ||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
int main(){ | ||
//you can increase the no of denominations here. | ||
int den[5]={0,1,2,5,10}; | ||
int value; | ||
cout<<"Enter the amount\n"; | ||
cin>>value; | ||
int mem[5][value+1]; | ||
mem[0][0]=0; | ||
for(int i=1;i<=4;i++) | ||
mem[i][0]=0; | ||
for(int i=1;i<=value;i++) | ||
mem[0][i]=value+1; | ||
for(int i=1;i<5;i++){ | ||
for(int j=1;j<value+1;j++) | ||
mem[i][j]=min(1+mem[i][j-den[i]],mem[i-1][j]); | ||
} | ||
for(int i=0;i<5;i++){ | ||
for(int j=0;j<value+1;j++) | ||
cout<<mem[i][j]<<" "; | ||
cout<<"\n"; | ||
} | ||
cout<<"Minimum no of coins required are: "<<mem[4][value]; | ||
return 0; | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.