Given a 2D list, we want to extract a sublist given the starting point at the top-left (tuple of form (x,y)
), and the dimensions of the sublists x (horizontal size) and y (vertical size).
def extract_sublist(l, s, x, y):
res = []
for i in range(list(s)[1],y+list(s)[1]):
res.append(l[i][list(s)[0]:list(s)[0]+x])
return res
import codewars_test as test
# TODO Write tests
import solution # or from solution import example
# test.assert_equals(actual, expected, [optional] message)
@test.describe("Example")
def test_group():
@test.it("test case")
def test_case():
l=[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]
test.assert_equals(extract_sublist(l,(0,0),2,2),[[1,2],[5,6]])
test.assert_equals(extract_sublist(l,(1,1),2,2),[[6,7],[10,11]])
test.assert_equals(extract_sublist(l,(0,1),3,3),[[5,6,7],[9,10,11],[13,14,15]])
test.assert_equals(extract_sublist(l,(0,1),1,3),[[5],[9],[13]])
@test.describe("Kumite Tests") def tests(): @test.it("Testing...") def samples(): test.assert_equals(av(3),3) test.assert_equals(av(-3),3) test.assert_equals(av(-6),6)
// TODO: Add your tests here// Starting from Node 10.x, [Mocha](https://mochajs.org) is used instead of our custom test framework.// [Codewars' assertion methods](https://github.com/Codewars/codewars.com/wiki/Codewars-JavaScript-Test-Framework)// are still available for now.//// For new tests, using [Chai](https://chaijs.com/) is recommended.// You can use it by requiring:// const assert = require("chai").assert;// If the failure output for deep equality is truncated, `chai.config.truncateThreshold` can be adjusted.describe("Solution", function() {it("should test for something", function() {Test.assertEquals(av(-1),1);Test.assertEquals(av(-1.41313), 1.41313);Test.assertEquals(av(-22.22), 22.22);});});- @test.describe("Kumite Tests")
- def tests():
- @test.it("Testing...")
- def samples():
- test.assert_equals(av(3),3)
- test.assert_equals(av(-3),3)
- test.assert_equals(av(-6),6)
Bad practice to override exisitng module function.
factorial_=lambda x:reduce(lambda x,y:x*y,range(1,x+1))
def factorial(n):if n == 0:return 1else:recurse = factorial(n-1)result = n * recursereturn result# thats how i learned it from "think python"- factorial_=lambda x:reduce(lambda x,y:x*y,range(1,x+1))
import codewars_test as test # TODO Write tests import solution # or from solution import example # test.assert_equals(actual, expected, [optional] message) @test.describe("Example") def test_group(): @test.it("test case") def test_case(): test.assert_equals(factorial_(5), 120) test.assert_equals(factorial_(6), 720) test.assert_equals(factorial_(3), 6)
- import codewars_test as test
- # TODO Write tests
- import solution # or from solution import example
- # test.assert_equals(actual, expected, [optional] message)
- @test.describe("Example")
- def test_group():
- @test.it("test case")
- def test_case():
test.assert_equals(factorial(5), 120)test.assert_equals(factorial(6), 720)test.assert_equals(factorial(3), 6)- test.assert_equals(factorial_(5), 120)
- test.assert_equals(factorial_(6), 720)
- test.assert_equals(factorial_(3), 6)
def abbrev(s): return s[0] + str(len(s[1:-1])) + s[-1]
def abbrev(str):return str[0] + str(len(str[1::-1])) + str[-1]- def abbrev(s):
- return s[0] + str(len(s[1:-1])) + s[-1]
import codewars_test as test # TODO Write tests import solution # or from solution import example # test.assert_equals(actual, expected, [optional] message) @test.describe("Example") def test_group(): @test.it("test case") def test_case(): test.assert_equals(abbrev('abcdefghijklmnopqrstuvwxyz'), 'a24z')
- import codewars_test as test
- # TODO Write tests
- import solution # or from solution import example
- # test.assert_equals(actual, expected, [optional] message)
- @test.describe("Example")
- def test_group():
- @test.it("test case")
- def test_case():
test.assert_equals(1 + 1, 2)- test.assert_equals(abbrev('abcdefghijklmnopqrstuvwxyz'), 'a24z')
Used a lambda function instead.
Input should not be used as a variable/parameter name as it coincides with a method name.
special_fission=lambda s:list(filter(lambda x:x.isalpha(),list(s)))
def Special_Fission(input):return list(filter(lambda letter: letter.isalpha(), input))- special_fission=lambda s:list(filter(lambda x:x.isalpha(),list(s)))
import codewars_test as test @test.describe("Example 1") def test_group(): @test.it("test case") def test_case(): test.assert_equals(special_fission('Hello'), ['H', 'e', 'l', 'l', 'o']) @test.describe("Example 2") def test_group(): @test.it("test case 2") def test_case(): test.assert_equals(special_fission('Hello World!'), ['H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd']) @test.describe("Example 3") def test_group(): @test.it("test case 3") def test_case(): test.assert_equals(special_fission('!!!!'), []) @test.describe("Example 4") def test_group(): @test.it("test case 4") def test_case(): test.assert_equals(special_fission(''), [])
- import codewars_test as test
- @test.describe("Example 1")
- def test_group():
- @test.it("test case")
- def test_case():
test.assert_equals(Special_Fission('Hello'), ['H', 'e', 'l', 'l', 'o'])- test.assert_equals(special_fission('Hello'), ['H', 'e', 'l', 'l', 'o'])
- @test.describe("Example 2")
- def test_group():
- @test.it("test case 2")
- def test_case():
test.assert_equals(Special_Fission('Hello World!'), ['H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd'])- test.assert_equals(special_fission('Hello World!'), ['H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd'])
- @test.describe("Example 3")
- def test_group():
- @test.it("test case 3")
- def test_case():
test.assert_equals(Special_Fission('!!!!'), [])- test.assert_equals(special_fission('!!!!'), [])
- @test.describe("Example 4")
- def test_group():
- @test.it("test case 4")
- def test_case():
test.assert_equals(Special_Fission(''), [])- test.assert_equals(special_fission(''), [])
remove=lambda s:''.join([c for c in s if c.islower()])
CAPITAL_LETTERS = ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M','N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z')NUMBERS = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9')SPACE = (' ')def remove(ಠ‿ಠ):for category in (CAPITAL_LETTERS, NUMBERS, SPACE):for item in category:while item in ಠ‿ಠ:ಠ‿ಠ = remove(ಠ‿ಠ.replace(item, ''))return ಠ‿ಠ- remove=lambda s:''.join([c for c in s if c.islower()])
prod function is added as of 3.8, but it does not work on an empty list.
from math import prod def prod_(l): return prod(l) if len(l)>0 else 0
from functools import reducefrom operator import __mul__def prod(numbers):return reduce(__mul__, numbers) if numbers else 0- from math import prod
- def prod_(l):
- return prod(l) if len(l)>0 else 0
import codewars_test as test # TODO Write tests import solution # or from solution import example # test.assert_equals(actual, expected, [optional] message) @test.describe("Simple tests") def test_group(): @test.it("Short lists") def test_case(): test.assert_equals(prod_([1,1]), 1) test.assert_equals(prod_([]), 0) test.assert_equals(prod_([7, 2, -3, 8]), -336) test.assert_equals(prod_([239, 210, 100, 0, 67, -321, 1]), 0) test.assert_equals(prod_(range(1,10)), 362880)
- import codewars_test as test
- # TODO Write tests
- import solution # or from solution import example
- # test.assert_equals(actual, expected, [optional] message)
- @test.describe("Simple tests")
- def test_group():
- @test.it("Short lists")
- def test_case():
test.assert_equals(prod([1,1]), 1)test.assert_equals(prod([]), 0)test.assert_equals(prod([7, 2, -3, 8]), -336)test.assert_equals(prod([239, 210, 100, 0, 67, -321, 1]), 0)test.assert_equals(prod(range(1,10)), 362880)- test.assert_equals(prod_([1,1]), 1)
- test.assert_equals(prod_([]), 0)
- test.assert_equals(prod_([7, 2, -3, 8]), -336)
- test.assert_equals(prod_([239, 210, 100, 0, 67, -321, 1]), 0)
- test.assert_equals(prod_(range(1,10)), 362880)
I'm only using true
and operands this time
def returnhundred(s): return (True + True + True + True + True + True + True + True + True + True) ** (True + True)
def returnhundred(word):return 100- def returnhundred(s):
- return (True + True + True + True + True + True + True + True + True + True) ** (True + True)
test.assert_equals(returnhundred("hi"),100) test.assert_equals(returnhundred("Hello there"),100) test.assert_equals(returnhundred(""),100) test.assert_equals(returnhundred(12098),100) test.assert_equals(returnhundred(0),100)
test.assert_equals(returnhundred("hi"),100)test.assert_equals(returnhundred("Hello there"),100)test.assert_equals(returnhundred(""),100)test.assert_equals(returnhundred(12098),100)- test.assert_equals(returnhundred("hi"),100)
- test.assert_equals(returnhundred("Hello there"),100)
- test.assert_equals(returnhundred(""),100)
- test.assert_equals(returnhundred(12098),100)
- test.assert_equals(returnhundred(0),100)
Sum a 2D array/list, but try to do both the following:
- play code golf
- write performant code - push the amount of huge 1000000 * 1000000 lists multiplied further
f=lambda x:sum(sum(y)for y in x)
# small example
test.assert_equals(f([list(range(0,3)),list(range(3,6)),list(range(6,9))]),36)
# medium example
test.assert_equals(f([list(range(0,101))])*100,5050*100)
# big example
test.assert_equals(f([list(range(0,1001))])*1000,500500*1000)
# huge examples
for i in range(170):
test.expect(f([list(range(0,1000001))])*1000000,500000500000*1000000,'Test Failed') # using expect because list will be too big
6 chars saved
validateKey=(k,s=k.split('-'))=>k.length+s[0].length==14&&!isNaN(s[0])&&!isNaN(s[1])&&!'333-444-555-666-777-888-999'.includes(s[0])&&s[1]%7==0&&!'089'.includes(k.slice(-1))
const validateKey=(k,s=k.split('-'))=>k.length+s[0].length==14&&!isNaN(s[0])&&!isNaN(s[1])&&!'333-444-555-666-777-888-999'.includes(s[0])&&s[1]%7==0&&!'089'.includes(k.slice(-1))- validateKey=(k,s=k.split('-'))=>k.length+s[0].length==14&&!isNaN(s[0])&&!isNaN(s[1])&&!'333-444-555-666-777-888-999'.includes(s[0])&&s[1]%7==0&&!'089'.includes(k.slice(-1))