6道题,45分钟!G-Research的科技与狠活
量化投资与机器学习微信公众号,是业内垂直于量化投资、对冲基金、Fintech、人工智能、大数据等领域的主流自媒体。公众号拥有来自公募、私募、券商、期货、银行、保险、高校等行业30W+关注者,荣获2021年度AMMA优秀品牌力、优秀洞察力大奖,连续2年被腾讯云+社区评选为“年度最佳作者”。
Q1:Everyone has heard of "FizzBuzz". If you haven't: Google it. You are allowed to use the internet during this test. We'd like you to implement "FizzBuzz" please. Except at G-Research we often see the Pareto principle (or "80/20 rule") and generally believe in agile incremental delivery, so to make it easier we actually just want the Fizzes and not any Buzzes, please.
第一题一上来可能有些闷,但如果大家熟悉'FizzBuzz',意思就是给你一个整数比如100,需要你输出一个列表。列表中的每个元素,如果能背3整除则输出'Fizz',如果被5整除则输出'Buzz',如果能同时被3和5整除,则输出'FizzBuzz'。比如给出整数15,则答案是:['1', '2', 'Fizz', '4', 'Buzz', 'Fizz', '7', '8', 'Fizz', 'Buzz', '11', 'Fizz', '13', '14', 'FizzBuzz']。
import typing
def solution(n: int) -> typing.List[str]:
res = []
for i in range(1, n+1):
if i % 3 == 0
res.append('Fizz')
else:
res.append(str(i))
return res
Q2:It was hopefully obvious this was coming. FizzBuzz, please.
第二题是让你实现一个完整的FizzBuzz。
import typing
def solution(n: int) -> typing.List[str]:
res = []
for i in range(1, n+1):
if i % 3 == 0 and i % 5 == 0:
res.append('FizzBuzz')
elif i % 3 == 0:
res.append('Fizz')
elif i % 5 == 0:
res.append('Buzz')
else:
res.append(str(i))
return res
Q3:We quite like prime numbers at G-Research. We also often face requirements that are unknown at the start of software engineering and have to evolve to follow where the research leads. We'd now like FizzBuzz but with the added complexity of prime numbers being preferentially emitted as "Prime".
import typing
def is_prime(n: int) -> bool:
if n == 1:
return False
else:
for i in range(2,n):
if n%i == 0:
return False
else:
return True
def solution(n: int) -> typing.List[str]:
res = []
for i in range(1, n+1):
if is_prime(i):
res.append('Prime')
elif i % 3 == 0 and i % 5 == 0:
res.append('FizzBuzz')
elif i % 3 == 0:
res.append('Fizz')
elif i % 5 == 0:
res.append('Buzz')
else:
res.append(str(i))
return res
Q4:G-Research also has an inexplicable penchant for pi. The requirements are now such that if the number being tested, represented as a decimal's present in the first p digits of pi, also represented as a decimal and ignoring any decimal point, then"Pi" is emitted preferentially to Fizzes Buzzes or Prime. e.g. if p is 3 then we test against 314 and so 3,1,4,14,31 and 314 would all return "Pi".
Q5:The underlying problem is the same: FizzBuzzPrimePi. If your previous submission cannot cope with arbitrarily large input, for either input, that is now a requirement. If this seems unreasonable, optimising maximum size of n is more valuable than optimising maximum size of p. The test cases here will remain simple the quality of the code matters.
Q6:What is the minimum p such that every response is"Pi" for a given input n?
微信扫码关注该文公众号作者