This solution avoids creating any objects or creating copies of the sequence or any temporary variables.
Using a class in the previous solutions was silly. >.<
from typing import Sequence, Optional def missing_integer(sequence: Sequence[int]) -> Optional[int]: for x, y in zip(sequence, range(sequence[0], sequence[-1])): if x != y: return y
class MissingInteger:def __init__(self, sequence):self.sequence = sequenceself.perfect_sequence = range(min(self.sequence), max(self.sequence) +1 )- from typing import Sequence, Optional
def solution(self):for number in self.perfect_sequence:if number not in self.sequence:return number- def missing_integer(sequence: Sequence[int]) -> Optional[int]:
- for x, y in zip(sequence, range(sequence[0], sequence[-1])):
- if x != y:
- return y
import codewars_test as test from solution import missing_integer @test.describe("Find the missing integer in a sorted sequence of natural numbers.") def test_group(): @test.it("Comparing to expected return values.") def test_case(): test.assert_equals(missing_integer([1, 2, 3, 4, 5, 7, 8, 9]), 6) test.assert_equals(missing_integer([1, 2, 4, 5, 6, 7, 8, 9]), 3) test.assert_equals(missing_integer([-2, 0, 1, 2, 3, 4, 5, 6]), -1) test.assert_equals(missing_integer([1, 2, 3, 4, 5, 6, 7, 8, 9]), None)
- import codewars_test as test
from solution import MissingInteger- from solution import missing_integer
@test.describe("Example")- @test.describe("Find the missing integer in a sorted sequence of natural numbers.")
- def test_group():
@test.it("test case")- @test.it("Comparing to expected return values.")
- def test_case():
test.assert_equals(MissingInteger([1, 2, 3, 4, 5, 7, 8, 9]).solution(), 6)test.assert_equals(MissingInteger([1, 2, 4, 5, 6, 7, 8, 9]).solution(), 3)test.assert_equals(MissingInteger([-2, 0, 1, 2, 3, 4, 5, 6]).solution(), -1)test.assert_equals(MissingInteger([1, 2, 3, 4, 5, 6, 7, 8, 9]).solution(), None)- test.assert_equals(missing_integer([1, 2, 3, 4, 5, 7, 8, 9]), 6)
- test.assert_equals(missing_integer([1, 2, 4, 5, 6, 7, 8, 9]), 3)
- test.assert_equals(missing_integer([-2, 0, 1, 2, 3, 4, 5, 6]), -1)
- test.assert_equals(missing_integer([1, 2, 3, 4, 5, 6, 7, 8, 9]), None)