Last active
December 16, 2015 08:39
-
-
Save PyPool/5407180 to your computer and use it in GitHub Desktop.
Three short scripts produced by the attendees of #PyPool (Python meet-up in Liverpool, UK). These three pieces of code were used as a guide / introduction to Python for those new to the programming language to assist them in grasping the foundations of the language and syntax
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
#!/usr/bin/env python | |
# By David, Jonathan, David, and Zarino | |
import sys | |
names = sys.argv | |
score = 0.0 | |
p1 = names[1].lower() | |
p2 = names[2].lower() | |
# People with "x" in their name are awesome | |
if "x" in p1 or "x" in p2: | |
score += 1 | |
# Same length means compatibility | |
if len(p1) == len(p2): | |
score += 1 | |
# Number of common chars | |
for c in p1: | |
if c in p2: | |
score += 2 | |
# Vowels are cool | |
vowels = 'aeiou' | |
for c in vowels: | |
if c in p1 and c in p2: | |
score += 1 | |
# No, really, we like vowels | |
def vowelscheck(p): | |
vowelsmissing = 0 | |
for c in vowels: | |
if c not in p: | |
vowelsmissing += 0.2 | |
return vowelsmissing | |
score -= vowelscheck(p1) | |
score -= vowelscheck(p2) | |
# The perfect score | |
maximum = len(p1) + len(p2) | |
# Your score, loser | |
percent = score / maximum * 100 | |
print "%s's compatibility with %s is %.1f%%!" % (names[1],names[2],percent) | |
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
#!/usr/bin/env python | |
# coding: utf-8 | |
# lovematch asks for two names and determines their compatibility | |
# IPO imminent | |
import sys | |
VOWELS = "aeiou" | |
def get_score_for(person): | |
score = 0 | |
for letter in person.lower(): | |
if letter in VOWELS: | |
score = score + 1 | |
return score | |
# return sum(1 for x in person.lower() if x in VOWELS) | |
def print_separator(): | |
print "=" * 80, "" | |
def perform_love_match(): | |
arg_count = len(sys.argv) | |
first_person = "" | |
first_score = 0 | |
second_person = "" | |
second_score = 0 | |
# Get the name of the potential lovers | |
first_person = sys.argv[1] if arg_count >= 2 else None | |
second_person = sys.argv[2] if arg_count >= 3 else None | |
while not first_person: | |
print "Enter the first name:" | |
first_person = raw_input() | |
while not second_person: | |
print "Enter the second name:" | |
second_person = raw_input() | |
print_separator() | |
# Calculate the love scores | |
print "Calculating scores for %s and %s" %\ | |
(first_person, second_person,) | |
print_separator() | |
first_score = get_score_for(first_person) | |
second_score = get_score_for(second_person) | |
# Calculate how long they'll be together | |
duration = len(first_person) + len(second_person) | |
period = "years" | |
if first_score == second_score: | |
print " ♥ ♥ ♥ A PERFECT match ♥ ♥ ♥ " | |
duration = duration * 2 | |
elif first_score > second_score: | |
print "%s loves %s more than the other way around" % \ | |
(first_person, second_person) | |
duration = duration / 2 | |
period = "weeks" | |
else: | |
print "%s loves %s more than the other way around" % \ | |
(second_person, first_person) | |
duration = duration / 2 | |
period = "weeks" | |
print "It is estimated that %s and %s will be together for %d %s!!!" % \ | |
(first_person, second_person, duration, period) | |
print "Post to FACEBOOK?!?! [y/n]" | |
if raw_input().lower() == 'y': | |
print "Posted results to FACEBOOK!!!!" | |
print "" | |
if __name__ == "__main__": | |
perform_love_match() |
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
digits = ('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine') | |
teens = ('ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen') | |
tens= ('twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety') | |
units = ('thousand', 'million', 'billion') | |
numbers = [1, 5, 17, 12, 10, 23, 46, 78, 123, 400, 703, 999] | |
for number in numbers: | |
number_string = str(number) | |
sub_str = number_string[-3:] | |
lower_digits = int(sub_str[-2:]) | |
final_string = '' | |
if len(sub_str) == 3: | |
hundreds = int(sub_str[0]) | |
final_string = digits[hundreds - 1] | |
final_string += ' hundred' | |
if lower_digits > 0: | |
if len(final_string) > 0: | |
final_string += ' and ' | |
if lower_digits < 10: | |
final_string += digits[lower_digits - 1] | |
elif lower_digits < 20: | |
final_string += teens[lower_digits - 10] | |
else: | |
ten_num, unit_num = divmod(lower_digits, 10) | |
final_string += tens[ten_num - 2] | |
if unit_num > 0: | |
final_string += ' ' + digits[unit_num - 1] | |
print(final_string) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Wasn't able to make it to the meetup but having a Python group in Liverpool is really cool and I hope to make future ones!
Not sure if this came up but if you've worked with sets before, Python has nice implementations of basic set operations which could have been used in love.py: