Created
April 28, 2018 22:58
-
-
Save SharanSMenon/47ff334c0358d36e00314913c9850b83 to your computer and use it in GitHub Desktop.
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
from numbers import * | |
from strings import * | |
def print_instructions(): | |
print("Welcome") | |
print("0 - Print instructions") | |
print("1 - Choose a number function") | |
print("2 - Choose a string function") | |
print("3 - Exit the program") | |
def print_number_instructions(): | |
print("These are instructions for the number functions") | |
print("0 - Print Instructions") | |
print("1 - Find Gamma using law of cosines") | |
print("2 - Find length of side c using law of cosines") | |
print("3 - Find nth fibonacci number") | |
print("4 - Check is number is even") | |
print("5 - Use Pythagorean theorem to find hypotenuse") | |
def number_handler(): | |
print_number_instructions() | |
choice = int(input("Choose an option: ")) | |
if choice == 0: | |
print_number_instructions() | |
elif choice == 1: | |
a = float(input("Enter a: ")) | |
b = float(input("Enter b: ")) | |
c = float(input("Enter c: ")) | |
print("Gamma: " + str(law_of_cosines_gamma(a, b, c))) | |
elif choice == 2: | |
a = float(input("Enter a: ")) | |
b = float(input("Enter b: ")) | |
gamma = float(input("Enter gamma: ")) | |
print("Gamma: " + str(law_of_cosines_normal(a, b, gamma))) | |
elif choice == 3: | |
n = int(input("Enter n: ")) | |
print("nth fibonacci number: " + str(fib(n))) | |
elif choice == 4: | |
e = int(input("Enter your number: ")) | |
print("Answer: " + str(is_even(e))) | |
elif choice == 5: | |
a = float(input("Enter a: ")) | |
b = float(input("Enter b: ")) | |
print("Answer: " + str(pythagorean_theorem(a, b))) | |
else: | |
print("Not a valid choice") | |
def print_string_instructions(): | |
print("0 - Print Instructions") | |
print("1 - Encrypt a string") | |
print("2 - Decrypt a string") | |
print("3 - Check if string is a palindrome") | |
print("4 - Reverse a string") | |
def string_handler(): | |
print_string_instructions() | |
choice = int(input("Enter your choice: ")) | |
default_cipher = "qwertyuiopasdfghjklzxcvbnm QWERTYUIOPASDFGHJKLZXCVBNM0987654321" | |
if choice == 0: | |
print_string_instructions() | |
elif choice == 1: | |
str_to_encrypt = input("Enter your string: ") | |
print("Enter 1 to use default cipher.") | |
cipher = input("Enter your cipher: ") | |
if cipher == "1": | |
print("Encrypted string: " + encrypt(str_to_encrypt, default_cipher)) | |
else: | |
print("Encrypted string: " + encrypt(str_to_encrypt, cipher)) | |
elif choice == 2: | |
str_to_decrypt = input("Enter your string: ") | |
print("Enter 1 to use default cipher.") | |
cipher = input("Enter your cipher: ") | |
if cipher == "1": | |
print("Decrypted string: " + decrypt(str_to_decrypt, default_cipher)) | |
else: | |
print("Decrypted string: " + decrypt(str_to_decrypt, cipher)) | |
elif choice == 3: | |
str_to_check = input("Enter your string: ") | |
print("Answer: " + check_palindrome(str_to_check)) | |
elif choice == 4: | |
str_to_reverse = input("Enter string: ") | |
print("Reversed string: "+ reverse(str_to_reverse)) | |
else: | |
print("Not a valid choice") | |
quit_ = False | |
while not quit_: | |
choice = int(input("Choose an option: ")) | |
if choice == 0: | |
print_instructions() | |
elif choice == 1: | |
number_handler() | |
elif choice == 2: | |
string_handler() | |
else: | |
print("Bye") | |
quit(0) | |
# | |
# # Tests | |
# # Lists | |
# l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | |
# print("List " + str(l)) | |
# print("Sum of l: " + str(addList(l))) | |
# print("All elements in l sqared: " + str(squareList(l))) | |
# lSort = list(list(numpy.random.randint(low=0, high=10, size=(1, 10)))[0]) | |
# print("Random list to sort: " + str(lSort)) | |
# print("Sorted: " + str(sort(lSort))) | |
# print("-----") | |
# # Numbers | |
# print("5 is prime: " + str(is_prime(5))) | |
# print("10 is prime: " + str(is_prime(10))) | |
# print("5 is even: " + str(is_even(5))) | |
# print("10 is even: " + str(is_even(10))) | |
# print("5 is odd: " + str(is_odd(5))) | |
# print("10 is odd: " + str(is_odd(10))) | |
# print("10 - 5 = " + str(subtract(10, 5))) | |
# print("10 * 5 = " + str(multipliy(10, 5))) | |
# print("10 / 5 = " + str(divide(10, 5))) | |
# print("Circle of Numbers for n = 10 and firstNumber = 2: " + str(circle_of_numbers(10, 2))) | |
# print("Perimeter of rectangle with side length 10 and 8: " + str(perimeter_of_rectangle(10, 8))) | |
# print("Area of rectangle with side length 10 and 8: " + str(area_of_rectangle(10, 8))) | |
# print("Area of circle with radius 5: " + str(area_of_circle(radius=5))) | |
# print("Area of circle with diameter 20: " + str(area_of_circle(diameter=20))) | |
# print("Area of square with side 10: " + str(area_of_square(10))) | |
# print("Perimeter of square with side 10: " + str(perimeter_of_square(10))) | |
# print("Circumference of circle with radius 5: " + str(circumference(radius=5))) | |
# print("Circumference of circle with diameter 7: " + str(circumference(diameter=7))) | |
# print("Hypotenuse for right triangle legs 3 and 4: " + str(pythagorean_theorem(3, 4))) | |
# print("Law of cosines for a, b = 10 and C = 25: " + str(law_of_cosines_normal(10, 10, 25))) | |
# print("Calculating C with law of cosines when a = 5, b = 4, and c = 6: " + str(law_of_cosines_gamma(5, 4, 6))) | |
# # print("423th fibonacci number: " + str(fib(423))) | |
# print("-----") | |
# print("String tests") | |
# print("Check if 'civic' is a palindrome: " + str(check_palindrome("civic"))) | |
# print("Check if 'hello' is a palindrome: " + str(check_palindrome("hello"))) | |
# print("'Hello world' reversed: " + reverse("Hello world")) | |
# print("Most common character in string 'Hello World': " + find_most_common_character("Hello World")) | |
# cipher = "qwertyuiopasdfghjklzxcvbnm QWERTYUIOPASDFGHJKLZXCVBNM0987654321" | |
# print("Encrypt 'Hello world 45' with cipher '" + cipher + "': " + encrypt("Hello world 45", cipher)) | |
# print("Decrypt 'Itssg vgksr 76' with cipher '" + cipher + "': " + decrypt("Itssg vgksr 76", cipher)) |
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
""" | |
Functions that can be applied on numbers | |
""" | |
import math | |
def is_prime(x): | |
""" | |
Checks if x is prime | |
:param x: | |
:return: True or False | |
""" | |
for i in range(2, x // 2): | |
if x % i == 0: | |
return False | |
return True | |
def is_even(x): | |
""" | |
Checks if number is even | |
:param x: | |
:return: | |
""" | |
return x % 2 == 0 | |
def is_odd(x): | |
""" | |
Checks if number is odd | |
:param x: | |
:return: | |
""" | |
return x % 2 == 1 | |
def squared(x): | |
""" | |
Squares x | |
:param x: | |
:return: x**2 | |
""" | |
return x ** 2 | |
def subtract(x, y): | |
""" | |
Subtracts two numbers | |
:param x: | |
:param y: | |
:return: | |
""" | |
return x - y | |
def multipliy(x, y): | |
""" | |
Multiplies two numbers | |
:param x: | |
:param y: | |
:return: | |
""" | |
return x * y | |
def divide(x, y): | |
""" | |
Divides two numbers. | |
:param x: | |
:param y: | |
:return: | |
""" | |
return x / y | |
def circle_of_numbers(n, firstNumber): | |
""" | |
Circle of numbers challenge from CodeFights. | |
:param n: | |
:param firstNumber: | |
:return: | |
""" | |
if firstNumber >= n // 2: | |
return firstNumber - (n // 2) | |
else: | |
return firstNumber + (n // 2) | |
def perimeter_of_rectangle(s1, s2): | |
""" | |
Finds perimeter of rectangle given length and width | |
:param s1: | |
:param s2: | |
:return: | |
""" | |
return s1 * 2 + s2 * 2 | |
def perimeter_of_square(s): | |
""" | |
Finds perimeter of square given side s | |
:param s: | |
:return: | |
""" | |
return s * 4 | |
def area_of_square(s): | |
""" | |
Finds area of a square given side s | |
:param s: | |
:return: | |
""" | |
return s ** 2 | |
def area_of_rectangle(s1, s2): return s1 * s2 | |
def circumference(radius=0, diameter=0): | |
""" | |
Finds circumference of circles | |
:param radius: | |
:param diameter: | |
:return: | |
""" | |
if diameter > 0: | |
return diameter * math.pi | |
elif radius > 0: | |
return (2 * radius) * math.pi | |
def area_of_circle(radius=0, diameter=0): | |
""" | |
Finds the area of a circle | |
:param radius: int | |
:param diameter: int | |
:return: (pi)r**2 | |
""" | |
if diameter > 0: | |
return math.pi * ((diameter / 2) ** 2) | |
elif radius > 1: | |
return math.pi * (radius ** 2) | |
def pythagorean_theorem(a, b): | |
""" | |
Uses the Pythagorean Theorem | |
:param a: | |
:param b: | |
:return: | |
""" | |
return math.sqrt(a ** 2 + b ** 2) | |
def law_of_cosines_normal(a, b, C): | |
""" | |
Uses law of cosines to find length of side c | |
:param a: | |
:param b: | |
:param C: | |
:return: | |
""" | |
return math.sqrt(a ** 2 + b ** 2 - (2 * a * b * (math.cos(math.radians(C))))) | |
def law_of_cosines_gamma(a, b, c): | |
""" | |
Calcutes gamma using law of cosines | |
Should be 82.81 for a = 5, b = 4 and c = 6 | |
:param a: | |
:param b: | |
:param c: | |
:return: C | |
""" | |
return math.degrees(math.acos(((a ** 2 + b ** 2 - c ** 2) / (2 * a * b)))) | |
def _fib(n): | |
""" | |
Helper function to calculate fibonacci numbers | |
:param n: | |
:return: tuple | |
""" | |
if n == 0: | |
return (0, 1) | |
else: | |
a, b = _fib(n // 2) | |
c = a * (b * 2 - a) | |
d = a * a + b * b | |
if n % 2 == 0: | |
return (c, d) | |
else: | |
return (d, c + d) | |
def fib(n): | |
""" | |
Extremely efficient algorithm to calculate fibonacci numbers | |
:param n: | |
:return: nth fibonacci number | |
""" | |
return _fib(n)[0] | |
def gcd(a, b): | |
""" | |
An efficient algorithm to compute GCD | |
:param a: | |
:param b: | |
:return: gcd of a and b | |
""" | |
if b == 0: | |
return a | |
ap = a % b | |
return gcd(b, ap) | |
def lcm(a, b): | |
""" | |
An efficient algorithm to calculate LCM \n | |
This algorithm works efficiently. | |
:param a: | |
:param b: | |
:return: lcm of a, b | |
""" | |
return a * b // gcd(a, b) |
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
""" | |
Functions that can be applied on strings | |
""" | |
from functools import reduce | |
import operator | |
def check_palindrome(x): | |
""" | |
Checks if a string is a palindrome | |
:param x: | |
:return: True or False | |
""" | |
if x[::-1] == x: | |
return True | |
return False | |
def reverse(s): | |
""" | |
Reverses a string | |
:param s: | |
:return: Reverse of s | |
""" | |
return s[::-1] | |
def find_most_common_character(s): | |
""" | |
Finds most common character in string s | |
:param s: | |
:return: character | |
""" | |
letters = {i: 0 for i in s} | |
for i in s: | |
letters[i] += 1 | |
return max(letters.items(), key=operator.itemgetter(1))[0] | |
def encrypt(s, cipher): | |
""" | |
Encrpyts the word with the given cipher | |
:param s: | |
:param cipher: | |
:return: sting | |
""" | |
encrpyted_word = "" | |
alphabet = "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" | |
for i in s: | |
index_ = alphabet.find(i) | |
encrpyted_word += cipher[index_] | |
return encrpyted_word | |
def decrypt(s, cipher): | |
""" | |
Decrypts a word with the given cipher | |
:param s: | |
:param cipher: | |
:return: string | |
""" | |
decrpyted_word = "" | |
alphabet = "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" | |
for i in s: | |
index_ = cipher.find(i) | |
decrpyted_word += alphabet[index_] | |
return decrpyted_word |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment