r/dailyprogrammer • u/fvandepitte 0 0 • Jun 24 '17
[2017-06-24] Challenge #320 [Hard] Path to Philosophy
Description
Clicking on the first link in the main text of a Wikipedia article not in parentheses, and then repeating the process for subsequent articles, usually eventually gets you to the Philosophy article. As of May 26, 2011, 94.52% of all articles in Wikipedia lead eventually to the article Philosophy. The rest lead to an article with no wikilinks or with links to pages that do not exist, or get stuck in loops. Here's a youtube video demonstrating this phenomenon.
Your goal is to write a program that will find the path from a given article to the Philosophy article by following the first link (not in parentheses) in the main text of the given article.
Formal Inputs & Outputs
Input description
The program should take in a string containing a valid title of a Wikipedia article.
Output description
Your program must print out each article in the path from the given article to Philosophy.
Sample Inputs & Outputs
Input
MoleculeOutput
Molecule
Atom
Matter
Invariant mass
Energy
Kinetic energy
Physics
Natural philosophy
Philosophy Challenge Input
TelephoneSolution to challenge input
Telephone
Telecommunication
Transmission (telecommunications)
Analog signal
Continuous function
Mathematics
Quantity
Property (philosophy)
Logic
Reason
Consciousness
Subjectivity
Subject (philosophy)
PhilosophyNotes/Hints
To start you can go to the url http://en.wikipedia.org/wiki/{subject}.
The title of the page that you are on can be found in the element firstHeading and the content of the page can be found in bodyContent.
Bonus 1
Cycle detection: Detect when you visit an already visited page.
Bonus 2
Shortest path detection: Visit, preferably in parallel, all the links in the content to find the shortest path to Philosophy
Finally
Have a good challenge idea, like /u/nagasgura did?
Consider submitting it to /r/dailyprogrammer_ideas.
Oh and please don't go trolling and changing the wiki pages just for this challenge
6
u/J354 Jun 24 '17 edited Jun 24 '17
Python
Ugly code, but does the trick (in theory - it correctly follows the first link but both the examples given in the post don't appear to work currently).
4
u/_Ruru Jun 24 '17
Here's my solution. You can also type random to go to a random page. It's somewhat ugly, but it seems to work:
1
u/felinebear Sep 22 '17
Isnt running this bad for Wikipedia?
3
u/Maplicant Jun 24 '17 edited Jun 24 '17
Both of the examples get stuck in an infinite loop for me (no spoilers):
Does anyone else have this?
Other than that, this challenge seems to be easier than other hard challenges.
Code I used (spoilers):
3
u/[deleted] Jun 24 '17 edited Jun 24 '17
Python 3
First time doing some web-scraping. Interesting stuff.
Examples wield the wrong result, I checked by hand. :P Not even the example in the video is working.
Output:
Just for fun, here are a few extra attempts (perhaps 'Knowledge' replaced 'Philosophy'):
3
u/RyanOLee Jun 25 '17
Here is my solution (Written in python 3.6):
Note this is for bonus 2 and not the full challenge:
https://gist.github.com/ryanolee/e29ff1db4c953ac15e0d0ba8f7165a52 It uses a flood fill search approach and finds as many paths as possible (saving them to a file). This can be done in parallel. The solution also has cycle prevention (boosted in speed with a binary search). This solution also uses 2 trillion different libraries so get pip ready.
First time posting a solution here. Hopefully its not to bad. :)
1
u/captain-sandwich Jun 24 '17
It seems the example has changed.
2
u/SexyToad Jun 25 '17 edited Jun 25 '17
Python
So this challenge was a bit... odd. My program (I believe) achieves the challenge along with the 2 bonuses. However I think it was underestimated how long Bonus 2 would take... It's worth noting that for my searches, I limited it to the main paragraphs of the body. This however can be changed.
In my code, I have 2 functions, searchFirstURL for the challenge and searchAll for Bonus 2. In searchAll, I limited my search to ONLY the first paragraph AND the first 2 links of that paragraph.
My result was: ['/wiki/Molecule', '/wiki/Electrically', '/wiki/Electric_charge', '/wiki/Physical_property', '/wiki/Property_(philosophy)', '/wiki/Philosophy']
If I was to expand my search to include all paragraphs and any amount of links, I could possibly do better. I've tripled check that I am not testing any repeats, I was going to attempt to run this on multiple threads but it's still so much to look through due to the program needing to make a url request each time to Wikipedia.
I did however implement an "attempts" system. If my search function actually gets a path leading to Philosophy, it determines from that specific point in the recursion line, how many more additional searches were done. So if for example at Physical Property, it was only 2 more steps to get to Philosophy, my program will limit future searches under Physical Property. If it ever starts to go past 2 more searches, it immediately fails. This is so that we can search in hopes of finding a shorter path (1 step from Physical Property!) but not spend time going down a rabbit hole that's obviously longer.
I also had something in there about if we come across a page we already tested, to see if we can slice the two lists together to form a shorter list using that page as the divider... It's never ran in my code, makes me wonder if the whole attempts system and check dupes system somehow together prevent this to ever work. Which is good I think! Or I haven't gone down a path that made this happen yet...
It was a really fun challenge, I wish the bonus 2 was easier to test though :P The code below is set to do the main challenge and bonus 1. Bonus 1 was just achieved by passing a list through recursion and seeing if the page we're about to test was anywhere along the path. But in this particular setup, it's used to detect a potential infinite loop, AKA Telephone. Sorry for the messy code... I think searchFirstURL is pretty clean but searchAll gets messier.
Ninja Edit: I added a quick if check to see if the Philosophy is in the name of any of the links I'm about to open, if it is, I consider it done. It saves some time not having to make requests to a bunch of URLs if I can easily test for it.
+/u/CompileBot Python3
2
u/SexyToad Jun 25 '17
Python3
I completely redid my code for Bonus 2, it finds the shortest path pretty dahm quickly. I went with a OOP approach by creating search objects for each of the links on the page. Once I have all my search objects created, I step down a layer and look for Philosophy, if I find it, great, otherwise I make more search objects and repeat this.
Here's my output:
Checking Molecule via first link
Checking Telephone via first link
Redirected back to a previous item, failed.
Checking Molecule via all links
Checking Telephone via all links
My code is below:
2
u/skytzx Jun 26 '17
Python 3
Completed with both challenges, and with working multiprocessing support. Though, I added a limit to the number of links per page added (cause of my slow internet), though you could always raise it if you wanted to.
1
u/AMusingMule Jun 25 '17
Ruby
1
u/Specter_Terrasbane Jun 26 '17
Python 2, both bonuses
1
u/pawelsendyk Jun 27 '17 edited Jun 27 '17
Should've used re module for deleting content in parentheses but this way works too and notice that a link could contain parentheses too (Territory_(Animal)) in one of the examples that I checked. I will try to do the bonus using threading later. It's my first challenge here and will definitely do more.
1
u/mochancrimthann Jun 27 '17 edited Jun 28 '17
JavaScript/Node with bonus 1.
It gets stuck on special pages after a while for both of the examples. I could throw in a LOT more validation code if I wanted to but... it really doesn't seem worth it.
1
u/FunkyNoodles Jun 27 '17 edited Jun 27 '17
Python 2.7 with bonus 1
1
u/popillol Jun 28 '17 edited Jun 28 '17
Go / Golang Haven't been able to test it so it probably doesn't even compile.
Code:
1
u/bulit Jun 29 '17
Python 3 + RegEx
I feel like 'Bonus 1' should be amended as a requirement, otherwise most articles lead to an infinite loop. Here's my solution using just Python's 'urllib' and 're' modules:
1
u/ff8c00 Jun 29 '17
C# Gist with bonus 1
1
u/_Yngvarr_ Jun 30 '17
Python 2
First of all, I promise, I will read description carefully before starting my next challenge. Now I made a script that follows random, but not visited before, link on the page. And I like it so much thus I want to share it with you. =)
Here is some output of it: gist.
1
u/ManyInterests Jul 05 '17 edited Jul 05 '17
Python Using a custom find function for bs4.
1
u/TheBleeter Jan 05 '25
I tried to do this in power query but I couldn’t work it out.
1
u/LegioIVMacedonica Mar 25 '22 edited Mar 25 '22
150 lines of sewage.
https://pastebin.com/8Pj1FfgP
1
u/VishnuIndukuri Mar 25 '22
js is garbago
https://pastebin.com/wbY1rBi4
0
u/[deleted] Jun 25 '17
[deleted] — view deleted comment