Find out how to To find the Stray Quantity in Python

The problem

You might be given an odd-length array of integers, by which they all are the similar, apart from for one unmarried quantity.

Entire the process which accepts such an array, and returns that unmarried other 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

Possibility 1:

def stray(arr):
    for x in arr:
        if arr.rely(x) == 1:
            go back x

Possibility 2:

def stray(arr):
    go back min(arr, key=arr.rely)

Possibility 3:

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

Take a look at circumstances to validate our resolution

import codewars_test as check
from resolution import stray

@check.describe("Mounted Checks")
def fixed_tests():
    @check.it('Elementary 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)

Like this post? Please share to your friends:
Leave a Reply

;-) :| :x :twisted: :smile: :shock: :sad: :roll: :razz: :oops: :o :mrgreen: :lol: :idea: :grin: :evil: :cry: :cool: :arrow: :???: :?: :!: