Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Added Python factorial calculating snippet #214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
SamratBarai wants to merge 9 commits into quicksnip-dev:main from SamratBarai:main
Closed
Show file tree
Hide file tree
Changes from 8 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
3 changes: 2 additions & 1 deletion package-lock.json
View file Open in desktop

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"prettier": "^3.4.2",
"typescript": "^5.5.3",
"typescript-eslint": "^8.7.0",
"vite": "^5.4.8",
"vite": "^5.4.11",
"vite-tsconfig-paths": "^5.1.4"
}
}
60 changes: 60 additions & 0 deletions public/consolidated/java.json
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,65 @@
"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"
}
]
},
{
"name": "Date Time",
"snippets": [
{
"title": "Date time formatting american",
"description": "Formats a timestamp to a human-readable date-time string in the format \"MM/dd/yyyy hh:mm:ss a\"",
"author": "Mcbencrafter",
"tags": [
"date",
"time",
"date-time",
"formatting",
"american"
],
"contributors": [],
"code": "import java.time.Instant;\nimport java.time.ZoneId;\nimport java.time.format.DateTimeFormatter;\nimport java.util.concurrent.TimeUnit;\n\n// using the system default time zone\npublic static String formatDateTimeAmerican(long time, TimeUnit timeUnit) {\n return formatDateTimeAmerican(time, timeUnit, ZoneId.systemDefault());\n}\n\npublic static String formatDateTimeAmerican(long time, TimeUnit timeUnit, ZoneId timeZone) {\n return DateTimeFormatter.ofPattern(\"MM/dd/yyyy hh:mm:ss a\")\n .withZone(\n timeZone != null ? timeZone : ZoneId.systemDefault()\n )\n .format(Instant.ofEpochSecond(\n timeUnit.toSeconds(time)\n ));\n}\n\n// Usage:\nSystem.out.println(formatDateTimeAmerican(1735689599, TimeUnit.SECONDS)); // \"12/31/2024 | 11:59:59 PM\" for GMT+0000\nSystem.out.println(formatDateTimeAmerican(1735689599, TimeUnit.SECONDS, ZoneId.of(\"GMT+0000\"))); // \"12/31/2024 | 11:59:59 PM\"\n"
},
{
"title": "Date time formatting european",
"description": "Formats a timestamp to a human-readable date-time string in the format \"dd.MM.yyyy HH:mm:ss\"",
"author": "Mcbencrafter",
"tags": [
"date",
"time",
"date-time",
"formatting",
"european"
],
"contributors": [],
"code": "import java.time.Instant;\nimport java.time.ZoneId;\nimport java.time.format.DateTimeFormatter;\nimport java.util.concurrent.TimeUnit;\n\n// using the system default time zone\npublic static String formatDateTimeEuropean(long time, TimeUnit timeUnit) {\n return formatDateTimeEuropean(time, timeUnit, ZoneId.systemDefault());\n}\n\npublic static String formatDateTimeEuropean(long time, TimeUnit timeUnit, ZoneId timeZone) {\n return DateTimeFormatter.ofPattern(\"dd.MM.yyyy HH:mm:ss\")\n .withZone(\n timeZone != null ? timeZone : ZoneId.systemDefault()\n )\n .format(Instant.ofEpochSecond(\n timeUnit.toSeconds(time)\n ));\n}\n\n// Usage:\nSystem.out.println(formatDateTimeEuropean(1735689599, TimeUnit.SECONDS)); // \"31.12.2024 | 23:59:59\" for GMT+0000\nSystem.out.println(formatDateTimeEuropean(1735689599, TimeUnit.SECONDS, ZoneId.of(\"GMT+0000\"))); // \"31.12.2024 | 23:59:59\"\n"
},
{
"title": "Duration formatting hours minutes seconds",
"description": "Converts a given time duration to a human-readable string in the format \"hh:mm(:ss)\"",
"author": "Mcbencrafter",
"tags": [
"time",
"formatting",
"hours",
"minutes",
"seconds"
],
"contributors": [],
"code": "import java.util.concurrent.TimeUnit;\n \npublic static String formatDurationToHoursMinutesAndSeconds(int time, TimeUnit timeUnit, boolean showSeconds) {\n long totalSeconds = timeUnit.toSeconds(time);\n\n if (totalSeconds < 0)\n throw new IllegalArgumentException(\"Duration must be a non-negative value.\");\n\n // These variables can be directly used in the return statement,\n // but are kept as separate variables here for better readability.\n long hours = totalSeconds / 3600;\n long minutes = (totalSeconds % 3600) / 60;\n long seconds = totalSeconds % 60;\n\n if (showSeconds) {\n return String.format(\"%02d:%02d:%02d\", hours, minutes, seconds);\n } else {\n return String.format(\"%02d:%02d\", hours, minutes);\n }\n}\n\n// Usage:\nSystem.out.println(formatDurationToHoursMinutesAndSeconds(3810, TimeUnit.SECONDS, true)); // \"01:03:30\"\nSystem.out.println(formatDurationToHoursMinutesAndSeconds(3810, TimeUnit.SECONDS, false)); // \"01:03\"\n"
},
{
"title": "Duration formatting minutes seconds",
"description": "Converts a given time duration to a human-readable string in the format \"mm:ss\"",
"author": "Mcbencrafter",
"tags": [
"time",
"formatting",
"minutes",
"seconds"
],
"contributors": [],
"code": "import java.util.concurrent.TimeUnit;\n\npublic static String formatDurationToMinutesAndSeconds(int time, TimeUnit timeUnit) {\n long totalSeconds = timeUnit.toSeconds(time);\n\n if (totalSeconds < 0)\n throw new IllegalArgumentException(\"Duration must be a non-negative value.\");\n\n // These variables can be directly used in the return statement,\n // but are kept here as separate variables for better readability.\n long minutes = totalSeconds / 60;\n long seconds = totalSeconds % 60;\n\n return String.format(\"%02d:%02d\", minutes, seconds);\n}\n\n// Usage:\nSystem.out.println(formatDurationToMinutesAndSeconds(120, TimeUnit.SECONDS)); // \"02:00\"\nSystem.out.println(formatDurationToMinutesAndSeconds(75, TimeUnit.SECONDS)); // \"01:15\"\n"
}
]
}
]
13 changes: 12 additions & 1 deletion public/consolidated/python.json
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -349,14 +349,25 @@
"description": "Calculates compound interest for a given principal amount, rate, and time period.",
"author": "axorax",
"tags": [
"python",
"math",
"compound interest",
"finance"
],
"contributors": [],
"code": "def compound_interest(principal, rate, time, n=1):\n return principal * (1 + rate / n) ** (n * time)\n\n# Usage:\ncompound_interest(1000, 0.05, 5) # Returns: 1276.2815625000003\ncompound_interest(1000, 0.05, 5, 12) # Returns: 1283.68\n"
},
{
"title": "Calculate Factiorial of a number",
"description": "Calculates factorial of a given number using recursive function",
"author": "Samrat",
"tags": [
"math",
"factorial",
"recursive-function"
],
"contributors": [],
"code": "def factorial(n):\n if n == 0 or n == 1: return 1\n else: return n * factorial(n-1)\n\n# Usage:\nprint(factorial(5)) # Returns 24\nprint(factorial(10)) # Returns 3628800\n"
},
{
"title": "Check Perfect Square",
"description": "Checks if a number is a perfect square.",
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Calculate Compound Interest
description: Calculates compound interest for a given principal amount, rate, and time period.
author: axorax
tags: python,math,compound interest,finance
tags: math,compound interest,finance
---

```py
Expand Down
18 changes: 18 additions & 0 deletions snippets/python/math-and-numbers/calculate-factiorial.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
title: Calculate Factiorial of a number
description: Calculates factorial of a given number using recursive function
author: SamratBarai
tags: math,factorial,recursive-function
---

```py
def factorial(n):
if type(n) != int or n < 0: raise TypeError("Invalid type of input: '" + str(n) + "'") # Raises an error for invalid input
if n == 0 or n == 1: return 1 # Returns 1 if n is 0 or 1
else: return n * factorial(n-1) # Recall the factorial function

# Usage:
print(factorial(4)) # Returns 24
print(factorial(-4)) # Returns type error for invalid inputs
Copy link
Collaborator

@Mathys-Gasnier Mathys-Gasnier Jan 8, 2025

Choose a reason for hiding this comment

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

IMO I don't think it's needed to showcase the errors

print(factorial("q")) # Returns type error for invalid inputs
```

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /