Fairly new to Python, and I have been doing a few Edabit challenges, to better help with my problem-solving. I have just completed a some what semi-difficult challenge, and I was hoping for some feed back.
The challenge itself:
Create a function that takes an imgur link (as a string) and extracts the unique id and type. Return an object containing the unique id, and a string indicating what type of link it is.
The link could be pointing to:
- An album (e.g. https://i.stack.imgur.com/6g7o8.jpg)
- A gallery (e.g. https://i.stack.imgur.com/IUcDq.jpg)
- An image (e.g. https://i.stack.imgur.com/bY3Z9.jpg)
- An image (direct link) (e.g. http://i.imgur.com/altd8Ld.png)
Examples
- "https://i.stack.imgur.com/6g7o8.jpg" ➞ { id: "cjh4E", type: "album" }
- "https://i.stack.imgur.com/IUcDq.jpg" ➞ { id: "59npG", type: "gallery" }
- "http://i.imgur.com/altd8Ld.png" ➞ { id: "altd8Ld", type: "image" }
I came up with the following.
import re
def imgurUrlParser(url):
url_regex = "^[http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/|www\.]*[imgur|i.imgur]*\.com"
url = re.match(url_regex, url).string
gallery_regex = re.match(url_regex + "(\/gallery\/)(\w+)", url)
album_regex = re.match(url_regex + "(\/a\/)(\w+)", url)
image_regex = re.match(url_regex + "\/(\w+)", url)
direct_link_regex = re.match(url_regex + "(\w+)(\.\w+)", url)
if gallery_regex:
return { "id" : gallery_regex.group(2), "type" : "gallery" }
elif album_regex:
return { "id" : album_regex.group(2), "type" : "album" }
elif image_regex:
return { "id" : image_regex.group(1), "type" : "image" }
elif direct_link_regex:
return { "id" : direct_link_regex.group(1), "type" : "image"}
direct_link_regex
is a string and strings don't have agroup()
method. Also, the parentheses for that regex are not balanced properly. Perhaps you copied an old version of your code? \$\endgroup\$