Ad

reduced character count by 14
also made the test cases inline (:

Code
Diff
  • C=lambda n:type("",(),{"e":lambda v=min(n,key=abs,default=0):abs(v)if abs(v)in n else v,"n":n})
    • ClosestToZero=lambda n:type("",(),{"e":lambda v=min(n,key=abs,default=0):dict(zip(n,n)).get(abs(v),v),"n":n})
    • C=lambda n:type("",(),{"e":lambda v=min(n,key=abs,default=0):abs(v)if abs(v)in n else v,"n":n})
Test Cases
Diff
  • (lambda T=__import__("codewars_test",0,0,["assert_equals"]):T.describe("")(lambda C=__import__("solution",0,0,["C"]).C,A=T.assert_equals:A([C([7,5,9,1,4]).e(),C([7,-4,-3,-12,5,9,-2, 4]).e(),C([-5,-5]).e(),C([]).e(),C([-5,0,1,5]).e(),C([-5,-1,1,5]).e()],[1,-2,-5,0,0,1])))()
    • import codewars_test as test
    • from solution import ClosestToZero
    • @test.describe("Example")
    • def test_group():
    • @test.it("test case")
    • def test_case():
    • test.assert_equals(ClosestToZero([7, 5, 9, 1, 4]).e(), 1)
    • test.assert_equals(ClosestToZero([7,-4, -3, -12, 5, 9, -2, 4]).e(), -2)
    • test.assert_equals(ClosestToZero([-5, -5]).e(), -5)
    • test.assert_equals(ClosestToZero([]).e(), 0)
    • test.assert_equals(ClosestToZero([-5, 0, 1, 5]).e(), 0)
    • test.assert_equals(ClosestToZero([-5, -1, 1, 5]).e(), 1)
    • (lambda T=__import__("codewars_test",0,0,["assert_equals"]):T.describe("")(lambda C=__import__("solution",0,0,["C"]).C,A=T.assert_equals:A([C([7,5,9,1,4]).e(),C([7,-4,-3,-12,5,9,-2, 4]).e(),C([-5,-5]).e(),C([]).e(),C([-5,0,1,5]).e(),C([-5,-1,1,5]).e()],[1,-2,-5,0,0,1])))()

1 Line class solution (I think this is minimum characters)

Code
Diff
  • ClosestToZero=lambda n:type("",(),{"execute":lambda v=min(n,key=abs,default=0):dict(zip(n,n)).get(abs(v),v),"n":n})
    • class ClosestToZero:
    • def __init__(s,p):s.p=p
    • def execute(s):
    • v=min(s.p,key=abs,default=0)
    • a=abs(v)
    • return a if a in s.p else v
    • ClosestToZero=lambda n:type("",(),{"execute":lambda v=min(n,key=abs,default=0):dict(zip(n,n)).get(abs(v),v),"n":n})

smol

You have received two names.

Verify if the sum of the ASCII codes of the letters of the first name is the same as sum of the ASCII codes of the letters of the second name. If the either name is none or an empty string, output None.

For example:

'''
"Anna" = 65 + 110 + 110 + 97 = 382
"Nana" = 78 + 97 + 110 + 97 = 382
'''
print(Kumite("Anna", "Nana").solve()) # True
print(Kumite("Sebastian","Patricia").solve()) # False
print(Kumite("John", None).solve()) # None
print(Kumite("", "Albert").solve()) # None
Code
Diff
  • Kumite=lambda*a:type("",(),{"solve":lambda f=lambda b:sum(map(lambda c:ord(c),a[b])):not f(0)-f(1)if set([None,""]).isdisjoint(a)else None})
    • class Kumite:
    • def __init__(self, name1, name2):
    • self.name1 = name1
    • self.name2 = name2
    • def solve(self):
    • return None if not self.name1 or not self.name2 else (
    • sum([ord(t)for t in self.name1]) == sum([ord(t) for t in self.name2]))
    • Kumite=lambda*a:type("",(),{"solve":lambda f=lambda b:sum(map(lambda c:ord(c),a[b])):not f(0)-f(1)if set([None,""]).isdisjoint(a)else None})
Code
Diff
  • class ClosestToZero:
     def __init__(s,p):s.p=p
     def execute(s):
      v=min(s.p,key=abs,default=0)
      a=abs(v)
      return a if a in s.p else v
    • class ClosetToZero:
    • """Return the integer in the params closest to zero"""
    • def __init__(self, params:list):
    • self.params = params
    • def execute(self):
    • if not self.params:
    • return 0
    • if min(self.params, key=abs) < 0 and abs(min(self.params, key=abs)) in self.params:
    • return abs(min(self.params, key=abs))
    • return min(self.params, key=abs)
    • class ClosestToZero:
    • def __init__(s,p):s.p=p
    • def execute(s):
    • v=min(s.p,key=abs,default=0)
    • a=abs(v)
    • return a if a in s.p else v
Test Cases
Diff
  • import codewars_test as test
    from solution import ClosestToZero
    
    @test.describe("Example")
    def test_group():
        @test.it("test case")
        def test_case():
            test.assert_equals(ClosestToZero([7, 5, 9, 1, 4]).execute(), 1)
            test.assert_equals(ClosestToZero([7,-4, -3, -12, 5, 9, -2, 4]).execute(), -2)
            test.assert_equals(ClosestToZero([-5, -5]).execute(), -5)
            test.assert_equals(ClosestToZero([]).execute(), 0)
            test.assert_equals(ClosestToZero([-5, 0, 1, 5]).execute(), 0)
            test.assert_equals(ClosestToZero([-5, -1, 1, 5]).execute(), 1)
    
    • import codewars_test as test
    • from solution import ClosetToZero
    • from solution import ClosestToZero
    • @test.describe("Example")
    • def test_group():
    • @test.it("test case")
    • def test_case():
    • test.assert_equals(ClosetToZero([7, 5, 9, 1, 4]).execute(), 1)
    • test.assert_equals(ClosetToZero([7,-4, -3, -12, 5, 9, -2, 4]).execute(), -2)
    • test.assert_equals(ClosetToZero([-5, -5]).execute(), -5)
    • test.assert_equals(ClosetToZero([]).execute(), 0)
    • test.assert_equals(ClosetToZero([-5, 0, 1, 5]).execute(), 0)
    • test.assert_equals(ClosetToZero([-5, -1, 1, 5]).execute(), 1)
    • test.assert_equals(ClosestToZero([7, 5, 9, 1, 4]).execute(), 1)
    • test.assert_equals(ClosestToZero([7,-4, -3, -12, 5, 9, -2, 4]).execute(), -2)
    • test.assert_equals(ClosestToZero([-5, -5]).execute(), -5)
    • test.assert_equals(ClosestToZero([]).execute(), 0)
    • test.assert_equals(ClosestToZero([-5, 0, 1, 5]).execute(), 0)
    • test.assert_equals(ClosestToZero([-5, -1, 1, 5]).execute(), 1)
Code
Diff
  • class MaxDigit:
        def __init__(s, n):
            s.n = n
    
        def execute(s):
            return int(''.join(sorted(str(s.n), reverse=True)))
    • class MaxDigit:
    • def __init__(self, n):
    • self.n = n
    • def __init__(s, n):
    • s.n = n
    • def execute(self):
    • return int(''.join(sorted(str(self.n), reverse=True)))
    • def execute(s):
    • return int(''.join(sorted(str(s.n), reverse=True)))