示例#1
0
from tests import test as Test
# from tests import test

from code_wars.seven_segment_display import segment_display

Test.assert_equals(
    segment_display(123456),
    "|       |  ###  |  ###  |       |  ###  |  ###  |\n" +
    "|     # |     # |     # | #   # | #     | #     |\n" +
    "|     # |     # |     # | #   # | #     | #     |\n" +
    "|     # |     # |     # | #   # | #     | #     |\n" +
    "|       |  ###  |  ###  |  ###  |  ###  |  ###  |\n" +
    "|     # | #     |     # |     # |     # | #   # |\n" +
    "|     # | #     |     # |     # |     # | #   # |\n" +
    "|     # | #     |     # |     # |     # | #   # |\n" +
    "|       |  ###  |  ###  |       |  ###  |  ###  |")

Test.assert_equals(
    segment_display(987654),
    "|  ###  |  ###  |  ###  |  ###  |  ###  |       |\n" +
    "| #   # | #   # |     # | #     | #     | #   # |\n" +
    "| #   # | #   # |     # | #     | #     | #   # |\n" +
    "| #   # | #   # |     # | #     | #     | #   # |\n" +
    "|  ###  |  ###  |       |  ###  |  ###  |  ###  |\n" +
    "|     # | #   # |     # | #   # |     # |     # |\n" +
    "|     # | #   # |     # | #   # |     # |     # |\n" +
    "|     # | #   # |     # | #   # |     # |     # |\n" +
    "|  ###  |  ###  |       |  ###  |  ###  |       |")

Test.assert_equals(
    segment_display(0), "|       |       |       |       |       |  ###  |\n" +
        return False
    elif l == 2:
        if string[:1] == string[1:]:
            return True
    elif l % 2 == 0:
        while j < l:
            for i in range(j - 1):
                if string[(i * l) // j:((i + 1) * l) //
                          j] == string[((i + 1) * l) // j:((i + 2) * l) // j]:
                    count += 1
            j += 1
        return count > 1
    else:
        return False


Test.assert_equals(has_subpattern("a"), False)
Test.assert_equals(has_subpattern("aaaa"), True)
Test.assert_equals(has_subpattern("abcd"), False)
Test.assert_equals(has_subpattern("abababab"), True)

Test.assert_equals(has_subpattern("ababababa"), False)

Test.assert_equals(has_subpattern("123a123a123a"), True)
Test.assert_equals(has_subpattern("123A123a123a"), False)
Test.assert_equals(has_subpattern("abbaabbaabba"), True)

Test.assert_equals(has_subpattern("abbabbabba"), False)

Test.assert_equals(has_subpattern("abcdabcabcd"), False)
示例#3
0
"""
Write a function that accepts a string, and returns true if it is in the form of a phone number.
Assume that any integer from 0-9 in any of the spots will produce a valid phone number.

Only worry about the following format:
(123) 456-7890 (don't forget the space after the close parentheses)

Examples:

validPhoneNumber("(123) 456-7890")  =>  returns true
validPhoneNumber("(1111)555 2345")  => returns false
validPhoneNumber("(098) 123 4567")  => returns false
"""

import re
from tests import test

pat = re.compile(r'^\(\d{3}\)\s\d{3}\-\d{4}$')

def validPhoneNumber(phoneNumber):
    return pat.search(phoneNumber) is not None

test.assert_equals(validPhoneNumber("(123) 456-7890"), True)

    def decode(self, string):
        return string.translate(self.rtable)


def encode(map1, map2, string):
    return "".join(map(lambda x: map2[map1.index(x)] if x in map1 else x, string))

def decode(map1, map2, string):
    return "".join(map(lambda x: map1[map2.index(x)] if x in map2 else x, string))

print(encode("abc", "edf", "a2xc"))
print(decode("abc", "edf", "e2xf"))

map1 = "abcdefghijklmnopqrstuvwxyz";
map2 = "etaoinshrdlucmfwypvbgkjqxz";

from tests import test

cipher = Cipher(map1, map2)
test.assert_equals(cipher.encode("abc"), "eta");
test.assert_equals(cipher.encode("xyz"), "qxz");
test.assert_equals(cipher.decode("eirfg"), "aeiou");
test.assert_equals(cipher.decode("erlang"), "aikcfu");

map2 = 'tfozcivbsjhengarudlkpwqxmy';
cipher = Cipher(map1, map2);
test.assert_equals(cipher.encode('abc'), 'tfo');
test.assert_equals(cipher.decode('tfo'), 'abc');
test.assert_equals(cipher.decode('kjpphi'), 'tjuukf');
test.assert_equals(cipher.encode('ajqqtb'), 'tjuukf');
def encode(map1, map2, string):
    return "".join(
        map(lambda x: map2[map1.index(x)] if x in map1 else x, string))


def decode(map1, map2, string):
    return "".join(
        map(lambda x: map1[map2.index(x)] if x in map2 else x, string))


print(encode("abc", "edf", "a2xc"))
print(decode("abc", "edf", "e2xf"))

map1 = "abcdefghijklmnopqrstuvwxyz"
map2 = "etaoinshrdlucmfwypvbgkjqxz"

from tests import test

cipher = Cipher(map1, map2)
test.assert_equals(cipher.encode("abc"), "eta")
test.assert_equals(cipher.encode("xyz"), "qxz")
test.assert_equals(cipher.decode("eirfg"), "aeiou")
test.assert_equals(cipher.decode("erlang"), "aikcfu")

map2 = 'tfozcivbsjhengarudlkpwqxmy'
cipher = Cipher(map1, map2)
test.assert_equals(cipher.encode('abc'), 'tfo')
test.assert_equals(cipher.decode('tfo'), 'abc')
test.assert_equals(cipher.decode('kjpphi'), 'tjuukf')
test.assert_equals(cipher.encode('ajqqtb'), 'tjuukf')
示例#6
0
def f1(x):
    return x * 2


def f2(x):
    return x + 2


def f3(x):
    return x**2


def f4(x):
    return x.split()


def f5(xs):
    return [x[::-1].title() for x in xs]


def f6(xs):
    return "_".join(xs)


test.assert_equals(chained([f1, f2, f3])(0), 4)
test.assert_equals(chained([f1, f2, f3])(2), 36)
test.assert_equals(chained([f3, f2, f1])(2), 12)

test.assert_equals(
    chained([f4, f5, f6])("lorem ipsum dolor"), "Merol_Muspi_Rolod")