CS1020 Coauthorship Lab2 Ex1

Link to the question – Coauthorship Lab 2 Ex 1

import java.util.*;

class Author
{
	private String mName;
	private static ArrayList<Author> allAuthors;

	private Author(String pName)
	{
		this.mName = pName;
	}

	public static Author findOrCreateAuthor(String pName)
	{
		if (allAuthors==null)
		{
			allAuthors = new ArrayList<Author> ();
		}
		int n = allAuthors.size();
		
		for (int i=0; i<n; i++)
		{
			if (allAuthors.get(i).getName().equals(pName)) return allAuthors.get(i);
		}
		Author author = new Author(pName);
		allAuthors.add(author);
		return author;
	}

	public String getName()
	{
		return mName;
	}

	public static ArrayList<Author> getAllAuthors()
	{
		return allAuthors;
	}

}


class Article
{
	private String mName;
	private ArrayList<Author> mCoAuthorList = new ArrayList<Author>();

	public Article(String pName)
	{
		this.mName = pName;
	}

	public String getName()
	{
		return mName;
	}

	public ArrayList<Author> getCoAuthorList()
	{
		return mCoAuthorList;
	}

	public void addCoAuthor(Author author)
	{
		mCoAuthorList.add(author);
	}	
}

class Coauthorship
{
	static Article [] articlesList;

	public static void main(String[]args)
	{
		Scanner sc = new Scanner(System.in);
		int numOfArticles = sc.nextInt();
		articlesList = new Article[numOfArticles];
		if(numOfArticles <= 50)
		{
			//get input for the data
			for(int i = 0; i < numOfArticles; i++)
			{
				String articleName = sc.next();
				Article article = new Article(articleName);
				int numCoAuthor = sc.nextInt();
				for(int j = 0; j < numCoAuthor; j++)
				{
					//input the data
					String coAuthor = sc.next();
					if(coAuthor.length() <20)
					{
						article.addCoAuthor(Author.findOrCreateAuthor(coAuthor));
					}
					else
					{
						return;
					}			
				}
				articlesList[i] = article;
			}
			//start getting the query input
			int queryNo = sc.nextInt();
			if(queryNo <= 50)
			{
				for(int i = 0; i < queryNo; i++)
				{
					//solve and print output
					String toFind = sc.next();
					HashSet coAuthorToFind = new HashSet();
					for(int ar = 0; ar < numOfArticles; ar++)
					{
						Boolean found = false;
						//retrieve all the coauthor for each article and check if the query is found
						ArrayList<Author> auList = articlesList[ar].getCoAuthorList();
						for(Author a : auList)
						{
							if(a.getName().equals(toFind))
							{
								found = true;
							}
						}
						//if the query is found, copy all the coauther with the query into a HashSet to get unique value only
						if(found)
						{
							for(Author a : auList)
							{
								if(!a.getName().equals(toFind))
								{
									coAuthorToFind.add(a.getName());
								}
							}
						}
					}
					System.out.println(toFind + " " + coAuthorToFind.size());
				}
			}
			else
			{
				return;
			}
		}
		else
		{
			return;
		}
	}
}
Advertisements
This entry was posted in NUS, SoC and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s