From e7588543ff084d4b416a49b93ac01590071a9ba6 Mon Sep 17 00:00:00 2001 From: Max Date: Thu, 25 Jul 2013 08:06:44 -0400 Subject: [PATCH 1/2] Erased solutions. --- Numbers/alarm.py | 49 ---------------------------- Numbers/binary_decimal.py | 50 ----------------------------- Numbers/calc.py | 22 ------------- Numbers/change.py | 43 ------------------------- Numbers/credit_card_validator.py | 41 ------------------------ Numbers/distance.py | 55 -------------------------------- Numbers/fibonacci.py | 15 --------- Numbers/next_prime.py | 25 --------------- Numbers/pi.py | 10 ------ Numbers/prime.py | 24 -------------- Numbers/tile.py | 11 ------- Text/count_vowels.py | 17 ---------- Text/count_words.py | 27 ---------------- Text/palindrome.py | 12 ------- Text/piglatin.py | 22 ------------- Text/reverse.py | 8 ----- 16 files changed, 431 deletions(-) delete mode 100644 Numbers/alarm.py delete mode 100644 Numbers/binary_decimal.py delete mode 100644 Numbers/calc.py delete mode 100644 Numbers/change.py delete mode 100644 Numbers/credit_card_validator.py delete mode 100644 Numbers/distance.py delete mode 100644 Numbers/fibonacci.py delete mode 100644 Numbers/next_prime.py delete mode 100644 Numbers/pi.py delete mode 100644 Numbers/prime.py delete mode 100644 Numbers/tile.py delete mode 100644 Text/count_vowels.py delete mode 100644 Text/count_words.py delete mode 100644 Text/palindrome.py delete mode 100644 Text/piglatin.py delete mode 100644 Text/reverse.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) - 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" diff --git a/Numbers/calc.py b/Numbers/calc.py deleted file mode 100644 index 3d8dd66a5..000000000 --- a/Numbers/calc.py +++ /dev/null @@ -1,22 +0,0 @@ -# -*- coding: cp1252 -*- -""" -Calculator � A simple calculator to do basic operators. -""" - -if __name__ == '__main__': - num1 = input("Number 1: ") - num2 = input("Number 2: ") - op = raw_input("Operation (+, -, /, *): ") - - if op not in '+-/*': - print "Invalid operator" - else: - if op == '+': - res = num1 + num2 - elif op == '-': - res = num1 - num2 - elif op == '/': - res = num1 / num2 - elif op == '*': - res = num1 * num2 - print "%d %s %d = %d" % (num1, op, num2, res) diff --git a/Numbers/change.py b/Numbers/change.py deleted file mode 100644 index 6d6f13cde..000000000 --- a/Numbers/change.py +++ /dev/null @@ -1,43 +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) - - # DEBUG - # print "Total change per the number of coins is %.2f" % \ - # ((q * .25) + (d * .10) + (n * 0.05) + (p * 0.01)) 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' diff --git a/Numbers/distance.py b/Numbers/distance.py deleted file mode 100644 index f0695eca0..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 (K = KM, M = 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!' \ No newline at end of file diff --git a/Numbers/fibonacci.py b/Numbers/fibonacci.py deleted file mode 100644 index 4e20a55a6..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 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 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))) diff --git a/Numbers/prime.py b/Numbers/prime.py deleted file mode 100644 index f711f0803..000000000 --- a/Numbers/prime.py +++ /dev/null @@ -1,24 +0,0 @@ -# Prime Factorization - Have the user enter a number -# and find all Prime Factors (if there are any) and -# display them. - -import math - -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): - if n % i == 0: - if is_a_prime(i): - factors.append(i) - n /= i - print factors 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) diff --git a/Text/count_vowels.py b/Text/count_vowels.py deleted file mode 100644 index 847764885..000000000 --- a/Text/count_vowels.py +++ /dev/null @@ -1,17 +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. -""" - -string = raw_input('Enter a string: ').lower() - -vowels = ['a', 'e', 'i', 'o', 'u'] -counts = dict(zip(vowels, [0, 0, 0, 0, 0])) - -for vowel in counts: - for char in string: - if vowel == char: - counts[vowel] += 1 - -print counts diff --git a/Text/count_words.py b/Text/count_words.py deleted file mode 100644 index a9ac636a7..000000000 --- a/Text/count_words.py +++ /dev/null @@ -1,27 +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 (i, (word, count)) in enumerate(sorted_counts): - if i < 5: - print (word, count) 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 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 diff --git a/Text/reverse.py b/Text/reverse.py deleted file mode 100644 index eb97aecf3..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 8aa8273a045968927b98147f7d36c2e6173cdef0 Mon Sep 17 00:00:00 2001 From: Max Date: Thu, 25 Jul 2013 23:02:53 -0400 Subject: [PATCH 2/2] Some initial Ruby solutions. --- Numbers/fibonacci.rb | 9 +++++++++ Text/check_palindrome.rb | 13 +++++++++++++ Text/pig_latin.rb | 20 ++++++++++++++++++++ Text/reverse_string.rb | 3 +++ Text/vowel_counter.rb | 27 +++++++++++++++++++++++++++ 5 files changed, 72 insertions(+) create mode 100644 Numbers/fibonacci.rb create mode 100644 Text/check_palindrome.rb create mode 100644 Text/pig_latin.rb create mode 100644 Text/reverse_string.rb create mode 100644 Text/vowel_counter.rb diff --git a/Numbers/fibonacci.rb b/Numbers/fibonacci.rb new file mode 100644 index 000000000..5a8f86523 --- /dev/null +++ b/Numbers/fibonacci.rb @@ -0,0 +1,9 @@ +def fib(key_num, num1 = 0, num2 = 1) + current = num1 + num2 + puts current + current == key_num ? return : fib(key_num, current, num1) +end + +print "Number to terminate Fibonacci sequence at: " +input = gets.strip.to_i +fib(input) \ No newline at end of file diff --git a/Text/check_palindrome.rb b/Text/check_palindrome.rb new file mode 100644 index 000000000..ab8c2f940 --- /dev/null +++ b/Text/check_palindrome.rb @@ -0,0 +1,13 @@ +class String + def palindrome? + self == self.reverse ? true : false + end +end + +print 'palindrome check: ' +input = gets.strip +if input.palindrome? + puts 'palindrome.' +else + puts 'not a palindrome.' +end \ No newline at end of file diff --git a/Text/pig_latin.rb b/Text/pig_latin.rb new file mode 100644 index 000000000..76d3da926 --- /dev/null +++ b/Text/pig_latin.rb @@ -0,0 +1,20 @@ +def pig(text) + text_arr = text.split + pig_text = text_arr.map { |word| + if /[[:punct:]]\A/.match(word) + word[0] + word[2..-1] + '-' + word[1] + 'ay' + elsif /[[:punct:]]\z/.match(word) + word[1..-2] + '-' + word[0] + 'ay' + word[-1] + elsif word.length > 2 + word[1..-1] + '-' + word[0] + 'ay' + else + word + '-ay' + end + } + return pig_text.join(' ') +end + +print 'Text: ' +input = gets.strip +output = pig(input) +puts output diff --git a/Text/reverse_string.rb b/Text/reverse_string.rb new file mode 100644 index 000000000..79188883a --- /dev/null +++ b/Text/reverse_string.rb @@ -0,0 +1,3 @@ +print "String to reverse: " +input = gets.strip +puts input.reverse \ No newline at end of file diff --git a/Text/vowel_counter.rb b/Text/vowel_counter.rb new file mode 100644 index 000000000..c273dd707 --- /dev/null +++ b/Text/vowel_counter.rb @@ -0,0 +1,27 @@ +require 'pp' + +def vowel_count(text) + + a = text.scan(/a/).length + e = text.scan(/e/).length + i = text.scan(/i/).length + o = text.scan(/o/).length + u = text.scan(/u/).length + + vowels = { + 'total' => (a+e+i+o+u), + 'a' => a, + 'e' => e, + 'i' => i, + 'o' => o, + 'u' => u + } + + return vowels +end + +print 'Count some vowels: ' +input = gets.strip +output = vowel_count(input) +puts output +