Please complete each of the following problems. Your submission should include t
ID: 3761743 • Letter: P
Question
Please complete each of the following problems. Your submission should include the prolog files with a pdf file with the code for each problem as well as the results of executing the included test cases. If you copy any code from the Internet, please list the site from where you borrowed the code.
Please refer to the following link for built-in Prolog predicates: http://www.gprolog.org/manual/html_node/gprolog024.html.
1. Write a predicate (first_middle_last/4) to find the first, middle, and last elements of a list. Some built-in predicates that may help you: nth0/3 (this is written as nth/3 in the above link, but it should actually be nth0/3), length/2, div/2 (performs integer division). You may use any built-in predicates you like. There is a section on List Processing that you might find helpful. After implementing your predicate, please run the following tests and include the answers in your pdf submission (screenshots might work best). a. first_middle_last ([1,2,3], First, Middle, Last). b. first_middle_last ([1,2,3,4,5], First, Middle, Last). c. first_middle_last ([19,25,72,9,4,15,23,19,32,41,53], First, Middle, Last).
2. Solve the following problem. Include all possible solutions in your pdf submission. You have the following image. Each component within the image must not touch its own color. For example, block 1 touches blocks 2, 3, 4, and 5; therefore, block 1 cannot be the same color as 2, 3, 4, or 5. Given the colors red, blue, yellow, and green, how can you solve this problem? Hint: you’ll need to create two predicates, blocks/2 and grid/5. Think about how the crossword puzzle is solved.
3. Solve the following crossword puzzle. Include all possible solutions in your pdf submission. The words are: C, Compiled, Declarative, Functional, Imperative, Interpreted, Java, Logic, ObjectOriented, Prolog, Python, Scheme.
2 5Explanation / Answer
1.
list_first_mid_last([E|Es],E,M,L) :-
ahead_of_mid_last([E|Es],[E|Es],M,L).
ahead_of_mid_last([],[M|_],M,M).
ahead_of_mid_last([F|Fs],Es,M,L) :-
more_ahead_of_mid_last(Fs,F,Es,M,L).
more_ahead_of_mid_last([],L,[E|_],E,L).
more_ahead_of_mid_last([F|Fs],_,Es,E,L) :-
evenmore_ahead_of_mid_last(Fs,F,Es,E,L).
evenmore_ahead_of_mid_last([],L,[E|_],E,L).
evenmore_ahead_of_mid_last([F|Fs],_,[_|Es],M,L) :-
more_ahead_of_mid_last(Fs,F,Es,M,L).
% Prolog
?- list_first_mid_last([1],F,M,L).
F = M, M = L, L = 1.
?- list_first_mid_last([1,2],F,M,L).
F = M, M = 1, L = 2.
?- list_first_mid_last([1,2,3],F,M,L).
F = 1, M = 2, L = 3.
?- list_first_mid_last([1,2,3,4],F,M,L).
F = 1, M = 2, L = 4.
?- list_first_mid_last([1,2,3,4,5],F,M,L).
F = 1, M = 3, L = 5.
?- list_first_mid_last([1,2,3,4,5,6],F,M,L).
F = 1, M = 3, L = 6.
?- list_first_mid_last([1,2,3,4,5,6,7],F,M,L).
F = 1, M = 4, L = 7.
2.
3.
import java.util.*;
import java.io.*;
public class CrosswordPuzzle {
private Scanner filePuzzle;
private Scanner scanLine;
private String[] animal = new String[18];
private String[] descrip = new String[18];
private char[][] gameBoard = new char[12][12];
Map<String, String> crosswordMap = new HashMap<String,String>();
Object[] names;
static final int GAME_BOARD_SIZE=12;
Boolean wordDirection;
public CrosswordPuzzle ()
{
openFile();
readFile();
printGrid();
}
private void openFile()
{
try
{
filePuzzle = new Scanner(new File ("puzzle.txt"));
}
catch (Exception e)
{
System.out.println("File does not exist");
}
}
private void readFile()
{
try
{
int count = 0;
while (filePuzzle.hasNext() )
{
String strLine = filePuzzle.nextLine();
scanLine = new Scanner(strLine);
animal[count] = scanLine.next();
descrip[count] = scanLine.nextLine();
//System.out.print(animal[count]);
System.out.println(descrip[count]);
System.out.println();
count++;
}
}
catch (Exception e)
{
System.err.print("Error: " + e.getMessage());
}
}
private void printGrid()
{
int row;
int col;
try
{
Scanner scan = new Scanner(new File("puzzle2.txt"));
wordDirection = true;
gameBoard = new char[12][12];
for(int i=0; i<GAME_BOARD_SIZE; i++)
{
for(int j=0; j<GAME_BOARD_SIZE; j++)
{
gameBoard[i][j] = '-';
}
}
while(scan.hasNext())
{
String key = scan.next();
String map = scan.nextLine();
crosswordMap.put(key, map);
}
names = crosswordMap.keySet().toArray();
Set<String> noGoes = new HashSet<String>();
for(int i=0; i<names.length; i++)
{
if (wordDirection)
{
System.out.println("Across");
}
else
{
System.out.println("Down");
}
String theKey = this.getRandom();
System.out.println(theKey);
Random rand = new Random();
if (wordDirection)
{
row = rand.nextInt(GAME_BOARD_SIZE);
col = rand.nextInt(GAME_BOARD_SIZE-theKey.length()-1);
}
else
{
col = rand.nextInt(GAME_BOARD_SIZE);
row = rand.nextInt(GAME_BOARD_SIZE-theKey.length()-1);
System.out.println();
}
this.add(row, col, theKey, wordDirection, noGoes);
wordDirection = !wordDirection;
}
System.out.println(this.gameBoardToString());
}
catch(IOException e) {}
}
private void add(int row, int col, String theKey, Boolean direction, Set<String> noGoes)
{
int count = 0;
if( canFit(theKey, row, col) )
{
while( count<theKey.length() )
{
gameBoard[row][col] = theKey.charAt(count);
if(direction)
{
col++;
}else{
row++;
}
count++;
}
}else{
noGoes.add(theKey);
}
}
private Boolean canFit( String theKey, int row, int col)
{
Boolean canFit = true;
int count = 0;
for(int i=0; i<theKey.length(); i++)
{
if( gameBoard[row][col] == '-' || gameBoard[row][col] == theKey.charAt(count) )
{
if(wordDirection)
{
col++;
}else{
row++;
}
}else{
canFit = false;
break;
}
}
return canFit;
}
private String getRandom()
{
Random rand = new Random();
String randKey = null;
int count = 0;
while(randKey == null && count<names.length)
{
int randInt = rand.nextInt(names.length);
if(names[randInt] != null)
{
randKey = names[randInt]+"";
count++;
//Remove word we got from keys and set that spot to null.
names[randInt] = null;
}
}
return randKey;
}
public String toString()
{
String ts = "";
for(int i=0; i<names.length; i++)
{
ts += names[i] + " ";
}
return ts;
}
public String gameBoardToString()
{
String ts = "";
for(int i=0; i<GAME_BOARD_SIZE; i++)
{
for(int j=0; j<GAME_BOARD_SIZE; j++)
{
ts += gameBoard[i][j];
}
ts += " ";
}
return ts;
}
private void fillArray(char[][] array)
{
for(int i =0; i< GAME_BOARD_SIZE; i++)
{
for(int k=0; k<GAME_BOARD_SIZE; k++)
{
array[i][k] = '-';
}
}
}
}
sorry i can write the program for 2nd question
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.