HomeSoftware EngineeringEasy methods to Discover the Stray Quantity in Python

Easy methods to Discover the Stray Quantity in Python


The problem

You might be given an odd-length array of integers, during which all of them are the identical, apart from one single quantity.

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

The enter array will all the time 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 check
from answer import stray

@check.describe("Mounted Assessments")
def fixed_tests():
    @check.it('Fundamental Take a look at Instances')
    def basic_test_cases():
        check.assert_equals(stray([1, 1, 1, 1, 1, 1, 2]), 2)
        check.assert_equals(stray([2, 3, 2, 2, 2]), 3)
        check.assert_equals(stray([3, 2, 2, 2, 2]), 3)
RELATED ARTICLES

Most Popular

Recent Comments