Given an MxN filled up cross word, write a program to print all the words present in the cross word. Cross word table is given as input in a text file that contains ‘0′ for a shaded square and any other valid alphabet [A-Z] for any other position. Each row is a line of text input. A word has to be at least 2 letters long. The program has to read in the input file, and then print out all the words present. Your program has to print the output in the following format: >i is the row and j is the column from which the word begins.
The input file would contain:
12345678901234567890
100000CONGRESS0000000
2000I0000000U00000L00
3000M0000000PRESIDENT
4000P0000000R00I00G0H
5INDEPENDENCE00X00I0E
6000A0000000M00000S0R
70VICEPRESIDENT000L0E
8000H0000000C00TRIAL0
90E0M0S00000O00000T00
0NINETEEN0FOUR000FIVE
10G0N0N00000R000L0V00
20H0T0A00000TWO0AMEND
30T00STATE000000W0000
400000E000000000S0000
The output should contain:<1,6>:CONGRESS:Across
<1,12>:SUPREMECOURT:Down
<2,4>:IMPEACHMENT:Down
<2,18>:LEGISLATIVE:Down
<3,12>:PRESIDENT:Across
<3,15>:SIX:Down
<3,20>:THERE:Down
<5,1>:INDEPENDENCE:Across
<7,2>:VICEPRESIDENT:Across
<8,15>:TRIAL:Across
<9,2>:EIGHT:Down
<9,6>:SENATE:Down
<10,1>:NINETEEN:Across
<10,10>:FOUR:Across
<10,17>:FIVE:Across
<11,16>:LAWS:Down
<12,12>:TWO:Across
<12,16>:AMEND:Across
<13,5>:STATE:Across
Note: The greyed out numbers are row/column number for reference. They are not part of either input or output. The filename of the input text file should be given as the command line argument to your program. The output should appear on STDOUT. Take care to remove all the debug messages before you submit your program.
3,413 Views |
#include
#include
#define MAXR 14
#define MAXC 20
int main(int argc,char *argv[])
{
FILE *fp;
int xcount=0,ycount=0,i,j;
int sx,sy,t;
char str[MAXC],ch,*ptr;
char a[MAXR][MAXC] ;
if (argc != 2)
{
printf(”Argument shuold be two only\n”);
return 0;
}
fp = fopen ( argv[1],”r”);
if ( fp == NULL )
{
printf(”can’t open the input file\n”);
return 0;
}
for (i = 0; i:%s:Acrosss\n”,sx+1,sy+1,str);
}
}
xcount++;
for(i=0;i 0)
if (a[j-1][i] != ‘0′) continue;
sx = j;
sy = i;
t=0;
while( a[j][i] != ‘0′ && j :%s:Down\n”,sx+1,sy+1,str);
}
}
ycount ++ ;
}
return 0;
}
//TODO: reading from file instead of dummy data in code
#include “stdafx.h”
#include “stdio.h”
#include “stdlib.h”
#include “string.h”
#define ROWS 14
#define COLS 22
char *cross[ROWS];
int processrow[ROWS][COLS];
int processcol[ROWS][COLS];
int processcw()
{
int i,j;
int jnew;
int inew;
for(i=0;iAcross\t”,i+1,j+1);
jnew=j;
while( jnewDown\t”,i+1,j+1);
inew=i;
while( inew
The solution to this is as follows.
1. Create a matrix class
2. Have a single function that returns all words horizontally
3. First call this function
4. Transpose the matrix
5. Call the function again.
Here is the full Python code.
“”" Read a crossword like grid and print the
words in it “”"
class Grid(object):
def __init__(self):
self.data=[]
def __str__(self):
rows = [”.join(row) for row in self.data]
return ‘\n’.join(rows)
def read(self, gridfile):
# Read data line by line
for line in open(gridfile):
line = line.strip()
row = []
# Read char by char and add it to
# the row if it is an alphabet, if
# not, add a null char
for c in line:
if (c>=’a’ and c=’A’ and c=len(row):break
if len(word)>=2:
if invert:
words.append((col, rowcnt, word))
else:
words.append((rowcnt, col, word))
word = ”
idx += 1
rowcnt += 1
return words
def _transpose(self, lists, defval=’ ‘):
return map(lambda *row: [elem or defval for elem in row], *lists)
def transpose(self):
self.data = self._transpose(self.data)
def getWords(self):
print ‘Across=>’,self.across()
# Now transpose
self.transpose()
print ‘Down=>’,self.across(True)
if __name__==”__main__”:
g = Grid()
g.read(’grid.txt’)
print g
g.getWords()
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace ConsoleApplication3
{
public class Program
{
public static void Main(string[] args)
{
string myString = string.Empty;
using (StreamReader myFile = new System.IO.StreamReader(”input.txt”))
{
int lineCount = 1;
string startingpoint = string.Empty, oldStartingPoint=string.Empty;
string[] eachLine = new string[30];
myString = myFile.ReadLine();
eachLine[lineCount - 1] = myString;
#region this is for across
while (myString !=null)
{
char[] achar = myString.ToCharArray();
for (int i = 0; i “;
if (startingpoint != oldStartingPoint)
{
Console.WriteLine(startingpoint);
}
oldStartingPoint = startingpoint;
while (i 0)
{
for (int a = 0; a “;
for (int i = row + 3; i
#include
#include
int main()
{
int r=0,c=0,i=0,k=0;
char temp[20];
char arr[15][20]={”00000CONGRESS0000000″,”000I0000000U00000L00″,”000M0000000PRESIDENT”,”000P0000000R00I00G0H”,”INDEPENDENCE00X00I0E”,”000A0000000M00000S0R”,”0VICEPRESIDENT000L0E”,”000H0000000C00TRIAL0″,”0E0M0S00000O00000T00″,”NINETEEN0FOUR000FIVE”,”0G0N0N00000R000L0V00″,”0H0T0A00000TWO0AMEND”,”0T00STATE000000W0000″,”00000E000000000S0000″};
for(r=0;r 2)
{
temp[i]=”;
printf(”:%s:Across\n”,r+1,k,temp);
}
if( i != 0)
{
i=0;
temp[i]=”;
}
}
}
}
for(c=0;c 2)
{
temp[i]=”;
printf(”:%s:Down\n”,k,c+1,temp);
}
if( i != 0)
{
i=0;
temp[i]=”;
}
}
}
}
}
Leave an Answer/Comment