Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In PYTHON Code your lab in a text editor. I prefer vim: $ vim code.py vim has two modes. A command mode where you enter

In PYTHON

Code your lab in a text editor. I prefer vim: $ vim code.py vim has two modes. A command mode where you enter commands to save the docu- ment, cut, paste, etc. To enter the command mode hit ESC. The command you need for today is :wq which will save (write) the document, and quit vim. You may also want to use the bang to override any prompts (:wq!). If you want to start writing stuff, when in command mode, hit i. You will get a confirmation in the lower left that states -- INSERT --, indicating that whatever youre typing will be inserted into the document. Some other simple tips: When in command mode hitting dd (without the colon) will cut the current line. This is an easy way to just delete a whole line.

When in command mode hitting p (without the colon) will paste. In any mode, hitting HOME brings you to the start of the current line. In any mode, hitting END brings you to the end of the current line. 2. Your code is a script. Python will execute it with the following command: $ python3 code.py This command will tell Python to execute your script line-by-line and close after exe- cution. Calling python3 by itself will invoke the Python 3 command-line interface or shell, where you can insert commands sequentially. This can get a bit tedious for the length of code were implementing so I recommend that you use the above workflow. Optional: Hello World! Open your favorite text editor and enter: print("Hello, World!") Save it as hello.py and close the editor. Make sure your path is in the same directory as the file you just created. In the shell enter: $ python3 hello.py Hello, World! Sidebar: Python CLI Conventionally you can just launch python3 and type commands one-by-one. If you want to keep the Python shell open after executing your script add a -i flag: $ python3 -i code.py This is useful if you want to check on the value of variables after a script is complete, say for debugging purposes. Use quit() or CTRL + D to quit out of the Python shell.

Implement the following algorithm in python3: DB = ["looks","swims","quacks"] # A set of facts KB = [(["looks","swims","quacks"],"duck"),(["barks"],"dog"), (["hoots","flies"],"owl")] # A set of rules count = 1 # Number of times we have iterated over the whole rule set changes = True while changes: changes = False # Set the flag that there have been no changes to false print( "Starting iteration " + str(count) ) for p in KB: # For each rule in the set of rules ... antecedent, consequent = p print( "Consider a rule where: " ) print( antecedent ) print( "implies: " ) print( consequent ) # Determine if all literals in antecedent are also in KB satisfied = True # Flag for entire premise being satisfied

for q in antecedent: # q will be a string # KB is a list of strings if q not in DB: satisfied = False # Flag as false, all clauses must be inferred # If it passes the above, then antecedent is satisfied # ...and consequent should be entailed if satisfied and consequent not in DB: DB.append( consequent ) changes = True print( "Antecedent is in DB, consequent is implied, DB is now: " ) print(DB) elif satisfied and consequent in DB: print( "Consequent is implied, but was already in DB") else: print( "Consequent is not implied" ) count = count + 1 print( "No more changes. DB is: " ) print(DB) Do not cut and paste this into the terminal. The PDF document most likely has spaces in it, and blocks are defined by tabs in Python. This may lead to a syntax error when cutting- and-pasting. In the following, we discuss what happens line-by-line. The first line: DB = ["looks","swims","quacks"]

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Database 10g Insider Solutions

Authors: Arun R. Kumar, John Kanagaraj, Richard Stroupe

1st Edition

0672327910, 978-0672327919

More Books

Students also viewed these Databases questions