Ad
  • Custom User Avatar

    Gotcha! ehehehehhe

    Looks real, I believe it, thanks :3

  • Custom User Avatar

    I have a stupid question: When n is a constant, should we use is? ahhahaha

  • Custom User Avatar

    Yes, but time complexity of sorted() is O(n*log(n)) because behind used Timsort alg.

    edited or I miss understood something XD

    edited 1 and it is 5 loops i think

  • Custom User Avatar

    Okey ghost watcher, thx

  • Custom User Avatar

    Now I think it's O(1) or O(3n)

    edit
    I'm not remember what alg used in sorted func of python

    edit 1
    O(1) or O(n*log(n))

  • Custom User Avatar

    Why not use? Constant values affect the speed of program execution, a program using 2 loops instead of one will run slower.

  • Custom User Avatar

    According to my tests it isn't better. Check the seraph776 fork number 2

  • Custom User Avatar

    Yes sorry O(3n) on True, not O(n^3)

    Edited
    Mb because of the python version, I'm using 3.9, when codewars can use 3.8, 3.10, 3.11

  • Custom User Avatar

    Where is perfomance? Mb I did something wrong, but all of my tests shows that this is slower... And this ans to return False is O(n) or O(1) alg., to return True O(n^3). My ans returns True in O(1) or O(n), False O(n)...

    import time
    from pprint import pprint
    
    
    def timeit(to_call: int = 1000000):
        def _func(func: callable):
            def wrapper(*args, **kwargs):
                avg = [0.0 for i in range(to_call)]
                for i in range(to_call):
                    start = time.perf_counter()
                    res = func(*args, **kwargs)
                    end = time.perf_counter()
                    avg[i] += end - start
                # print(f'{func.__name__}: Average time: {sum(avg) / to_call}')
                return sum(avg) / to_call * 1_000_000
            return wrapper
        return _func
    
    
    @timeit()
    def knight_move3d(pos=(0, 0, 0)) -> bool:
        if pos == (0, 0, 0):
            return True
        if max(pos, key=abs) > 2 or 0 not in pos:
            return False
        return sum(map(abs, pos)) == 3
    
    
    @timeit()
    def using_math(position=(0, 0, 0)) -> bool:
        if position == (0, 0, 0):
            return True
    
        _sum = 0
        mul = 1
        for i in position:
    
            if i > 2 or -2 > i:
                return False
    
            _sum += abs(i)
            mul = mul * i
    
        return _sum == 3 and mul == 0
    
    
    if __name__ == '__main__':
    
        tests: list = []
        max_num = 5
        cols: int = 3
    
        cur_col: int = 0
        cur_nums: list = [-max_num for i in range(cols)]
        while sum(cur_nums) != max_num**cols+max_num+1:
            if cur_nums[cur_col] >= max_num:
                cur_col += 1
                if cur_col == cols:
                    break
    
            tests.append(cur_nums[::])
            cur_nums[cur_col] += 1
    
        pprint(tests)
        for ind, i in enumerate(tests):
    
            print(f'{ind}: 1. {using_math(i)-knight_move3d(i)}\n')```
    
  • Custom User Avatar

    And yes it's faster that the code before

  • Custom User Avatar