Ejemplo n.º 1
0
 def testing(actual, expected):
     Test.assert_equals(actual, expected)
Ejemplo n.º 2
0
            if temp not in include_prime:
                include_prime.append(temp)
            temp = temp / temp
            last_i = temp

    out_temp = n
    for i in include_prime:
        out_temp = out_temp * (1 - 1.0 / i)
    return int(out_temp)


if __name__ == "__main__":

    t0 = time.time()

    Test.assert_equals(proper_fractions(1), 0)
    Test.assert_equals(proper_fractions(2), 1)
    Test.assert_equals(proper_fractions(5), 4)
    Test.assert_equals(proper_fractions(15), 8)
    Test.assert_equals(proper_fractions(25), 20)
    Test.assert_equals(proper_fractions(12345656798), 82260072)

    print(time.time() - t0)
'''
def proper_fractions(n):
    phi = n > 1 and n
    for p in range(2, int(n ** .5) + 1):
        if not n % p:
            phi -= phi // p
            while not n % p:
                n //= p
Ejemplo n.º 3
0
    out_temp= []
    while array:
        out_temp.extend(array[0][:])
        array= rotate(array[1:])
    return out_temp


if __name__ == "__main__":

    t0 = time.perf_counter()

    array = [[1,2,3],
            [4,5,6],
            [7,8,9]]
    expected = [1,2,3,6,9,8,7,4,5]
    test.assert_equals(snail(array), expected)


    array = [[1,2,3],
            [8,9,4],
            [7,6,5]]
    expected = [1,2,3,4,5,6,7,8,9]
    test.assert_equals(snail(array), expected)


    print(time.perf_counter() - t0)




'''
            else:
                pass
        out_temp = out_temp[::-1]
        for i in range(min(out_flag, 2)):
            out_temp = out_temp.replace(" ,", format_t[i], 1)
        out_temp = out_temp[::-1]
        return out_temp
    else:
        return "now"


if __name__ == "__main__":

    #    t0 = time.perf_counter()

    test.assert_equals(format_duration(1), "1 second")
    test.assert_equals(format_duration(62), "1 minute and 2 seconds")
    test.assert_equals(format_duration(120), "2 minutes")
    test.assert_equals(format_duration(3600), "1 hour")
    test.assert_equals(format_duration(3662), "1 hour, 1 minute and 2 seconds")
    test.assert_equals(format_duration(31536000), "1 year")

#    print(time.perf_counter() - t0)
'''
import sys
sys.path.append('../')
import cw as test
#import time

debug = 1
def issquare(x):
    return sqrt(x) == int(sqrt(x))


def list_squared(m, n):
    out_list = []
    for x in range(m, n + 1):
        sum_t = sum((i**2 + (x // i)**2 * (x / i != i))
                    for i in range(1,
                                   int(sqrt(x)) + 1) if x % i == 0)
        if issquare(sum_t):
            out_list.append([x, sum_t])
    return out_list


if __name__ == "__main__":

    t0 = time.perf_counter()

    list_squared(394, 16049)
    Test.assert_equals(list_squared(1, 250),
                       [[1, 1], [42, 2500], [246, 84100]])
    Test.assert_equals(list_squared(42, 250), [[42, 2500], [246, 84100]])
    Test.assert_equals(list_squared(250, 500), [[287, 84100]])

    print(time.perf_counter() - t0)
'''
o(nlogn)
I think I'm best,hhhh!
'''
            if isinstance(array[array.index(0)], bool):
                pos.append(array.index(0) + bool_times)
                bool_times += 1
            else:
                array.append(0)
            array.remove(0)
        for i in pos:
            array.insert(i, False)
    else:
        pass
    return array


if __name__ == "__main__":
    Test.describe("Basic tests")
    Test.assert_equals(move_zeros([1, 2, 0, 1, 0, 1, 0, 3, 0, 1]),
                       [1, 2, 1, 1, 3, 1, 0, 0, 0, 0])
    Test.assert_equals(
        move_zeros(
            [9, 0.0, 0, 9, 1, 2, 0, 1, 0, 1, 0.0, 3, 0, 1, 9, 0, 0, 0, 0, 9]),
        [9, 9, 1, 2, 1, 1, 3, 1, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
    Test.assert_equals(
        move_zeros([
            "a", 0, 0, "b", "c", "d", 0, 1, 0, 1, 0, 3, 0, 1, 9, 0, 0, 0, 0, 9
        ]),
        ["a", "b", "c", "d", 1, 1, 3, 1, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
    Test.assert_equals(
        move_zeros([
            "a", 0, 0, "b", None, "c", "d", 0, 1, False, 0, 1, 0, 3, [], 0, 1,
            9, 0, 0, {}, 0, 0, 9
        ]), [
            "a", "b", None, "c", "d", 1, False, 1, 3, [], 1, 9, {}, 9, 0, 0, 0,
Ejemplo n.º 7
0
            else:
                pass
        else:
            if flag:
                out_temp.append([j, sum_temp])
            else:
                pass
    return out_temp


if __name__ == "__main__":

    t0 = time.perf_counter()

    a = [12, 15]
    test.assert_equals(sum_for_list(a), [[2, 12], [3, 27], [5, 15]])

    print(time.perf_counter() - t0)
'''
def last_digit(lst):
    if not lst:
        return 1
    else:
        out = 1
        for n in lst[len(lst):0:-1]:
            out = n**out
            if out > 2:
                out -= 2
                out %= 4
                out += 2
    return lst[0]**out% 10
Ejemplo n.º 8
0
    t1 = (count_str_alpha(s1))
    print(t1)
    t2 = (count_str_alpha(s2))
    print(t2)
    t2.update(t1)
    print(t2)
    return 0
    #count_alpha(count_str_alpha(s1))


if __name__ == "__main__":

    t0 = time.perf_counter()
    Test.describe("Mix")
    Test.it("Basic Tests")
    Test.assert_equals(mix("Are they here", "yes, they are here"),
                       "2:eeeee/2:yy/=:hh/=:rr")
    # Test.assert_equals(mix("looping is fun but dangerous", "less dangerous than coding"), "1:ooo/1:uuu/2:sss/=:nnn/1:ii/2:aa/2:dd/2:ee/=:gg")
    # Test.assert_equals(mix(" In many languages", " there's a pair of functions"), "1:aaa/1:nnn/1:gg/2:ee/2:ff/2:ii/2:oo/2:rr/2:ss/2:tt")
    # Test.assert_equals(mix("Lords of the Fallen", "gamekult"), "1:ee/1:ll/1:oo")
    # Test.assert_equals(mix("codewars", "codewars"), "")
    # Test.assert_equals(mix("A generation must confront the looming ", "codewarrs"), "1:nnnnn/1:ooooo/1:tttt/1:eee/1:gg/1:ii/1:mm/=:rr")

    print(time.perf_counter() - t0)
'''
def pick_peaks(arr):
    pos = []
    prob_peak = False
    for i in range(1, len(arr)):
        if arr[i] > arr[i-1]:
            prob_peak = i
        elif arr[i] < arr[i-1] and prob_peak:
Ejemplo n.º 9
0
    start_t = 1
    decimal.getcontext().prec = 7
    decimal.getcontext().rounding = decimal.ROUND_DOWN

    first_num = factorial(n)
    log_t = ceil(log(1000000, max(2, n)))
    if log_t < n:
        start_t = n - log_t
    debug_print(1, start_t)
    second_num = sum(factorial(x) for x in range(start_t, n + 1))
    return float(decimal.Decimal(second_num) / decimal.Decimal(first_num))


if __name__ == "__main__":
    print(factorial(100))
    test.assert_equals(going(5), 1.275)
    test.assert_equals(going(40), 1.2125)
    test.assert_equals(going(100), 1.173214)
    test.assert_equals(going(101), 1.005434)
    test.assert_equals(going(501), 1.005025)
    test.assert_equals(going(1000), 1.007246)

    print(going(40000))
'''
def going(n):  
    s = 1.0
    for i in range(2, n + 1):
        s = s/i + 1
    return int(s * 1e6) / 1e6
'''
Ejemplo n.º 10
0
    len_t =int(log10(n))+2
    for i in range(2,len_t) :
        num_temp = num_list[len_t-1-i:]
        temp=next_bigger_t(num_temp)
        if  temp!= -1:
            return n //10**i * 10**i +temp
        else :
            pass
    return -1



if __name__ == "__main__":

    t0 = time.perf_counter()
    Test.assert_equals(next_bigger(12),21)
    Test.assert_equals(next_bigger(513),531)
    Test.assert_equals(next_bigger(2017),2071)
    Test.assert_equals(next_bigger(414),441)
    Test.assert_equals(next_bigger(144),414)
    Test.assert_equals(next_bigger(54864190394230),54864190394302)
    Test.assert_equals(next_bigger(6119200482),6119200824)
    Test.assert_equals(next_bigger(996540),-1)
    Test.assert_equals(next_bigger(9545044),9545404)
    Test.assert_equals(next_bigger(6877),7678)
    Test.assert_equals(next_bigger(560086),560608)
    print(time.perf_counter() - t0)


'''
import itertools
    for i in range(1, len_t):
        for j in range(1, len_t - i + 1):
            slice_temp = int(string[i:i + j] + string[:i])
            find_first = slice_temp
            pos = find_position_from_n(string, find_first)
            if pos != -1:
                out_temp.append(pos + find_pos_for_n(find_first))
            else:
                pass
    return min(out_temp)


if __name__ == "__main__":

    t0 = time.perf_counter()
    test.assert_equals(find_position("456"), 3, "...3456...")
    test.assert_equals(find_position("454"), 79, "...444546...")
    test.assert_equals(find_position("455"), 98, "...545556...")
    test.assert_equals(find_position("001"), 190, "...100101...")
    test.assert_equals(find_position("00"), 190, "...100101...")
    test.assert_equals(find_position("99"), 168)
    test.assert_equals(find_position("091"), 170)
    test.assert_equals(find_position("0910"), 2927)
    #    test.assert_equals(find_position("555899959741198") ,1686722738828503)
    test.assert_equals(find_position("09991"), 35286)
    test.assert_equals(find_position("99940"), 14886)
    test.assert_equals(find_position("995"), 1387)
    test.assert_equals(find_position("58257860625"), 24674951477)

    print(time.perf_counter() - t0)
'''
            out_temp += right_rest
            left_t =  1
            right_t = right & (int(pow(2, right_log2)) - 1)
            out_temp += countOnes(left_t, right_t)
        else:
            pass
    return out_temp


if __name__ == "__main__":

    t0 = time.perf_counter()

    test.describe("Sample Tests")
    test.it('Easy')
    test.assert_equals(countOnes(5, 7), 7)
    test.assert_equals(countOnes(4, 7), 8)
    test.assert_equals(countOnes(12, 29), 51)
    test.assert_equals(countOnes(12, 2900000000000000), 51)   #test time

    print(time.perf_counter() - t0)



'''
import math

def count(n):
    if n is 0: return 0
    x = int(math.log(n, 2))
    return x * 2 ** (x - 1) + n - 2 ** x + 1 + count(n - 2 ** x)
Ejemplo n.º 13
0
#from math import *;circleIntersection=lambda a,b,r:int([r*r*(i-sin(i))for i in[2*acos(min(sqrt((a[0]-b[0])**2+(a[1]-b[1])**2)/2/r,1))]][0])

#from math import *;circleIntersection=lambda a,b,r:int([2*r*r*acos(i/r)-2*i*sqrt(r*r-i*i)if i<r else 0 for i in[sqrt((a[0]-b[0])**2+(a[1]-b[1])**2)/2]][0])

#code length is 130
from math import *
circleIntersection = lambda a, b, r: int([
    r * r * (i - sin(i))
    for i in [2 * acos(min(hypot(a[0] - b[0], a[1] - b[1]) / 2 / r, 1))]
][0])

if __name__ == "__main__":

    t0 = time.time()

    test.assert_equals(circleIntersection([0, 0], [7, 0], 5), 14)

    test.assert_equals(circleIntersection([0, 0], [0, 10], 10), 122)

    test.assert_equals(circleIntersection([5, 6], [5, 6], 3), 28)

    test.assert_equals(circleIntersection([-5, 0], [5, 0], 3), 0)

    test.assert_equals(circleIntersection([10, 20], [-5, -15], 20), 15)

    test.assert_equals(circleIntersection([-7, 13], [-25, -5], 17), 132)

    test.assert_equals(circleIntersection([-20, -4], [-40, 29], 7), 0)

    test.assert_equals(circleIntersection([38, -18], [46, -29], 10), 64)
            remainder_temp = remainder_temp_t
    return refer_temp


if __name__ == "__main__":

    t0 = time.perf_counter()

    test.it('Basic tests')
    test_data = [([], 1), ([0, 0], 1), ([0, 0, 0], 0), ([1, 2], 1),
                 ([3, 4, 5], 1), ([4, 3, 6], 4), ([7, 6, 21], 1),
                 ([12, 30, 21], 6), ([2, 2, 2, 0], 4),
                 ([937640, 767456, 981242], 0), ([123232, 694022, 140249], 6),
                 ([499942, 898102, 846073], 6)]
    for test_input, test_output in test_data:
        test.assert_equals(last_digit(test_input), test_output)

    print(time.perf_counter() - t0)
'''
def last_digit(lst):
    if not lst:
        return 1
    else:
        out = 1
        for n in lst[len(lst):0:-1]:
            out = n**out
            if out > 2:
                out -= 2
                out %= 4
                out += 2
    return lst[0]**out% 10
Ejemplo n.º 15
0
        print("temp" + str(flag) + ":" + str(out))


def duplicate_count(text):
    # Your code goes here
    out_temp = 0
    count_t = [0] * 128
    for temp in text:
        if ord(temp) >= ord('a')  :
            count_t[ord(temp)-32] += 1
        else:
            count_t[ord(temp)] += 1
    for i in count_t:
        if i > 1:
            out_temp += 1
    return out_temp


if __name__ == "__main__":

    test.assert_equals(duplicate_count("abcde"), 0)
    test.assert_equals(duplicate_count("abcdea"), 1)
    test.assert_equals(duplicate_count("indivisibility"), 1)
    test.assert_equals(duplicate_count("aabBcde"), 2)
    test.assert_equals(duplicate_count("aA11"), 2)


'''
def duplicate_count(s):
  return len([c for c in set(s.lower()) if s.lower().count(c)>1])
'''
def last_digit(n1, n2):
    out_temp=n1%10
    if n2==0:
        out_temp=1
    else :
        refer_temp=refer[out_temp]
        out_temp=refer_temp[(n2%len(refer_temp))-1]
    return out_temp


if __name__ == "__main__":

    t0 = time.perf_counter()

    Test.it("Example tests")
    Test.assert_equals(last_digit(4, 1), 4)
    Test.assert_equals(last_digit(4, 2), 6)
    Test.assert_equals(last_digit(9, 7), 9)
    Test.assert_equals(last_digit(10, 10 ** 10), 0)
    Test.assert_equals(last_digit(2 ** 200, 2 ** 300), 6)
    Test.assert_equals(last_digit(3715290469715693021198967285016729344580685479654510946723, 68819615221552997273737174557165657483427362207517952651), 7)

    Test.it("x ** 0")
    for nmbr in range(1, 9):
        a = nmbr ** nmbr
        Test.it("Testing %d and %d" % (a, 0))
        Test.assert_equals(last_digit(a, 0), 1, "x ** 0 must return 1")



    print(time.perf_counter() - t0)