15-Section-Handout - CS106X Autumn 2010 Handout 15 October...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS106X Handout 15 Autumn 2010 October 4 th , 2010 Section Handout Problem 1: URL Parameter Map A URL has many components. a protocol, a domain, and a file path are almost always included. Sometimes the URL includes a query , which the portion of the URL that appears after the ? and (if present) before the # (which defines the URL’s hash). Here are some examples: http://www.google.com/ig?hl=en&source=iglk http://www.facebook.com/profile.php?id=1160&viewas=214707 http://store.apple.com/us/browse/family/iphone?mco=MTAyNTM5ODU And while you may recognize the ? as a common character within URLs, you may not realize that the part following the ? is the serialization of a Map<string> . The query string is an & - delimited string of key-value pairs, where each key-value pair takes the form <key>=<value> . When a web server gets an HTTP request, it digests the query string and re-hydrates it into Map<string> form, and uses that map to programmatically shape how the response page is generated. That’s why www.google.com/ig?hl=en&source=iglk generates English, and www.google.com/ig?hl=fr&source=iglk generates French. One little wrinkle: the value is technically optional, so that query strings like type=5&seeall and source_id=9074&read= are legitimate. When a key is present without a value, then the value is arbitrary and the presence of the key is the only thing of interest. In such cases, the Map<string> should include the key and attach an arbitrary value to it. Write a function that takes a legitimate URL, extracts the query string, and returns a Map<string> with all of its key-value pairs. You can safely assume that the URL is properly formatted, and that the structure of an URL is no more complicated than what’s described here. Make sure you deal with URLs that don’t include a query string, and URLs that include a hash. Map<string> extractQueryMap(string url); This problem is as much about string manipulation as it is about the Map , so don’t be surprised if you’re using some advanced string methods that haven’t come up in any previous examples. Problem 2: Publishing Stories Social networking sites like Facebook, LinkedIn, Orkut, Friendster, and MySpace typically record and publish stories about actions taken by you and your friends. Stories such as:
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 John Dixon accepted your friend request. Jeff Barbose is no longer in a relationship. Scott James wrote a note called "The Two Percent Solution". Arlene Heitner commented on Melodie Bowsher’s video. Antonio Melara gave The French Laundry a 5-star review. are created from story templates like {name} accepted your friend request. {name} is no longer in a relationship. {name} wrote a note called "{title}". {name} commented on {target}’s video. {actor} gave {restaurant} a {rating}-star review. The specific story is generated from the skeletal one by replacing the tokens—substrings like "{name}" , "{title}" , and "{rating}" —with event-specific values, like "John Dixon" , "The Two Percent Solution" , and "5" . The token-value pairs can be packaged in a Map<string> , and given a story template and a data map, it’s possible to generate an actual story.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/13/2011 for the course CS 106X taught by Professor Cain,g during the Fall '08 term at Stanford.

Page1 / 8

15-Section-Handout - CS106X Autumn 2010 Handout 15 October...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online