I have a Ruby program that takes as input an HTML document. The structure of this HTML document is unknown and can really be anything.
Also it is guaranteed that the document uses CSS for formatting.
The output needs to be a hash where each key is a CSS class used in the document and the value is the number of times that CSS class appears in the body of the document.
Described:
xpathto find allclass="..."attributes, e.g.class="a b",class="c",class="b"spliton whitespace, e.g.class="a b"⇒["a","b"]flatteninto a single list, e.g.[["a","b"],["c"],["b"]]⇒["a","b","c","b"]group_byto hash from string to hits, e.g. ⇒{"a"=>["a"],"b"=>["b","b"],"c"=>["c"]}mapto pair name withlength, e.g. ⇒[["a",1],["b",2],["c",1]]sort_byoccurrences descending, e.g. ⇒[["b",2],["a",1],["c",1]]Hash.[]to turn into a hash again, e.g. ⇒{"b"=>2,"a"=>1",c"=>1}Alternatively (not functional-style, more straightforward) after step 3 just loop through and add counts to a hash. For bonus points, initialize the hash with default ‘0’ values: