Thursday, September 21, 2023
HomeSoftware EngineeringTips on how to Discover the Distinctive String in Python

Tips on how to Discover the Distinctive String in Python

The problem

There’s an array of strings. All strings comprise comparable letters besides one. Attempt to discover it!

find_uniq([ 'Aa', 'aaa', 'aaaaa', 'BbBb', 'Aaaa', 'AaAaAa', 'a' ]) # => 'BbBb'
find_uniq([ 'abc', 'acb', 'bac', 'foo', 'bca', 'cab', 'cba' ]) # => 'foo'

Strings could comprise areas. Areas are usually not important, solely non-spaces symbols matter.
E.g. A string that accommodates solely areas is like an empty string.

It’s assured that the array accommodates greater than 3 strings.

The answer in Python code

Choice 1:

def find_uniq(arr):
    arr.type(key=lambda x: x.decrease())
    arr1 = [set(i.lower()) for i in arr]
    return arr[0] if arr1.depend(arr1[0]) == 1 and str(arr1[0]) != 'set()' else arr[-1]

Choice 2:

def find_uniq(arr):
    for phrase in set(arr):
        for letter in set(phrase):
            if sum([1 if letter in w else 0 for w in arr]) == 1:
                return phrase
            else: proceed

Choice 3:

from collections import Counter
def find_uniq(arr):
    res = Counter(''.be part of(arr)).most_common()
    return ''.be part of([x for x in arr if res[-1][0] in x])

Check circumstances to validate our answer

check.describe('ought to deal with pattern circumstances')
check.assert_equals(find_uniq([ 'Aa', 'aaa', 'aaaaa', 'BbBb', 'Aaaa', 'AaAaAa', 'a' ]), 'BbBb')
check.assert_equals(find_uniq([ 'abc', 'acb', 'bac', 'foo', 'bca', 'cab', 'cba' ]), 'foo')
check.assert_equals(find_uniq([ '    ', 'a', '  ' ]), 'a')


Please enter your comment!
Please enter your name here

Most Popular

Recent Comments