Skip to content

Comment on Python Multiple Assignment Is a Puzzle

Comments

  A = [1, 2, 4, 5, 7]
  A_s = sorted(A)
  b = A_s[0]
  for a in A_s[1:]:
    if a - b > 1:
      print b + 1
      break
    else:
      b = a
  else:
    print 'No missing element'
Trying to be more Pythonic:
  def test(l):
    l_s = sorted(l)
    print [l0 + 1 for l1, l0 in zip(l_s[1:], l_s[:-1]) if l1 - l0 > 1]

Sorting is not O(N), at best it is O(NlogN)

But the code is not competitive.

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.