Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # def opt_dist(bin_list, D):
- # steps = []
- # ones = bin_list.count(1)
- # for i in range(0, (len(bin_list) - D)):
- # x = bin_list[i:i+D].count(1)
- # y = D - x
- # steps.append(y + ones - x)
- # return min(steps)
- def ones(zeros, n, D):
- result = []
- for _ in range(zeros):
- result.append(0)
- for _ in range(D):
- result.append(1)
- for _ in range(n - zeros - D):
- result.append(0)
- return result
- def sequences(n,D):
- result = []
- for i in range(n - D + 1):
- result.append(ones(i,n,D))
- return result
- def compare(sequence, bin_list):
- moves = 0
- for i in range(len(sequence)):
- if (sequence[i] == 1 and bin_list[i] == 0):
- moves += 1
- elif (sequence[i] == 0 and bin_list[i] == 1):
- moves += 1
- return moves
- def opt_dist(bin_list, D):
- n = len(bin_list)
- min_moves = n
- list_of_sequences = sequences(n,D)
- for seq in list_of_sequences:
- moves = compare(seq,bin_list)
- if(moves < min_moves):
- min_moves = moves
- return min_moves
- print(opt_dist([0,0,1,0,0,0,1,0,0,0], 5))
- print(opt_dist([0,0,1,0,0,0,1,0,0,0], 4))
- print(opt_dist([0,0,1,0,0,0,1,0,0,0], 3))
- print(opt_dist([0,0,1,0,0,0,1,0,0,0], 2))
- print(opt_dist([0,0,1,0,0,0,1,0,0,0], 1))
- print(opt_dist([0,0,1,0,0,0,1,0,0,0], 0))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement