From 794eccd629ad0c17d407d0c4eee716c3638e72a2 Mon Sep 17 00:00:00 2001 From: Emosans Date: Sat, 4 Jan 2025 12:53:07 +0530 Subject: [PATCH 1/6] new snippet - string to int --- .../cpp/string-manipulation/string-int.md | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 snippets/cpp/string-manipulation/string-int.md diff --git a/snippets/cpp/string-manipulation/string-int.md b/snippets/cpp/string-manipulation/string-int.md new file mode 100644 index 00000000..39f1ff8c --- /dev/null +++ b/snippets/cpp/string-manipulation/string-int.md @@ -0,0 +1,20 @@ +--- +title: String Int +description: Converts string to int without built in stoi() +author: Emosans +tags: string,int +--- + +```cpp +std::int to_num(const std::string& input){ + int num=0; + int len=input.size(); + for(int i=0;iint) +``` \ No newline at end of file From 943300610102c47d7aab1859e7617487abc768d7 Mon Sep 17 00:00:00 2001 From: Emosans Date: Sat, 4 Jan 2025 16:10:11 +0530 Subject: [PATCH 2/6] new snippet- swap numbers --- snippets/cpp/math-and-numbers/swap-numbers.md | 19 ++++++++++++++++++ .../cpp/string-manipulation/string-int.md | 20 ------------------- 2 files changed, 19 insertions(+), 20 deletions(-) create mode 100644 snippets/cpp/math-and-numbers/swap-numbers.md delete mode 100644 snippets/cpp/string-manipulation/string-int.md diff --git a/snippets/cpp/math-and-numbers/swap-numbers.md b/snippets/cpp/math-and-numbers/swap-numbers.md new file mode 100644 index 00000000..27496a31 --- /dev/null +++ b/snippets/cpp/math-and-numbers/swap-numbers.md @@ -0,0 +1,19 @@ +--- +title: Swap numbers +description: Swaps two numbers without using third variable +author: Emosans +tags: swap,numbers +--- + +```cpp +#include +std::tuple swap(int num1,int num2){ + num1=num1+num2; + num2=num1-num2; + num1=num1-num2; + return std::make_tuple(num1,num2); +} + +// Usage +auto swapped=swap(3,4); // Returns a tuple (access the values using std::get<0>(swapped)/std::get<1>(swapped)) +``` \ No newline at end of file diff --git a/snippets/cpp/string-manipulation/string-int.md b/snippets/cpp/string-manipulation/string-int.md deleted file mode 100644 index 39f1ff8c..00000000 --- a/snippets/cpp/string-manipulation/string-int.md +++ /dev/null @@ -1,20 +0,0 @@ ---- -title: String Int -description: Converts string to int without built in stoi() -author: Emosans -tags: string,int ---- - -```cpp -std::int to_num(const std::string& input){ - int num=0; - int len=input.size(); - for(int i=0;iint) -``` \ No newline at end of file From 78fa4a1d96cde3279d9a8e5997f37c937f4e1208 Mon Sep 17 00:00:00 2001 From: Emosans Date: Sat, 4 Jan 2025 16:33:41 +0530 Subject: [PATCH 3/6] snippet- changed to C --- .../c/mathematical-functions/swap-numbers.md | 19 +++++++++++++++++++ snippets/cpp/math-and-numbers/swap-numbers.md | 19 ------------------- 2 files changed, 19 insertions(+), 19 deletions(-) create mode 100644 snippets/c/mathematical-functions/swap-numbers.md delete mode 100644 snippets/cpp/math-and-numbers/swap-numbers.md diff --git a/snippets/c/mathematical-functions/swap-numbers.md b/snippets/c/mathematical-functions/swap-numbers.md new file mode 100644 index 00000000..c07d39b0 --- /dev/null +++ b/snippets/c/mathematical-functions/swap-numbers.md @@ -0,0 +1,19 @@ +--- +title: Swap numbers +description: Swaps two numbers without using third variable +author: Emosans +tags: swap,numbers +--- + +```c +#include +void swap(int* num1,int* num2){ + *num1= *num1 + *num2; + *num2= *num1 - *num2; + *num1= *num1 - *num2; +} + +// Usage: +int a=3,b=4; +auto swapped=swap(&a,&b); // simply use printf after this to print swapped values +``` \ No newline at end of file diff --git a/snippets/cpp/math-and-numbers/swap-numbers.md b/snippets/cpp/math-and-numbers/swap-numbers.md deleted file mode 100644 index 27496a31..00000000 --- a/snippets/cpp/math-and-numbers/swap-numbers.md +++ /dev/null @@ -1,19 +0,0 @@ ---- -title: Swap numbers -description: Swaps two numbers without using third variable -author: Emosans -tags: swap,numbers ---- - -```cpp -#include -std::tuple swap(int num1,int num2){ - num1=num1+num2; - num2=num1-num2; - num1=num1-num2; - return std::make_tuple(num1,num2); -} - -// Usage -auto swapped=swap(3,4); // Returns a tuple (access the values using std::get<0>(swapped)/std::get<1>(swapped)) -``` \ No newline at end of file From bf49d9c496adf9f42df75e74c0341e784896112d Mon Sep 17 00:00:00 2001 From: Emosans Date: Sat, 4 Jan 2025 16:45:06 +0530 Subject: [PATCH 4/6] changes --- snippets/c/mathematical-functions/swap-numbers.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/snippets/c/mathematical-functions/swap-numbers.md b/snippets/c/mathematical-functions/swap-numbers.md index c07d39b0..a655a4cf 100644 --- a/snippets/c/mathematical-functions/swap-numbers.md +++ b/snippets/c/mathematical-functions/swap-numbers.md @@ -8,12 +8,12 @@ tags: swap,numbers ```c #include void swap(int* num1,int* num2){ - *num1= *num1 + *num2; - *num2= *num1 - *num2; - *num1= *num1 - *num2; + *num1 = *num1 + *num2; + *num2 = *num1 - *num2; + *num1 = *num1 - *num2; } // Usage: -int a=3,b=4; -auto swapped=swap(&a,&b); // simply use printf after this to print swapped values +int a = 3,b = 4; +swap(&a,&b); // simply use printf after this to print swapped values ``` \ No newline at end of file From 6381a89ca52eeece5dae4165d26bdc26a5921c88 Mon Sep 17 00:00:00 2001 From: Emosans Date: Sat, 4 Jan 2025 16:57:37 +0530 Subject: [PATCH 5/6] small typo --- public/consolidated/c.json | 11 +++++++++++ public/consolidated/cpp.json | 17 +++++++++++++++++ public/consolidated/javascript.json | 18 ++++++------------ 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/public/consolidated/c.json b/public/consolidated/c.json index a9a23234..1588cd69 100644 --- a/public/consolidated/c.json +++ b/public/consolidated/c.json @@ -28,6 +28,17 @@ ], "contributors": [], "code": "int factorial(int x) {\n int y = 1;\n\n for (int i = 2; i <= x; i++)\n y *= i;\n\n return y;\n}\n\n// Usage:\nfactorial(4); // Returns: 24\n" + }, + { + "title": "Swap numbers", + "description": "Swaps two numbers without using third variable", + "author": "Emosans", + "tags": [ + "swap", + "numbers" + ], + "contributors": [], + "code": "#include\nvoid swap(int* num1,int* num2){\n *num1 = *num1 + *num2;\n *num2 = *num1 - *num2;\n *num1 = *num1 - *num2;\n}\n\n// Usage:\nint a = 3,b = 4;\nswap(&a,&b); // simply use printf after this to print swapped values\n" } ] } diff --git a/public/consolidated/cpp.json b/public/consolidated/cpp.json index e560e82d..6427f04f 100644 --- a/public/consolidated/cpp.json +++ b/public/consolidated/cpp.json @@ -32,6 +32,23 @@ } ] }, + { + "categoryName": "Debuging", + "snippets": [ + { + "title": "Vector Print", + "description": "Overloads the << operator to print the contents of a vector just like in python.", + "author": "Mohamed-faaris", + "tags": [ + "printing", + "debuging", + "vector" + ], + "contributors": [], + "code": "#include \n#include \n\ntemplate \nstd::ostream& operator<<(std::ostream& os, const std::vector& vec) {\n os << \"[\"; \n for (size_t i = 0; i < vec.size(); ++i) {\n os << vec[i]; // Print each vector element\n if (i != vec.size() - 1) {\n os << \", \"; // Add separator\n }\n }\n os << \"]\"; \n return os; // Return the stream\n}\n\n// Usage:\nstd::vector numbers = {1, 2, 3, 4, 5};\nstd::cout << numbers << std::endl; // Outputs: [1, 2, 3, 4, 5]\n\n" + } + ] + }, { "categoryName": "Math And Numbers", "snippets": [ diff --git a/public/consolidated/javascript.json b/public/consolidated/javascript.json index 99588330..c7b78ff8 100644 --- a/public/consolidated/javascript.json +++ b/public/consolidated/javascript.json @@ -85,10 +85,8 @@ "description": "Converts RGB color values to hexadecimal color code.", "author": "jjcantu", "tags": [ - "javascript", "color", - "conversion", - "utility" + "conversion" ], "contributors": [], "code": "function rgbToHex(r, g, b) {\n const toHex = (n) => {\n const hex = n.toString(16);\n return hex.length === 1 ? '0' + hex : hex;\n };\n \n return '#' + toHex(r) + toHex(g) + toHex(b);\n}\n\n// Usage:\nconsole.log(rgbToHex(255, 128, 0)); // Output: \"#ff8000\"\nconsole.log(rgbToHex(0, 255, 0)); // Output: \"#00ff00\"\n" @@ -407,10 +405,8 @@ "description": "Converts bytes into human-readable file size format.", "author": "jjcantu", "tags": [ - "javascript", "format", - "size", - "utility" + "size" ], "contributors": [], "code": "function formatFileSize(bytes) {\n if (bytes === 0) return '0 Bytes';\n \n const k = 1024;\n const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];\n const i = Math.floor(Math.log(bytes) / Math.log(k));\n \n return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];\n}\n\n// Usage:\nconsole.log(formatFileSize(1234)); // Output: \"1.21 KB\"\nconsole.log(formatFileSize(1234567)); // Output: \"1.18 MB\"\n" @@ -506,13 +502,11 @@ "description": "Creates a deep copy of an object or array without reference.", "author": "jjcantu", "tags": [ - "javascript", "object", - "clone", - "utility" + "clone" ], "contributors": [], - "code": "function deepClone(obj) {\n if (obj === null || typeof obj !== 'object') return obj;\n \n const clone = Array.isArray(obj) ? [] : {};\n \n for (let key in obj) {\n if (Object.prototype.hasOwnProperty.call(obj, key)) {\n clone[key] = deepClone(obj[key]);\n }\n }\n \n return clone;\n}\n\n// Usage:\nconst original = { a: 1, b: { c: 2 }, d: [1, 2, 3] };\nconst cloned = deepClone(original);\nconsole.log(cloned); // Output: { a: 1, b: { c: 2 }, d: [1, 2, 3] }\n" + "code": "function deepClone(obj) {\n if (obj === null || typeof obj !== 'object') return obj;\n \n const clone = Array.isArray(obj) ? [] : {};\n \n for (let key in obj) {\n if (Object.prototype.hasOwnProperty.call(obj, key)) {\n clone[key] = deepClone(obj[key]);\n }\n }\n \n return clone;\n}\n\n// Usage:\nconst original = { a: 1, b: { c: 2 }, d: [1, 2, 3] };\nconst cloned = deepClone(original);\nconsole.log(cloned); // Output: 'original' but cloned\n" }, { "title": "Filter Object", @@ -758,9 +752,9 @@ "description": "Generates a UUID (v4) string.", "author": "jjcantu", "tags": [ - "javascript", "uuid", - "utility" + "generate", + "string" ], "contributors": [], "code": "function generateUUID() {\n return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {\n const r = Math.random() * 16 | 0;\n const v = c === 'x' ? r : (r & 0x3 | 0x8);\n return v.toString(16);\n });\n}\n\n// Usage:\nconsole.log(generateUUID()); // Output: \"a1b2c3d4-e5f6-4g7h-8i9j-k0l1m2n3o4p5\"\n" From e4b3b5f8c8568a36082495959190807fdd4828cc Mon Sep 17 00:00:00 2001 From: Emosans Date: Sat, 4 Jan 2025 21:45:42 +0530 Subject: [PATCH 6/6] changes --- snippets/c/mathematical-functions/swap-numbers.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/c/mathematical-functions/swap-numbers.md b/snippets/c/mathematical-functions/swap-numbers.md index a655a4cf..bbbc4a2b 100644 --- a/snippets/c/mathematical-functions/swap-numbers.md +++ b/snippets/c/mathematical-functions/swap-numbers.md @@ -15,5 +15,5 @@ void swap(int* num1,int* num2){ // Usage: int a = 3,b = 4; -swap(&a,&b); // simply use printf after this to print swapped values +swap(&a,&b); // swaps the values of the a and b variables ``` \ No newline at end of file