一亩三分地论坛

 找回密码
 获取更多干货,去instant注册!

一亩三分地官方iOS手机应用下载
查看: 950|回复: 1
收起左侧

2014-04-14 Yelp skype面试

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

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

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

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

x
鏉ユ簮涓浜.涓夊垎鍦拌鍧.

技术问题如下。
. From 1point 3acres bbs

""". 涓浜-涓夊垎-鍦帮紝鐙鍙戝竷
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'


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


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

我全部拿Python写的代码。代码如下:.1point3acres缃
  1. """. 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴
  2. 1. Write a function to add the two binary number strings.
  3. e.g.
  4. add('100', '10') -> '110'

  5. 2. Follow up, add two number strings with specific base
  6. add('100', '10', 2) -> '110'. 1point 3acres 璁哄潧
  7. add('100', '9', 10) -> '109'
  8. .1point3acres缃
  9. 3. Follow up, sum list number strings with specific base
  10. add_strs(['9', '3', '2'], 10) -> '14'. 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴
  11. """. 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴

  12. 鏉ユ簮涓浜.涓夊垎鍦拌鍧.
  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)
  18.     carry = 0
  19.     i = 0
  20.     res = []
  21.     while i < len_a or i < len_b or carry != 0:
  22.         tmp = carry
  23.         if i < len_a:. From 1point 3acres bbs
  24.             tmp += alist[i]. 1point 3acres 璁哄潧
  25.         if i < len_b:
  26.             tmp += blist[i]

  27.         if tmp == 3:
  28.             carry = 1
    . from: 1point3acres.com/bbs
  29.             tmp = 1
  30.         elif tmp == 2:
  31.             carry = 1. 鐣欏鐢宠璁哄潧-涓浜╀笁鍒嗗湴
  32.             tmp = 0
  33.         else:
  34.             carry = 0
  35.         res.append(tmp). Waral 鍗氬鏈夋洿澶氭枃绔,
  36.         i += 1

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


  38. def add_str_2(a, b, base):
  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
    . Waral 鍗氬鏈夋洿澶氭枃绔,
  49.     i = 0
  50.     res = []
  51.     while i < len_a or i < len_b or carry != 0:
  52.         tmp = carry. From 1point 3acres bbs
  53.         if i < len_a:
  54.             tmp += alist[i]
  55.         if i < len_b:. 涓浜-涓夊垎-鍦帮紝鐙鍙戝竷
  56.             tmp += blist[i]

  57.         carry = tmp / base
  58.         tmp = tmp % base

  59.         res.append(tmp)
  60.         i += 1
  61. . 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴
  62.     return ''.join([str(x) for x in res[::-1]])
  63. . 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴

  64. def add_str_3(input_list, base):
  65.     """
  66.     3. Follow up, sum list number strings with specific base. From 1point 3acres bbs
  67.     add_strs(['9', '3', '2'], 10) -> '14'
  68.     """
  69.     if not input_list:
  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). visit 1point3acres.com for more.
  75.         input_list.append(tmp)

  76.     return input_list[0]
复制代码

. Waral 鍗氬鏈夋洿澶氭枃绔,
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'
  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. . from: 1point3acres.com/bbs
  18.     def test_add_str_2_2(self):
  19.         a = '999'.1point3acres缃
  20.         b = '1'
  21.         base = 10
  22.         res = add_str_2(a, b, base)
  23.         self.assertEqual('1000', res)

  24.     def test_add_str_2_3(self):
  25.         a = '7'
  26.         b = '2'
  27.         base = 8
  28.         res = add_str_2(a, b, base)
  29.         self.assertEqual('11', res)

  30.     def test_add_str_3_1(self):
  31.         input_list = ['9', '4', '3']. From 1point 3acres bbs
  32.         base = 10. from: 1point3acres.com/bbs
  33.         res = add_str_3(input_list, base)
  34.         self.assertEqual('16', res)

  35. if __name__ == '__main__':. From 1point 3acres bbs
  36.     unittest.main(verbosity=2).鏈枃鍘熷垱鑷1point3acres璁哄潧
复制代码


发题攒人品,求过。 鏉ユ簮涓浜.涓夊垎鍦拌鍧.

评分

3

查看全部评分

本帖被以下淘专辑推荐:

  • · Yelp|主题: 11, 订阅: 4
 楼主| johnnywsd 发表于 2014-11-25 05:16:48 | 显示全部楼层
关注一亩三分地公众号:
Warald_一亩三分地
这个没人回?
lz就是个jb
回复 支持 反对

使用道具 举报

本版积分规则

请点这里访问我们的新网站:一亩三分地Instant.

Instant搜索更强大,不扣积分,内容组织的更好更整洁!目前仍在beta版本,努力完善中!反馈请点这里

关闭

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

手机版|小黑屋|一亩三分地论坛声明 ( 沪ICP备11015994号 )

custom counter

GMT+8, 2017-2-26 12:25

Powered by Discuz! X3

© 2001-2013 Comsenz Inc. Design By HUXTeam

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