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
# 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] |
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
# 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 |
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
# 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 |
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
# 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) + "€" |
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
# 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) |
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
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: |
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
# 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 |
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
# 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) |
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
# 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): |
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
# 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 |
OlderNewer