Skip to content

Commit

Permalink
Calculating factorial & sorting alphabetically
Browse files Browse the repository at this point in the history
  • Loading branch information
RuchirChawdhry committed Feb 16, 2020
1 parent e4cc09e commit 05e1f5a
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
46 changes: 46 additions & 0 deletions factorial.py
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))}")
33 changes: 33 additions & 0 deletions sort_alphabetically.py
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)}")

0 comments on commit 05e1f5a

Please sign in to comment.