-
-
Notifications
You must be signed in to change notification settings - Fork 130
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
Closed
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b5dbb65
Adding calculate factorial.md
SamratBarai b2037d6
Adding calculate factorial.md
SamratBarai 7e4a112
Adding calculate factorial.md
SamratBarai 75ecb1b
Added error handling for invalid inputs
SamratBarai 3692593
Remove testing file
SamratBarai c077be5
Remove language name from tags.md
SamratBarai 58a44ba
Remove language name from tags
SamratBarai c262cad
Remove unneccesary tags
SamratBarai 2532409
Remove /public/consolidated
SamratBarai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Oops, something went wrong.
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.