Skip to content

Instantly share code, notes, and snippets.

View MohanSha's full-sized avatar
👨‍💻
Hello stranger looking at my work!

Mohan Sha MohanSha

👨‍💻
Hello stranger looking at my work!
View GitHub Profile
@MohanSha
MohanSha / Fibonacci Sequence.py
Created February 13, 2018 07:30
Generate a Fibonacci Sequence
# Fibonacci Sequence
# Enter a number and have the program generate the Fibonacci sequence to
# that number or to the Nth number.
def get_fibonacci_seq(n):
res = [1,1]
while len(res) < n:
res.append(res[-1] + res[-2])
return res[:n]
@MohanSha
MohanSha / PrimeFactor.py
Created February 13, 2018 07:50
Have the user enter a number and find all Prime Factors (if there are any) and display them.
# Prime Factorization
# Have the user enter a number and find all Prime Factors (if there are any) and display them.
import math
def is_prime(num):
if num < 2:
return False
@MohanSha
MohanSha / NextPrime.py
Created February 13, 2018 07:52
Have the program find prime numbers until the user chooses to stop asking for the next one.
# Next Prime Number
# Have the program find prime numbers until the user chooses to stop asking for the next one.
import math
def is_prime(num):
if num < 2:
return False
@MohanSha
MohanSha / TileCost.py
Created February 13, 2018 07:53
Calculate the total cost of tile it would take to cover a floor plan of width and height
# Find Cost of Tile to Cover W x H Floor
# Calculate the total cost of tile it would take to cover a floor plan of width and height,
# using a cost entered by the user.
height = int(raw_input("Height: "))
width = int(raw_input("Width: "))
cost = int(raw_input("Cost: "))
print "Total cost is " + str(width * height * cost) + "€"
@MohanSha
MohanSha / Binary2Decimal.py
Created February 13, 2018 07:55
A converter to convert a decimal number to binary or a binary number to its decimal equivalent.
# Binary to Decimal and Back Converter
# Develop a converter to convert a decimal number to binary
# or a binary number to its decimal equivalent.
def binary_to_decimal(binary):
decimal = 0
i = 0
while binary > 0:
binary, last_digit = divmod(binary, 10)
@MohanSha
MohanSha / ValidateCard.py
Created February 13, 2018 07:57
Takes in a credit card number from a common credit card vendor (Visa, MasterCard, American Express, Discoverer) and validates it to make sure that it is a valid number (look into how credit cards use a checksum).
def main():
credit_card_num = list(raw_input("Credit card #: "))
checksum_nums = [int(v) * 2 for i, v in enumerate(credit_card_num) if i % 2 == 0]
checksum = 0
for i, v in enumerate(credit_card_num):
checksum += (i % 2 != 0 and int(credit_card_num[i]) or 0)
for v in checksum_nums:
if v >= 10:
@MohanSha
MohanSha / Factorial.py
Created February 13, 2018 08:39
The Factorial of a positive integer, n, is defined as the product of the sequence n, n-1, n-2, ...1 and the factorial of zero, 0, is defined as being 1. Solve this using both loops and recursion.
# Factorial Finder
# The Factorial of a positive integer, n, is defined as the product of the
# sequence n, n-1, n-2, ...1 and the factorial of zero, 0, is defined as
# being 1. Solve this using both loops and recursion.
def factorial(n):
result = 1
if n == 0:
return result
@MohanSha
MohanSha / ReverseStr.py
Created February 13, 2018 08:41
Enter a string and the program will reverse it and print it out.
# Reverse a String
# Enter a string and the program will reverse it and print it out.
def reverse_string(string):
return string[::-1]
string = raw_input('String: ')
print reverse_string(string)
@MohanSha
MohanSha / VowelCount.py
Created February 13, 2018 08:47
Enter a string and the program counts the number of vowels in the text. For added complexity have it report a sum of each vowel found.
# Count Vowels
# Enter a string and the program counts the number of vowels in the text.
# For added complexity have it report a sum of each vowel found.
vowels = ['a', 'e', 'i', 'o', 'u']
counter = [0, 0, 0, 0, 0]
def count_vowels(string):
for i in range(0, 5):
@MohanSha
MohanSha / Palindrome.py
Created February 13, 2018 08:47
Checks if the string entered by the user is a palindrome. That is that it reads the same forwards as backwards like “racecar”
# Check if Palindrome
# Checks if the string entered by the user is a palindrome.
# That is that it reads the same forwards as backwards like “racecar”
def is_palindrome(string):
if len(string) <= 1:
return True
if string[0] != string[-1]:
return False