Python firstname on left lastnames on right Given 2 files be
Python firstname on left, lastnames on right
Given 2 files below, one with first names and one with last names. Ask the user to enter a first name and last name. If the first name and last name are found in their separate files print name found. Name is only found if the first name and last name are on the same line in each file. For example, if the user enters first name: Michael, last name: Garrett, there should be a print message saying name found. If the user enters first name: Michael and last name: Saunders, name is not found.
Solution
import string
import math
def is_prime(n):
if n == 1:
return False
f = n/2
while f > 1:
if n%f == 0:
return False
break
f -= 1
else:
return True
def getfactors(n):
return [x for x in range(1,n+1) if n%x == 0]
def get_prime_factors(n):
f = [x for x in getfactors(n) if is_prime(x) or x == 1]
while True:
m = reduce(lambda x, y: x*y, f)
q = n/m
if q == 1:
break
else:
f.extend(get_prime_factors(q))
return sorted(f)
def isperfect(n):
factors = filter(lambda x: x != n, getfactors(n))
if sum(factors) == n:
return 1
else:
return 0
def factorial(n):
return 1 if n == 1 else n * factorial(n-1)
def fibonacci(n):
c = 0
f = [1]
while c < n-1:
c_1 = f[-1]
c_2 = 0 if len(f) <= 1 else f[-2]
f.append(c_1 + c_2)
c += 1
return f[-1]
def text_processor(sentence):
v = [\'a\', \'e\', \'i\', \'o\', \'u\']
words = sentence.split(\" \")
vowels = [s for s in sentence if s in v]
c = [s for s in list(string.letters) if s not in v]
consonants = [s for s in \"\".join(words) if s in c]
result = {
\"words\": len(words),
\"vowels\": len(vowels),
\"consonants\": len(consonants)
}
return result
class ContactBook(object):
def __init__(self):
self.mistakes = 0
self.contacts = []
def __repr__(self):
return \"\ \" + \"\ \".join(sorted(self.contacts))
def take_input(self):
name = raw_input(\"Please enter name #%d > \" % len(self.contacts))
if len(name.split(\',\')) == 1:
self.mistakes += 1
print \"Wrong Format... Should be LastName, FirstName\"
print \"You have done this %d time(s) already! Fixing name..\" % self.mistakes
first, last = name.split(\' \')
name = \"%s, %s\" % (last, first)
self.contacts.append(name)
@staticmethod
def run():
print \"Please enter names in format Lastname, Firstname\"
cb = ContactBook()
another = True
while another:
cb.take_input()
ask_another = raw_input(\"Add another? (yes/no) > \")
another = False if ask_another == \'no\' else True
print cb
def bit_table(start, to):
cols = (\'DEC\', \'BIN\', \'OCT\', \'HEX\')
data = [(str(n), bin(n), hex(n), oct(n)) for n in range(start, to+1)]
width = 12.0
def ml(content):
return int(math.ceil(width - len(content))/2)
def mr(content):
return int((width - len(c))/2)
head_row = \'\'.join([ml(c) * \' \' + c + mr(c) * \' \' for c in cols])
print head_row
dash = (len(head_row)+4) * \'-\'
print dash
for d in data:
row = [ml(s) * \' \' + s + mr(s) * \' \' for s in d]
print \'\'.join(row)
print dash
if __name__ == \'__main__\':
bit_table(9, 36)
pass




