《数据科学面试40+真题讲解》,K神本年度最后一次开课


一亩三分地论坛

 找回密码
 Sign Up 注册获取更多干货
码农求职神器Triplebyte:
不用海投,内推你去多家公司面试
Airbnb 数据科学职位
in analytics and inference
天天打游戏、照样领工资,
你要不要来?
把贵司招聘信息放这里
查看: 1386|回复: 1
收起左侧

2014-04-14 Yelp skype面试

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

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

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

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

x


技术问题如下。


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


2. Follow up, add two number strings with specific base
add('100', '10', 2) -> '110'
add('100', '9', 10) -> '109'

. From 1point 3acres bbs

3. Follow up, sum list number strings with specific base
add_strs(['9', '3', '2'], 10) -> '14'
"""


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

我全部拿Python写的代码。代码如下:
  1. """
  2. 1. Write a function to add the two binary number strings.. 鐣欏鐢宠璁哄潧-涓浜╀笁鍒嗗湴
  3. e.g.. 1point 3acres 璁哄潧
  4. add('100', '10') -> '110'
    . 1point 3acres 璁哄潧

  5. 2. Follow up, add two number strings with specific base
  6. add('100', '10', 2) -> '110'
  7. add('100', '9', 10) -> '109'

  8. 3. Follow up, sum list number strings with specific base
  9. add_strs(['9', '3', '2'], 10) -> '14'. 涓浜-涓夊垎-鍦帮紝鐙鍙戝竷
  10. """. 1point3acres.com/bbs

  11. . From 1point 3acres bbs
  12. def add_str_1(a, b):
  13.     alist = [int(x) for x in a[::-1]]
  14.     blist = [int(x) for x in b[::-1]]
  15.     len_a = len(alist)
  16.     len_b = len(blist)
  17.     carry = 0
  18.     i = 0. more info on 1point3acres.com
  19.     res = []
  20.     while i < len_a or i < len_b or carry != 0:. more info on 1point3acres.com
  21.         tmp = carry
  22.         if i < len_a:
  23.             tmp += alist[i]
  24.         if i < len_b:. from: 1point3acres.com/bbs
  25.             tmp += blist[i]

  26.         if tmp == 3:
  27.             carry = 1
  28.             tmp = 1
  29.         elif tmp == 2:
  30.             carry = 1
  31.             tmp = 0
  32.         else:
  33.             carry = 0
  34.         res.append(tmp).鏈枃鍘熷垱鑷1point3acres璁哄潧
  35.         i += 1

  36.     return ''.join([str(x) for x in res[::-1]])

  37. . 鐣欏鐢宠璁哄潧-涓浜╀笁鍒嗗湴
  38. def add_str_2(a, b, base):.1point3acres缃
  39.     """
  40.     2. Follow up, add two number strings with specific base
  41.     add('100', '10', 2) -> '110'.鏈枃鍘熷垱鑷1point3acres璁哄潧
  42.     add('100', '9', 10) -> '109'
  43.     """

  44.     alist = [int(x) for x in a[::-1]]
  45.     blist = [int(x) for x in b[::-1]]
  46.     len_a = len(alist) 鏉ユ簮涓浜.涓夊垎鍦拌鍧.
  47.     len_b = len(blist)
  48.     carry = 0
  49.     i = 0
  50.     res = []. visit 1point3acres.com for more.
  51.     while i < len_a or i < len_b or carry != 0:
  52.         tmp = carry
  53.         if i < len_a:
  54.             tmp += alist[i]
  55.         if i < len_b:
  56.             tmp += blist[i]
  57. 鏉ユ簮涓浜.涓夊垎鍦拌鍧.
  58.         carry = tmp / base
  59.         tmp = tmp % base

  60.         res.append(tmp)
  61.         i += 1

  62.     return ''.join([str(x) for x in res[::-1]])
  63. .1point3acres缃

  64. def add_str_3(input_list, base):
  65.     """
  66.     3. Follow up, sum list number strings with specific base
  67.     add_strs(['9', '3', '2'], 10) -> '14'
  68.     """
  69.     if not input_list:. From 1point 3acres bbs
  70.         return 0

  71.     while len(input_list) >= 2:
  72.         a = input_list.pop()
  73.         b = input_list.pop()
  74.         tmp = add_str_2(a, b, base)
  75.         input_list.append(tmp)

  76.     return input_list[0]
复制代码
.鏈枃鍘熷垱鑷1point3acres璁哄潧

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. . 鍥磋鎴戜滑@1point 3 acres

  6. class Test(unittest.TestCase):. 1point3acres.com/bbs

  7.     def test_add_str_1(self):
  8.         a = '110'
  9.         b = '110'
  10.         res = add_str_1(a, b)
  11.         self.assertEqual('1100', res)
  12. . more info on 1point3acres.com
  13.     def test_add_str_2_1(self):
  14.         a = '110'
  15.         b = '110'
  16.         base = 2. more info on 1point3acres.com
  17.         res = add_str_2(a, b, base)
  18.         self.assertEqual('1100', res)

  19.     def test_add_str_2_2(self):
  20.         a = '999'
  21.         b = '1'
  22.         base = 10-google 1point3acres
  23.         res = add_str_2(a, b, base)
  24.         self.assertEqual('1000', res)

  25.     def test_add_str_2_3(self):
  26.         a = '7'
  27.         b = '2'
  28.         base = 8. 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴
  29.         res = add_str_2(a, b, base)
  30.         self.assertEqual('11', res)
  31. 鏉ユ簮涓浜.涓夊垎鍦拌鍧.
  32.     def test_add_str_3_1(self):
  33.         input_list = ['9', '4', '3']
  34.         base = 10
  35.         res = add_str_3(input_list, base)
  36.         self.assertEqual('16', res)

  37. if __name__ == '__main__':. more info on 1point3acres.com
  38.     unittest.main(verbosity=2)
复制代码

.1point3acres缃
发题攒人品,求过。

评分

3

查看全部评分

本帖被以下淘专辑推荐:

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

使用道具 举报

本版积分规则

关闭

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

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

custom counter

GMT+8, 2017-11-24 15:48

Powered by Discuz! X3

© 2001-2013 Comsenz Inc. Design By HUXTeam

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