diff --git a/python/math-and-stats/01.FindKthPermutation.py b/python/math-and-stats/01.FindKthPermutation.py new file mode 100644 index 0000000..840b818 --- /dev/null +++ b/python/math-and-stats/01.FindKthPermutation.py @@ -0,0 +1,35 @@ +import math +import time + +def get_first(block_size,nums,k): + if nums is []: + return k, None + digit_num = 0 + while k > block_size: + digit_num+=1 + k-=block_size + return k,digit_num + +def get_permutation(v, k): + nums = list(range(1,v+1)) + digit = [] + while len(nums) > 0: + block_size = math.factorial(len(nums)-1) + k, d = get_first(block_size,nums,k) + digit += str(nums[d]) + del nums[d] + return ''.join(digit) + + +start = time.time() +print(get_permutation(2,2)) +print(get_permutation(3,3)) +print(get_permutation(4,4)) +print(get_permutation(4,8)) +print(get_permutation(10,1000000)) +print(get_permutation(11,10000)) +print(get_permutation(15,1000000)) +print(get_permutation(10,1000012)) +end = time.time() +print('-'*20) +print(f"Iteration: \tTime taken: {(end-start)*10**3:.03f}ms") \ No newline at end of file diff --git a/python/math-and-stats/02.integerDivide.py b/python/math-and-stats/02.integerDivide.py new file mode 100644 index 0000000..a6cfa65 --- /dev/null +++ b/python/math-and-stats/02.integerDivide.py @@ -0,0 +1,30 @@ +def divide_integer(dividend, divisor): + if divisor == 0: + return -1 + if dividend < divisor: + return 0 + if dividend == divisor: + return 1 + q = 1 + val = divisor + while val < (dividend>>1): + val <<= 1 + q <<= 1 + return q + divide_integer(dividend-val, divisor) + + +def main(): + print("1. 7/2 =", divide_integer(7, 2)) + print("-----------------------------------------------------------------------------------------------------\n") + print("2. 5/4 =", divide_integer(5, 4)) + print("-----------------------------------------------------------------------------------------------------\n") + print("3. 1/3 =", divide_integer(1, 3)) + print("-----------------------------------------------------------------------------------------------------\n") + print("4. 40/5 =", divide_integer(40, 5)) + print("-----------------------------------------------------------------------------------------------------\n") + print("5. 40/4 =", divide_integer(40, 4)) + print("-----------------------------------------------------------------------------------------------------\n") + + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/python/math-and-stats/03.PythagoreanTriples.py b/python/math-and-stats/03.PythagoreanTriples.py new file mode 100644 index 0000000..77bfc0c --- /dev/null +++ b/python/math-and-stats/03.PythagoreanTriples.py @@ -0,0 +1,50 @@ +def pythagorean_sum(a, b, c): + if a>b and a>c: + c, a = a, c + elif b>a and b>c: + c, b = b, c + return a**2 + b**2 - c**2 + + + +def find_pythagorean_triples(arr): + triples = [] + arr.sort() + n = len(arr) + for i in range(0,n): + if arr[i] == 0: + continue + c = arr[i]**2 + j = 0 + k = n-1 + while j c: + k -= 1 + else: + j += 1 + + + return triples + + +def main(): + pythagorean_list = [[4,16,1,2,3,5,6,8,25,10], [3,4,5,10,12,13,14,14,15],[2, 4, 6, 8, 10, 12, 14, 16, 18, 20],[1, 2, 3, 44, 5],[3, -1, 2, 5, 4]] + for i in range(len(pythagorean_list)): + result = find_pythagorean_triples(pythagorean_list[i]) + print("1. Original: ", pythagorean_list[i]) + print(" Pythagorean triples: ", result) + print("-"*100) + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/python/math-and-stats/04.AllPossibleCombinationsForAGivenSum.py b/python/math-and-stats/04.AllPossibleCombinationsForAGivenSum.py new file mode 100644 index 0000000..22520b9 --- /dev/null +++ b/python/math-and-stats/04.AllPossibleCombinationsForAGivenSum.py @@ -0,0 +1,39 @@ +import copy + +def print_all_sum_rec(target, current_sum, start, output, result): + if current_sum is target: + output.append(copy.copy(result)) + + for i in range(start,target): + temp_sum = current_sum + i + + if temp_sum <= target: + result.append(i) + print_all_sum_rec(target, temp_sum, i, output, result) + result.pop() + else: + return +def print_all_sum(target): + output = [] + result = [] + print_all_sum_rec(target, 0, 1, output, result) + return output + +def main(): + result = print_all_sum(2) + print("1. All sum combinations of" + " 2: " + str(result)) + print("\n-----------------------------------------------------------------------------------------------------\n") + result1 = print_all_sum(3) + print("2. All sum combinations of" + " 3: " + str(result1)) + print("\n-----------------------------------------------------------------------------------------------------\n") + result2 = print_all_sum(4) + print("3. All sum combinations of" + " 4: " + str(result2)) + print("\n-----------------------------------------------------------------------------------------------------\n") + result3 = print_all_sum(7) + print("3. All sum combinations of" + " 7: " + str(result3)) + print("\n-----------------------------------------------------------------------------------------------------\n") + + + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/python/math-and-stats/05.FindMissingNumber.py b/python/math-and-stats/05.FindMissingNumber.py new file mode 100644 index 0000000..4a87680 --- /dev/null +++ b/python/math-and-stats/05.FindMissingNumber.py @@ -0,0 +1,18 @@ +def find_missing(arr): + n = len(arr) + s = n*(n+1)//2 + for i in arr: + s -= i + return s + +def main(): + v_list = [[0], [3,7,1,2,0,4,5], [9,6,4,2,3,5,7,0,1]] + + for i in range(len(v_list)): + print("1. Original:", v_list[i]) + missing_number = find_missing(v_list[i]) + print(" The missing number is: " + str(missing_number)) + print("-" * 100) + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/python/math-and-stats/06.isValidNumber.py b/python/math-and-stats/06.isValidNumber.py new file mode 100644 index 0000000..07e3da7 --- /dev/null +++ b/python/math-and-stats/06.isValidNumber.py @@ -0,0 +1,36 @@ +def is_number_valid(s): + dot = 0 + for i in range(len(s)): + if s[i] == '.': + dot += 1 + if dot>1: + return False + elif s[i] == '-': + if i==0: + continue + else: + return False + elif s[i]<'0' or s[i]>'9': + return False + return True + + +def main(): + print("Is the number valid 4.325? ", + "Yes" if is_number_valid("4.325") else "No") + print("Is the number valid 1.1.1? ", + "Yes" if is_number_valid("1.1.1") else "No") + print("Is the number valid 222? ", + "Yes" if is_number_valid("222") else "No") + print("Is the number valid 22.? ", + "Yes" if is_number_valid("22.") else "No") + print("Is the number valid 0.1? ", + "Yes" if is_number_valid("0.1") else "No") + print("Is the number valid 22.22.? ", + "Yes" if is_number_valid("22.22.") else "No") + print("Is the number valid 1.? ", + "Yes" if is_number_valid("1.") else "No") + + +if __name__ == '__main__': + main() diff --git a/python/math-and-stats/07.PowerOfANumber.py b/python/math-and-stats/07.PowerOfANumber.py new file mode 100644 index 0000000..21af73c --- /dev/null +++ b/python/math-and-stats/07.PowerOfANumber.py @@ -0,0 +1,23 @@ +def pos_power(x,n): + if n == 0: + return 1 + if n == 1: + return x + if n % 2 == 0: + return power(x*x,n//2) + return x * power(x*x,n//2) + +def power(x, n): + if n<0: + return 1/pos_power(x,-n) + return pos_power(x,n) + +def main(): + print("1. Power(0, 0) = %.3f" %power(0, 0)) + print("2. Power(2, 5) = %.3f" %power(2, 5)) + print("3. Power(3, 4) = %.3f" %power(3, 4)) + print("4. Power(1.5, 3) = %.3f" %power(1.5, 3)) + print("5. Power(2, -2) = %.3f" %power(2, -2)) + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/python/math-and-stats/08.SquareRoot.py b/python/math-and-stats/08.SquareRoot.py new file mode 100644 index 0000000..c73a1ae --- /dev/null +++ b/python/math-and-stats/08.SquareRoot.py @@ -0,0 +1,32 @@ +EP = 0.00001 + +def square_root(n): + is_negative = False + if n < 0: + is_negative = True + n = -n + low = 0 + high = n + guess = (low + high) / 2 + while abs(guess * guess - n) >= EP: + if guess * guess > n: + high = guess + else: + low = guess + guess = (low + high) / 2 + if is_negative: + return str(guess)+"i" + return guess + +def main(): + arr = [-16, 16, 17, 2.25] + for i in range(len(arr)): + ans = square_root(arr[i]) + ans_str = str(ans) + print(str(i + 1) + ". Square root of " + + str(arr[i]) + ": " + str(ans_str)) + print("-"*100) + + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/python/math-and-stats/09.IsRectangleOverlap.py b/python/math-and-stats/09.IsRectangleOverlap.py new file mode 100644 index 0000000..5c5ed78 --- /dev/null +++ b/python/math-and-stats/09.IsRectangleOverlap.py @@ -0,0 +1,20 @@ +def is_rectangle_overlap(rec1, rec2): + + x_rec1 = rec1[0]