回复: 1
跳转到指定楼层
上一主题 下一主题
收起左侧

2014-04-14 Yelp skype面试

全局:

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

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

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x


技术问题如下。


"""
1. Write a function to add the two binary number strings.
您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
al">面试小哥很热情,人很好。


我全部拿Python写的代码。代码如下:
  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'
  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. """


  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]]
  14.     len_a = len(alist)
  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
  31.         else:
  32.             carry = 0
  33.         res.append(tmp)
  34.         i += 1

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


  36. def add_str_2(a, b, base):
  37.     """
  38.     2. Follow up, add two number strings with specific base
  39.     add('100', '10', 2) -> '110'
  40.     add('100', '9', 10) -> '109'
  41.     """

  42.     alist = [int(x) for x in a[::-1]]
  43.     blist = [int(x) for x in b[::-1]]
  44.     len_a = len(alist)
  45.     len_b = len(blist)
  46.     carry = 0
  47.     i = 0
  48.     res = []
  49.     while i < len_a or i < len_b or carry != 0:
  50.         tmp = carry
  51.         if i < len_a:
  52.             tmp += alist[i]
  53.         if i < len_b:
  54.             tmp += blist[i]

  55.         carry = tmp / base
  56.         tmp = tmp % base

  57.         res.append(tmp)
  58.         i += 1

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


  60. def add_str_3(input_list, base):
  61.     """
  62.     3. Follow up, sum list number strings with specific base
  63.     add_strs(['9', '3', '2'], 10) -> '14'
  64.     """
  65.     if not input_list:
  66.         return 0

  67.     while len(input_list) >= 2:
  68.         a = input_list.pop()
  69.         b = input_list.pop()
  70.         tmp = add_str_2(a, b, base)
  71.         input_list.append(tmp)

  72.     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'
  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'
  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):
  30.         input_list = ['9', '4', '3']
  31.         base = 10
  32.         res = add_str_3(input_list, base)
  33.         self.assertEqual('16', res)

  34. if __name__ == '__main__':
  35.     unittest.main(verbosity=2)
复制代码


发题攒人品,求过。

评分

参与人数 4大米 +19 收起 理由
bryanjhy + 3 给你点个赞!
ycsung + 3 感谢分享!
mtfront + 10 很有用的信息!
neomiracle + 3 感谢分享!

查看全部评分


上一篇:e-bay intern 面经
下一篇:Amazon Offer 报面经回馈本版

本帖被以下淘专辑推荐:

  • · Yelp|主题: 11, 订阅: 3
🔗
 楼主| johnnywsd 2014-11-25 05:16:48 | 只看该作者
全局:
这个没人回?
lz就是个jb
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

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