Midterm Exam 2 Review Spring 2003: Address book functions
Comments? Concerns? Answers? Questions? Questions about Answers?
Back to Sp2003 Midterm Review 2
def lookup(name):
addressFile= "address.txt"
file = open (addressFile, "rt")
address = file.read()
file.close()
nameloc = address.find("name")
if nameloc != -1:
namebegin = address.rfind("\n", nameloc)
endaddress = address.find("\n", nameloc)
print address[namebegin+1:endaddress]
if nameloc == -1:
print "Not found"
def phone(name):
addressFile = "address.txt"
file = open (addressFile, "rt")
address = file.read()
file.close()
nameloc = address.find("name"
if nameloc != -1:
phonebegin = address.find("GA:", nameloc)
phoneend = address.find("\n", nameloc)
print address[phonebegin+1:phoneend]
if nameloc == -1
print "Not found"
This is what I got. I'm not sure about the part where I get the address file, or about finding the beginning of the phone number.
Katie Graybeal
| I'd probably count colons rather require the addresses to be in Georgia. Mark Guzdial |
Does anybody know how to count the colons? Katie Graybeal
| Find the end of the line '\n', then rfind each colon. Mark Guzdial |
def phone(name):
addressFile = "address.txt"
file = open (addressFile, "rt")
address = file.read()
file.close()
nameloc = address.find("name")
if nameloc != -1:
phonebegin = address.rfind("\n",?:?, nameloc)
phoneend = address.find("\n", nameloc)
print address[phonebegin+1:phoneend]
if nameloc == -1
print "Not found"
How about this?
| I think you'll find that, if you try it, this won't work. rfind takes three inputs: A string to search for, a starting position, and an ending position. Look at the examples from lecture. Mark Guzdial |
I looked at the lecture stuff. I thought that I was specifying a string, a start and an end when I put rfind("/n",":",nameloc). What am I missing?
| Try it! Look at the rfind examples. The 2nd and 3rd inputs must be numbers. Mark Guzdial |
>>> 'abcabc'.rfind('a',0,6)
3
| Notice that the result isn't 0. It's not finding the first a. It's finding the second one, the one that's at position 3. Mark Guzdial |
so this?
def phone(name):
addressFile = "address.txt"
file = open (addressFile, "rt")
address = file.read()
file.close()
nameloc = address.find("name")
if nameloc != -1:
phonebegin = address.rfind(":",0,12)
phoneend = address.find("\n", nameloc)
print address[phonebegin+1:phoneend]
if nameloc == -1
print "Not found"
| Do you really want to hardcode 0,12? The phone number won't always be on the first line. Mark Guzdial |
I don't understand!!! does that mean that it should be (":", 0, "\n")? No idea at this point.
hmph... I don't get this one
def phone(name):
addressFile = "address.txt"
file = open (addressFile, "rt")
address = file.read()
file.close()
nameloc = address.find("name")
if nameloc != -1:
phoneend = address.find("\n", nameloc)
phonebegin = address.rfind(":", phoneend, 0 )
print address[phonebegin+1:phoneend]
if nameloc == -1:
print "Not found"
This is my guess.
def lookup(string):
lowerstring = string.lower()
file=open("address.txt","rt")
complete=file.readlines()
listings = 0
for entry in complete:
entrylower = entry.lower()
colonloc = entry.find(":")
nameloc = entrylower.find(lowerstring,0,colonloc)
if nameloc <> -1:
listings = listings + 1
print entry
if listings == 0:
print "Not found"
file.close()
Here is what I got for the first function. Scott Forbus
def phone(string):
lowerstring = string.lower()
file=open("address.txt","rt")
complete=file.readlines()
listings = 0
for entry in complete:
entrylower = entry.lower()
colonloc = entry.find(":")
nameloc = entrylower.find(lowerstring,0,colonloc)
if nameloc <> -1:
phonebegin = entry.rfind(":") + 1
phoneno= entry[phonebegin:len(entry)-1]
print phoneno
listings = listings + 1
if listings == 0:
print "Not found"
file.close()
Here is what I got for the second function Scott Forbus
| Interesting solution! You're basically checking each line, rather than searching overall. Nice! Mark Guzdial |
From recitation:
Phone Number:
def phone(string):
file = open("addresses.txt")
addresses = file.read()
file.close()
lines = addresses.split("\n")
numFound = 0
for line in lines:
lineData = line.split(":")
found = lineData[0].find(string)
if found != -1:
print lineData[3]
numFound = numFound + 1
if numFound == 0:
print "Not Found"
That was Adam's phone solution he worked on in recitation.
| It's similar to Scott's approach. Adam breaks it up into pieces using "split" which makes it easier still. Mark Guzdial |
Here is the lookup function that Jim Gruen worked on in recitation:
def lookup(partOfName):
addressFile = open("address.txt", "rt")
addresses = addressFile.read()
addressFile.close()
numberOfAddressesFound = 0
listOfAddresses = addresses.split("\n")
for currentAddress in listOfAddresses:
location = currentAddress.find(partOfName)
if location != -1: # we found a matching address
print currentAddress
numberOfAddressesFound = numberOfAddressesFound + 1
if numberOfAddressesFound == 0:
print "Not found"
| Does this one actually print just the addresses? There is still a solution using find and rfind. Here's a suggestion: Assume that there's a character at the START of each line, like an asterisk or a dash. Does that make it any easier? Mark Guzdial |
<>
Scott,why did you include the lowercase function in your code?
I included that because I do not like to have case sensative strings. Its a matter of personal preference. If you do not have this and you search for "charlie", it will give you nothing; you would have to have it "Charlie" (as I tested, I found that it is case sensative).
Scott, how did you test this? Where did you make a text file to test this on?
also, why didn't you close your file?
You can close it. I didn't put it in that file, but I have since updated mine to say that. Sorry bout that. I just copied the examples that the Professor gave into a file called address.txt in the JES directory. Scott Forbus
From the Function that Jim did in recitation wouldn't you need a function that made sure it only took from the name part so you wouldn't get a address as a return and can you do that by adding
lineData = line.split(":")
found = lineData[0].find(string)
and then telling it to print all 3 sections when it returns???
Alright, here is my attempt, everyone please let me know how it looks! Thanks! Brittany Selden
def lookup(string)
file = open("address.txt","rt")
addresses = file.readlines()
file.close()
addressListing = 0
for entry in addresses:
colonlocation = entry.find(":")
namelocation = entry.find("string",0,colonlocation)
if namelocation <> -1:
addressListing = addressListing + 1
print entry
if addressListing == 0:
print "No Addresses Found"
| VERY close, Brittany! You forgot the colon on line 1, and you don't want string in quotes – you want the VALUE of string (e.g., "Charlie") not the string "string". I changed your program (below) and it works fine. Mark Guzdial |
def lookup(string):
file = open("address.txt","rt")
addresses = file.readlines()
file.close()
addressListing = 0
for entry in addresses:
colonlocation = entry.find(":")
namelocation = entry.find(string,0,colonlocation)
if namelocation <> -1:
addressListing = addressListing + 1
print entry
if addressListing == 0:
print "No Addresses Found"
I know the test has already been made out, but Dr. Guzdial, I beg of you, if there is any way possible to not inclue this part, I would be eternally greatful to you. I have worked on it for so long, and still just can not seem to understand it to the point I could create my own.
What does this mean?
TypeError: slice index must be int
| The [:] stuff are called "slices". The numbers in there must be integers. Mark Guzdial |
Is it important that our solution doesn't find the #? Mine printed this:
Found in #Charlie Brown:1919 Peanuts Lane:Atlanta, GA:404-992-9292
Can we assume that for the final it will have the # sign in front or will it be like the first one for the midterm?
This is my code that found the above.
def lookup(string):
addressFile=getMediaPath("address.txt")
file=open(addressFile,"rt")
lookup=file.read()
file.close()
lookuploc=lookup.find(string)
if lookuploc!=-1:
loc=lookup.rfind("#",0,lookuploc)
floc=lookup.find(":",loc)
sloc=lookup.find(":",floc)
tloc=lookup.find(":",sloc)
endline=lookup.find("#",tloc)
print "Found in ",lookup[loc:endline]
if lookuploc==-1:
print "Not found."
| There's also a way to use rfind to find the ":" without doing three .finds. Try the phone() one, too. Mark Guzdial |
I'm working on that one. But I can't get it to print what's between the last colon and the # sign of the next one.
New version that eliminates the #
def lookup(string):
addressFile=getMediaPath("address.txt")
file=open(addressFile,"rt")
lookup=file.read()
file.close()
lookuploc=lookup.find(string)
if lookuploc!=-1:
loc=lookup.rfind("#",0,lookuploc)
floc=lookup.find(":",loc)
sloc=lookup.find(":",floc)
tloc=lookup.find(":",sloc)
endline=lookup.find("#",tloc)
print "Found in ",lookup[loc+1:endline]
if lookuploc==-1:
print "Not found."
This is the code for the midterm 2:
def contacts(key):
file=open("contacts.txt","rt")
contacts=file.read()
file.close()
keyloc=contacts.find(key)
if keyloc==-1:
print "Nothing Found"
if keyloc !=-1
conlonloc=contacts.rfind(":",0,keyloc)
beginloc=contacts.rfind(">",0,keyloc)
print contacts[beginloc+1:colonloc]
Bijal Nagrashna
This one works for finding the phone number and any given partial or whole name.
def phone(string):
addressFile=getMediaPath("address.txt")
file=open(addressFile,"rt")
lookup=file.read()
file.close()
lookuploc=lookup.find(string)
if lookuploc!=-1:
loc=lookup.rfind("#",0,lookuploc)
floc=lookup.find(":",loc)
sloc=lookup.find(":",floc+1)
tloc=lookup.find(":",sloc+1)
ftloc=lookup.find("#",tloc+1)
print "Found in, ",lookup[tloc+1:ftloc]
if lookuploc==-1:
print "Not Found."
Bijal, exactly what is yours finding?
There aren't any ">" in the question.
You have to use preformatted html tags.
ok if you had picked up your midterm #2... you would have noticed the last problem on the exam about writing a function called contacts. what i posted was the answer to the question on midterm #2 not the review but the actually test. =)
I did pickup midterm 2, but I had not gone back through everything. Also, this page wasn't for that, so how was I supposed to know.
Links to this Page