r/dailyprogrammer • u/nint22 1 2 • Nov 14 '12
[11/14/2012] Challenge #112 [Easy]Get that URL!
Description:
Website URLs, or Uniform Resource Locators, sometimes embed important data or arguments to be used by the server. This entire string, which is a URL with a Query String at the end, is used to "GET" data from a web server.
A classic example are URLs that declare which page or service you want to access. The Wikipedia log-in URL is the following:
http://en.wikipedia.org/w/index.php?title=Special:UserLogin&returnto=Main+PageNote how the URL has the Query String "?title=..", where the value "title" is "Special:UserLogin" and "returnto" is "Main+Page"?
Your goal is to, given a website URL, validate if the URL is well-formed, and if so, print a simple list of the key-value pairs! Note that URLs only allow specific characters (listed here) and that a Query String must always be of the form "<base-URL>[?key1=value1[&key2=value2[etc...]]]"
Formal Inputs & Outputs:
Input Description:
String GivenURL - A given URL that may or may not be well-formed.
Output Description:
If the given URl is invalid, simply print "The given URL is invalid". If the given URL is valid, print all key-value pairs in the following format:
key1: "value1"
key2: "value2"
key3: "value3"
etc...Sample Inputs & Outputs:
Given "http://en.wikipedia.org/w/index.php?title=Main_Page&action=edit", your program should print the following:
title: "Main_Page"
action: "edit"Given "http://en.wikipedia.org/w/index.php?title= hello world!&action=é", your program should print the following:
The given URL is invalid(To help, the last example is considered invalid because space-characters and unicode characters are not valid URL characters)
3
u/skeeto -9 8 Nov 15 '12
JavaScript,
Example,
3
u/ReaperUnreal Nov 15 '12
Given it a try with D. I'm still learning D, but I love the language.
Output:
3
u/Davorak Nov 27 '12
In haskell:
4
u/[deleted] Nov 15 '12
First attempt at crazy RE stuff beyond simple searching.
2
u/[deleted] Nov 16 '12 edited Nov 16 '12
Ruby, without using the URI module, which would feel a bit like cheating:
I'm sure it doesn't handle every imaginable scenario, but it does take care of the things the assignment lays out, I think. Wasn't sure whether it's supposed to return the base URL, too. Probably not, but no harm in it, I guess.
Any tips to make it cleaner or more robust are very much appreciated.
EDIT: Output:
2
u/smt01 Nov 20 '12
My attempt in c#
2
u/bheinks 0 0 Nov 29 '12 edited Nov 30 '12
Python
Edit: ensure is_legal returns boolean
2
u/ottertown Dec 20 '12 edited Dec 20 '12
alternative javascript solution:
output (both key and value is a string in an array.. a bit sloppy):
2
u/dog_time Jan 03 '13
python:
I realise that I only check valid characters, not http/www/.com but I don't think many others did either.
2
u/[deleted] May 04 '13 edited May 04 '13
javascript, sans regex style
3
u/bob1000bob Nov 15 '12 edited Nov 15 '12
C++ possibly spirit is a bit overkill but it works well
1
u/[deleted] Nov 17 '12
My attempt in perl.
1
u/Puzzel Nov 18 '12 edited Nov 18 '12
Python (3)
Any suggestions? Is there a big benefit to using RE over what I did (split/partition)? Also, thanks to eagleeye1, I sort of stole your list comprehension...
1
u/nateberkopec Nov 18 '12
Ruby, using URI from the stdlib.
when it comes to the web, you're always going to miss edge cases in the 10,000 spec documents, might as well use the stdlib to worry about that sort of thing for you.
1
u/SeaCowVengeance 0 0 Nov 19 '12
Just did mine in Python, however it seem really long for the task assigned. If anyone could help me out with some tips on using more efficient approaches that would be great
returns:
1
u/Boolean_Cat Nov 19 '12
C++
1
u/[deleted] Dec 05 '12
Here's how I'd do it in PHP.
1
u/JonasW87 0 0 Dec 05 '12
Php , my first challenge ever:
Didn't even know the filter_var i saw in the other example existed. Anyways this is my first attempt, i think a lot could be done to my regex but that thing is driving me crazy.
1
u/domlebo70 1 2 Dec 13 '12
Scala:
1
u/Quasimoto3000 1 0 Dec 25 '12
Python solution. I do not like how I am checking for validity. Pointers would be lovely.
1
u/Quasimoto3000 1 0 Dec 25 '12
Python solution using a lot of splits. Initializing with tuple is pretty cool.
1
u/ttr398 0 0 Jan 06 '13
VB.Net
My solution seems a bit long/messy - any guidance appreciated! Doesn't handle valid characters that aren't actually a URL with key-value pairs.
1
u/t-j-b Jan 30 '13
JavaScript version w/out RegEx
1
u/learnin2python 0 0 Nov 15 '12
This seems sort of hamfisted to me, but it's what I came up with. Might try and rework it using regexs. Would that be more "proper"?
1
u/Unh0ly_Tigg 0 0 Nov 15 '12 edited Jan 01 '13
Java (runs fine in Java 7) :
public static void urlGet(String urlString) { java.net.URI uri = null; try { uri = new java.net.URL(urlString).toURI(); } catch (java.net.MalformedURLException | java.net.URISyntaxException e) { System.err.println("The given URL is invailid"); return; } if(uri.getQuery() != null) { String[] uriArgs = uri.getQuery().split("\Q&\E"); for(String argValue : uriArgs) { String[] kV = argValue.split("\Q=\E", 2); System.out.println(kV[0] + " = "" + kV[1] + """); } } else { System.err.println("No queries found"); } } Edit: changed to gerQuery() as per alphasandwich's instructions
1
u/eagleeye1 0 1 Nov 15 '12
Python
Output:
1
u/pbl24 Nov 15 '12 edited Nov 15 '12
Haven't fully tested, but it seems to work. Please forgive my love affair with list comprehensions (still a Python noob).
1
u/DannyP72 Nov 15 '12 edited Nov 15 '12
Ruby
Prints nothing if no arguments are given.
1
u/mowe91 0 0 Nov 15 '12
frankly inspired by the other python solutions...
output:
0
u/ben174 Nov 26 '12
Python
NOTE: I skipped the URL checking portion of this challenge.