Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Someone will probably come up with a solution that works with copy&amp;paste and you will think that this solves your problem. Your problem isn't the code, though! It's your strategy. There is a software design principle called "divide and conquer" which you should apply in a redesign to your code: Seperate the code that interprets your HTML strings as trees/graphs from searching the nearest node (probably <a href="https://stackoverflow.com/questions/8922060/breadth-first-search-trace-path">breadth-first-search</a>). Not only will you learn to design better software, <strong>your problem will probably just cease to exist</strong>.</p> <p>I think you are smart enough to solve this yourself, yet I also want to provide a skeleton:</p> <pre><code>def parse_html(txt): """ reads a string of html and returns a dict/list/tuple presentation""" pass def breadth_first_search(graph, start, end): """ finds the shortest way from start to end You can probably customize start and end to work well with the input you want to provide. For implementation details see the link in the text above. """ pass def find_nearest_link(html,name): """putting it all together""" return breadth_first_search(parse_html(html),name,"link") </code></pre> <p>PS: Doing this also applies another principle, but from mathematics: Assuming there is a problem you don't know a solution to (finding links close to a chosen substring) and there are a group of problems that you know a solution to (graph traversal), then try to transform your problem to match the group of problems you can solve, so you can just use basic solution patterns (that are probably even implemented already in a language/framework of your choice) and you are done.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload