Software Engineering

The right way to Discover the Stray Quantity in Python

The right way to Discover the Stray Quantity in Python
Written by admin


The problem

You’re given an odd-length array of integers, during which all of them are the identical, aside from one single quantity.

Full the tactic which accepts such an array, and returns that single completely different quantity.

The enter array will at all times be legitimate! (odd-length >= 3)

Examples

[1, 1, 2] ==> 2
[17, 17, 3, 17, 17, 17, 17] ==> 3

The answer in Python

Choice 1:

def stray(arr):
    for x in arr:
        if arr.depend(x) == 1:
            return x

Choice 2:

def stray(arr):
    return min(arr, key=arr.depend)

Choice 3:

def stray(arr):
    return [x for x in set(arr) if arr.count(x) == 1][0]

Take a look at instances to validate our answer

import codewars_test as take a look at
from answer import stray

@take a look at.describe("Fastened Assessments")
def fixed_tests():
    @take a look at.it('Primary Take a look at Circumstances')
    def basic_test_cases():
        take a look at.assert_equals(stray([1, 1, 1, 1, 1, 1, 2]), 2)
        take a look at.assert_equals(stray([2, 3, 2, 2, 2]), 3)
        take a look at.assert_equals(stray([3, 2, 2, 2, 2]), 3)

About the author

admin

Leave a Comment