Last active
February 18, 2020 13:56
-
-
Save primaryobjects/5ddc68b094d5affc492d2072e85be05b to your computer and use it in GitHub Desktop.
The Parking Lot Problem: A Google Interview Question for Software Engineers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| This question was found at https://www.careercup.com/question?id=5750868554022912 | |
| Suppose a row of parking lot with n spots, one of them is empty and n-1 spots are occupied with cars. | |
| Only one operation is allowed: move one car from its position to the empty spot. | |
| Given a initial order of cars and a final order, output steps needed to convert initial order to final oder with that operation. | |
| Solution | |
| ======== | |
| This type of planning problem can be solved using the AI planning algorithm STRIPS. | |
| Solution at: | |
| http://stripsfiddle.herokuapp.com/?d=n8edpSRhwdgQP4fq8&p=HnYPMpoRm6zf5F6SG&a=BFS | |
| Click "Run" to see a demo of it running! | |
| For a tutorial on STRIPS, see: | |
| "Artificial Intelligence Planning with STRIPS, A Gentle Introduction" | |
| http://www.primaryobjects.com/2015/11/06/artificial-intelligence-planning-with-strips-a-gentle-introduction/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Problem 1 | |
| ========= | |
| Start: {1 2 3 X 4 5} | |
| End: {X 2 3 1 4 5} | |
| Solution found in 1 steps! | |
| 1. move c1 s1 s4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Problem 2 | |
| ========= | |
| Start: {1 2 3 X 4 5} | |
| End: {5 1 X 3 2 4} | |
| Solution found in 6 steps! | |
| 1. move c1 s1 s4 | |
| 2. move c5 s6 s1 | |
| 3. move c4 s5 s6 | |
| 4. move c2 s2 s5 | |
| 5. move c1 s4 s2 | |
| 6. move c3 s3 s4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;; Suppose a row of parking lot with n spots, one of them is empty and n-1 spots are occupied with cars. | |
| ;; Only one operation is allowed: move one car from its position to the empty spot. | |
| ;; Given a initial order of cars and a final order, output steps needed to convert initial order to final order with that operation. | |
| (define (domain parking) | |
| (:requirements :strips :typing) | |
| (:types car spot) | |
| (:action move | |
| :parameters (?c - car ?s1 - spot ?s2 - spot) | |
| :precondition (and (vehicle ?c) (location ?s1) (location ?s2) (at ?c ?s1) (empty ?s2)) | |
| :effect (and (at ?c ?s2) (empty ?s1)) (not (empty ?s2)) (not (at ?c ?s1)) | |
| ) | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (define (problem 123x56) | |
| (:domain parking) | |
| (:objects | |
| c1 c2 c3 c4 c5 - car | |
| s1 s2 s3 s4 s5 s6 - spot) | |
| (:init (and (vehicle c1) (vehicle c2) (vehicle c3) (vehicle c4) (vehicle c5) | |
| (location s1) (location s2) (location s3) (location s4) (location s5) (location s6) | |
| (at c1 s1) (at c2 s2) (at c3 s3) (at c4 s5) (at c5 s6) | |
| (empty s4)) | |
| (:goal (and (at c1 s4))) | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (define (problem 123x56to51x324) | |
| (:domain parking) | |
| (:objects | |
| c1 c2 c3 c4 c5 - car | |
| s1 s2 s3 s4 s5 s6 - spot) | |
| (:init (and (vehicle c1) (vehicle c2) (vehicle c3) (vehicle c4) (vehicle c5) | |
| (location s1) (location s2) (location s3) (location s4) (location s5) (location s6) | |
| (at c1 s1) (at c2 s2) (at c3 s3) (at c4 s5) (at c5 s6) | |
| (empty s4)) | |
| (:goal (and (at c5 s1) (at c1 s2) (at c3 s4) (at c2 s5) (at c4 s6))) | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks a lot for this solution. I guess you should contribute to the following post on this blog: https://www.cleveroad.com/blog/develop-an-advanced-parking-management-software-for-your-lot and I'm sure the editor will add your comments to the article