Python Question: Johnny has some difficulty memorizing the small prime numbers. So, his computer science teacher has...

60.1K

Verified Solution

Question

Programming

Python Question:

Johnny has some difficulty memorizing the small prime numbers.So, his computer science teacher has asked him to play with thefollowing puzzle game frequently. The puzzle is a 3x3 boardconsisting of numbers from 1 to 9. The objective of the puzzle isto swap the tiles until the following final state is reached:

1 2 3

4 5 6

7 8 9

At each step, Johnny may swap two adjacent tiles if their sum isa prime number. Two tiles are considered adjacent if they have acommon edge. Help Johnny to figure out whether the given 3-by-3board can reach the goal state or not. Define a function`solve_puzzle` that takes an 3-by-3 numpy array (i.e., an ndarray)as its only argument and returns Python Boolean that is True if andonly if the given 3-by-3 board can reach the goal state. Hint: Youcan start from the goal state, walk through all the possible statesand save them into a global variable. You function just need tojudge if the given state is inside the possible states or not.

Answer & Explanation Solved by verified expert
4.1 Ratings (726 Votes)
Ans Source Codeimport numpy as npswaps 011234456778031425364758primes 2357111317goalstates setdef swapxsXcopy listxXcopys0Xcopys1 Xcopys1Xcopys0return    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students