7
Oct

Final Quiz Solution – Intro to Computer Science


So here’s the answer. What we have to do is find these two double quotes. So we know that we want to start looking from start_link, we can’t start from the beginning of page. So we want to use find on page, passing in the double quote, which is what we’re looking for, starting from the start_link position. So here’s how we need to do that, we’re going to initialize the variable, start_quote, which is where that double quote that starts the url is. And what we want to do is use page.find, to find in page. The string that we want to find is that double quote. To make a double quote as a string, we need to use a single quote, followed by a double quote, followed by a single quote. We also need to pass in a second parameter. We don’t want to find the first double quote in the page, because that might not be part of a link tag. So we also need to pass in the variable start_link. So that makes find start from the position of start_link look for the double quote, and it will return the location where that start quote appears. We also need to find the end quote. We’ll call that end_quote. To find the end_quote we don’t want to start from the start_link. If we started looking from the start_link we’d find the start_quote again. To find the end_quote we need to look starting from after the start_quote. So we’re going to use page.find again. Again looking for a double quote, so passing in the double quote as the string we’re searching for. We need to start from the position of start_quote. We can’t start from just the position of start_quote. If we started from start_quote, we’d find just the start_quote itself. We knew to start one after that position, so we need to add 1 to start_quote. And if we start looking from there, we’ll find the first quote after that. That’s the quote that ends the string. So the final thing we want to do, is set the variable url. So we’ll initialize url, to the string that we find between the start quote and the end quote. And so we can do that using the string selection. We’re selecting from the string page. We have to think carefully about where we start. We don’t want to start with a start_quote because that would include the double quote in the url. We just want the url not including the double quote, so we’re going to start with the start_quote plus 1. And we’re going to go from there until the end_quote. We don’t want to include the closing double-quote. We don’t include that one in the string. So we don’t need to subtract 1 from the end_quote. And that will get us the url, extracted from the first link on the web page.

Tags: , , , , , , ,

12 Comments

  • Juan Cantor says:

    This was my answer, a single line

    url = page[(page[(page.find('<a href=')):].find('"')+(page.find('<a href=')))+1:(page[(page[(page.find('<a href=')):].find('"')+(page.find('<a href=')))+1:].find('"')+(page[(page.find('<a href=')):].find('"')+(page.find('<a href=')))+1)]

  • Philippe Tremblay says:

    This is how I did it: 
    http://pastebin.com/qS2Z9Fg0

  • Pow Ranger says:

    Startquote? Endquote? Please be more thorough. The foundation is something i need to understand this stuff. First timer here.

  • daddyhughes111 says:

    I used .replace ¯(ツ)

  • El Wachin says:

    I did it like this:
    start_link = page.find('http')
    final = page.find('""', 99)
    url = page[start_link:final – 1]
    print url

  • VertigoFree says:

    no way will first timers know about start_quote….is this course catered for absolute newbies to programming? or to people who've been stealing school computers just so they could take them apart ? Seriously I really want to learn computer science and programming but I am a complete newbie and this whole course i have been paying attention listening taking notes and the final quiz just bitch slaps me with dave saying ' oh ya gotta use this thing I never told ya about'. If this is the first lesson I have no interest in whats to come because I already see what this course is….cheaply assembled patchwork of crap.

  • Lightning Wave says:

    Can someone write the answer ? Thanks

  • Demilade Olaleye says:

    My answer:
    start_link = page.find('<a href=')
    end_link = page.find('>',89)
    #This finds the first closing bracket (>) after the start_link, had to use find with numbers as we already know starts from 89 or unless you would find any closing bracket (>) that comes before start_link
    end_link = end_link – 1
    #To ensure it ends at .com and not at the closing bracket (>), you might ask why not just change the line above to " instead of > but this is better practice as not every a href uses quotes
    url = page[start_link+9:end_link]
    # The '+9' is the position of the first 'h' from http
    print url

  • Ace says:

    Here’s how i did it with one code url=(page[page.find(‘ “ ‘,start_link)+1:page.find(‘ “ ‘,page.find(‘ “ ‘,start_link)+1)]) im such a nerd lol

  • Pointl Ess says:

    To be honest, the question is very ambiguous. I don't really know what is being asked so its hard to even start this question. All the concepts seems really easy to comprehend, its just the question that is tripping me up. I only get whats being asked once I actually see the guy's answer.

  • Ray Gun says:

    i didnt even come close

  • young boss says:

    can someone please explain to me why end_quote doesnt print out the ' " ' String??i thought u would have to subtract 1 from end_quote so it prints out just the link without the quotation mark?

Leave a Reply

Your email address will not be published. Required fields are marked *