From bddfe81f3d04a36d29204831bec2a6571d92fb75 Mon Sep 17 00:00:00 2001 From: Nolan Knill Date: Fri, 13 Sep 2013 12:26:13 -0400 Subject: [PATCH 01/30] Delete product_inventory.py --- Classes/product_inventory.py | 80 ------------------------------------ 1 file changed, 80 deletions(-) delete mode 100644 Classes/product_inventory.py diff --git a/Classes/product_inventory.py b/Classes/product_inventory.py deleted file mode 100644 index df1ef1afa..000000000 --- a/Classes/product_inventory.py +++ /dev/null @@ -1,80 +0,0 @@ -""" -Product Inventory Project - Create an application which manages -an inventory of products. Create a product class which has a -price, id, and quantity on hand. Then create an inventory class -which keeps track of various products and can sum up the inventory -value. -""" - -class Product: - - def __init__(self, price, pid, qty): - """ - Class constructor that needs a price, a product id, - and quantity. - """ - self.price = price - self.pid = pid - self.qty = qty - - def update_qty(self, qty, method='add'): - """ - Updates the quantity of produts. By default, adds the - passed quantity. Pass method as 'subtract' to subtract - the quantity. - """ - if method == 'add': - self.qty += qty - elif method == 'subtract': - self.qty = max(0, self.qty - qty) - - def print_product(self): - """ - Prints a single product. - """ - print '%d\t%s\t%.02f each' % (self.pid, self.qty, self.price) - -class Inventory: - - def __init__(self): - """ - Initializes the class instance. - """ - self.products = [] # list to hold all products - - def add(self, product): - """ - Adds a passed Product to the list of products. - """ - self.products.append(product) - - def print_inventory(self): - """ - Prints the current inventory, and the total value - of products. - """ - value = 0 - for product in self.products: - print '%d\t%s\t%.02f each' % (product.pid, - product.qty, - product.price) - value += (product.price * product.qty) - print '\nTotal value: %.02f' % value - -if __name__ == '__main__': - p1 = Product(1.4, 123, 5) - p2 = Product(1, 3432, 100) - p3 = Product(100.4, 2342, 99) - - - i = Inventory() - i.add(p1) - i.add(p2) - i.add(p3) - i.print_inventory() - - p1.update_qty(10) - i.print_inventory() - - p1.update_qty(10, method='subtract') - i.print_inventory() From 8cd5f92c7bb1231391abd5f3a53f98f6e4270e05 Mon Sep 17 00:00:00 2001 From: Nolan Knill Date: Fri, 13 Sep 2013 12:26:33 -0400 Subject: [PATCH 02/30] Delete collatz.py --- Classic Algorithms/collatz.py | 29 ----------------------------- 1 file changed, 29 deletions(-) delete mode 100644 Classic Algorithms/collatz.py diff --git a/Classic Algorithms/collatz.py b/Classic Algorithms/collatz.py deleted file mode 100644 index 3881ea11e..000000000 --- a/Classic Algorithms/collatz.py +++ /dev/null @@ -1,29 +0,0 @@ -""" -Collatz Conjecture - Start with a number n > 1. -Find the number of steps it takes to reach one using -the following process: If n is even, divide it by 2. -If n is odd, multiply it by 3 and add 1. -""" - -def main(): - try: - n = int(raw_input('Enter a number: ')) - except ValueError: - print 'Enter only an integer value, n > 1.' - - steps = 0 - - print '\n%d' % n, - - while n > 1: - if n % 2 == 0: - n /= 2 - else: - n = (n * 3) + 1 - steps += 1 - print ' -> %d' % n, - - print '\n\n%d steps take to reach ONE.' % steps - -if __name__ == '__main__': - main() From 9635b59a56a8cf057d47400ee83d2e43ce23fc07 Mon Sep 17 00:00:00 2001 From: Nolan Knill Date: Fri, 13 Sep 2013 12:27:24 -0400 Subject: [PATCH 03/30] Delete alarm.py --- Numbers/alarm.py | 49 ------------------------------------------------ 1 file changed, 49 deletions(-) delete mode 100644 Numbers/alarm.py diff --git a/Numbers/alarm.py b/Numbers/alarm.py deleted file mode 100644 index a0a1c7367..000000000 --- a/Numbers/alarm.py +++ /dev/null @@ -1,49 +0,0 @@ -""" -Alarm Clock - A simple clock where it plays a sound after -X number of minutes/seconds or at a particular time. - -Dependencies: -pyglet - pip install pyglet -""" - -import time -import winsound -import pyglet - -def play(hh, mm): - not_alarmed = 1 - - while(not_alarmed): - cur_time = list(time.localtime()) # get the time right now - hour = cur_time[3] # find the hour - minute = cur_time[4] # and the minute - if hour == hh and minute == mm: - song = pyglet.media.load('bin/sound.wav') - song.play() # play the sound - pyglet.app.run() - not_alarmed = 0 # stop the loop - -if __name__ == '__main__': - - print """ - 1. Play sound after X minutes - 2. Play sound at an exact time - """ - choice = input('What do you want to do? ') - - if choice == 1: - mins = input('How many minutes from now? ') - hh_from_now = mins / 60 # if minutes > 60, this will adjust the hours - mm_from_now = mins % 60 # and then the minutes - cur_time = list(time.localtime()) # get the time right now - hour = cur_time[3] # find the current hour - minute = cur_time[4] # and the current minute - hh = (hour + hh_from_now) % 24 # cycle through the clock if hh > 24 - mm = (minute + mm_from_now) % 60 # cycle through the clock if mm > 60 - play(hh, mm) - elif choice == 2: - hh = input('What hour do you want to wake up (0-23)? ') - mm = input('What minute do you want to wake up (0-59)? ') - play(hh, mm) - From 5071010e1f8763194d3865103e21a330fde5de46 Mon Sep 17 00:00:00 2001 From: Nolan Knill Date: Fri, 13 Sep 2013 12:27:42 -0400 Subject: [PATCH 04/30] Delete page_scraper.py --- Web/page_scraper.py | 33 --------------------------------- 1 file changed, 33 deletions(-) delete mode 100644 Web/page_scraper.py diff --git a/Web/page_scraper.py b/Web/page_scraper.py deleted file mode 100644 index ccfd191fa..000000000 --- a/Web/page_scraper.py +++ /dev/null @@ -1,33 +0,0 @@ -# -*- coding: cp1252 -*- -""" -Page Scraper - Create an application which connects to a -site and pulls out all links, or images, and saves them to -a list. Optional: Organize the indexed content and don�t -allow duplicates. Have it put the results into an easily -searchable index file. -""" - -import urllib2 -from bs4 import BeautifulSoup - - -def print_list(stuff): - print '\n'.join(stuff) - print '\n====================\n' - -if __name__ == '__main__': - - url = raw_input('Enter a URL: ') - - choice = input('What to scrape?\n1. Links\n2. Images\n3. Both\n') - - soup = BeautifulSoup(urllib2.urlopen(url).read()) - - if choice == 1 or choice == 3: - urls = [link.get('href') for link in soup.findAll('a')] - print 'URLs:' - print_list(urls) - if choice == 2 or choice ==3: - images = [image['src'] for image in soup.findAll("img")] - print 'Images:' - print_list(images) From 5a73847bf499ad81af6f758001f4cb925d808d14 Mon Sep 17 00:00:00 2001 From: Nolan Knill Date: Fri, 13 Sep 2013 12:27:50 -0400 Subject: [PATCH 05/30] Delete time.py --- Web/time.py | 25 ------------------------- 1 file changed, 25 deletions(-) delete mode 100644 Web/time.py diff --git a/Web/time.py b/Web/time.py deleted file mode 100644 index 073d75f47..000000000 --- a/Web/time.py +++ /dev/null @@ -1,25 +0,0 @@ -""" -Get Atomic Time from Internet Clock - This program will get -the true atomic time from an atomic time clock on the Internet. -Use any one of the atomic clocks returned by a simple Google search. -""" - -import re -from urllib2 import urlopen - - -def main(): - url = 'http://time.is/just' - content = urlopen(url).read() - pattern = re.compile('
(.*)(AM|PM)
') - - find_match = re.search(pattern, content) - - location_pat = re.compile('

(.*)

') - location_match = re.search(location_pat, content) - - print 'The time in %s is %s %s' % \ - (location_match.group(1), find_match.group(1), find_match.group(2)) - -if __name__ == '__main__': - main() From 7f19eac81b0fb3b2d07c6209da1965d42686a91f Mon Sep 17 00:00:00 2001 From: Nolan Knill Date: Fri, 13 Sep 2013 12:28:08 -0400 Subject: [PATCH 06/30] Delete rss.py --- Text/rss.py | 29 ----------------------------- 1 file changed, 29 deletions(-) delete mode 100644 Text/rss.py diff --git a/Text/rss.py b/Text/rss.py deleted file mode 100644 index 25f8c8f1d..000000000 --- a/Text/rss.py +++ /dev/null @@ -1,29 +0,0 @@ -""" -RSS Feed Creator - Given a link to RSS/Atom Feed, -get all posts and display them. -""" - -import re -import urllib2 - - -def main(): - """ - Takes in a Feedburned feed URL. - - Eg: http://feeds.feedburner.com/WebDesignLedger - """ - feed_url = raw_input('Enter Feedburner RSS URL: ') - content = urllib2.urlopen(feed_url).read() # get the source code of feed - - link_pattern = re.compile('(.*)') - title_pattern = re.compile('(.*)') - - links = re.findall(link_pattern, content)[1:] # skip blog url - titles = re.findall(title_pattern, content)[1:] # skip the page title - - for (link, title) in zip(links, titles): - print '{0}\n{1}\n'.format(title, link) - -if __name__ == '__main__': - main() From 9bb8717bd6125b3e1fadc399256ba527ea27f315 Mon Sep 17 00:00:00 2001 From: Nolan Knill Date: Fri, 13 Sep 2013 12:28:22 -0400 Subject: [PATCH 07/30] Delete reverse.py --- Text/reverse.py | 8 -------- 1 file changed, 8 deletions(-) delete mode 100644 Text/reverse.py diff --git a/Text/reverse.py b/Text/reverse.py deleted file mode 100644 index 5cec022c3..000000000 --- a/Text/reverse.py +++ /dev/null @@ -1,8 +0,0 @@ -# -*- coding: cp1252 -*- -""" -Reverse a String - Enter a string and the program -will reverse it and print it out. -""" - -string = raw_input("Whatchu wanna say to me? ") -print "You say %s, I say %s" % (string, string[::-1]) From 456beef9c0e4787b57f8b766d86a8b9e731f4bb7 Mon Sep 17 00:00:00 2001 From: Nolan Knill Date: Fri, 13 Sep 2013 12:28:29 -0400 Subject: [PATCH 08/30] Delete piglatin.py --- Text/piglatin.py | 22 ---------------------- 1 file changed, 22 deletions(-) delete mode 100644 Text/piglatin.py diff --git a/Text/piglatin.py b/Text/piglatin.py deleted file mode 100644 index 07af62233..000000000 --- a/Text/piglatin.py +++ /dev/null @@ -1,22 +0,0 @@ -""" -Pig Latin - Pig Latin is a game of alterations played -on the English language game. To create the Pig Latin -form of an English word the initial consonant sound is -transposed to the end of the word and an ay is affixed -(Ex.: "banana" would yield anana-bay). Read Wikipedia -for more information on rules. -""" - -word = raw_input('What\'s your word? ').lower() -vowels = 'aeiou' - -pig = 'ay' - -first = word[0] - -if first in vowels: - new = word + pig -else: - new = word[1:] + first + pig - -print new From aecd23489911b5fa486cb3d0232c36b7a78d8457 Mon Sep 17 00:00:00 2001 From: Nolan Knill Date: Fri, 13 Sep 2013 12:28:35 -0400 Subject: [PATCH 09/30] Delete palindrome.py --- Text/palindrome.py | 12 ------------ 1 file changed, 12 deletions(-) delete mode 100644 Text/palindrome.py diff --git a/Text/palindrome.py b/Text/palindrome.py deleted file mode 100644 index 43c7f4145..000000000 --- a/Text/palindrome.py +++ /dev/null @@ -1,12 +0,0 @@ -""" -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" -""" - -string = raw_input('Enter a string: ').lower() - -if string == string[::-1]: - print '%s is a palindrome' % string -else: - print '%s is not a palindrome' % string From 3c0b7fc2154df525a6cc04ebb3d9038926ba47f1 Mon Sep 17 00:00:00 2001 From: Nolan Knill Date: Fri, 13 Sep 2013 12:28:40 -0400 Subject: [PATCH 10/30] Delete count_words.py --- Text/count_words.py | 26 -------------------------- 1 file changed, 26 deletions(-) delete mode 100644 Text/count_words.py diff --git a/Text/count_words.py b/Text/count_words.py deleted file mode 100644 index 113c31f81..000000000 --- a/Text/count_words.py +++ /dev/null @@ -1,26 +0,0 @@ -""" -Count Words in a String - Counts the number of individual -words in a string and display the top 5/10 most used words. -""" - -from collections import defaultdict -import operator - -if __name__ == '__main__': - text = raw_input('Enter some text: \n') - words = text.split() # very naive approach, split at space - - counts = defaultdict(int) # no need to check existence of a key - - # find count of each word - for word in words: - counts[word] += 1 - - # sort the dict by the count of each word, returns a tuple (word, count) - sorted_counts = sorted(counts.iteritems(), \ - key=operator.itemgetter(1), \ - reverse=True) - - # print top 5 words - for (word,count) in sorted_counts[:5]: # thanks @jrwren for this! - print (word, count) From c366550c1ac95233f92137117005ca9980b143e8 Mon Sep 17 00:00:00 2001 From: Nolan Knill Date: Fri, 13 Sep 2013 12:28:49 -0400 Subject: [PATCH 11/30] Delete count_vowels.py --- Text/count_vowels.py | 19 ------------------- 1 file changed, 19 deletions(-) delete mode 100644 Text/count_vowels.py diff --git a/Text/count_vowels.py b/Text/count_vowels.py deleted file mode 100644 index fe55626f1..000000000 --- a/Text/count_vowels.py +++ /dev/null @@ -1,19 +0,0 @@ -""" -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. -""" - -from collections import defaultdict - -if __name__ == '__main__': - string = raw_input('Enter a string: ').lower() - - vowels = ['a', 'e', 'i', 'o', 'u'] - counts = defaultdict(int) - - for char in string: - if char in vowels: - counts[char] += 1 - - print counts.items() From 1c519cd1cff36ecec239651983cb5ce5b6c2c89a Mon Sep 17 00:00:00 2001 From: Nolan Knill Date: Fri, 13 Sep 2013 12:29:48 -0400 Subject: [PATCH 12/30] Delete unit.py --- Numbers/unit.py | 91 ------------------------------------------------- 1 file changed, 91 deletions(-) delete mode 100644 Numbers/unit.py diff --git a/Numbers/unit.py b/Numbers/unit.py deleted file mode 100644 index 383044e99..000000000 --- a/Numbers/unit.py +++ /dev/null @@ -1,91 +0,0 @@ -""" -Unit Converter (temp, currency, volume, mass and more) - Converts -various units between one another. The user enters the type of unit -being entered, the type of unit they want to convert to and then -the value. The program will then make the conversion. -""" - -from __future__ import division -from urllib2 import urlopen -import json - -# 1 (std unit) = these many units -MULTIPLIERS_TO_STD = { - 'length': { - 'cm': 0.01, - 'm': 1, # std unit - 'km': 1000, - 'mi': 1609.34, - 'ft': 0.3048 - }, - 'temp': { - 'C': 1, # std unit - 'F': 33.8 - } -} - -# These many units = 1 (std unit) -MULTIPLIERS_FROM_STD = { - 'length': { - 'cm': 100, - 'm': 1, # std unit - 'km': 0.001, - 'mi': 0.000621371, - 'ft': 3.28084 - }, - 'temp': { - 'C': 1, # std unit - 'F': -17.2222 - } -} - - -def get_user_input(choice): - units = ', '.join(MULTIPLIERS_TO_STD[choice].keys()) - source_unit = raw_input('\nEnter source unit (%s): ' % units) - source_val = float(raw_input('How many %s\'s? ' % source_unit)) - convert_to = raw_input('Convert to? (%s): ' % units) - return source_unit, source_val, convert_to - -def get_currency(source_unit, source_val, convert_to): - url = 'http://rate-exchange.appspot.com/currency?from=%s&to=%s&q=%s' % ( - source_unit, convert_to, str(source_val)) - content = urlopen(url).read() - return json.loads(content)['v'] - -def main(): - print """Unit Converter - 1. Length - 2. Temperature - 3. Currency""" - - choice = int(raw_input('What do you want to convert: ')) - - if choice == 1: - source_unit, source_val, convert_to = get_user_input('length') - print '%f%s = %f%s' % (source_val, source_unit, - source_val * \ - MULTIPLIERS_TO_STD['length'][source_unit] * \ - MULTIPLIERS_FROM_STD['length'][convert_to], \ - convert_to) - elif choice == 2: - source_unit, source_val, convert_to = get_user_input('temp') - if (source_unit, convert_to) == ('F', 'C'): # F -> C - value = (source_val - 32) * (5/9) - elif (source_unit, convert_to) == ('C', 'F'): # C -> F - value = (source_val * (9/5)) + 32 - else: - value = source_val - print '%f%s = %f%s' % (source_val, source_unit, - value, convert_to) - - elif choice == 3: - source_unit = raw_input('\nEnter source currency (eg USD, INR etc): ') - source_val = float(raw_input('How many %s\'s? ' % source_unit)) - convert_to = raw_input('Convert to? (eg USD, INR etc): ') - print '%f%s = %f%s' % (source_val, source_unit, - get_currency(source_unit, source_val, convert_to), - convert_to) - -if __name__ == '__main__': - main() From 004e942b61e2b6fce296afcfbfc6bb531a5a270b Mon Sep 17 00:00:00 2001 From: Nolan Knill Date: Fri, 13 Sep 2013 12:29:53 -0400 Subject: [PATCH 13/30] Delete tile.py --- Numbers/tile.py | 11 ----------- 1 file changed, 11 deletions(-) delete mode 100644 Numbers/tile.py diff --git a/Numbers/tile.py b/Numbers/tile.py deleted file mode 100644 index 6f4d7cf62..000000000 --- a/Numbers/tile.py +++ /dev/null @@ -1,11 +0,0 @@ -# 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. - -# Use input as the input can be integer and float -cost = input("What's the cost per sq. feet? ") -width = input("What's the width of the floor? ") -height = input("What's the height of the floor? ") - -print "The total cost is $%.2f for %.2f square feet" \ - % (width * height * cost, width * height) From 1685207463917ae93cbfe115c7da0260c21adef4 Mon Sep 17 00:00:00 2001 From: Nolan Knill Date: Fri, 13 Sep 2013 12:29:59 -0400 Subject: [PATCH 14/30] Delete prime.py --- Numbers/prime.py | 23 ----------------------- 1 file changed, 23 deletions(-) delete mode 100644 Numbers/prime.py diff --git a/Numbers/prime.py b/Numbers/prime.py deleted file mode 100644 index ffa1ea83c..000000000 --- a/Numbers/prime.py +++ /dev/null @@ -1,23 +0,0 @@ -# Prime Factorization - Have the user enter a number -# and find all Prime Factors (if there are any) and -# display them. - - -def is_a_prime(x): - for i in range(2, x): - if x % i == 0: - return False - return True - -# standard boilerplate -if __name__ == '__main__': - n = int(raw_input('Enter the number to find prime factors of: ')) - - factors = [] - - for i in range(2, n + 1): - while n % i == 0: # Thanks @madsulrik - if is_a_prime(i): - factors.append(i) - n /= i - print factors From 410d718e922df197ed24a32cf3cc3255a34eda8c Mon Sep 17 00:00:00 2001 From: Nolan Knill Date: Fri, 13 Sep 2013 12:30:07 -0400 Subject: [PATCH 15/30] Delete pi.py --- Numbers/pi.py | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 Numbers/pi.py diff --git a/Numbers/pi.py b/Numbers/pi.py deleted file mode 100644 index 559480110..000000000 --- a/Numbers/pi.py +++ /dev/null @@ -1,10 +0,0 @@ -# Find PI to the Nth Digit - -from math import * - -digits = raw_input('Enter number of digits to round PI to: ') - -# print ('{0:.%df}' % min(20, int(digits))).format(math.pi) # nested string formatting - -# calculate pi using Machin-like Formula -print ('{0:.%df}' % min(30, int(digits))).format(4 * (4 * atan(1.0/5.0) - atan(1.0/239.0))) From d3f9e4853a1d2426d2bec6d130d875270c15d19d Mon Sep 17 00:00:00 2001 From: Nolan Knill Date: Fri, 13 Sep 2013 12:30:16 -0400 Subject: [PATCH 16/30] Delete next_prime.py --- Numbers/next_prime.py | 25 ------------------------- 1 file changed, 25 deletions(-) delete mode 100644 Numbers/next_prime.py diff --git a/Numbers/next_prime.py b/Numbers/next_prime.py deleted file mode 100644 index 0f356acee..000000000 --- a/Numbers/next_prime.py +++ /dev/null @@ -1,25 +0,0 @@ -# Next Prime Number - Have the program find prime -# numbers until the user chooses to stop asking for -# the next one. - -def next_prime(current): - next_prime = current + 1 # start checking for primes 1 number after the current one - i = 2 - while next_prime > i: # check with numbers up to next_prime - 1 - if next_prime % i == 0: # if number is divisible - next_prime += 1 # ready to check the next number - i = 2 # reset i to check divisibility again from 2 - else: - i += 1 # increment the divisor - return next_prime - -if __name__ == '__main__': - current_prime = 2 - while True: - response = raw_input('Do you want the next prime? (Y/N) ') - - if response.lower().startswith('y'): - print current_prime - current_prime = next_prime(current_prime) - else: - break From ab5774213ec39324e126b24e9fbad64408d682ad Mon Sep 17 00:00:00 2001 From: Nolan Knill Date: Fri, 13 Sep 2013 12:30:23 -0400 Subject: [PATCH 17/30] Delete happy_numbers.py --- Numbers/happy_numbers.py | 40 ---------------------------------------- 1 file changed, 40 deletions(-) delete mode 100644 Numbers/happy_numbers.py diff --git a/Numbers/happy_numbers.py b/Numbers/happy_numbers.py deleted file mode 100644 index a50f05f92..000000000 --- a/Numbers/happy_numbers.py +++ /dev/null @@ -1,40 +0,0 @@ -""" -Happy Numbers - A happy number is defined by the -following process. Starting with any positive integer, -replace the number by the sum of the squares of its -digits, and repeat the process until the number equals -1 (where it will stay), or it loops endlessly in a -cycle which does not include 1. Those numbers for which -this process ends in 1 are happy numbers, while those -that do not end in 1 are unhappy numbers. Take an input -number from user, and find first 8 happy numbers from -that input. -""" - -NUMBERS_REQUIRED = 8 # number of happy numbers required - -def is_happy_number(num): - seen = [] - while True: - sum_digits = sum(int(digit) ** 2 for digit in str(num)) - if sum_digits == 1: - return True - elif sum_digits in seen: - return False - else: - num = sum_digits - seen.append(num) - -if __name__ == '__main__': - - happies = [] # list of happy numbers found - - num = input('Start at: ') - - while len(happies) != NUMBERS_REQUIRED: - if is_happy_number(num): - happies.append(num) - num += 1 - - print happies - From 7058d3c68bbbf1ad8fd0716be8f128c36e4ff71b Mon Sep 17 00:00:00 2001 From: Nolan Knill Date: Fri, 13 Sep 2013 12:30:30 -0400 Subject: [PATCH 18/30] Delete fibonacci.py --- Numbers/fibonacci.py | 15 --------------- 1 file changed, 15 deletions(-) delete mode 100644 Numbers/fibonacci.py diff --git a/Numbers/fibonacci.py b/Numbers/fibonacci.py deleted file mode 100644 index a6692a94f..000000000 --- a/Numbers/fibonacci.py +++ /dev/null @@ -1,15 +0,0 @@ -# -*- coding: cp1252 -*- -# Fibonacci Sequence - Enter a number and have the -# program generate the Fibonacci sequence to that number -# or to the Nth number - -n = int(raw_input('How many numbers do you need? ')) -series = [1] - -while len(series) < n: - if len(series) == 1: - series.append(1) - else: - series.append(series[-1] + series[-2]) - -print series From 6f2a2e24a44865243ece56d82c61f7c6eafee412 Mon Sep 17 00:00:00 2001 From: Nolan Knill Date: Fri, 13 Sep 2013 12:30:36 -0400 Subject: [PATCH 19/30] Delete factorial.py --- Numbers/factorial.py | 33 --------------------------------- 1 file changed, 33 deletions(-) delete mode 100644 Numbers/factorial.py diff --git a/Numbers/factorial.py b/Numbers/factorial.py deleted file mode 100644 index b09d5e2f4..000000000 --- a/Numbers/factorial.py +++ /dev/null @@ -1,33 +0,0 @@ -""" -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 fact_loop(n): - """ - Returns the factorial of a given positive, non-zero integer - using loops. - """ - fact = 1 - while n > 0: - fact *= n - n -= 1 - return fact - -def fact_recursion(n): - """ - Returns the factorial of a given positive, non-zero integer - using recursion. - """ - return 1 if n == 0 else n * fact_recursion(n - 1) # if user as ternary operator - -if __name__ == '__main__': - n = input('Enter a positive number: ') - - if n >= 0: - print 'Factorial of %d by loops is %d' % (n, fact_loop(n)) - print 'Factorial of %d by recursion is %d' % (n, fact_recursion(n)) - else: - print 'Not a valid number' From 2db5388dfbecb1f8483903ae9d9566c2821cb085 Mon Sep 17 00:00:00 2001 From: Nolan Knill Date: Fri, 13 Sep 2013 12:30:42 -0400 Subject: [PATCH 20/30] Delete distance.py --- Numbers/distance.py | 55 --------------------------------------------- 1 file changed, 55 deletions(-) delete mode 100644 Numbers/distance.py diff --git a/Numbers/distance.py b/Numbers/distance.py deleted file mode 100644 index 65cfc9a9b..000000000 --- a/Numbers/distance.py +++ /dev/null @@ -1,55 +0,0 @@ -#!/usr/bin/env python - -""" -Distance Between Two Cities - Calculates the distance between -two cities and allows the user to specify a unit of distance. -This program may require finding coordinates for the cities -like latitude and longitude. - -Uses the Haversine formula -(http://www.movable-type.co.uk/scripts/latlong.html) - -Dependencies: -geopy - pip install geopy -""" - -from geopy import geocoders # to find lat/lon for the city -import math - -R = 6373 # km - -city1 = raw_input('Enter city 1: ') -city2 = raw_input('Enter city 2: ') -unit = raw_input('Enter unit of distance (Enter "K" for KM or "M" for MI): ').lower() - -if unit in 'km': - - g = geocoders.GoogleV3() - - try: - city1, (lat1, lon1) = g.geocode(city1) - city2, (lat2, lon2) = g.geocode(city2) - except: - raise Exception('Unable to locate the citites. Check the city names.') - - # convert decimal locations to radians - lat1 = math.radians(lat1) - lon1 = math.radians(lon1) - lat2 = math.radians(lat2) - lon2 = math.radians(lon2) - - # start haversne formula - dlon = lon2 - lon1 - dlat = lat2 - lat1 - a = (math.sin(dlat/2) ** 2) + math.cos(lat1) * math.cos(lat2) * \ - (math.sin(dlon/2) ** 2) - c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a)) - d = R * c - - if unit == 'k': - print 'Distance between %s and %s is %.04f km' % (city1, city2, d) - else: - print 'Distance between %s and %s is %.04f mi' % (city1, city2, d / 1.60934) -else: - print 'Invalid unit input!' From 347119495c4ad7ff37771c80c77b9aefdc5765bc Mon Sep 17 00:00:00 2001 From: Nolan Knill Date: Fri, 13 Sep 2013 12:30:47 -0400 Subject: [PATCH 21/30] Delete credit_card_validator.py --- Numbers/credit_card_validator.py | 41 -------------------------------- 1 file changed, 41 deletions(-) delete mode 100644 Numbers/credit_card_validator.py diff --git a/Numbers/credit_card_validator.py b/Numbers/credit_card_validator.py deleted file mode 100644 index 7bb0a39cf..000000000 --- a/Numbers/credit_card_validator.py +++ /dev/null @@ -1,41 +0,0 @@ -""" -Credit Card Validator - 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). - -This program works with *most* credit card numbers. - -Uses Luhn Algorithm (http://en.wikipedia.org/wiki/Luhn_algorithm). - -1. From the rightmost digit, which is the check digit, moving -left, double the value of every second digit; if product of this -doubling operation is greater than 9 (e.g., 7 * 2 = 14), then -sum the digits of the products (e.g., 10: 1 + 0 = 1, 14: 1 + 4 = 5). - -2. Add together doubled digits with the undoubled digits from the -original number. - -3. If the total modulo 10 is equal to 0 (if the total ends in zero) -then the number is valid according to the Luhn formula; else it is -not valid. -""" - -if __name__ == '__main__': - number = raw_input('Enter the credit card number of check: ').replace(' ', '') - #if not number.isdigit(): - # raise Exception('Invalid credit card number. Make sure it\'s all digits (with optional spaces in between).' - - digits = [int(char) for char in number] - - # double alternate digits (step 1) - doubled = [(digit * 2) if (i % 2 == 0) else digit \ - for (i, digit) in enumerate(digits)] # i <3 python - # sum digits of number > 10 (step 2) - summed = [num if num < 10 else sum([int(dig) for dig in str(num)]) \ - for num in doubled] # i <3 python ** 2 - # step 3 - if sum(summed) % 10 == 0: - print 'The number is valid' - else: - print 'The number is invalid' From 137dcdfba3f8ec0dbdf74be7309424a3145e5b23 Mon Sep 17 00:00:00 2001 From: Nolan Knill Date: Fri, 13 Sep 2013 12:30:52 -0400 Subject: [PATCH 22/30] Delete change.py --- Numbers/change.py | 39 --------------------------------------- 1 file changed, 39 deletions(-) delete mode 100644 Numbers/change.py diff --git a/Numbers/change.py b/Numbers/change.py deleted file mode 100644 index 8bce7273e..000000000 --- a/Numbers/change.py +++ /dev/null @@ -1,39 +0,0 @@ -# Change Return Program - The user enters a cost and -# then the amount of money given. The program will figure -# out the change and the number of quarters, dimes, nickels, -# pennies needed for the change. - -if __name__ == '__main__': - cost = input("What's the cost in dollars? ") - given = input("What's the amount of dollars given? ") - - change = given - cost - - print "\n" - if change < 0: - print "Please ask for $%.2f more from the customer." % (-change) # double negation - else: - print "The change is $%.2f." % change - - q = 0 # 0.25 - d = 0 # 0.10 - n = 0 # 0.05 - p = 0 # 0.01 - - change = int(change * 100) # let's talk about cents - - if change >= 25: - q = int(change / 25) - change = change % 25 - if change >= 10: - d = int(change / 10) - change = change % 10 - if change >= 5: - n = int(change / 5) - change = change % 5 - if change >= 1: - p = change # rest all change is in pennies - - print "Give the following change to the customer:" - print "Quarters: %d\tDimes: %d\tNickels: %d\tPennies: %d" \ - % (q, d, n, p) From be17f072822dda6ba80f708027749ae22ecc3b5f Mon Sep 17 00:00:00 2001 From: Nolan Knill Date: Fri, 13 Sep 2013 12:30:57 -0400 Subject: [PATCH 23/30] Delete calc.py --- Numbers/calc.py | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 Numbers/calc.py diff --git a/Numbers/calc.py b/Numbers/calc.py deleted file mode 100644 index e6ea5f7f2..000000000 --- a/Numbers/calc.py +++ /dev/null @@ -1,17 +0,0 @@ -""" -Calculator - A simple calculator to do basic operators. -""" - -if __name__ == '__main__': - try: - num1 = int(raw_input("Number 1: ")) - num2 = int(raw_input("Number 2: ")) - except: - print 'Invalid input' - else: - op = raw_input("Operation (+, -, /, *): ") - if op not in '+-/*': - print "Invalid operator" - else: - print "%d %s %d = %d" % \ - (num1, op, num2, eval(str(num1) + op + str(num2))) From 143068a05e8342932253291ee51db2924a6e1998 Mon Sep 17 00:00:00 2001 From: Nolan Knill Date: Fri, 13 Sep 2013 12:31:21 -0400 Subject: [PATCH 24/30] Delete binary_decimal.py --- Numbers/binary_decimal.py | 50 --------------------------------------- 1 file changed, 50 deletions(-) delete mode 100644 Numbers/binary_decimal.py diff --git a/Numbers/binary_decimal.py b/Numbers/binary_decimal.py deleted file mode 100644 index f378fb76f..000000000 --- a/Numbers/binary_decimal.py +++ /dev/null @@ -1,50 +0,0 @@ -""" -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): - """ - Converts a binary number into a decimal number. - """ - decimal = 0 - index = 0 - while binary > 0: - last = binary % 10 - binary = binary / 10 - decimal += (last * (2 ** index)) - index += 1 - return decimal - -def decimal_to_binary(decimal): - """ - Converts a decimal number into a binary number. - """ - binary = "" - remainders = [] - while decimal > 0: - remainders.append(str(decimal % 2)) - decimal /= 2 - remainders.reverse() - binary = "".join(remainders) - return binary - -if __name__ == '__main__': - print """ - 1. Binary to Decimal - 2. Decimal to Binary\n - """ - - choice = input("Make a choice: ") - - if choice == 1: - binary = input("Binary to convert: ") - print "The binary number %d in decimal is %d" % \ - (binary, binary_to_decimal(binary)) - elif choice == 2: - decimal = input("Decimal to convert: ") - print "The decimal number %d in binary is %s" % \ - (decimal, decimal_to_binary(decimal)) - else: - print "Invalid choice" From e37961231e20094b775321532171cfc09de801d0 Mon Sep 17 00:00:00 2001 From: Nolan Knill Date: Wed, 2 Oct 2013 16:13:33 -0400 Subject: [PATCH 25/30] Initialize reverse_string.php file --- Text/reverse_string.php | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 Text/reverse_string.php diff --git a/Text/reverse_string.php b/Text/reverse_string.php new file mode 100644 index 000000000..e69de29bb From 1c4d0a23407159ee85cc37a1ee07b807e6da63f1 Mon Sep 17 00:00:00 2001 From: Nolan Knill Date: Wed, 2 Oct 2013 16:17:56 -0400 Subject: [PATCH 26/30] Finished reverse a string --- Text/reverse_string.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Text/reverse_string.php b/Text/reverse_string.php index e69de29bb..2677537a2 100644 --- a/Text/reverse_string.php +++ b/Text/reverse_string.php @@ -0,0 +1,14 @@ +0; $i--){ + $final.=$test[$i]; +} +echo $final; +?> \ No newline at end of file From 0bf7538869bc0c6c2e95bdcdb2c87e43ef6d351c Mon Sep 17 00:00:00 2001 From: Nolan Knill Date: Wed, 2 Oct 2013 16:39:51 -0400 Subject: [PATCH 27/30] Add base inventory and product classes. Inventory::printInventory() prints all products, Product class has set and get overloading --- Classes/product_inventory.php | 58 +++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 Classes/product_inventory.php diff --git a/Classes/product_inventory.php b/Classes/product_inventory.php new file mode 100644 index 000000000..562b9440a --- /dev/null +++ b/Classes/product_inventory.php @@ -0,0 +1,58 @@ +id = $id; + $this->name = $name; + $this->cost = $cost; + $this->quantity = $quantity; + } + + public function __set($property, $value){ + if (property_exists(Product, $property)){ + $this->$property = $value; + } + } + + public function __get($property){ + if (property_exists(Product, $property)){ + return $this->$property; + } + } +} + +class Inventory{ + private $products = array(); + + function __construct($products){ + $this->products = $products; + } + + public function printInventory(){ + foreach ($this->products as $product){ + echo "\n\nProduct id: ".$product->id."\nName: ".$product->name."\nCost: $".$product->cost."\nQuantity: ".$product->quantity."\n\n"; + } + } + +} + + +$products[] = new Product(12, "First", 30.25, 14); +$products[] = new Product(12, "First", 30.25, 14); +$products[] = new Product(12, "First", 30.25, 14); +$products[] = new Product(12, "First", 30.25, 14); +$products[] = new Product(12, "First", 30.25, 14); +$products[] = new Product(12, "First", 30.25, 14); +$products[] = new Product(12, "First", 30.25, 14); +$inv = new Inventory($products); +$inv->printInventory(); From 2bb4f6cc8a63d401238bc1e10e845c3b8ffc731c Mon Sep 17 00:00:00 2001 From: Nolan Knill Date: Sun, 24 Nov 2013 21:24:23 -0500 Subject: [PATCH 28/30] Classic Algorithms/Collatz Conjecture complete --- .../CollatzConjecture.class.php | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Classic Algorithms/CollatzConjecture.class.php diff --git a/Classic Algorithms/CollatzConjecture.class.php b/Classic Algorithms/CollatzConjecture.class.php new file mode 100644 index 000000000..30c6f123d --- /dev/null +++ b/Classic Algorithms/CollatzConjecture.class.php @@ -0,0 +1,32 @@ +n = intval($number); + } + + public function calculateSteps(){ + while ($this->n != 1){ + if($this->n % 2 == 0){ + $this->n = $this->n / 2; + } + else{ + $this->n = $this->n * 3 + 1; + } + $this->numberOfSteps += 1; + } + } + + public function getNumberOfSteps(){ + return $this->numberOfSteps; + } +} + +$x = new CollatzConjecture(74); +$x->calculateSteps(); +echo $x->getNumberOfSteps(); \ No newline at end of file From 3fb28fe71bfd2c52c5c237b0ae4e44796463418c Mon Sep 17 00:00:00 2001 From: Nolan Knill Date: Sun, 24 Nov 2013 21:24:43 -0500 Subject: [PATCH 29/30] Classic Algorithms/Collatz Conjecture complete --- Classic Algorithms/CollatzConjecture.class.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/Classic Algorithms/CollatzConjecture.class.php b/Classic Algorithms/CollatzConjecture.class.php index 30c6f123d..99485031b 100644 --- a/Classic Algorithms/CollatzConjecture.class.php +++ b/Classic Algorithms/CollatzConjecture.class.php @@ -25,8 +25,4 @@ public function calculateSteps(){ public function getNumberOfSteps(){ return $this->numberOfSteps; } -} - -$x = new CollatzConjecture(74); -$x->calculateSteps(); -echo $x->getNumberOfSteps(); \ No newline at end of file +} \ No newline at end of file From 073a09163cb79336b1e88469f60711e6c9c9b7dd Mon Sep 17 00:00:00 2001 From: Nolan Knill Date: Sun, 24 Nov 2013 21:44:35 -0500 Subject: [PATCH 30/30] Sort parent class added. MergeSot and BubbleSort classes added with sort() methods incomplete --- Classic Algorithms/Sort.class.php | 36 +++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 Classic Algorithms/Sort.class.php diff --git a/Classic Algorithms/Sort.class.php b/Classic Algorithms/Sort.class.php new file mode 100644 index 000000000..53cd3582c --- /dev/null +++ b/Classic Algorithms/Sort.class.php @@ -0,0 +1,36 @@ +set = $set; + else + throw new Exception("Set is not an array"); + } + + public function getSortedSet(){ + return $this->sortedSet; + } +} + +class MergeSort extends Sort{ + public function sort(){ + foreach ($set as $number){ + $this->sortedSet[] = $number; + } + } +} + +class BubbleSort extends Sort{ + public function sort(){ + + } +} + + +$set = array(31,16,221,55,8,34,41,19,130,911,133,222,1,55,10); +$merge = new MergeSort($set); +$merge->sort(); +print_r($merge->getSortedSet()); \ No newline at end of file