10月28,K神开课讲数据科学,你来吗?


一亩三分地论坛

 找回密码
 获取更多干活,快来注册
Babeltime游戏工作室招工程师、美术和策划
Tubi TV招安卓、前端和机器学习工程师
把贵司招聘信息放这里
查看: 1358|回复: 1
收起左侧

2014-04-14 Yelp skype面试

[复制链接] |试试Instant~ |关注本帖
johnnywsd 发表于 2014-4-15 06:50:22 | 显示全部楼层 |阅读模式

2014(1-3月) 码农类 硕士 全职@Yelp - 网上海投 - 技术电面 |Other

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看,没有帐号?获取更多干活,快来注册

x


技术问题如下。


"""
1. Write a function to add the two binary number strings.
e.g.
add('100', '10') -> '110'

-google 1point3acres
2. Follow up, add two number strings with specific base. 涓浜-涓夊垎-鍦帮紝鐙鍙戝竷
add('100', '10', 2) -> '110'
add('100', '9', 10) -> '109'


3. Follow up, sum list number strings with specific base
add_strs(['9', '3', '2'], 10) -> '14'. from: 1point3acres.com/bbs
"""


面试小哥很热情,人很好。

我全部拿Python写的代码。代码如下:
  1. """
  2. 1. Write a function to add the two binary number strings.
  3. e.g.
    .1point3acres缃
  4. add('100', '10') -> '110'

  5. . 鍥磋鎴戜滑@1point 3 acres
  6. 2. Follow up, add two number strings with specific base. 鍥磋鎴戜滑@1point 3 acres
  7. add('100', '10', 2) -> '110'
  8. add('100', '9', 10) -> '109'

  9. 3. Follow up, sum list number strings with specific base
  10. add_strs(['9', '3', '2'], 10) -> '14'
  11. """
  12. . from: 1point3acres.com/bbs

  13. def add_str_1(a, b):
  14.     alist = [int(x) for x in a[::-1]]
  15.     blist = [int(x) for x in b[::-1]]
  16.     len_a = len(alist)
  17.     len_b = len(blist).1point3acres缃
  18.     carry = 0
  19.     i = 0
  20.     res = []. visit 1point3acres.com for more.
  21.     while i < len_a or i < len_b or carry != 0:
  22.         tmp = carry
  23.         if i < len_a:
  24.             tmp += alist[i]
  25.         if i < len_b:
  26.             tmp += blist[i]

  27. . visit 1point3acres.com for more.
  28.         if tmp == 3:
  29.             carry = 1
  30.             tmp = 1
  31.         elif tmp == 2:
  32.             carry = 1
  33.             tmp = 0
  34.         else:
  35.             carry = 0
  36.         res.append(tmp)
  37.         i += 1

  38.     return ''.join([str(x) for x in res[::-1]])
  39. . 1point3acres.com/bbs
  40. . 涓浜-涓夊垎-鍦帮紝鐙鍙戝竷
  41. def add_str_2(a, b, base):. From 1point 3acres bbs
  42.     """
  43.     2. Follow up, add two number strings with specific base
  44.     add('100', '10', 2) -> '110'
  45.     add('100', '9', 10) -> '109'
  46.     """

  47.     alist = [int(x) for x in a[::-1]]. from: 1point3acres.com/bbs
  48.     blist = [int(x) for x in b[::-1]]
  49.     len_a = len(alist)
  50.     len_b = len(blist)
  51.     carry = 0
  52.     i = 0.鏈枃鍘熷垱鑷1point3acres璁哄潧
  53.     res = []
  54.     while i < len_a or i < len_b or carry != 0:. 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴
  55.         tmp = carry
    . 鐣欏鐢宠璁哄潧-涓浜╀笁鍒嗗湴
  56.         if i < len_a:
  57.             tmp += alist[i]
  58.         if i < len_b:
  59.             tmp += blist[i]
  60. . from: 1point3acres.com/bbs
  61.         carry = tmp / base
  62.         tmp = tmp % base

  63.         res.append(tmp)
  64.         i += 1

  65.     return ''.join([str(x) for x in res[::-1]]). 鐣欏鐢宠璁哄潧-涓浜╀笁鍒嗗湴


  66. def add_str_3(input_list, base):.鐣欏璁哄潧-涓浜-涓夊垎鍦
  67.     """
  68.     3. Follow up, sum list number strings with specific base
  69.     add_strs(['9', '3', '2'], 10) -> '14'
  70.     """
  71.     if not input_list:
  72.         return 0

  73.     while len(input_list) >= 2:
  74.         a = input_list.pop()
  75.         b = input_list.pop()
  76.         tmp = add_str_2(a, b, base).1point3acres缃
  77.         input_list.append(tmp)

  78.     return input_list[0]
复制代码


Unit Test 测试用例如下:. 涓浜-涓夊垎-鍦帮紝鐙鍙戝竷
  1. import unittest
  2. from solution import add_str_1
  3. from solution import add_str_2
  4. from solution import add_str_3


  5. class Test(unittest.TestCase):

  6.     def test_add_str_1(self):
  7.         a = '110'. 1point3acres.com/bbs
  8.         b = '110'
  9.         res = add_str_1(a, b). 鐣欏鐢宠璁哄潧-涓浜╀笁鍒嗗湴
  10.         self.assertEqual('1100', res)

  11.     def test_add_str_2_1(self):
  12.         a = '110'
  13.         b = '110'
  14.         base = 2
  15.         res = add_str_2(a, b, base)
  16.         self.assertEqual('1100', res)

  17.     def test_add_str_2_2(self):
  18.         a = '999'
  19.         b = '1'. 涓浜-涓夊垎-鍦帮紝鐙鍙戝竷
  20.         base = 10
  21.         res = add_str_2(a, b, base)
  22.         self.assertEqual('1000', res)

  23.     def test_add_str_2_3(self):
  24.         a = '7'. 鍥磋鎴戜滑@1point 3 acres
  25.         b = '2'
  26.         base = 8
  27.         res = add_str_2(a, b, base)
  28.         self.assertEqual('11', res)

  29.     def test_add_str_3_1(self):.1point3acres缃
  30.         input_list = ['9', '4', '3']
  31.         base = 10
  32.         res = add_str_3(input_list, base). 鍥磋鎴戜滑@1point 3 acres
  33.         self.assertEqual('16', res). 鐣欏鐢宠璁哄潧-涓浜╀笁鍒嗗湴
  34. 鏉ユ簮涓浜.涓夊垎鍦拌鍧.
  35. if __name__ == '__main__':
  36.     unittest.main(verbosity=2) 鏉ユ簮涓浜.涓夊垎鍦拌鍧.
复制代码


发题攒人品,求过。

评分

3

查看全部评分

本帖被以下淘专辑推荐:

  • · Yelp|主题: 11, 订阅: 4
 楼主| johnnywsd 发表于 2014-11-25 05:16:48 | 显示全部楼层
这个没人回?
lz就是个jb
回复 支持 反对

使用道具 举报

本版积分规则

关闭

一亩三分地推荐上一条 /5 下一条

手机版|小黑屋|一亩三分地论坛声明

custom counter

GMT+8, 2017-9-21 05:56

Powered by Discuz! X3

© 2001-2013 Comsenz Inc. Design By HUXTeam

快速回复 返回顶部 返回列表