2022年8月24日 星期三

Note

 import sys                                                                                                                                                   import bisect

import math



def Lcm(a, b):

    return abs(a*b) // math.gcd(a, b)


def find_lt(a, x):

    'Find rightmost value less than x'

    i = bisect.bisect_left(a, x)

    if i:

        return a[i - 1]

    return 0


def find_gt(a, x):

    'Find leftmost value greater than x'

    i = bisect.bisect_right(a, x)

    if i != len(a):

        return a[i]

    return a[len(a) - 1]

if __name__ == "__main__":


    llp = int(sys.argv[1])

    ulp = int(sys.argv[2])


    llt = [i*llp for i in range(Lcm(llp, ulp) // llp + 1)]

    ult = [i*ulp for i in range(Lcm(llp, ulp) // ulp + 1)]

    via_size = int(sys.argv[3])

    prl = int(sys.argv[4])

    print(llt)

    print(ult)

    res = []

    space_list = []

    vios = 0

    vios_list = []

    for t in ult[:-1]:

        left_bnd = find_lt(llt, t)

        right_bnd = find_gt(llt, t)

        left_dis = t - left_bnd - via_size

        right_dis = right_bnd - t - via_size

        space_list.append((left_dis, right_dis))

        res.append((left_bnd, right_bnd))

        if left_dis < prl:

            vios = vios + 1

            vios_list.append(left_dis)

        if right_dis < prl:

            vios = vios + 1

            vios_list.append(right_dis)


    vios_list.sort()

    print(res)


    print(space_list)

    print("upper tracks: ", len(ult) - 1)

    print("vios: ", vios)

    print("vios_list: ", vios_list)   

2022年8月14日 星期日

Modify multiple lines after matching lines in Vim

Just note down issues i faced in vim.

Problem: (How to add prefix "#" before ever lines between "#For testing" and " ; ?)

Command: 
 insert # prefix      :g/^# For testing/+1,/^"/ norm! I#
 remove # prefix   :g/^# For testing/+1,/#^"/ norm! ^x

power g: :g/Pattern/cmd
pattern here is "^# For testing"

cmd here is +1, /^"/ norm! I#,  where +1, /^"/ is a ranges command which means that from next line of current line to line of matching pattern (which is ^" here).
norm! I# means that insert "#" at each lines in normal mode.
norm! ^x means that remove first character at each lines in normal mode . (^: begin of a line,  x: delete character)