Guest User

Untitled

a guest
Jan 19th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. def summmaryranges(nums):
  2. ranges = []
  3. for num in nums:
  4. if not ranges or num - ranges[-1][-1] > 1:
  5. ranges.append([])
  6. ranges[-1][1:] = [num]
  7. return ['->'.join(map(str, r)) for r in ranges]
  8.  
  9. def reverse(l, r, cl):
  10. while l < r:
  11. cl[l], cl[r] = cl[r], cl[l]
  12. l += 1
  13. r -= 1
  14.  
  15. def reversestring(s):
  16. n = len(s)
  17. s = list(s)
  18. reverse(0, n - 1, s)
  19. l = 0
  20. for r in range(1, n + 1):
  21. if r == n or s[r] == ' ':
  22. reverse(l, r - 1, s)
  23. l = r + 1
  24. return ''.join(s)
  25.  
  26. def reverseHTML(h):
  27. n = len(h)
  28. h = list(h[::-1])
  29. l = 0
  30. while l < n - 1:
  31. if l == ';':
  32. while l < n and h[l] == h[l - 1]:
  33. l += 1
  34. r = l + 1
  35. while r < n and h[r] != '&':
  36. r += 1
  37. reverse(l, r, h)
  38. l = r + 1
  39. l += 1
  40. return ''.join(h)
  41.  
  42. def findduplicate(s):
  43. d = {}
  44. for word in s.split():
  45. d[word] = d.get(word, 0) + 1
  46. return [item[0] for item in filter(lambda x:x[1] > 1, d.items())]
  47.  
  48. def findfirstdup(s):
  49. d = {}
  50. words = s.split()
  51. min_idx = len(words)
  52. for idx, word in enumerate(words):
  53. if word not in d:
  54. d[word] = idx
  55. else:
  56. min_idx = min(min_idx, d[word])
  57. if min_idx == len(words):
  58. return None
  59. return words[min_idx]
  60.  
  61. def a2num(s):
  62. '''
  63. A 1 AA 26 + 1
  64. B 2 AB 26 + 2
  65. Z 26 AZ 26 + 26
  66. '''
  67. res = 0
  68. for idx, c in enumerate(s[::-1]):
  69. res += (ord(c) - ord('A') + 1) * 26 ** idx
  70. return res
  71.  
  72. def num2a(n):
  73. res = ''
  74. while n > 0:
  75. res += chr((n - 1) % 26 + ord('A'))
  76. n = (n - 1) // 26
  77. return res[::-1]
  78.  
  79. def findkbucket(nums, k):
  80. d = {}
  81. res = []
  82. for num in nums:
  83. d[num] = d.get(num, 0) + 1
  84. freqdict = [[] for _ in range(len(nums) + 1)]
  85. for key in d:
  86. freqdict[d[key]] += [key]
  87. for item in freqdict[::-1]:
  88. res += item
  89. return res[:k]
Add Comment
Please, Sign In to add comment