Exemplo n.º 1
0
def test_and_print(got, expected):
    if got == expected:
        test.expect(True)
    else:
        print("<pre style='display:inline'>Got {}, expected {}</pre>".format(
            got, expected))
        test.expect(False)
Exemplo n.º 2
0
import codewars_test as Test

# Take the following IPv4 address: 128.32.10.1 This address has 4 octets where each octet is a single byte (or 8 bits).
#
# 1st octet 128 has the binary representation: 10000000
# 2nd octet 32 has the binary representation: 00100000
# 3rd octet 10 has the binary representation: 00001010
# 4th octet 1 has the binary representation: 00000001
# So 128.32.10.1 == 10000000.00100000.00001010.00000001
#
# Because the above IP address has 32 bits, we can represent it as the 32 bit number: 2149583361.
#
# Write a function ip_to_int32(ip) ( JS: ipToInt32(ip) ) that takes an IPv4 address and returns a 32 bit number.
#
#   ip_to_int32("128.32.10.1") => 2149583361

def ip_to_int32(ip):
  ipv6 = ""
  for number in ip.split("."):
      to_add = bin(int(number))[2:]
      if len(to_add) < 8:
          to_add = "0"*(8 - len(to_add)) + to_add
      ipv6 += to_add
  return int(ipv6, 2)


Test.describe("Basic Tests")
Test.expect(ip_to_int32("128.114.17.104") == 2154959208, "wrong integer for ip: 128.114.17.104")
Test.expect(ip_to_int32("0.0.0.0") == 0, "wrong integer for ip: 0.0.0.0")
Test.expect(ip_to_int32("128.32.10.1") == 2149583361, "wrong integer for ip: 128.32.10.1")
Exemplo n.º 3
0
#!/usr/bin/env python

import codewars_test as test
from calc import Calculator

test.it("Tests")
for key, val in {
        "127": 127,
        "2 + 3": 5,
        "2 - 3 - 4": -5,
        "10 * 5 / 2": 25,
        "2 / 2 + 3 * 4 - 6": 7,
        "2 + 3 * 4 / 3 - 6 / 3 * 3 + 8": 8,
        "1.1 + 2.2 + 3.3": 6.6,
        "1.1 * 2.2 * 3.3": 7.986
}.items():
    actual = Calculator().evaluate(key)
    test.expect(isinstance(actual, (float, int)),
                "Your result should be a number, not: " + str(type(actual)))
    test.expect(
        abs(actual - val) < 1e-12,
        "Expected %s == %s, but got %s" % (key, val, actual))
Exemplo n.º 4
0
# ------------  KATA DESCRIPTION ------------
"""
https://www.codewars.com/kata/515e271a311df0350d00000f

8 kyu - Square(n) Sum

Complete the square sum function so that it squares each number passed into it and then sums the results together.
For example, for [1, 2, 2] it should return 9 because 1^2 + 2^2 + 2^2 = 9.

"""

# ---------------  SOLUTION ---------------
import codewars_test as test


def square_sum(numbers):
    return sum(map(lambda x: x * x, numbers))


# ---------------  TEST CASES ---------------
test.expect(square_sum([1, 2]), 'squareSum did not return a value')
test.assert_equals(square_sum([1, 2]), 5)
test.assert_equals(square_sum([0, 3, 4, 5]), 50)
 def example_test_case():
     for name in rooms.values():
         test.expect(
             len(name) >= 3, f'Not enough properties for room: {name}')
 def example_test_case():
     for name in rooms.values():
         test.expect(isinstance(name, dict),
                     f'{name} should be a dictionary')
 def example_test_case():
     test.expect(len(rooms) >= 3, 'Should have at least three rooms')
Exemplo n.º 8
0
 def testing(act, exp):
     Test.expect(
         abs(act - exp) < 1e-6, "{} should equal {}".format(act, exp))
Exemplo n.º 9
0
def do():
    def testing(act, exp):
        Test.expect(
            abs(act - exp) < 1e-6, "{} should equal {}".format(act, exp))

    Test.it("Fixed tests")
    tests = [["2 + 3 * 4 / 3 - 6 / 3 * 3 + 8", 8], ["1 + 2 * 3 * 3 - 8", 11],
             ["1 + 2 * 3 * (5 - 2) - 8", 11],
             ["1 + 2 * 3 * (5 - (3 - 1)) - 8", 11], ["4 + -(1)", 3],
             ["4 - -(1)", 5], ["4 * -(1)", -4], ["4 / -(1)", -4], ["-1", -1],
             ["-(-1)", 1], ["-(-(-1))", -1], ["-(-(-(-1)))", 1],
             ["(((((-1)))))", -1], ["5 - 1", 4], ["5- 1", 4], ["5 -1", 4]]

    try:
        if calc("'x'") == "x": Test.expect(False, "Trying to use eval?")
    except:
        pass

    for test in tests:
        testing(calc(test[0]), test[1])

    Test.it("Random tests")
    for i in range(100):
        try:
            expression = "{}{} {} {}{} {} {}{} {} {}{} {} {}{} {} {}{} {} {}{} {} {}{}".format(
                random.choice(["-", ""]), random.randint(1, 100),
                random.choice(["+", "-", "*", "/"]), random.choice(["-", ""]),
                random.randint(1, 100), random.choice(["+", "-", "*", "/"]),
                random.choice(["-", ""]), random.randint(1, 100),
                random.choice(["+", "-", "*", "/"]), random.choice(["-", ""]),
                random.randint(1, 100), random.choice(["+", "-", "*", "/"]),
                random.choice(["-", ""]), random.randint(1, 100),
                random.choice(["+", "-", "*", "/"]), random.choice(["-", ""]),
                random.randint(1, 100), random.choice(["+", "-", "*", "/"]),
                random.choice(["-", ""]), random.randint(1, 100),
                random.choice(["+", "-", "*", "/"]), random.choice(["-", ""]),
                random.randint(1, 100))
            testing(calc(expression), eval(expression))
        except ZeroDivisionError:
            pass

    for i in range(100):
        try:
            expression = "{}({}{}) {} ({}{} {} {}{} {} {}({})) {} ({}{} {} {}((({}({}{} {} {}{})))) {} {}{})".format(
                random.choice(["-", ""]), random.choice(["-", ""]),
                random.randint(1, 100), random.choice(["+", "-", "*", "/"]),
                random.choice(["-", ""]), random.randint(1, 100),
                random.choice(["+", "-", "*", "/"]), random.choice(["-", ""]),
                random.randint(1, 100), random.choice(["+", "-", "*", "/"]),
                random.choice(["-", ""]), random.randint(1, 100),
                random.choice(["+", "-", "*", "/"]), random.choice(["-", ""]),
                random.randint(1, 100), random.choice(["+", "-", "*", "/"]),
                random.choice(["-", ""]), random.choice(["-", ""]),
                random.choice(["-", ""]), random.randint(1, 100),
                random.choice(["+", "-", "*", "/"]), random.choice(["-", ""]),
                random.randint(1, 100), random.choice(["+", "-", "*", "/"]),
                random.choice(["-", ""]), random.randint(1, 100))
            testing(calc(expression), eval(expression))
        except ZeroDivisionError:
            pass

    for i in range(100):
        expression = "{}{}- {}{}- {}{}- {}{}".format(random.choice(["-", ""]),
                                                     random.randint(1, 100),
                                                     random.choice(["-", ""]),
                                                     random.randint(1, 100),
                                                     random.choice(["-", ""]),
                                                     random.randint(1, 100),
                                                     random.choice(["-", ""]),
                                                     random.randint(1, 100))
        testing(calc(expression), eval(expression))
You can call it like so to make sure the name is unique

// at this point, the website has only one photo, hosted on the 'ABCDEF' url
photoManager.nameExists('ABCDEF'); // returns true
photoManager.nameExists('BBCDEF'); // returns false

Note: We consider two names with same letters but different cases to be unique.

"""


# ---------------  SOLUTION ---------------
import codewars_test as test
import random, string

def generateName():
    while True:
        name = ''.join(random.choice(string.ascii_letters) for _ in range(6))
        if not photoManager.nameExists(name):
            return name


# ---------------  TEST CASES ---------------
for i in range(10):
    name = generateName();

    test.expect(isinstance(name, str), "Name has to be a string.");
    test.expect(photoManager.nameWasUnique(name), "Name has to be unique.");
    test.assert_equals(len(name), 6, "Name has to be 6 digits long.");
    
# ---------------  SOLUTION ---------------
import codewars_test as test


def solution(digits):

    maximum = max(digits)

    index = -1
    numbers = []

    while True:
        index = digits.find(maximum, index + 1)
        numbers.append(digits[index:index + 5])
        if index == -1:
            break

    return int(max(numbers))


# ---------------  TEST CASES ---------------
number = "7316717653133062491922511967442657474235534919493496983520368542506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753123457977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257540920752963450"
actual = solution(number)

test.expect(actual != 0, 'solution returned zero')
test.expect(actual, 'solution did not return a value')
test.assert_equals(actual, 99890, 'solution did not return correct value')
test.assert_equals(solution('1234567898765'), 98765,
                   'Failed when max 5 digits is at end of number')
Exemplo n.º 12
0
    return None


# ---------------  TEST CASES ---------------
l1 = [1, 4, 8, 7, 3, 15]
l2 = [1, -2, 3, 0, -6, 1]
l3 = [20, -13, 40]
l4 = [1, 2, 3, 4, 1, 0]
l5 = [10, 5, 2, 3, 7, 5]
l6 = [4, -2, 3, 3, 4]
l7 = [0, 2, 0]
l8 = [5, 9, 13, -3]

test.describe("Testing For Sum of Pairs")
test.expect(
    sum_pairs(l1, 8) == [1, 7],
    "Basic: %s should return [1, 7] for sum = 8" % l1)
test.expect(
    sum_pairs(l2, -6) == [0, -6],
    "Negatives: %s should return [0, -6] for sum = -6" % l2)
test.expect(
    sum_pairs(l3, -7) == None,
    "No Match: %s should return None for sum = -7" % l3)
test.expect(
    sum_pairs(l4, 2) == [1, 1],
    "First Match From Left: %s should return [1, 1] for sum = 2 " % l4)
test.expect(
    sum_pairs(l5, 10) == [3, 7],
    "First Match From Left REDUX!: %s should return [3, 7] for sum = 10 " % l5)
test.expect(
    sum_pairs(l6, 8) == [4, 4],
Exemplo n.º 13
0
# ------------  KATA DESCRIPTION ------------
"""
8 kyu - Reversed Strings

Complete the solution so that it reverses the string value passed into it.

solution('world') # returns 'dlrow'
"""


# ---------------  SOLUTION ---------------
def solution(string):
    return string[::-1]


# ---------------  TEST CASES ---------------
import codewars_test as Test

Test.expect(solution('world') == 'dlrow')
Test.expect(solution('hello') == 'olleh')
Test.expect(solution('') == '')
Test.expect(solution('h') == 'h')
Exemplo n.º 14
0
import codewars_test as Test


def xo(s):
    return True if s.lower().count('o') == s.lower().count('x') else False


Test.expect(xo('xo'), 'True expected')
Test.expect(xo('xo0'), 'True expected')
Test.expect(not xo('xxxoo'), 'False expected')