#bmi_calculator = lambda w, h: f'there is{" no"*(w / h / h < 25)} excess weight' bmi_calculator=lambda w,h:f'there is{" no"*(w/h/h<25)} excess weight'
def bmi_calculator (weight, height):return "there is no excess weight" if weight / (height ** 2) < 25 else "there is excess weight"- #bmi_calculator = lambda w, h: f'there is{" no"*(w / h / h < 25)} excess weight'
- bmi_calculator=lambda w,h:f'there is{" no"*(w/h/h<25)} excess weight'
is_even=lambda*x:~x[int()]%(True+True) #This comment is allowed tho
is_even(){__asm("mov%rdi,%rax\nshr%rax\nshl%rax\ncmp%rax,%rdi\nsete%al\nleave\nret");}- is_even=lambda*x:~x[int()]%(True+True)
- #This comment is allowed tho
import re import codewars_test as test import solution @test.describe('Check') def test_group(): @test.it('Submission Check') def check_code(): code = (re.sub('#.*', '', open('solution.py').read()).strip()) test.expect(not re.findall('\s|\d', code), 'Should not have a space nor any number') @test.describe('Test') def test_group(): @test.it('Even case') def is_even(): for i in range(0, 30, 2): test.assert_equals(solution.is_even(i), True) @test.it('Odd case') def is_even(): for i in range(1, 30, 2): test.assert_equals(solution.is_even(i), False)
#include <criterion/criterion.h>#include <stdio.h>#include <stdlib.h>#include <ctype.h>- import re
- import codewars_test as test
- import solution
_Bool is_even(int);- @test.describe('Check')
- def test_group():
- @test.it('Submission Check')
- def check_code():
- code = (re.sub('#.*', '', open('solution.py').read()).strip())
- test.expect(not re.findall('\s|\d', code), 'Should not have a space nor any number')
Test(the_even_checker, return_true_for_even_numbers) {int evens[] = {2, 456, 987654};cr_assert(is_even(evens[0]));cr_assert(is_even(evens[1]));cr_assert(is_even(evens[2]));}Test(the_even_checker, return_false_for_odd_numbers) {int odds[] = {3, 123, 987654321};cr_assert_not(is_even(0[odds]));cr_assert_not(is_even(1[odds]));cr_assert_not(is_even(2[odds]));}Test(the_even_checker, should_not_contain_spaces_or_numbers) {FILE *fp = fopen("/workspace/solution.c", "r");if (!fp) exit(8);int c;while ((c = getc(fp)) != EOF) {if (isspace(c) | isdigit(c)) {fclose(fp);cr_assert_fail("Spaces and numbers are not allowed here");}}fclose(fp);cr_assert(1);}- @test.describe('Test')
- def test_group():
- @test.it('Even case')
- def is_even():
- for i in range(0, 30, 2):
- test.assert_equals(solution.is_even(i), True)
- @test.it('Odd case')
- def is_even():
- for i in range(1, 30, 2):
- test.assert_equals(solution.is_even(i), False)
sort = lambda x: ''.join(sorted(filter(str.isalnum, x), key=lambda y: (y.isdigit(), y)))
def sort(s):from string import ascii_lowercase, ascii_uppercasealphabet = list(ascii_uppercase) + list(ascii_lowercase) + list("0123456789")out = ""for letter in alphabet:for char in s:if char == letter:out += charreturn out- sort = lambda x: ''.join(sorted(filter(str.isalnum, x), key=lambda y: (y.isdigit(), y)))
#define true 1 #define false 0 int three_in(int x) { return x%10 == 3? true : (x > 9? three_in(x/10) : false); }
fn solution(x: i32) -> bool {x.to_string().chars().any(|x| x == '3')- #define true 1
- #define false 0
- int three_in(int x) {
- return x%10 == 3? true : (x > 9? three_in(x/10) : false);
- }
#include <criterion/criterion.h> int three_in(int); Test(ExampleTests, should_pass_all_the_tests_provided) { cr_assert_eq(three_in(2147483647), true); cr_assert_eq(three_in(2147482999), false); }
// Add your tests here.// See https://doc.rust-lang.org/stable/rust-by-example/testing/unit_testing.html- #include <criterion/criterion.h>
- int three_in(int);
#[cfg(test)]mod tests {use super::*;#[test]fn test_add() {assert_eq!(solution(525458), false);assert_eq!(solution(2222), false);assert_eq!(solution(354523), true)}}- Test(ExampleTests, should_pass_all_the_tests_provided) {
- cr_assert_eq(three_in(2147483647), true);
- cr_assert_eq(three_in(2147482999), false);
- }
Now what if we follow this rule: select one of item and repeat it for selected ranges
. Assuming the value will only repeat it once in the first place it's found
#spawn_to_range = lambda l, x, n: l[:(i:=l.index(x))] + [x]*n + l[i+1:] if x in l else l spawn_to_range=lambda l,x,n:l[:(i:=l.index(x))]+[x]*n+l[i+1:]if x in l else l
#spawn_to_range = s = lambda l, x, n: l[:(i:=l.index(x))] + [x]*n + s(l[i+1:], x, n) if x in l else lspawn_to_range=s=lambda l,x,n:l[:(i:=l.index(x))]+[x]*n+s(l[i+1:],x,n)if x in l else l- #spawn_to_range = lambda l, x, n: l[:(i:=l.index(x))] + [x]*n + l[i+1:] if x in l else l
- spawn_to_range=lambda l,x,n:l[:(i:=l.index(x))]+[x]*n+l[i+1:]if x in l else l
import codewars_test as test from solution import spawn_to_range @test.describe("Example") def test_group(): @test.it("test case") def test_case(): test.assert_equals(spawn_to_range([], 13, 4), []) test.assert_equals(spawn_to_range([13, 13], 13, 4), [13, 13, 13, 13, 13]) test.assert_equals(spawn_to_range([0, 10, 20, 13], 13, 4), [0, 10, 20, 13, 13, 13, 13]) test.assert_equals(spawn_to_range([0, 10, 12, 14], 10, 3), [0, 10, 10, 10, 12, 14]) test.assert_equals(spawn_to_range([0, 10, 12, 14, 10], 10, 3), [0, 10, 10, 10, 12, 14, 10]) test.assert_equals(spawn_to_range([0, 1, 1, 12, 14], 1, 3), [0, 1, 1, 1, 1, 12, 14])
- import codewars_test as test
- from solution import spawn_to_range
- @test.describe("Example")
- def test_group():
- @test.it("test case")
- def test_case():
- test.assert_equals(spawn_to_range([], 13, 4), [])
test.assert_equals(spawn_to_range([13], 13, 4), [13, 13, 13, 13])- test.assert_equals(spawn_to_range([13, 13], 13, 4), [13, 13, 13, 13, 13])
- test.assert_equals(spawn_to_range([0, 10, 20, 13], 13, 4), [0, 10, 20, 13, 13, 13, 13])
- test.assert_equals(spawn_to_range([0, 10, 12, 14], 10, 3), [0, 10, 10, 10, 12, 14])
test.assert_equals(spawn_to_range([0, 10, 12, 14, 10], 10, 3), [0, 10, 10, 10, 12, 14, 10, 10, 10])test.assert_equals(spawn_to_range([0, 1, 1, 12, 14], 1, 3), [0, 1, 1, 1, 1, 1, 1, 12, 14])- test.assert_equals(spawn_to_range([0, 10, 12, 14, 10], 10, 3), [0, 10, 10, 10, 12, 14, 10])
- test.assert_equals(spawn_to_range([0, 1, 1, 12, 14], 1, 3), [0, 1, 1, 1, 1, 12, 14])
Assuming that the value can appear more than once (even though it says select one of item and repeat it for selected ranges
), you can do it like this.
#spawn_to_range = s = lambda l, x, n: l[:(i:=l.index(x))] + [x]*n + s(l[i+1:], x, n) if x in l else l spawn_to_range=s=lambda l,x,n:l[:(i:=l.index(x))]+[x]*n+s(l[i+1:],x,n)if x in l else l
def spawn_to_range(arr, val, ran):return- #spawn_to_range = s = lambda l, x, n: l[:(i:=l.index(x))] + [x]*n + s(l[i+1:], x, n) if x in l else l
- spawn_to_range=s=lambda l,x,n:l[:(i:=l.index(x))]+[x]*n+s(l[i+1:],x,n)if x in l else l
import codewars_test as test from solution import spawn_to_range @test.describe("Example") def test_group(): @test.it("test case") def test_case(): test.assert_equals(spawn_to_range([], 13, 4), []) test.assert_equals(spawn_to_range([13], 13, 4), [13, 13, 13, 13]) test.assert_equals(spawn_to_range([0, 10, 20, 13], 13, 4), [0, 10, 20, 13, 13, 13, 13]) test.assert_equals(spawn_to_range([0, 10, 12, 14], 10, 3), [0, 10, 10, 10, 12, 14]) test.assert_equals(spawn_to_range([0, 10, 12, 14, 10], 10, 3), [0, 10, 10, 10, 12, 14, 10, 10, 10]) test.assert_equals(spawn_to_range([0, 1, 1, 12, 14], 1, 3), [0, 1, 1, 1, 1, 1, 1, 12, 14])
- import codewars_test as test
# TODO Write testsimport solution # or from solution import example- from solution import spawn_to_range
# test.assert_equals(actual, expected, [optional] message)- @test.describe("Example")
- def test_group():
- @test.it("test case")
- def test_case():
- test.assert_equals(spawn_to_range([], 13, 4), [])
- test.assert_equals(spawn_to_range([13], 13, 4), [13, 13, 13, 13])
- test.assert_equals(spawn_to_range([0, 10, 20, 13], 13, 4), [0, 10, 20, 13, 13, 13, 13])
- test.assert_equals(spawn_to_range([0, 10, 12, 14], 10, 3), [0, 10, 10, 10, 12, 14])
- test.assert_equals(spawn_to_range([0, 10, 12, 14, 10], 10, 3), [0, 10, 10, 10, 12, 14, 10, 10, 10])
- test.assert_equals(spawn_to_range([0, 1, 1, 12, 14], 1, 3), [0, 1, 1, 1, 1, 1, 1, 12, 14])
Using recursion approach that obviously is limited to the stack limit. Or you know just edit the recursion limit using sys
#collatz = c = lambda n, x=0: c( [ n//2, 3*n + 1 ][ n%2 ], x + 1 ) if n > 1 else x #collatz = c = lambda n: 1 + c( [ n//2, 3*n + 1 ][ n%2 ] ) if n > 1 else 0 collatz=c=lambda n:1+c([n//2,3*n+1][n%2])if n>1else 0
def collatz(n):attempts = 0while n != 1:if n % 2:n = (3*n+1) // 2attempts += 2else:n //= 2attempts += 1return attempts- #collatz = c = lambda n, x=0: c( [ n//2, 3*n + 1 ][ n%2 ], x + 1 ) if n > 1 else x
- #collatz = c = lambda n: 1 + c( [ n//2, 3*n + 1 ][ n%2 ] ) if n > 1 else 0
- collatz=c=lambda n:1+c([n//2,3*n+1][n%2])if n>1else 0
#grade_calc = lambda s: 'FDCBAA'[max(5, int(s//10)) - 5] if 0 <= s <= 100 else 'Not a grade' grade_calc = lambda s: 'FFFFFFDCBAA'[int(s//10)] if 0 <= s <= 100 else 'Not a grade'
def grade_calc(score):return 'Not a grade' if score not in range(101) else ["F", "F", "F", "F", "F", "F", "D", "C", "B", "A", "A"][score // 10]- #grade_calc = lambda s: 'FDCBAA'[max(5, int(s//10)) - 5] if 0 <= s <= 100 else 'Not a grade'
- grade_calc = lambda s: 'FFFFFFDCBAA'[int(s//10)] if 0 <= s <= 100 else 'Not a grade'
# 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(grade_calc(100),'A') test.assert_equals(grade_calc(99.9),'A') test.assert_equals(grade_calc(90),'A') test.assert_equals(grade_calc(89.9),'B') test.assert_equals(grade_calc(89),'B') test.assert_equals(grade_calc(80),'B') test.assert_equals(grade_calc(79.9),'C') test.assert_equals(grade_calc(79),'C') test.assert_equals(grade_calc(70),'C') test.assert_equals(grade_calc(69.9),'D') test.assert_equals(grade_calc(69),'D') test.assert_equals(grade_calc(60),'D') test.assert_equals(grade_calc(59),'F') test.assert_equals(grade_calc(0.1),'F') test.assert_equals(grade_calc(0),'F') test.assert_equals(grade_calc(100.1),'Not a grade') test.assert_equals(grade_calc(-27),'Not a grade') test.assert_equals(grade_calc(127),'Not a grade')
- # 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(grade_calc(100),'A')
test.assert_equals(grade_calc(96),'A')test.assert_equals(grade_calc(30),'F')test.assert_equals(grade_calc(70),'C')test.assert_equals(grade_calc(79),'C')- test.assert_equals(grade_calc(99.9),'A')
- test.assert_equals(grade_calc(90),'A')
- test.assert_equals(grade_calc(89.9),'B')
- test.assert_equals(grade_calc(89),'B')
- test.assert_equals(grade_calc(80),'B')
- test.assert_equals(grade_calc(79.9),'C')
- test.assert_equals(grade_calc(79),'C')
- test.assert_equals(grade_calc(70),'C')
- test.assert_equals(grade_calc(69.9),'D')
- test.assert_equals(grade_calc(69),'D')
- test.assert_equals(grade_calc(60),'D')
- test.assert_equals(grade_calc(59),'F')
- test.assert_equals(grade_calc(0.1),'F')
- test.assert_equals(grade_calc(0),'F')
- test.assert_equals(grade_calc(100.1),'Not a grade')
- test.assert_equals(grade_calc(-27),'Not a grade')
- test.assert_equals(grade_calc(127),'Not a grade')
import re; count_vowel = lambda x: len(re.sub('(?i)[^aiueo]', '', x))
def count_vowel(s):return len([1 for i in s.lower() if i in 'aeiou'])- import re; count_vowel = lambda x: len(re.sub('(?i)[^aiueo]', '', x))
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(count_vowel('Eat, Sleep, Code, Repeat'), 9) test.assert_equals(count_vowel('Python is better than Javascript!'), 8) test.assert_equals(count_vowel('Codewars Kumite'), 6) test.assert_equals(count_vowel('Hahahahaha!'), 5) test.assert_equals(count_vowel('AAaaIIiiUUuuEEeeOOoo1234567890`~!@#$%^&*()_+-={}|[]\:;"\'<>?,./!'), 20)
- 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(count_vowel('Eat, Sleep, Code, Repeat'), 9)
- test.assert_equals(count_vowel('Python is better than Javascript!'), 8)
- test.assert_equals(count_vowel('Codewars Kumite'), 6)
test.assert_equals(count_vowel('Hahahahaha!'),5)- test.assert_equals(count_vowel('Hahahahaha!'), 5)
- test.assert_equals(count_vowel('AAaaIIiiUUuuEEeeOOoo1234567890`~!@#$%^&*()_+-={}|[]\:;"\'<>?,./!'), 20)
def convert_binary_decimal(number): return sum(int(val)*2**power for power, val in enumerate(str(number)[::-1]))
- def convert_binary_decimal(number):
number = [int(figure) for figure in str(number)]number.reverse()return sum(number[bit]*2**bit for bit in range(len(number)))- return sum(int(val)*2**power for power, val in enumerate(str(number)[::-1]))
Bitwise Not, shorter and shorter
is_even = lambda x: 1 + x&1
function isEven(input) {return true;}- is_even = lambda x: 1 + x&1
import codewars_test as test import solution @test.describe('Test') def test_group(): @test.it('Even case') def is_even(): for i in range(0, 30, 2): test.assert_equals(solution.is_even(i), True) @test.it('Odd case') def is_even(): for i in range(1, 30, 2): test.assert_equals(solution.is_even(i), False)
const expect = require("chai").expect;describe("Solution", function() {it("should be true for 4", function() {expect(isEven(4)).to.equal(true);});});- import codewars_test as test
- import solution
- @test.describe('Test')
- def test_group():
- @test.it('Even case')
- def is_even():
- for i in range(0, 30, 2):
- test.assert_equals(solution.is_even(i), True)
- @test.it('Odd case')
- def is_even():
- for i in range(1, 30, 2):
- test.assert_equals(solution.is_even(i), False)