def flip_the_number(x): return int(str(x)[::-1])
# Takes int or string input# Returns int- def flip_the_number(x):
return int(str(x) if len(str(x)) <= 1 else str(flip_the_number(str(x)[1:])) + str(x)[0])- return int(str(x)[::-1])
from random import randint test.describe("Basic Tests") test.assert_equals(flip_the_number("12345"), 54321) test.assert_equals(flip_the_number("1973572"), 2753791) test.assert_equals(flip_the_number(1973572), 2753791) test.describe("Random tests") for x in range(100): num = randint(0, 1000000000) if x % 2 == 0: test.assert_equals(flip_the_number(num), int(str(num)[::-1])) else: test.assert_equals(flip_the_number(str(num)), int(str(num)[::-1]))
test.assert_equals(flip_the_number("12345"), 54321);test.assert_equals(flip_the_number("1973572"), 2753791);test.assert_equals(flip_the_number(1973572), 2753791);- from random import randint
- test.describe("Basic Tests")
- test.assert_equals(flip_the_number("12345"), 54321)
- test.assert_equals(flip_the_number("1973572"), 2753791)
- test.assert_equals(flip_the_number(1973572), 2753791)
- test.describe("Random tests")
- for x in range(100):
- num = randint(0, 1000000000)
- if x % 2 == 0:
- test.assert_equals(flip_the_number(num), int(str(num)[::-1]))
- else:
- test.assert_equals(flip_the_number(str(num)), int(str(num)[::-1]))
import string def rubegoldberg(): alpha = string.ascii_lowercase find, total = "codewars", 1 for x, y in enumerate(find): if x % 2 == 0: total += alpha.index(y) else: total -= alpha.index(y) total -= len(find) return total
- import string
- def rubegoldberg():
return 2-1- alpha = string.ascii_lowercase
- find, total = "codewars", 1
- for x, y in enumerate(find):
- if x % 2 == 0:
- total += alpha.index(y)
- else:
- total -= alpha.index(y)
- total -= len(find)
- return total
palindrome=lambda s:s.lower()==s.lower()[::-1]
def palindrome(s): return s.lower() == s.lower()[::-1]- palindrome=lambda s:s.lower()==s.lower()[::-1]
import string from random import randint Test.describe("Basic Tests") test.assert_equals(palindrome("hello, world"), False) test.assert_equals(palindrome("racecar"), True) test.assert_equals(palindrome("tacocat"), True) Test.describe("Complex Tests") test.assert_equals(palindrome("ALLCAPS_SPACLLA"), True) test.assert_equals(palindrome(""), True) Test.describe("Random Tests") alpha = string.ascii_letters for x in range(100): strng = "".join([alpha[randint(0, 51)] for x in range(randint(50, 100))]) test.assert_equals(palindrome(strng), strng.lower()==strng.lower()[::-1])
- import string
- from random import randint
- Test.describe("Basic Tests")
- test.assert_equals(palindrome("hello, world"), False)
- test.assert_equals(palindrome("racecar"), True)
- test.assert_equals(palindrome("tacocat"), True)
- Test.describe("Complex Tests")
- test.assert_equals(palindrome("ALLCAPS_SPACLLA"), True)
- test.assert_equals(palindrome(""), True)
- Test.describe("Random Tests")
- alpha = string.ascii_letters
- for x in range(100):
- strng = "".join([alpha[randint(0, 51)] for x in range(randint(50, 100))])
- test.assert_equals(palindrome(strng), strng.lower()==strng.lower()[::-1])
Functions
Control Flow
Basic Language Features
Fundamentals
Added test cases
from random import randint Test.describe("Basic Tests") test.assert_equals(largest(2, 3 ,1), 3) test.assert_equals(largest(4, 5, 3), 5) test.assert_equals(largest(7, 8, 6), 8) Test.describe("Complex Tests") test.assert_equals(largest(-5, -10, -15), -5) test.assert_equals(largest(0, 0, 0), 0) Test.describe("Random Tests") for x in range(100): nums = [randint(-100000000, 100000000) for x in range(3)] test.assert_equals(largest(nums), max(nums))
Test.it('Basic Tests')test.assert_equals(largest(4,5,3), 5, "should return 5")- from random import randint
- Test.describe("Basic Tests")
- test.assert_equals(largest(2, 3 ,1), 3)
- test.assert_equals(largest(4, 5, 3), 5)
- test.assert_equals(largest(7, 8, 6), 8)
- Test.describe("Complex Tests")
- test.assert_equals(largest(-5, -10, -15), -5)
- test.assert_equals(largest(0, 0, 0), 0)
- Test.describe("Random Tests")
- for x in range(100):
- nums = [randint(-100000000, 100000000) for x in range(3)]
- test.assert_equals(largest(nums), max(nums))
def find_short(string): return len(sorted(string.split(" "),key=len)[0])
import java.util.stream.*;public class Kata {public static int findShort(String s) {return Stream.of(s.split(" ")).mapToInt(String::length).min().getAsInt();}}- def find_short(string):
- return len(sorted(string.split(" "),key=len)[0])
from random import randint import string Test.describe("Basic Tests") test.assert_equals(find_short("bitcoin take over the world maybe who knows perhaps"), 3) test.assert_equals(find_short("turns out random test cases are easier than writing out basic ones"), 3) test.assert_equals(find_short("this is a very long basic test for this kumite"), 1) Test.describe("Basic Tests") test.assert_equals(find_short(""), 0) test.assert_equals(find_short("Hello_World!"), 12) Test.describe("Basic Tests") alpha = string.ascii_letters + string.punctuation for x in range(100): string = " ".join(["".join(alpha[randint(0, 83)] for x in range(50, 100)) for x in range(75, 100)]) test.assert_equals(find_short(string), len(sorted(string.split(" "), key=len)[0]))
import org.junit.Test;- from random import randint
- import string
import static org.junit.Assert.assertEquals;- Test.describe("Basic Tests")
- test.assert_equals(find_short("bitcoin take over the world maybe who knows perhaps"), 3)
- test.assert_equals(find_short("turns out random test cases are easier than writing out basic ones"), 3)
- test.assert_equals(find_short("this is a very long basic test for this kumite"), 1)
/*** Created by Javatlacati on 01/03/2017.*/public class KataTest {@Testpublic void findShort() throws Exception {assertEquals(3, Kata.findShort("bitcoin take over the world maybe who knows perhaps"));assertEquals(3, Kata.findShort("turns out random test cases are easier than writing out basic ones"));}- Test.describe("Basic Tests")
- test.assert_equals(find_short(""), 0)
- test.assert_equals(find_short("Hello_World!"), 12)
}- Test.describe("Basic Tests")
- alpha = string.ascii_letters + string.punctuation
- for x in range(100):
- string = " ".join(["".join(alpha[randint(0, 83)] for x in range(50, 100)) for x in range(75, 100)])
- test.assert_equals(find_short(string), len(sorted(string.split(" "), key=len)[0]))
Python Intepretation
import string def HeLlOwOrLddddd(strng): strng = [x for x in strng if x in string.ascii_letters] return "".join([strng[x].upper() if x % 2 == 0 else strng[x].lower() for x in range(len(strng))])
public class HeLlOwOrLddddd {public static String convert(String input) {String salida = "";boolean mayus = true;for (int i=0;i<input.length();i++){if (Character.isLetter(input.charAt(i))){if (mayus){salida+=Character.toUpperCase(input.charAt(i));mayus=false;}else{salida+=Character.toLowerCase(input.charAt(i));mayus=true;}}}return salida;- import string
}}- def HeLlOwOrLddddd(strng):
- strng = [x for x in strng if x in string.ascii_letters]
- return "".join([strng[x].upper() if x % 2 == 0 else strng[x].lower() for x in range(len(strng))])
import string from random import randint Test.describe("Basic Tests") test.assert_equals(HeLlOwOrLddddd("Hello World"), "HeLlOwOrLd") test.assert_equals(HeLlOwOrLddddd(" Ola QueAse"), "OlAqUeAsE") test.assert_equals(HeLlOwOrLddddd(" Ola QueAse"), "OlAqUeAsE") Test.describe("Complex Tests") test.assert_equals(HeLlOwOrLddddd(" "), "") test.assert_equals(HeLlOwOrLddddd("H^*@kJpcni(!0dcmniwp"), "HkJpCnIdCmNiWp") Test.describe("Random Tests") for x in range(100): alpha = string.ascii_letters + string.punctuation strng = "".join([alpha[randint(0, 83)] for x in range(randint(100, 150))]) proper_strng = [x for x in strng if x in string.ascii_letters] proper_strng = "".join([proper_strng[x].upper() if x % 2 == 0 else proper_strng[x].lower() for x in range(len(proper_strng))]) test.assert_equals(HeLlOwOrLddddd(strng), proper_strng)
import org.junit.Test;import static org.junit.Assert.assertEquals;import org.junit.runners.JUnit4;- import string
- from random import randint
// TODO: Replace examples and use TDD development by writing your own tests- Test.describe("Basic Tests")
- test.assert_equals(HeLlOwOrLddddd("Hello World"), "HeLlOwOrLd")
- test.assert_equals(HeLlOwOrLddddd(" Ola QueAse"), "OlAqUeAsE")
- test.assert_equals(HeLlOwOrLddddd(" Ola QueAse"), "OlAqUeAsE")
public class SolutionTest {@Testpublic void testSomething() {// assertEquals("expected", "actual");assertEquals("HeLlOwOrLd", HeLlOwOrLddddd.convert("Hello World"));assertEquals("OlAqUeAsE", HeLlOwOrLddddd.convert(" Ola QueAse"));}}- Test.describe("Complex Tests")
- test.assert_equals(HeLlOwOrLddddd(" "), "")
- test.assert_equals(HeLlOwOrLddddd("H^*@kJpcni(!0dcmniwp"), "HkJpCnIdCmNiWp")
- Test.describe("Random Tests")
- for x in range(100):
- alpha = string.ascii_letters + string.punctuation
- strng = "".join([alpha[randint(0, 83)] for x in range(randint(100, 150))])
- proper_strng = [x for x in strng if x in string.ascii_letters]
- proper_strng = "".join([proper_strng[x].upper() if x % 2 == 0 else proper_strng[x].lower() for x in range(len(proper_strng))])
- test.assert_equals(HeLlOwOrLddddd(strng), proper_strng)
def areMetaStrings(str1, str2): return sorted(str1.lower()) == sorted(str2.lower())
func areMetaStrings(_ str1: String, _ str2: String) -> Bool {return str1.lowercased().sorted() == str2.lowercased().sorted()}- def areMetaStrings(str1, str2):
- return sorted(str1.lower()) == sorted(str2.lower())
from random import randint, sample import string Test.describe("Simple Tests") test.assert_equals(areMetaStrings("geeks", "keegs"), True) test.assert_equals(areMetaStrings("rsting", "string"), True) test.assert_equals(areMetaStrings("geeks", "peegs"), False) Test.describe("Complex Tests") test.assert_equals(areMetaStrings("", ""), True) test.assert_equals(areMetaStrings("Tom Marvolo Riddle ", "I Am Lord Voldemort"), True) Test.describe("Random Tests") alpha = string.ascii_lowercase for x in range(100): if x % 2 == 0: str1 = "".join([alpha[randint(0, 25)] for x in range(randint(100, 150))]) str2 = "".join(sample(str1, len(str1))) test.assert_equals(areMetaStrings(str1, str2), True) else: size = randint(100, 150) str1 = "".join([alpha[randint(0, 25)] for x in range(size)]) str2 = "".join([alpha[randint(0, 25)] for x in range(size)]) meta_string = sorted(str1.lower()) == sorted(str2.lower()) test.assert_equals(areMetaStrings(str1, str2), meta_string)
import XCTest// XCTest Spec Example:// TODO: replace with your own tests (TDD), these are just how-to examples to get you started- from random import randint, sample
- import string
class SolutionTest: XCTestCase {static var allTests = [("Test Example", testExample),]- Test.describe("Simple Tests")
- test.assert_equals(areMetaStrings("geeks", "keegs"), True)
- test.assert_equals(areMetaStrings("rsting", "string"), True)
- test.assert_equals(areMetaStrings("geeks", "peegs"), False)
func testExample() {XCTAssertTrue(areMetaStrings("geeks", "keegs"))XCTAssertTrue(areMetaStrings("rsting", "string"))XCTAssertFalse(areMetaStrings("geeks", "peegs"))}}- Test.describe("Complex Tests")
- test.assert_equals(areMetaStrings("", ""), True)
- test.assert_equals(areMetaStrings("Tom Marvolo Riddle ", "I Am Lord Voldemort"), True)
XCTMain([testCase(SolutionTest.allTests)])- Test.describe("Random Tests")
- alpha = string.ascii_lowercase
- for x in range(100):
- if x % 2 == 0:
- str1 = "".join([alpha[randint(0, 25)] for x in range(randint(100, 150))])
- str2 = "".join(sample(str1, len(str1)))
- test.assert_equals(areMetaStrings(str1, str2), True)
- else:
- size = randint(100, 150)
- str1 = "".join([alpha[randint(0, 25)] for x in range(size)])
- str2 = "".join([alpha[randint(0, 25)] for x in range(size)])
- meta_string = sorted(str1.lower()) == sorted(str2.lower())
- test.assert_equals(areMetaStrings(str1, str2), meta_string)
def rot13(s): rot = "abcdefghijklmnopqrstuvwxyzabcdefghijklm" return "".join([rot[rot.index(x)+13] for x in s])
- def rot13(s):
return rot.encode(s,"rot_13")- rot = "abcdefghijklmnopqrstuvwxyzabcdefghijklm"
- return "".join([rot[rot.index(x)+13] for x in s])
from random import randint from string import ascii_lowercase Test.describe("Basic Tests") test.assert_equals(rot13("thisisastring"), "guvfvfnfgevat") test.assert_equals(rot13("codewars"), "pbqrjnef") test.assert_equals(rot13("westwardland"), "jrfgjneqynaq") Test.describe("Complex Tests") test.assert_equals(rot13("nopqrstuvwxyz"), "abcdefghijklm") test.assert_equals(rot13('abcdefghijklmnopqrstuvwxyz'), 'nopqrstuvwxyzabcdefghijklm') Test.describe("Random Tests") for x in range(100): string = "".join([ascii_lowercase[randint(0, 25)] for x in range(randint(100, 150))]) test.assert_equals(rot13(string), rot.encode(string,"rot_13"))
# TODO: Replace examples and use TDD development by writing your own tests# These are some of the methods available:# test.expect(boolean, [optional] message)# test.assert_equals(actual, expected, [optional] message)# test.assert_not_equals(actual, expected, [optional] message)- from random import randint
- from string import ascii_lowercase
# You can use Test.describe and Test.it to write BDD style test groupings- Test.describe("Basic Tests")
- test.assert_equals(rot13("thisisastring"), "guvfvfnfgevat")
- test.assert_equals(rot13("codewars"), "pbqrjnef")
- test.assert_equals(rot13("westwardland"), "jrfgjneqynaq")
test.assert_equals(rot13('abcdefghijklmnopqrstuvwxyz'), 'nopqrstuvwxyzabcdefghijklm')- Test.describe("Complex Tests")
- test.assert_equals(rot13("nopqrstuvwxyz"), "abcdefghijklm")
- test.assert_equals(rot13('abcdefghijklmnopqrstuvwxyz'), 'nopqrstuvwxyzabcdefghijklm')
- Test.describe("Random Tests")
- for x in range(100):
- string = "".join([ascii_lowercase[randint(0, 25)] for x in range(randint(100, 150))])
- test.assert_equals(rot13(string), rot.encode(string,"rot_13"))
def calculator(a, operator, b): operations = {"+": lambda x, y: x + y, "-": lambda x, y: x - y, "*": lambda x, y: x * y, "/": lambda x, y: x / y} return operations[operator](a, b)
func calculator(_ int1: Int, _ operation: String, _ int2: Int) {//Code Here}- def calculator(a, operator, b):
- operations = {"+": lambda x, y: x + y, "-": lambda x, y: x - y,
- "*": lambda x, y: x * y, "/": lambda x, y: x / y}
- return operations[operator](a, b)
from random import randint, choice Test.describe("Basic Tests") Test.assert_equals(calculator(2, "-", 2), 0) Test.assert_equals(calculator(5, "+", 5), 10) Test.assert_equals(calculator(10, "-", 20), -10) Test.describe("Complex Test") Test.assert_equals(calculator(-10, "/", -5), 2.0) Test.assert_equals(calculator(100, "*", 0), 0) Test.describe("Random Test") for x in range(100): a = randint(-10000, 10000) b = randint(-10000, 10000) operator = choice(["+", "-", "*", "/"]) operations = {"+": lambda x, y: x + y, "-": lambda x, y: x - y, "*": lambda x, y: x * y, "/": lambda x, y: x / y} Test.assert_equals(calculator(a, operator, b), operations[operator](a, b))
import XCTest// XCTest Spec Example:// TODO: replace with your own tests (TDD), these are just how-to examples to get you started- from random import randint, choice
class SolutionTest: XCTestCase {static var allTests = [("Test Example", testExample),]- Test.describe("Basic Tests")
- Test.assert_equals(calculator(2, "-", 2), 0)
- Test.assert_equals(calculator(5, "+", 5), 10)
- Test.assert_equals(calculator(10, "-", 20), -10)
func testExample() {let actual = 1XCTAssertEqual(actual, 1)}}- Test.describe("Complex Test")
- Test.assert_equals(calculator(-10, "/", -5), 2.0)
- Test.assert_equals(calculator(100, "*", 0), 0)
XCTMain([testCase(SolutionTest.allTests)])- Test.describe("Random Test")
- for x in range(100):
- a = randint(-10000, 10000)
- b = randint(-10000, 10000)
- operator = choice(["+", "-", "*", "/"])
- operations = {"+": lambda x, y: x + y, "-": lambda x, y: x - y,
- "*": lambda x, y: x * y, "/": lambda x, y: x / y}
- Test.assert_equals(calculator(a, operator, b), operations[operator](a, b))
def middle_string(string): return string[1:-1] if len(string) >= 3 else ""
func substringWithoutFirstAndLastElement(_ string: String) -> String {guard string.count > 2 else { return "" }return String(String(string.dropFirst()).dropLast())}- def middle_string(string):
- return string[1:-1] if len(string) >= 3 else ""
import string from random import randint Test.describe("Basic Tests") Test.assert_equals(middle_string("a"), "") Test.assert_equals(middle_string("aa"), "") Test.assert_equals(middle_string("aba"), "b") Test.describe("Complex Test") Test.assert_equals(middle_string(""), "") Test.assert_equals(middle_string("dzlaednzed"), "zlaednze") Test.describe("Random Test") chars = string.printable[:-6] for x in range(100): string = "".join([chars[randint(0, 93)] for x in range(randint(50, 100))]) Test.assert_equals(middle_string(string), string[1:-1])
import XCTest// XCTest Spec Example:// TODO: replace with your own tests (TDD), these are just how-to examples to get you started- import string
- from random import randint
class SolutionTest: XCTestCase {static var allTests = [("Test Empty", testEmpty),("Test Single Character", testSingleCharacter),("Test Two Characters", testTwoCharacters),("Test Three Characters", testThreeCharacters),("Test More Than Three Characters", testMoreThanThreeCharacters),]- Test.describe("Basic Tests")
- Test.assert_equals(middle_string("a"), "")
- Test.assert_equals(middle_string("aa"), "")
- Test.assert_equals(middle_string("aba"), "b")
func testEmpty() {XCTAssertEqual(substringWithoutFirstAndLastElement(""), "")}func testSingleCharacter() {XCTAssertEqual(substringWithoutFirstAndLastElement("a"), "")}func testTwoCharacters() {XCTAssertEqual(substringWithoutFirstAndLastElement("aa"), "")}func testThreeCharacters() {XCTAssertEqual(substringWithoutFirstAndLastElement("aba"), "b")}func testMoreThanThreeCharacters() {XCTAssertEqual(substringWithoutFirstAndLastElement("dzlaednzed"), "zlaednze")}}- Test.describe("Complex Test")
- Test.assert_equals(middle_string(""), "")
- Test.assert_equals(middle_string("dzlaednzed"), "zlaednze")
XCTMain([testCase(SolutionTest.allTests)])- Test.describe("Random Test")
- chars = string.printable[:-6]
- for x in range(100):
- string = "".join([chars[randint(0, 93)] for x in range(randint(50, 100))])
- Test.assert_equals(middle_string(string), string[1:-1])
def divisibility_by_3(x): return str(x/3).endswith(".0")
- def divisibility_by_3(x):
x = abs(x)while x > 9:list_of_values = [int(d) for d in str(x)]x = sum(list_of_values)return x in [0, 3, 6, 9]- return str(x/3).endswith(".0")
import math from random import randint Test.describe("Basic Tests") Test.assert_equals(divisibility_by_3(454), False) Test.assert_equals(divisibility_by_3(222), True) Test.assert_equals(divisibility_by_3(10593), True) Test.describe("Complex Test") Test.assert_equals(divisibility_by_3(0), True) Test.assert_equals(divisibility_by_3(-10593), True) Test.describe("Random Test") for x in range(100): num = randint(-10000, 10000) Test.assert_equals(divisibility_by_3(num), str(num/3).endswith(".0"))
# TODO: Replace examples and use TDD development by writing your own tests# These are some of the methods available:# test.expect(boolean, [optional] message)# test.assert_equals(actual, expected, [optional] message)# test.assert_not_equals(actual, expected, [optional] message)- import math
- from random import randint
# You can use Test.describe and Test.it to write BDD style test groupingstest.assert_equals(divisibility_by_3(454), False)test.assert_equals(divisibility_by_3(222), True)test.assert_equals(divisibility_by_3(10593), True)test.assert_equals(divisibility_by_3(-10593), True)- Test.describe("Basic Tests")
- Test.assert_equals(divisibility_by_3(454), False)
- Test.assert_equals(divisibility_by_3(222), True)
- Test.assert_equals(divisibility_by_3(10593), True)
- Test.describe("Complex Test")
- Test.assert_equals(divisibility_by_3(0), True)
- Test.assert_equals(divisibility_by_3(-10593), True)
- Test.describe("Random Test")
- for x in range(100):
- num = randint(-10000, 10000)
- Test.assert_equals(divisibility_by_3(num), str(num/3).endswith(".0"))
import math def circle_area(r): return round(math.pi*r*r, 2) def circle_cir(r): return round(math.pi*r*2, 2)
function CircleArea(r){return Number((Math.PI*r*r).toFixed(2));}- import math
function CircleCir(r){return Number((Math.PI*r*2).toFixed(2));}- def circle_area(r):
- return round(math.pi*r*r, 2)
- def circle_cir(r):
- return round(math.pi*r*2, 2)
import math from random import randint Test.describe("Basic Tests") Test.assert_equals(circle_area(12), 452.39) Test.assert_equals(circle_area(0.57), 1.02) Test.assert_equals(circle_area(1), 3.14) Test.assert_equals(circle_cir(0.16), 1.01) Test.assert_equals(circle_cir(1), 6.28) Test.assert_equals(circle_cir(12), 75.4) Test.describe("Complex Test") Test.assert_equals(circle_area(0), 0) Test.assert_equals(circle_area(3.14), 30.97) Test.assert_equals(circle_cir(0), 0) Test.assert_equals(circle_cir(3.14), 19.73) Test.describe("Random Test") for x in range(100): if x % 2 == 0: num = randint(500, 1000) Test.assert_equals(circle_area(num), round(math.pi*num*num, 2)) else: num = randint(500, 1000) Test.assert_equals(circle_cir(num), round(math.pi*num*2, 2))
// 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.- import math
- from random import randint
describe("Solution", function() {it("should test for something", function() {Test.assertEquals(CircleArea(0), 0);Test.assertEquals(CircleArea(0.57), 1.02);Test.assertEquals(CircleArea(1), 3.14);Test.assertEquals(CircleCir(0), 0);Test.assertEquals(CircleCir(0.16), 1.01);Test.assertEquals(CircleCir(1), 6.28);// assert.strictEqual(1 + 1, 2);});});- Test.describe("Basic Tests")
- Test.assert_equals(circle_area(12), 452.39)
- Test.assert_equals(circle_area(0.57), 1.02)
- Test.assert_equals(circle_area(1), 3.14)
- Test.assert_equals(circle_cir(0.16), 1.01)
- Test.assert_equals(circle_cir(1), 6.28)
- Test.assert_equals(circle_cir(12), 75.4)
- Test.describe("Complex Test")
- Test.assert_equals(circle_area(0), 0)
- Test.assert_equals(circle_area(3.14), 30.97)
- Test.assert_equals(circle_cir(0), 0)
- Test.assert_equals(circle_cir(3.14), 19.73)
- Test.describe("Random Test")
- for x in range(100):
- if x % 2 == 0:
- num = randint(500, 1000)
- Test.assert_equals(circle_area(num), round(math.pi*num*num, 2))
- else:
- num = randint(500, 1000)
- Test.assert_equals(circle_cir(num), round(math.pi*num*2, 2))
highAndLow=lambda string:max(string.split(" "))+" "+min(string.split(" "))
import java.util.Arrays;import java.util.stream.Collectors;import java.util.List;public class Kumite {public static String highAndLow(String numbers) {List<Integer> list = Arrays.stream(numbers.split(" ")).map(Integer::valueOf).collect(Collectors.toList());return list.stream().max(Integer::compareTo).get() + " " + list.stream().min(Integer::compareTo).get();}}- highAndLow=lambda string:max(string.split(" "))+" "+min(string.split(" "))
from random import randint Test.describe("Basic Tests") Test.assert_equals(highAndLow("1 2 3 4 5"), "5 1") Test.assert_equals(highAndLow("9 4 2 7 3"), "9 2") Test.assert_equals(highAndLow("3 1 4 5 8"), "8 1") Test.assert_equals(highAndLow("1 4 5 3 6"), "6 1") Test.describe("Complex Test") Test.assert_equals(highAndLow("9 9 9 9"), "9 9") Test.assert_equals(highAndLow("1"), "1 1") Test.describe("Random Test") for x in range(100): string= "".join([str(randint(-1000, 1000)) for x in range(randint(500, 1000))]) Test.assert_equals(highAndLow(string), max(string.split(" "))+" "+min(string.split(" ")))
import org.junit.Test;import static org.junit.Assert.assertEquals;import org.junit.runners.JUnit4;- from random import randint
// TODO: Replace examples and use TDD development by writing your own tests- Test.describe("Basic Tests")
- Test.assert_equals(highAndLow("1 2 3 4 5"), "5 1")
- Test.assert_equals(highAndLow("9 4 2 7 3"), "9 2")
- Test.assert_equals(highAndLow("3 1 4 5 8"), "8 1")
- Test.assert_equals(highAndLow("1 4 5 3 6"), "6 1")
public class SolutionTest {@Testpublic void testSomething() {assertEquals("9 1", Kumite.highAndLow("1 4 2 6 1 6 1 5 2 8 9"));assertEquals("9 -10", Kumite.highAndLow("1 4 2 6 -10 6 1 5 2 8 9"));}}- Test.describe("Complex Test")
- Test.assert_equals(highAndLow("9 9 9 9"), "9 9")
- Test.assert_equals(highAndLow("1"), "1 1")
- Test.describe("Random Test")
- for x in range(100):
- string= "".join([str(randint(-1000, 1000)) for x in range(randint(500, 1000))])
- Test.assert_equals(highAndLow(string), max(string.split(" "))+" "+min(string.split(" ")))
def rotateLeft(array): return [[x[-i-1] for x in array] for i in range(len(array[0]))] def rotateRight(array): return [[x[i] for x in array][::-1] for i in range(len(array[0]))]
const rotateLeft = (arr) => {let retArr = [[],[],[]]for (let i = 0; i < arr.length; i++){for (let j = 0; j < arr[i].length; j++){retArr[i][j] = arr[j][arr.length - (i + 1)]}}return retArr}- def rotateLeft(array):
- return [[x[-i-1] for x in array] for i in range(len(array[0]))]
const rotateRight = (arr) => {let retArr = [[],[],[]]for (let i = 0; i < arr.length; i++){for (let j = 0; j < arr[i].length; j++){retArr[i][j] = arr[arr.length - (j + 1)][i]}}return retArr}- def rotateRight(array):
- return [[x[i] for x in array][::-1] for i in range(len(array[0]))]
from random import randint Test.describe("Basic Tests") Test.assert_equals(rotateLeft([[1,2,3],[4,5,6],[7,8,9]]), [[3,6,9],[2,5,8],[1,4,7]]) Test.assert_equals(rotateRight([[1,2,3],[4,5,6],[7,8,9]]), [[7,4,1],[8,5,2],[9,6,3]]) Test.assert_equals(rotateLeft([[3,6,9],[2,5,8],[1,4,7]]), [[9,8,7],[6,5,4],[3,2,1]]) Test.assert_equals(rotateRight([[7,4,1],[8,5,2],[9,6,3]]), [[9,8,7],[6,5,4],[3,2,1]]) Test.describe("Complex Test") Test.assert_equals(rotateLeft([[1,2,3,4,5],[6,7,8,9,1],[2,3,4,5,6]]), [[5,1,6],[4,9,5],[3,8,4],[2,7,3],[1,6,2]]) Test.assert_equals(rotateRight([[1,2,3,4,5],[6,7,8,9,1],[2,3,4,5,6]]), [[2,6,1],[3,7,2],[4,8,3],[5,9,4],[6,1,5]]) Test.assert_equals(rotateLeft([[1,2,3,4,5,6,7],[2,4,6,8,10,12,14],[3,6,9,12,15,18,21]]), [[7,14,21],[6,12,18],[5,10,15],[4,8,12],[3,6,9],[2,4,6],[1,2,3]]) Test.assert_equals(rotateRight([[1,2,3,4,5,6,7],[2,4,6,8,10,12,14],[3,6,9,12,15,18,21]]), [[3,2,1],[6,4,2],[9,6,3],[12,8,4],[15,10,5],[18,12,6],[21,14,7]]) Test.assert_equals(rotateLeft([[1]]), [[1]]) Test.assert_equals(rotateRight([[1]]), [[1]]) Test.describe("Random Test") for x in range(100): section = randint(10, 15) arr = [[randint(5, 30) for x in range(section)] for x in range(10, 15)] if x % 2 == 0: Test.assert_equals(rotateRight(arr), [[x[i] for x in arr][::-1] for i in range(len(arr[0]))]) else: Test.assert_equals(rotateLeft(arr), [[x[-i-1] for x in arr] for i in range(len(arr[0]))])
describe("Solution", function() {it("should test for something", function() {Test.assertEquals(rotateLeft([[1,2,3],[4,5,6],[7,8,9]]).toString(), [[3,6,9],[2,5,8],[1,4,7]].toString()),Test.assertEquals(rotateRight([[1,2,3],[4,5,6],[7,8,9]]).toString(), [[7,4,1],[8,5,2],[9,6,3]].toString())- from random import randint
});});- Test.describe("Basic Tests")
- Test.assert_equals(rotateLeft([[1,2,3],[4,5,6],[7,8,9]]), [[3,6,9],[2,5,8],[1,4,7]])
- Test.assert_equals(rotateRight([[1,2,3],[4,5,6],[7,8,9]]), [[7,4,1],[8,5,2],[9,6,3]])
- Test.assert_equals(rotateLeft([[3,6,9],[2,5,8],[1,4,7]]), [[9,8,7],[6,5,4],[3,2,1]])
- Test.assert_equals(rotateRight([[7,4,1],[8,5,2],[9,6,3]]), [[9,8,7],[6,5,4],[3,2,1]])
- Test.describe("Complex Test")
- Test.assert_equals(rotateLeft([[1,2,3,4,5],[6,7,8,9,1],[2,3,4,5,6]]), [[5,1,6],[4,9,5],[3,8,4],[2,7,3],[1,6,2]])
- Test.assert_equals(rotateRight([[1,2,3,4,5],[6,7,8,9,1],[2,3,4,5,6]]), [[2,6,1],[3,7,2],[4,8,3],[5,9,4],[6,1,5]])
- Test.assert_equals(rotateLeft([[1,2,3,4,5,6,7],[2,4,6,8,10,12,14],[3,6,9,12,15,18,21]]), [[7,14,21],[6,12,18],[5,10,15],[4,8,12],[3,6,9],[2,4,6],[1,2,3]])
- Test.assert_equals(rotateRight([[1,2,3,4,5,6,7],[2,4,6,8,10,12,14],[3,6,9,12,15,18,21]]), [[3,2,1],[6,4,2],[9,6,3],[12,8,4],[15,10,5],[18,12,6],[21,14,7]])
- Test.assert_equals(rotateLeft([[1]]), [[1]])
- Test.assert_equals(rotateRight([[1]]), [[1]])
- Test.describe("Random Test")
- for x in range(100):
- section = randint(10, 15)
- arr = [[randint(5, 30) for x in range(section)] for x in range(10, 15)]
- if x % 2 == 0:
- Test.assert_equals(rotateRight(arr), [[x[i] for x in arr][::-1] for i in range(len(arr[0]))])
- else:
- Test.assert_equals(rotateLeft(arr), [[x[-i-1] for x in arr] for i in range(len(arr[0]))])
This translation assumes that the first day of each month is a Sunday.
def day_finder(day, month, year): week = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"] dates = {"Jan":[31, 0], "Feb":[28, 31], "Mar":[31, 59], "Apr":[30, 90], "May":[31, 120], "Jun":[30, 151], "Jul":[31, 181], "Aug":[31, 212], "Sep":[30, 243], "Oct":[31, 273], "Nov":[30, 304], "Dec":[31, 334]} full_months = {"Jan":"January", "Feb":"February", "Mar":"March", "Apr":"April", "May":"May", "Jun":"June", "Jul":"July", "Aug":"August", "Sep":"September", "Oct":"October", "Nov":"November", "Dec":"December"} def week_day(num): inc = -1 for x in range(num): inc += 1 if inc == 7: inc = 0 return week[inc] if month not in dates: return "Invalid Date" if [year % x for x in [4, 100, 40]] == [0, 0, 0]: dates["Feb"][0] = 29 for x in dates: if x != "Jan": dates[x][1] += 1 if month in dates: if day >= 1 and day <= dates[month][0]: total_day = dates[month][1] + day day_type = week_day(day) total_mark = None mark = None if str(day).endswith("1") and str(day) != "11": mark = "st" elif str(day).endswith("2") and str(day) != "12": mark = "nd" elif str(day).endswith("3") and str(day) != "13": mark = "rd" else: mark = "th" if str(total_day).endswith("1") and str(day) != "11": total_mark = "st" elif str(total_day).endswith("2") and str(day) != "12": total_mark = "nd" elif str(total_day).endswith("3") and str(day) != "13": total_mark = "rd" else: total_mark = "th" return f"The {day}{mark} of {full_months[month]} is a {day_type} and the {total_day}{total_mark} day of {year}." return "Invalid Date"
import java.io.*;class Date{public static void main()throws IOException{BufferedReader in=new BufferedReader(new InputStreamReader(System.in));System.out.print("Enter the Date:(DD MM YYYY): ");String k=in.readLine();int d=Integer.parseInt(k.substring(0,2)), c,m=Integer.parseInt(k.substring(3,5)), y=Integer.parseInt(k.substring(6)), i, a1[]={0,31,28,31,30,31,30,31,31,30,31,30,31};String a2[]={"Sunday","Monday","Tuesday","Tuesday","Wednesday","Thursday","Friday","Saturday"};if((y%4==0 && y%100!=0)||y%400==0)a1[2]=29;if(m<1 || m>12 || d<1 || d>a1[m])System.out.println("Invalid Date..............");else{System.out.println("Valid Date.............");for(i=1;i<m;i++)d+=a1[i];c=d;for(i=1;i<y;i++)if((i%4==0 && i%100!=0)||i%400==0)d+=366;elsed+=365;System.out.println("The day value is: "+c+" days and thr day is "+a2[d%7]);}}}- def day_finder(day, month, year):
- week = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
- dates = {"Jan":[31, 0], "Feb":[28, 31], "Mar":[31, 59], "Apr":[30, 90],
- "May":[31, 120], "Jun":[30, 151], "Jul":[31, 181], "Aug":[31, 212],
- "Sep":[30, 243], "Oct":[31, 273], "Nov":[30, 304], "Dec":[31, 334]}
- full_months = {"Jan":"January", "Feb":"February", "Mar":"March", "Apr":"April",
- "May":"May", "Jun":"June", "Jul":"July", "Aug":"August", "Sep":"September",
- "Oct":"October", "Nov":"November", "Dec":"December"}
- def week_day(num):
- inc = -1
- for x in range(num):
- inc += 1
- if inc == 7:
- inc = 0
- return week[inc]
- if month not in dates:
- return "Invalid Date"
- if [year % x for x in [4, 100, 40]] == [0, 0, 0]:
- dates["Feb"][0] = 29
- for x in dates:
- if x != "Jan":
- dates[x][1] += 1
- if month in dates:
- if day >= 1 and day <= dates[month][0]:
- total_day = dates[month][1] + day
- day_type = week_day(day)
- total_mark = None
- mark = None
- if str(day).endswith("1") and str(day) != "11":
- mark = "st"
- elif str(day).endswith("2") and str(day) != "12":
- mark = "nd"
- elif str(day).endswith("3") and str(day) != "13":
- mark = "rd"
- else:
- mark = "th"
- if str(total_day).endswith("1") and str(day) != "11":
- total_mark = "st"
- elif str(total_day).endswith("2") and str(day) != "12":
- total_mark = "nd"
- elif str(total_day).endswith("3") and str(day) != "13":
- total_mark = "rd"
- else:
- total_mark = "th"
- return f"The {day}{mark} of {full_months[month]} is a {day_type} and the {total_day}{total_mark} day of {year}."
- return "Invalid Date"
Test.describe("Basic Test") Test.assert_equals(day_finder(2, "Jan", 2003), "The 2nd of January is a Monday and the 2nd day of 2003.") Test.assert_equals(day_finder(28, "Sep", 1936), 'The 28th of September is a Saturday and the 271st day of 1936.') Test.assert_equals(day_finder(12, "Mar", 2020), 'The 12th of March is a Thursday and the 71st day of 2020.') Test.assert_equals(day_finder(30, "Jun", 1999), 'The 30th of June is a Monday and the 181st day of 1999.') Test.assert_equals(day_finder(8, "Oct", 2010), 'The 8th of October is a Sunday and the 281st day of 2010.') test.describe("Complex Test") Test.assert_equals(day_finder(-13, "Oct", 2081), "Invalid Date") Test.assert_equals(day_finder(14, "Spooktober", 1874), "Invalid Date")
import org.junit.Test;import static org.junit.Assert.assertEquals;import org.junit.runners.JUnit4;- Test.describe("Basic Test")
- Test.assert_equals(day_finder(2, "Jan", 2003),
- "The 2nd of January is a Monday and the 2nd day of 2003.")
- Test.assert_equals(day_finder(28, "Sep", 1936),
- 'The 28th of September is a Saturday and the 271st day of 1936.')
- Test.assert_equals(day_finder(12, "Mar", 2020),
- 'The 12th of March is a Thursday and the 71st day of 2020.')
- Test.assert_equals(day_finder(30, "Jun", 1999),
- 'The 30th of June is a Monday and the 181st day of 1999.')
- Test.assert_equals(day_finder(8, "Oct", 2010),
- 'The 8th of October is a Sunday and the 281st day of 2010.')
// TODO: Replace examples and use TDD development by writing your own testspublic class SolutionTest {@Testpublic void testSomething() {// assertEquals("expected", "actual");up}}- test.describe("Complex Test")
- Test.assert_equals(day_finder(-13, "Oct", 2081), "Invalid Date")
- Test.assert_equals(day_finder(14, "Spooktober", 1874), "Invalid Date")