diff --git a/.github/workflows/consolidate-snippets.yml b/.github/workflows/consolidate-snippets.yml index a252a721..7dad2cbe 100644 --- a/.github/workflows/consolidate-snippets.yml +++ b/.github/workflows/consolidate-snippets.yml @@ -29,11 +29,19 @@ jobs: run: | node utils/consolidateSnippets.js # Run the script located in the utils/ folder - - name: Commit and push changes - run: | - git config --global user.name "GitHub Action" - git config --global user.email "action@github.com" - git add public/consolidated/* - git add public/icons/* - git diff-index --quiet HEAD || git commit -m "Update consolidated snippets" - git push + - name: Commit and push changes + run: | + # Configure git + git config --global user.name "GitHub Action" + git config --global user.email "action@github.com" + + # If there are changes in public/consolidated or public/icons, + # commit and push them. Otherwise, skip. + if ! git diff-index --quiet HEAD -- public/consolidated public/icons; then + echo "Changes detected. Committing and pushing." + git add public/consolidated/* public/icons/* + git commit -m "Update consolidated snippets" + git push + else + echo "No changes to commit. Skipping." + fi diff --git a/public/consolidated/c.json b/public/consolidated/c.json index a9258611..80937cba 100644 --- a/public/consolidated/c.json +++ b/public/consolidated/c.json @@ -29,6 +29,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": "Fibonacci Series", + "description": "Iterative approach to generate first N Fibonacci numbers in C.", + "author": "Karanjot786", + "tags": [ + "fibonacci", + "math" + ], + "contributors": [], + "code": "#include \n\nvoid fibonacci(int n) {\n int a = 0, b = 1, c, i;\n printf(\"%d \", a);\n if(n > 1) {\n printf(\"%d \", b);\n }\n for(i = 2; i < n; i++) {\n c = a + b;\n printf(\"%d \", c);\n a = b;\n b = c;\n }\n printf(\"\\n\");\n}\n\n// Usage:\nint main() {\n fibonacci(5); // Output: 0 1 1 2 3\n return 0;\n}\n" + }, { "title": "Swap numbers", "description": "Swaps two numbers without using third variable", diff --git a/public/consolidated/cpp.json b/public/consolidated/cpp.json index 6427f04f..ccc8f417 100644 --- a/public/consolidated/cpp.json +++ b/public/consolidated/cpp.json @@ -33,7 +33,7 @@ ] }, { - "categoryName": "Debuging", + "categoryName": "Debugging", "snippets": [ { "title": "Vector Print", @@ -62,6 +62,17 @@ ], "contributors": [], "code": "bool is_prime(int n) {\n if (n < 2) return false;\n if (n == 2 || n == 3) return true;\n if (n % 2 == 0) return false;\n for (int i = 3; i * i <= n; i += 2) {\n if (n % i == 0) return false;\n }\n return true;\n}\n\n// Usage:\nis_prime(29); // Returns: true\n" + }, + { + "title": "Fibonacci Series", + "description": "Iterative approach to generate first N Fibonacci numbers in C++.", + "author": "Karanjot786", + "tags": [ + "fibonacci", + "math" + ], + "contributors": [], + "code": "#include \n\nvoid fibonacci(int n) {\n int a = 0, b = 1;\n std::cout << a << \" \";\n if(n > 1) {\n std::cout << b << \" \";\n }\n for(int i = 2; i < n; i++) {\n int c = a + b;\n std::cout << c << \" \";\n a = b;\n b = c;\n }\n std::cout << std::endl;\n}\n\n// Usage:\nint main() {\n fibonacci(5); // Output: 0 1 1 2 3\n return 0;\n}\n" } ] }, diff --git a/public/consolidated/csharp.json b/public/consolidated/csharp.json index 63ff0ec1..b1e8e23f 100644 --- a/public/consolidated/csharp.json +++ b/public/consolidated/csharp.json @@ -85,6 +85,22 @@ } ] }, + { + "categoryName": "Math And Numbers", + "snippets": [ + { + "title": "Fibonacci Series", + "description": "Iterative approach to generate first N Fibonacci numbers in C#.", + "author": "Karanjot786", + "tags": [ + "fibonacci", + "math" + ], + "contributors": [], + "code": "using System;\n\npublic class FibonacciSeries {\n public static void fibonacci(int n) {\n int a = 0, b = 1;\n Console.Write(a + \" \");\n if(n > 1) {\n Console.Write(b + \" \");\n }\n\n for(int i = 2; i < n; i++) {\n int c = a + b;\n Console.Write(c + \" \");\n a = b;\n b = c;\n }\n Console.WriteLine();\n }\n\n // Usage:\n public static void Main() {\n fibonacci(5); // Output: 0 1 1 2 3\n }\n}\n" + } + ] + }, { "categoryName": "String Utilities", "snippets": [ diff --git a/public/consolidated/java.json b/public/consolidated/java.json index f00ab589..74e71b9e 100644 --- a/public/consolidated/java.json +++ b/public/consolidated/java.json @@ -15,5 +15,21 @@ "code": "// This is the main class of the Java program\npublic class Main {\n // The main method is the entry point of the program\n public static void main(String args[]) {\n // This statement prints \"Hello, World!\" to the console\n System.out.println(\"Hello, World!\");\n }\n}\n\n" } ] + }, + { + "categoryName": "Math And Numbers", + "snippets": [ + { + "title": "Fibonacci Series", + "description": "Iterative approach to generate first N Fibonacci numbers in Java.", + "author": "Karanjot786", + "tags": [ + "fibonacci", + "math" + ], + "contributors": [], + "code": "public class Fibonacci {\n public static void fibonacci(int n) {\n int a = 0, b = 1;\n System.out.print(a + \" \");\n if(n > 1) {\n System.out.print(b + \" \");\n }\n\n for(int i = 2; i < n; i++) {\n int c = a + b;\n System.out.print(c + \" \");\n a = b;\n b = c;\n }\n System.out.println();\n }\n\n // Usage:\n public static void main(String[] args) {\n fibonacci(5); // Output: 0 1 1 2 3\n }\n}\n" + } + ] } ] \ No newline at end of file diff --git a/public/consolidated/javascript.json b/public/consolidated/javascript.json index 2e8c8f97..0a4c4bf2 100644 --- a/public/consolidated/javascript.json +++ b/public/consolidated/javascript.json @@ -367,6 +367,17 @@ { "categoryName": "Mathematical Functions", "snippets": [ + { + "title": "Fibonacci Series", + "description": "Iterative approach to generate first N Fibonacci numbers in JavaScript.", + "author": "Karanjot786", + "tags": [ + "fibonacci", + "math" + ], + "contributors": [], + "code": "function fibonacci(n) {\n let a = 0, b = 1;\n let result = a + ' ';\n if (n > 1) {\n result += b + ' ';\n }\n\n for (let i = 2; i < n; i++) {\n let c = a + b;\n result += c + ' ';\n a = b;\n b = c;\n }\n console.log(result.trim());\n}\n\n// Usage:\nfibonacci(5); // Output: 0 1 1 2 3\n" + }, { "title": "Greatest Common Divisor", "description": "Calculates the largest positive integer that divides each of the integers without leaving a remainder. Useful for calculating aspect ratios.", diff --git a/public/consolidated/python.json b/public/consolidated/python.json index 953272b8..4bcc3481 100644 --- a/public/consolidated/python.json +++ b/public/consolidated/python.json @@ -405,6 +405,17 @@ "contributors": [], "code": "def bytes_to_human_readable(num):\n for unit in ['B', 'KB', 'MB', 'GB', 'TB', 'PB']:\n if num < 1024:\n return f\"{num:.2f} {unit}\"\n num /= 1024\n\n# Usage:\nbytes_to_human_readable(123456789) # Returns: '117.74 MB'\n" }, + { + "title": "Fibonacci Series", + "description": "Iterative approach to generate first N Fibonacci numbers in Python.", + "author": "Karanjot786", + "tags": [ + "fibonacci", + "math" + ], + "contributors": [], + "code": "def fibonacci(n):\n a, b = 0, 1\n print(a, end=\" \")\n if n > 1:\n print(b, end=\" \")\n for _ in range(2, n):\n c = a + b\n print(c, end=\" \")\n a, b = b, c\n print()\n\n# Usage:\nfibonacci(5) # Output: 0 1 1 2 3\n" + }, { "title": "Find LCM (Least Common Multiple)", "description": "Calculates the least common multiple (LCM) of two numbers.", diff --git a/public/consolidated/rust.json b/public/consolidated/rust.json index 4a150d77..7dbda81a 100644 --- a/public/consolidated/rust.json +++ b/public/consolidated/rust.json @@ -42,6 +42,22 @@ } ] }, + { + "categoryName": "Math And Numbers", + "snippets": [ + { + "title": "Fibonacci Series", + "description": "Iterative approach to generate first N Fibonacci numbers in Rust.", + "author": "Karanjot786", + "tags": [ + "fibonacci", + "math" + ], + "contributors": [], + "code": "fn fibonacci(n: usize) {\n let (mut a, mut b) = (0, 1);\n \n print!(\"{} \", a);\n if n > 1 {\n print!(\"{} \", b);\n }\n\n for _ in 2..n {\n let c = a + b;\n print!(\"{} \", c);\n a = b;\n b = c;\n }\n println!();\n}\n\n// Usage:\nfn main() {\n fibonacci(5); // Output: 0 1 1 2 3\n}\n" + } + ] + }, { "categoryName": "String Manipulation", "snippets": [ diff --git a/snippets/c/mathematical-functions/fibonacci-series.md b/snippets/c/mathematical-functions/fibonacci-series.md new file mode 100644 index 00000000..54e79824 --- /dev/null +++ b/snippets/c/mathematical-functions/fibonacci-series.md @@ -0,0 +1,31 @@ +--- +title: Fibonacci Series +description: Iterative approach to generate first N Fibonacci numbers in C. +author: Karanjot786 +tags: fibonacci, math +--- + +```c +#include + +void fibonacci(int n) { + int a = 0, b = 1, c, i; + printf("%d ", a); + if(n > 1) { + printf("%d ", b); + } + for(i = 2; i < n; i++) { + c = a + b; + printf("%d ", c); + a = b; + b = c; + } + printf("\n"); +} + +// Usage: +int main() { + fibonacci(5); // Output: 0 1 1 2 3 + return 0; +} +``` \ No newline at end of file diff --git a/snippets/cpp/math-and-numbers/fibonacci-series.md b/snippets/cpp/math-and-numbers/fibonacci-series.md new file mode 100644 index 00000000..535f81b9 --- /dev/null +++ b/snippets/cpp/math-and-numbers/fibonacci-series.md @@ -0,0 +1,31 @@ +--- +title: Fibonacci Series +description: Iterative approach to generate first N Fibonacci numbers in C++. +author: Karanjot786 +tags: fibonacci, math +--- + +```cpp +#include + +void fibonacci(int n) { + int a = 0, b = 1; + std::cout << a << " "; + if(n > 1) { + std::cout << b << " "; + } + for(int i = 2; i < n; i++) { + int c = a + b; + std::cout << c << " "; + a = b; + b = c; + } + std::cout << std::endl; +} + +// Usage: +int main() { + fibonacci(5); // Output: 0 1 1 2 3 + return 0; +} +``` \ No newline at end of file diff --git a/snippets/csharp/math-and-numbers/fibonacci-series.md b/snippets/csharp/math-and-numbers/fibonacci-series.md new file mode 100644 index 00000000..7f77946e --- /dev/null +++ b/snippets/csharp/math-and-numbers/fibonacci-series.md @@ -0,0 +1,33 @@ +--- +title: Fibonacci Series +description: Iterative approach to generate first N Fibonacci numbers in C#. +author: Karanjot786 +tags: fibonacci, math +--- + +```cs +using System; + +public class FibonacciSeries { + public static void fibonacci(int n) { + int a = 0, b = 1; + Console.Write(a + " "); + if(n > 1) { + Console.Write(b + " "); + } + + for(int i = 2; i < n; i++) { + int c = a + b; + Console.Write(c + " "); + a = b; + b = c; + } + Console.WriteLine(); + } + + // Usage: + public static void Main() { + fibonacci(5); // Output: 0 1 1 2 3 + } +} +``` \ No newline at end of file diff --git a/snippets/java/math-and-numbers/fibonacci-series.md b/snippets/java/math-and-numbers/fibonacci-series.md new file mode 100644 index 00000000..0b920da0 --- /dev/null +++ b/snippets/java/math-and-numbers/fibonacci-series.md @@ -0,0 +1,31 @@ +--- +title: Fibonacci Series +description: Iterative approach to generate first N Fibonacci numbers in Java. +author: Karanjot786 +tags: fibonacci, math +--- + +```java +public class Fibonacci { + public static void fibonacci(int n) { + int a = 0, b = 1; + System.out.print(a + " "); + if(n > 1) { + System.out.print(b + " "); + } + + for(int i = 2; i < n; i++) { + int c = a + b; + System.out.print(c + " "); + a = b; + b = c; + } + System.out.println(); + } + + // Usage: + public static void main(String[] args) { + fibonacci(5); // Output: 0 1 1 2 3 + } +} +``` \ No newline at end of file diff --git a/snippets/javascript/mathematical-functions/fibonacci-series.md b/snippets/javascript/mathematical-functions/fibonacci-series.md new file mode 100644 index 00000000..67d96061 --- /dev/null +++ b/snippets/javascript/mathematical-functions/fibonacci-series.md @@ -0,0 +1,27 @@ +--- +title: Fibonacci Series +description: Iterative approach to generate first N Fibonacci numbers in JavaScript. +author: Karanjot786 +tags: fibonacci, math +--- + +```js +function fibonacci(n) { + let a = 0, b = 1; + let result = a + ' '; + if (n > 1) { + result += b + ' '; + } + + for (let i = 2; i < n; i++) { + let c = a + b; + result += c + ' '; + a = b; + b = c; + } + console.log(result.trim()); +} + +// Usage: +fibonacci(5); // Output: 0 1 1 2 3 +``` \ No newline at end of file diff --git a/snippets/python/math-and-numbers/fibonacci-series.md b/snippets/python/math-and-numbers/fibonacci-series.md new file mode 100644 index 00000000..9f6597b8 --- /dev/null +++ b/snippets/python/math-and-numbers/fibonacci-series.md @@ -0,0 +1,22 @@ +--- +title: Fibonacci Series +description: Iterative approach to generate first N Fibonacci numbers in Python. +author: Karanjot786 +tags: fibonacci, math +--- + +```py +def fibonacci(n): + a, b = 0, 1 + print(a, end=" ") + if n > 1: + print(b, end=" ") + for _ in range(2, n): + c = a + b + print(c, end=" ") + a, b = b, c + print() + +# Usage: +fibonacci(5) # Output: 0 1 1 2 3 +``` \ No newline at end of file diff --git a/snippets/rust/math-and-numbers/fibonacci-series.md b/snippets/rust/math-and-numbers/fibonacci-series.md new file mode 100644 index 00000000..d3293714 --- /dev/null +++ b/snippets/rust/math-and-numbers/fibonacci-series.md @@ -0,0 +1,30 @@ +--- +title: Fibonacci Series +description: Iterative approach to generate first N Fibonacci numbers in Rust. +author: Karanjot786 +tags: fibonacci, math +--- + +```rust +fn fibonacci(n: usize) { + let (mut a, mut b) = (0, 1); + + print!("{} ", a); + if n > 1 { + print!("{} ", b); + } + + for _ in 2..n { + let c = a + b; + print!("{} ", c); + a = b; + b = c; + } + println!(); +} + +// Usage: +fn main() { + fibonacci(5); // Output: 0 1 1 2 3 +} +``` \ No newline at end of file