Tips on how to In finding the Stray Quantity in Python

The problem

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

Whole 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 instances to validate our resolution

import codewars_test as check
from resolution import stray

@check.describe("Mounted Exams")
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)

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: :???: :?: :!: