Convert to python

convert C to Python

Page 1 of 1

2 Replies - 2522 Views - Last Post: 12 December 2010 - 01:15 PM Rate Topic: -----

#1 leon8chicken  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 8
  • Joined: 13-November 10

Convert to python

Posted 12 December 2010 - 11:56 AM

How do i convert the following code into Python since i am learning Python now.

#include<stdio.h>

typedef struct
{
	int firstx; //denotes the row position of the first letter of the matching word
	int firsty; //denotes the column position of the first letter of the matching word
	int lastx; //denotes the row position of the last letter of the matching word
	int lasty; //denotes the column position of the last letter of the matching word
}position;

position Position,temp;

position wordsearch(char word[],char grid[][],int width,int height);

int main(void)
{
	int i=0,j=0,width,height,count=0,value=1;
	char grid[50][50],filename[30],text[50],word[50];

	char ch;

	printf("\nEnter the filename to be opened : ");
	scanf("%s",filename);

	FILE * f = fopen(filename,"r");

	if(f==NULL)
	{
		perror(filename);
		exit(0);
	}

	else
	{
		fscanf(f,"%d",&width);  //the first integer which is width is read into the variable 'width'
		fscanf(f,"%d",&height); //the second integer which is height is read into the variable 'height'

		//reading of the file grid and storing them in an array of strings - variable used is 'grid'
		while(fscanf(f,"%s",text)==1)
		{
			strcpy(grid[i],text);
			i++;
			count++;

		}

	}

	//printing of the grid
	for(i=0;i<count;i++)
	{
		printf("%s",grid[i]);
		putchar('\n');
	}



	while(value==1) //variable 'value' used to verify the user input (whether the word is alphabetic or not)
	{
		printf("\nEnter only one word [To exit enter any non-alphabetic character] : ");
		scanf("%s",word);

		for(i=0;i<strlen(word);i++)
		{
			if(isalpha(word[i]))
			value=1;

			else
			exit(0);

		}

		if(value==1)
		{
			Position=wordsearch(word,grid,width,height);
			if(Position.firstx!=-1)
			printf("\n%s found in : row %d of col %d to row %d of col %d",word,Position.firstx,Position.firsty,Position.lastx,Position.lasty);

			else
			printf("\n%s not found",word);
		}


	}

	fclose(f);

}


position wordsearch(char word[50], char grid[50][50], int width, int height)
{
	int i,j,k,l,m,length=strlen(word); //variable 'length' stores the length of the input word


	//searching the grid from beginning to end
	for(i=0;i<height;i++) //variable 'i' denotes the row of the grid
	for(j=0;j<width;j++) //variable 'j' denotes the column of the grid
	{
		k=0; //variable 'k' is used to run from the beginning to the end of the input word by the user
		if(word[k]==grid[i][j]) //checks whether the first letter of the word matches with a letter in the grid
		{
			l=j; //variable 'l' is used to search horizontally (left to right or right to left)
			m=i; //variable 'm' is used to search vertically (upwards or downwards)

			if((width-j)>=length || (height-i)>=length) // checks whether there is enough space in the row and column which is equivalent to the length of the input word
			{
				//left to right search
				while(k<length && grid[i][l]==word[k])
				{
					k++;
					l++;
				}

				if(k==length)
				{
					temp.firstx=i+1;
					temp.firsty=j+1;
					temp.lastx=i+1;
					temp.lasty=l;
					return temp;
				}

				//vertical downwards search
				k=0;

				while(k<length && grid[m][j]==word[k])
				{
					k++;
					m++;
				}

				if(k==length)
				{
					temp.firstx=i+1;
					temp.firsty=j+1;
					temp.lastx=m;
					temp.lasty=j+1;
					return temp;
				}

			}


				//right to left search
				k=0;
				l=j;
				while(k<length && grid[i][l]==word[k])
				{
					k++;
					l--;
				}
				l++;

				if(k==length)
				{
					temp.firstx=i+1;
					temp.firsty=j+1;
					temp.lastx=i+1;
					temp.lasty=l+1;
					return temp;
				}

				//vertical upwards search
				k=0;
				m=i;
				while(k<length && grid[m][j]==word[k])
				{
					k++;
					m--;
				}
				m++;

				if(k==length)
				{
					temp.firstx=i+1;
					temp.firsty=j+1;
					temp.lastx=m+1;
					temp.lasty=j+1;
					return temp;
				}

		}

	}

	//if the input word doesnt match with any word in the grid, all the positional values are assigned to '-1' indicating no matching word
	temp.firstx=-1;
	temp.firsty=-1;
	temp.lastx=-1;
	temp.lasty=-1;
	return temp;
}


This post has been edited by macosxnerd101: 12 December 2010 - 12:28 PM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? -1
  • +

Replies To: Convert to python

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10566
  • View blog
  • Posts: 39,110
  • Joined: 27-December 08

Re: Convert to python

Posted 12 December 2010 - 12:30 PM

The Python tutorials section is an excellent place to start learning Python. :)
Was This Post Helpful? 0
  • +
  • -

#3 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Convert to python

Posted 12 December 2010 - 01:15 PM

I'm sorry my friend, we do need you to put in a bit more effort that this. Try to convert it on your own and we'll help you if you get stuck. My blog Learning Python Programming is a great resource too :walkman:
Was This Post Helpful? 3
  • +
  • -

Page 1 of 1