From f7fffbf74011f49a05a94b84d5e22a302e295824 Mon Sep 17 00:00:00 2001 From: Sagar Shiroya Date: Thu, 29 Dec 2016 16:08:42 +0530 Subject: [PATCH 1/3] Update garage.py Check for the case: initial = [3,1,2,0] final = [2,3,0,1] between state: [3,0,2,1] [0,3,2,1] [2,3,0,1] So only 3 moves needed while yourt program gives five moves. --- array/garage.py | 49 +++++++++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/array/garage.py b/array/garage.py index 88f1bf845..2e8a65a3d 100644 --- a/array/garage.py +++ b/array/garage.py @@ -17,32 +17,33 @@ # Each step swap with 0 only. -def garage(beg, end): - moves = 0 +def count_moves(beg,end): i = 0 - while beg != end : - if beg[i] != end[i] and beg[i] != 0: - car = beg[i] #car that we will move - empty = beg.index(0) - beg[beg.index(car)], beg[empty] = beg[empty], beg[beg.index(car)] - moves += 1 - ## move car that's not in correct place into free space - print(beg) - if beg.index(car) != end.index(car): - # if the recently moved car is still not in its correct place - # then we want to move another car into the free space where - # it will be in its correct position - beg[beg.index(end[i])] = 0 - beg[i] = end[i] - print(beg) - moves += 1 - i += 1 #move onto the next car, check again + count = 0 + while beg != end: + if beg[i] != 0 and beg[i] != end[i]: + current_car = beg[i] + empty_slot = beg.index(0) + final_pos = end.index(beg[i]) + if empty_slot != final_pos: + beg[final_pos], beg[empty_slot] = beg[empty_slot], beg[final_pos] + print beg + empty_slot = beg.index(0) + beg[beg.index(current_car)], beg[empty_slot] = beg[empty_slot], beg[beg.index(current_car)] + print beg + count += 2 + else: + beg[beg.index(current_car)], beg[empty_slot] = beg[empty_slot], beg[beg.index(current_car)] + print beg + count += 1 + i += 1 if i == len(beg): i = 0 - return moves - + return count + + initial = [1,2,3,0,4] final = [0,3,2,1,4] -print("initial:", initial) -print("final:", final) -print(garage(initial, final)) +print ('Initially:',initial) +print ('Final',final) +print count_moves(initial,final) From 29debf34c359cdee7d24b0cd6ae0cc616db2996e Mon Sep 17 00:00:00 2001 From: Sagar Shiroya Date: Thu, 29 Dec 2016 16:11:01 +0530 Subject: [PATCH 2/3] Update garage.py --- array/garage.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/array/garage.py b/array/garage.py index 2e8a65a3d..da9db0fe6 100644 --- a/array/garage.py +++ b/array/garage.py @@ -44,6 +44,6 @@ def count_moves(beg,end): initial = [1,2,3,0,4] final = [0,3,2,1,4] -print ('Initially:',initial) -print ('Final',final) -print count_moves(initial,final) +print("initial:", initial) +print("final:", final) +print(garage(initial, final)) From 30efc30ece5226f14b2484df463c12225b259e21 Mon Sep 17 00:00:00 2001 From: Sagar Shiroya Date: Thu, 29 Dec 2016 16:13:21 +0530 Subject: [PATCH 3/3] Update garage.py --- array/garage.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/array/garage.py b/array/garage.py index da9db0fe6..3d6678030 100644 --- a/array/garage.py +++ b/array/garage.py @@ -17,29 +17,29 @@ # Each step swap with 0 only. -def count_moves(beg,end): +def garage(beg, end): i = 0 - count = 0 + moves = 0 while beg != end: if beg[i] != 0 and beg[i] != end[i]: - current_car = beg[i] - empty_slot = beg.index(0) + car = beg[i] + empty = beg.index(0) final_pos = end.index(beg[i]) - if empty_slot != final_pos: - beg[final_pos], beg[empty_slot] = beg[empty_slot], beg[final_pos] + if empty != final_pos: + beg[final_pos], beg[empty] = beg[empty], beg[final_pos] print beg - empty_slot = beg.index(0) - beg[beg.index(current_car)], beg[empty_slot] = beg[empty_slot], beg[beg.index(current_car)] + empty = beg.index(0) + beg[beg.index(car)], beg[empty] = beg[empty], beg[beg.index(car)] print beg - count += 2 + moves += 2 else: - beg[beg.index(current_car)], beg[empty_slot] = beg[empty_slot], beg[beg.index(current_car)] + beg[beg.index(car)], beg[empty] = beg[empty], beg[beg.index(car)] print beg - count += 1 + moves += 1 i += 1 if i == len(beg): i = 0 - return count + return moves initial = [1,2,3,0,4]