We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
# Standard ruby library for XML parsingrequire'rexml/document'includeREXML# Enter your code here. Read input from STDIN. Print output to STDOUTxmlText=""# Read the input XML Fragmentwhileline=gets()xmlText+=lineenddoc=Document.newxmlText# XPath Selector for computing the average popularity of the movies in the given XML fragment.# Fill in the blanks to complete the required XPath selector queryputsXPath.match(doc,"sum(collection/movie/popularity) div count(collection/movie/popularity)")
The problem description should warn that only XPath 1.0 functions can be used. XPath 2.0 contains "avg" in the specification. It seems that Ruby does not implement it.
No more comments
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Here is Querying XML Datastores with XPath - 7 problem solution - https://www.gyangav.com/2022/11/hackerrank-querying-xml-datastores-with-xpath-7-problem-solution.html
sum(//popularity) div count(//popularity)
answer:
puts XPath.match(doc,"sum(collection/movie/popularity) div count(collection/movie/popularity)")
The problem description should warn that only XPath 1.0 functions can be used. XPath 2.0 contains "avg" in the specification. It seems that Ruby does not implement it.