首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >分数点的Google foobar gearing_up_for_destruction

分数点的Google foobar gearing_up_for_destruction
EN

Stack Overflow用户
提问于 2020-02-25 17:38:37
回答 1查看 1.1K关注 0票数 0

我从Google的编码问题中提出了这个问题,并试图解决它,但是被一些特定的情况所困扰。

代码语言:javascript
复制
Challenge

As Commander Lambda's personal assistant, you've been assigned the task of configuring the LAMBCHOP doomsday device's axial orientation gears. It should be pretty simple - just add gears to create the appropriate rotation ratio. But the problem is, due to the layout of the LAMBCHOP and the complicated system of beams and pipes supporting it, the pegs that will support the gears are fixed in place.

The LAMBCHOP's engineers have given you lists identifying the placement of groups of pegs along various support beams. You need to place a gear on each peg (otherwise the gears will collide with unoccupied pegs). The engineers have plenty of gears in all different sizes stocked up, so you can choose gears of any size, from a radius of 1 on up. Your goal is to build a system where the last gear rotates at twice the rate (in revolutions per minute, or rpm) of the first gear, no matter the direction. Each gear (except the last) touches and turns the gear on the next peg to the right.

Given a list of distinct positive integers named pegs representing the location of each peg along the support beam, write a function answer(pegs) which, if there is a solution, returns a list of two positive integers a and b representing the numerator and denominator of the first gear's radius in its simplest form in order to achieve the goal above, such that radius = a/b. The ratio a/b should be greater than or equal to 1. Not all support configurations will necessarily be capable of creating the proper rotation ratio, so if the task is impossible, the function answer(pegs) should return the list [-1, -1].

For example, if the pegs are placed at [4, 30, 50], then the first gear could have a radius of 12, the second gear could have a radius of 14, and the last one a radius of 6. Thus, the last gear would rotate twice as fast as the first one. In this case, pegs would be [4, 30, 50] and answer(pegs) should return [12, 1].

The list pegs will be given sorted in ascending order and will contain at least 2 and no more than 20 distinct positive integers, all between 1 and 10000 inclusive.

这是我的解决方案

代码语言:javascript
复制
def get_last_radius(pegs, radius):
  for i in range(1, len(pegs)):
    pegs_range = pegs[i] - pegs[i - 1]
    radius = pegs_range - radius

  return radius

def get_first_radius(pegs):
  first_range = pegs[1] - pegs[0]
  for first_radius in range(2, first_range, 2):
    last_radius = get_last_radius(pegs, first_radius)
    if first_radius == last_radius * 2:
      return [first_radius, 1]
  return [-1, -1]

我假设不可能有浮点数,这就是为什么我要返回1作为所有情况的分母。但搜索一下,我找到了这篇文章

Google foobar gearing_up_for_destruction

这说明有浮点数的情况,但我还是找不到。

例如,如果第一个半径是*.x,则另一个半径应该是*.y,其中x + y = 1。因此,对于第三个齿轮,它应该再次是*.x,因为钉只放置在整数位置。有人能用浮动数字显示特定的测试用例吗?

EN

回答 1

Stack Overflow用户

发布于 2020-05-14 05:14:40

您的解决方案不符合要求(我一开始也不这么做)。

以下是你可能错过的:

  1. The engineers have plenty of gears in all different sizes stocked up, so you can choose gears of any size, from a radius of 1 on up.

这意味着,当您找到第一个齿轮半径时,您必须再次返回,以查看是否有任何齿轮的半径< 1.

  • return [first_radius, 1],半径并不总是整数。这是明确指出,returns a list of two positive integers a and b representing the numerator and denominator of the first gear's radius in its simplest form in order to achieve the goal above, such that radius = a/b。下面是返回半径不是整数的测试用例:[25,105,145,170],它返回[130, 3]

为了解决这个问题,你需要数学。试着找出n= 2,3,4,5的最后齿轮半径.(输入列表的长度),您将看到模式。祝你好运

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60400352

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档