近期论坛无法登录的解决方案


一亩三分地论坛

 找回密码
 获取更多干活,快来注册

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

2014-04-14 Yelp skype面试

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

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

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

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

x


技术问题如下。
. 鍥磋鎴戜滑@1point 3 acres

"""
1. Write a function to add the two binary number strings.
e.g..1point3acres缃
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缃


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

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

  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. 鍥磋鎴戜滑@1point 3 acres
  9. add_strs(['9', '3', '2'], 10) -> '14'
  10. """


  11. def add_str_1(a, b):
  12.     alist = [int(x) for x in a[::-1]]
  13.     blist = [int(x) for x in b[::-1]].鏈枃鍘熷垱鑷1point3acres璁哄潧
  14.     len_a = len(alist). 鍥磋鎴戜滑@1point 3 acres
  15.     len_b = len(blist)
  16.     carry = 0
  17.     i = 0
  18.     res = []
  19.     while i < len_a or i < len_b or carry != 0:
  20.         tmp = carry
  21.         if i < len_a:
  22.             tmp += alist[i]
  23.         if i < len_b:
  24.             tmp += blist[i]

  25.         if tmp == 3:
  26.             carry = 1
  27.             tmp = 1
  28.         elif tmp == 2:
  29.             carry = 1
  30.             tmp = 0. 鍥磋鎴戜滑@1point 3 acres
  31.         else:
  32.             carry = 0
  33.         res.append(tmp)
  34.         i += 1
  35. . more info on 1point3acres.com
  36.     return ''.join([str(x) for x in res[::-1]])


  37. . 涓浜-涓夊垎-鍦帮紝鐙鍙戝竷
  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'
  42.     add('100', '9', 10) -> '109'
  43.     """

  44. . 1point3acres.com/bbs
  45.     alist = [int(x) for x in a[::-1]]
  46.     blist = [int(x) for x in b[::-1]]
  47.     len_a = len(alist)
  48.     len_b = len(blist)
  49.     carry = 0. 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴
  50.     i = 0
  51.     res = []. 鍥磋鎴戜滑@1point 3 acres
  52.     while i < len_a or i < len_b or carry != 0:
  53.         tmp = carry
  54.         if i < len_a:
  55.             tmp += alist[i]
  56.         if i < len_b:
  57.             tmp += blist[i]

  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. def add_str_3(input_list, base):
  64.     """
  65.     3. Follow up, sum list number strings with specific base
  66.     add_strs(['9', '3', '2'], 10) -> '14'
  67.     """. Waral 鍗氬鏈夋洿澶氭枃绔,
  68.     if not input_list:. 1point3acres.com/bbs
  69.         return 0

  70.     while len(input_list) >= 2:
  71.         a = input_list.pop()
    . 涓浜-涓夊垎-鍦帮紝鐙鍙戝竷
  72.         b = input_list.pop()
  73.         tmp = add_str_2(a, b, base).鏈枃鍘熷垱鑷1point3acres璁哄潧
  74.         input_list.append(tmp). from: 1point3acres.com/bbs

  75.     return input_list[0]-google 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):
  7. -google 1point3acres
  8.     def test_add_str_1(self):. 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴
  9.         a = '110'
  10.         b = '110'
  11.         res = add_str_1(a, b)
  12.         self.assertEqual('1100', res)

  13.     def test_add_str_2_1(self):
  14.         a = '110'
  15.         b = '110'
  16.         base = 2
  17.         res = add_str_2(a, b, base)
  18.         self.assertEqual('1100', res)
  19. . 涓浜-涓夊垎-鍦帮紝鐙鍙戝竷
  20.     def test_add_str_2_2(self):
  21.         a = '999'
  22.         b = '1'
  23.         base = 10
  24.         res = add_str_2(a, b, base). more info on 1point3acres.com
  25.         self.assertEqual('1000', res)

  26.     def test_add_str_2_3(self):
  27.         a = '7'
  28.         b = '2'. 涓浜-涓夊垎-鍦帮紝鐙鍙戝竷
  29.         base = 8
  30.         res = add_str_2(a, b, base)
  31.         self.assertEqual('11', res)

  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. . 鍥磋鎴戜滑@1point 3 acres
  38. if __name__ == '__main__':
  39.     unittest.main(verbosity=2)
复制代码


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

评分

3

查看全部评分

本帖被以下淘专辑推荐:

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

使用道具 举报

本版积分规则

关闭

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

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

custom counter

GMT+8, 2017-6-26 10:17

Powered by Discuz! X3

© 2001-2013 Comsenz Inc. Design By HUXTeam

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