Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.38 KB | None | 0 0
  1. # def opt_dist(bin_list, D):
  2. #     steps = []
  3. #     ones = bin_list.count(1)
  4. #     for i in range(0, (len(bin_list) - D)):
  5. #         x = bin_list[i:i+D].count(1)
  6. #         y = D - x
  7. #         steps.append(y + ones - x)
  8. #     return min(steps)
  9.  
  10. def ones(zeros, n, D):
  11.     result = []
  12.     for _ in range(zeros):
  13.         result.append(0)
  14.     for _ in range(D):
  15.         result.append(1)
  16.     for _ in range(n - zeros - D):
  17.         result.append(0)
  18.     return result
  19.  
  20. def sequences(n,D):
  21.     result = []
  22.     for i in range(n - D + 1):
  23.         result.append(ones(i,n,D))
  24.     return result
  25.  
  26. def compare(sequence, bin_list):
  27.     moves = 0
  28.     for i in range(len(sequence)):
  29.         if (sequence[i] == 1 and bin_list[i] == 0):
  30.             moves += 1
  31.         elif (sequence[i] == 0 and bin_list[i] == 1):
  32.             moves += 1
  33.     return moves
  34.  
  35. def opt_dist(bin_list, D):
  36.     n = len(bin_list)
  37.     min_moves = n
  38.     list_of_sequences = sequences(n,D)
  39.  
  40.     for seq in list_of_sequences:
  41.         moves = compare(seq,bin_list)
  42.         if(moves < min_moves):
  43.             min_moves = moves
  44.    
  45.     return min_moves
  46.    
  47. print(opt_dist([0,0,1,0,0,0,1,0,0,0], 5))
  48. print(opt_dist([0,0,1,0,0,0,1,0,0,0], 4))
  49. print(opt_dist([0,0,1,0,0,0,1,0,0,0], 3))
  50. print(opt_dist([0,0,1,0,0,0,1,0,0,0], 2))
  51. print(opt_dist([0,0,1,0,0,0,1,0,0,0], 1))
  52. print(opt_dist([0,0,1,0,0,0,1,0,0,0], 0))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement