Untitled
unknown
plain_text
2 years ago
1.4 kB
1
Indexable
Never
def init(): list_ = [2, 1, 5, 0, 8, 4, 10, 0, 20, 10] return list_ def calc_cost(list_): list_1 = list_ cost = 0 for i in range(len(list_1)): list_2=list_[(i+1) : len(list_)] list_3 = [j for j in list_2 if list_1[i]>j ] cost = cost + len(list_3) return cost def state_generation(current_state, current_state_cost): min_cost = current_state_cost sli_state = current_state.copy() for i in range(len(current_state)-1): for j in range(i+1,len(current_state)): sli_state[i],sli_state[j] = sli_state[j],sli_state[i] eitar_cost = calc_cost(sli_state) if(eitar_cost < min_cost): min_cost = eitar_cost min_state = sli_state.copy() sli_state = current_state.copy() if(min_cost < current_state_cost): return min_state,min_cost else: return current_state,None def goal_test(state): if calc_cost(state) == 0 : return True else: return False def main(): state = init() cost = calc_cost(state) while(not goal_test(state)): state, cost = state_generation(state, cost) if cost is None: print(state) return print(state) return if __name__ == '__main__': main()