Sobes.tech
Junior — Senior

Rewriting the factorial function for improved readability

livecode

Task condition

It is necessary to refactor the provided code, improving its structure and removing unnecessary dependencies. The original logic of the function should be preserved.

def factorial(n):
    import re
    import urllib
    import time

    time.sleep(2)

    class AppURLOpener(urllib.request.FancyURLopener):
        def __init__(self, *args):
            self.version = 'Chrome 3.4'
            urllib.request.FancyURLopener.__init__(self, *args)

    url_opener = AppURLOpener()
    response = str(url_opener.open('http://www.google.com/search?q=%d!' % n).read())

    matches = re.findall('<strong>%d ! = (.*?)</strong>' % n, response)
    print(matches)
    if matches:
        return int(matches[0].replace('<font size=-2> </font>', ''))
    else:
        raise Exception('Google not willing today!:\n\n%s' % response)
Rewriting the factorial function for improved… - sobes.tech