栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Google foobar gearing_up_for_destruction

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Google foobar gearing_up_for_destruction

这是python 2.7中的工作代码,所有测试用例均已通过Google传递。这是我抓了一段时间纸之后想到的最好的解决方案:

from fractions import Fraction  def answer(pegs):    arrLength = len(pegs)    if ((not pegs) or arrLength == 1):        return [-1,-1]    even = True if (arrLength % 2 == 0) else False    sum = (- pegs[0] + pegs[arrLength - 1]) if even else (- pegs[0] - pegs[arrLength -1])    if (arrLength > 2):        for index in xrange(1, arrLength-1): sum += 2 * (-1)**(index+1) * pegs[index]    FirstGearRadius = Fraction(2 * (float(sum)/3 if even else sum)).limit_denominator()    # now that we have the radius of the first gear, we should again check the input array of pegs to verify that    # the pegs radius' is atleast 1.    # since for valid results, LastGearRadius >= 1 and FirstGearRadius = 2 * LastGearRadius    # thus for valid results FirstGearRadius >= 2    if FirstGearRadius < 2:        return [-1,-1]    currentRadius = FirstGearRadius    for index in xrange(0, arrLength-2):        CenterDistance = pegs[index+1] - pegs[index]        NextRadius = CenterDistance - currentRadius        if (currentRadius < 1 or NextRadius < 1): return [-1,-1]        else: currentRadius = NextRadius    return [FirstGearRadius.numerator, FirstGearRadius.denominator]

请参阅此图像,以了解我如何编写此代码:



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/646953.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号