12
返回列表 发新帖
楼主: turbotian
跳转到指定楼层
上一主题 下一主题
收起左侧

Pinterest 店面

🔗
zhizhong 2018-10-20 01:35:33 | 只看该作者
全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
回复

使用道具 举报

🔗
zhizhong 2018-10-20 01:38:06 | 只看该作者
全局:
martinggww 发表于 2018-10-19 12:18
[mw_shl_code=java,true]public String next(){
        StringBuilder sb = new StringBuilder();
      ...

另外,第3行也可以去掉。
回复

使用道具 举报

🔗
martinggww 2018-10-20 02:12:20 | 只看该作者
全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
回复

使用道具 举报

🔗
大Polo 2018-10-23 11:48:29 | 只看该作者
全局:
  1. import unittest
  2. from collections import deque


  3. class S3Reader:
  4.     def __init__(self, stream):
  5.         self.stream = stream
  6.         self.pos = 0

  7.     def next(self):
  8.         # read next 8 character
  9.         if self.pos >= len(self.stream):
  10.             return ""

  11.         nextPos = min(self.pos + 8, len(self.stream))
  12.         output = self.stream[self.pos:nextPos]
  13.         self.pos = nextPos
  14.         return output


  15. class S3ReaderWrapper:
  16.     def __init__(self, stream):
  17.         self.s3Reader = S3Reader(stream)
  18.         self.queue = deque()

  19.     def nextLine(self):
  20.         output = []
  21.         newLine = False
  22.         eof = False
  23.         while not eof and not newLine:
  24.             next8 = self.s3Reader.next()
  25.             if next8:
  26.                 for ch in next8:
  27.                     self.queue.append(ch)
  28.                 if "\n" in next8:
  29.                     newLine = True
  30.             else:
  31.                 eof = True
  32.         while self.queue and self.queue[0] != "\n":
  33.             output.append(self.queue.popleft())

  34.         if self.queue:
  35.             self.queue.popleft()  # \n
  36.             output.append("\n")
  37.         return ''.join(output)


  38. class S3ReaderTest(unittest.TestCase):
  39.     def testNext(self):
  40.         reader = S3Reader("This is the first line.\nThis is the second line.")
  41.         self.assertEqual('This is ', reader.next())
  42.         self.assertEqual('the firs', reader.next())
  43.         self.assertEqual('t line.\n', reader.next())
  44.         self.assertEqual('This is ', reader.next())
  45.         self.assertEqual('the seco', reader.next())
  46.         self.assertEqual('nd line.', reader.next())
  47.         self.assertEqual('', reader.next())

  48.     def testNextLine(self):
  49.         reader = S3ReaderWrapper(
  50.             "This is the first line.\nThis is the second line."
  51.         )
  52.         self.assertEqual("This is the first line.\n", reader.nextLine())
  53.         self.assertEqual("This is the second line.", reader.nextLine())


  54. if __name__ == '__main__':
  55.     unittest.main()
复制代码
回复

使用道具 举报

🔗
better1016 2018-11-23 03:16:57 | 只看该作者
全局:
难道这个不应还是写 next_line(){
    // using the interface S3Key.next()
   为什么大家写的都是反的,难道是我读错题了??

}
回复

使用道具 举报

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

本版积分规则

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