-
-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Insertion sort, palindrome code as well as Subset generator code (
#88) * added InsertionSort sort * added Palindrome.sol which checks whether steing is palindrome or not * added Subset.sol which finds total number of subsets in array * Update src/Maths/Palindrome.sol --------- Co-authored-by: @Yash9276 <[email protected]> Co-authored-by: Maxime Kubik <[email protected]>
- Loading branch information
1 parent
7e4dfc8
commit 08cba51
Showing
3 changed files
with
97 additions
and
0 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,24 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.0; | ||
|
||
contract PalindromeChecker { | ||
// Function to check if a given string is a palindrome | ||
function isPalindrome(string memory str) public pure returns (bool) { | ||
// Convert the input string to a bytes array | ||
bytes memory bytesStr = bytes(str); | ||
// Get the length of the string | ||
uint256 len = bytesStr.length; | ||
|
||
// Loop through the first half of the string | ||
for (uint256 i = 0; i < len / 2; i++) { | ||
// Compare characters from the start and end of the string | ||
if (bytesStr[i] != bytesStr[len - 1 - i]) { | ||
// If characters don't match, return false (not a palindrome) | ||
return false; | ||
} | ||
} | ||
|
||
// If the loop completes without finding mismatches, return true (a palindrome) | ||
return true; | ||
} | ||
} |
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,33 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.0; | ||
|
||
contract SubsetGenerator { | ||
// Function to generate all possible subsets of an input array | ||
function generateSubsets(uint[] memory arr) public pure returns (uint[][] memory) { | ||
uint n = arr.length; // Get the length of the input array | ||
uint[][] memory subsets = new uint[][](2**n); // Initialize an array to store subsets | ||
|
||
// Loop through all possible binary combinations | ||
for (uint i = 0; i < 2**n; i++) { | ||
uint[] memory subset = new uint[](n); // Create an array for the current subset | ||
uint count = 0; // Counter for the number of elements in the subset | ||
|
||
// Loop through the bits of the current binary combination | ||
for (uint j = 0; j < n; j++) { | ||
// Check if the j-th bit of i is set to 1 | ||
if ((i & (1 << j)) != 0) { | ||
subset[count] = arr[j]; // Include the element in the subset | ||
count++; // Increment the count of elements in the subset | ||
} | ||
} | ||
|
||
// Create a new array to store the final subset (with only the necessary elements) | ||
subsets[i] = new uint[](count); | ||
for (uint k = 0; k < count; k++) { | ||
subsets[i][k] = subset[k]; // Copy the elements to the final subset | ||
} | ||
} | ||
|
||
return subsets; // Return the array of subsets | ||
} | ||
} |
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,40 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.0; | ||
|
||
/** | ||
* @title InsertionSOrt | ||
* @author [Yash9276](https://github.com/Yash9276) | ||
* @notice A contract that implements the insertion sort algorithm. | ||
* @dev It repeatedly takes an element from the unsorted part and inserts | ||
* it into its correct position in the sorted part of the array, | ||
*shifting the elements if necessary. It has an average and worst-case time complexity of O(n^2) but | ||
* is efficient for small datasets or partially sorted data. | ||
*/ | ||
|
||
contract InsertionSort { | ||
/* @notice Sorts the given array in increasing order. | ||
@param input The array to sort. | ||
@return output The resulting sorted array in increasing order | ||
*/ | ||
|
||
function sort(uint256[] memory myArray) | ||
public | ||
pure | ||
returns (uint256[] memory) | ||
{ | ||
uint256 n = myArray.length; | ||
for (uint256 i = 1; i < n; i++) { | ||
uint256 key = myArray[i]; | ||
int256 j = int256(i - 1); | ||
|
||
while (j >= 0 && int256(myArray[uint256(j)]) > int256(key)) { | ||
myArray[uint256(j + 1)] = myArray[uint256(j)]; | ||
j--; | ||
} | ||
|
||
myArray[uint256(j + 1)] = key; | ||
} | ||
return myArray; | ||
} | ||
} |