-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Calculating factorial & sorting alphabetically
- Loading branch information
1 parent
e4cc09e
commit 05e1f5a
Showing
2 changed files
with
79 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,46 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
|
||
# script by Ruchir Chawdhry | ||
# released under MIT License | ||
# github.com/RuchirChawdhry/Python | ||
# ruchirchawdhry.com | ||
# linkedin.com/in/RuchirChawdhry | ||
|
||
""" | ||
Write a program which can compute the factorial of a given number | ||
""" | ||
|
||
|
||
from math import factorial | ||
from operator import mul | ||
from functools import reduce | ||
from scipy.special import factorial as scipy_fact | ||
|
||
|
||
def math_factorial(num): | ||
return factorial(num) | ||
|
||
|
||
def fcktorial(num): | ||
fact = 1 | ||
for i in range(2, num + 1): | ||
fact *= i # same as fact = fact * i | ||
return fact | ||
|
||
|
||
def mul_factorial(num): | ||
return reduce(mul, range(1, num + 1)) | ||
|
||
|
||
def scipy_factorial(num): | ||
return scipy_fact(num) | ||
|
||
|
||
def lambda_factorial(num): | ||
return reduce((lambda x, y: x * y), range(1, num + 1)) | ||
|
||
|
||
if __name__ == "__main__": | ||
ask = input("NUMBER: ") | ||
print(f"Factorial of {ask} is {math_factorial(int(ask))}") |
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 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
|
||
# script by Ruchir Chawdhry | ||
# released under MIT License | ||
# github.com/RuchirChawdhry/Python | ||
# ruchirchawdhry.com | ||
# linkedin.com/in/RuchirChawdhry | ||
|
||
""" | ||
Write a program that accepts a comma seperated sequence of words as input | ||
and prints the words in a comma-separated sequence after sorting them | ||
alphabetically. | ||
""" | ||
|
||
import re | ||
|
||
|
||
def to_sort_simple(sequence): | ||
return sorted(sequence.split(",")) | ||
|
||
|
||
def to_sort(sequence): | ||
s = re.sub(r"\s|\t", "", sequence).split(",") | ||
# strips tabs and newlines and splits by comma | ||
s.sort(key=lambda x: re.sub(r"\d", "", x).lower()) | ||
# ignores any digits in the each string in the list | ||
return ", ".join(s) | ||
|
||
|
||
if __name__ == "__main__": | ||
ask = input("ENTER COMMA-SEPERATED SEQUENCE: ") | ||
print(f"[RESULT] Your sequence, alphabetically sorted: {to_sort(ask)}") |