Jump to content
xisto Community
Sign in to follow this  
xboxrulz1405241485

Parallel Array Matching

Recommended Posts

Hey guys,Recently I had my test, but this question completely stumped me. I was wondering how you guys would approach this question:So basically I get two parallel arrays as such:userid_t[] 1001, 1002, 1003, 1001, 1001, 1002, 1004, 1005itemcode_t[] A , A , B , B , C , A , B , FSo basically, I have to go through the parallel arrays and see the frequency of them having two distinct users selecting the item code.So in that question, the array:itemcodes[] A, B, C, F <--- this part I got, just sort them and then drop in the ones that aren't duplicated.frequency[] 2, 3, 1, 1 <--- this is the part that I'm having trouble with.Many thanks,xboxrulz

Share this post


Link to post
Share on other sites

My first question is: What tools can you use? My choice is Python.As an example, I took your items and made them into a list of characters:>>> x = ['A' , 'A' , 'B' , 'B' , 'C' , 'A' , 'B' , 'F']then I got rid of duplicates by converting the list to a set and back to a list:>>> y = list(set(x))Printing the result gives>>> y['A', 'C', 'B', 'F']You can then tell how many duplicates there were by counting and subtraction:>>> print len(x) - len(y)3>>>Extending this example, you can build lists for each item and the corresponding userids, and remove duplicates for each item.Hope this helps! :rolleyes:

Share this post


Link to post
Share on other sites

Here's how I would do it in C#. It'll be a bit difficult to do it in C, especially since you don't have classes.

 

using System;using System.Collections.Generic;namespace Maxotek{	static class Program	{		static void Main()		{			int[] userid_t = new int[] { 1001, 1002, 1003, 1001, 1001, 1002, 1004, 1005 };			char[] itemcode_t = new char[] { 'A', 'A', 'B', 'B', 'C', 'A', 'B', 'F' };			// Remove Duplicate Itemcodes			List<char> itemcodes = new List<char>();			for (int i = 0; i < itemcode_t.Length; i++)				if (!itemcodes.Contains(itemcode_t[i]))					itemcodes.Add(itemcode_t[i]);			// Sort Itemcodes			itemcodes.Sort();			List<int>[] frequency = new List<int>[itemcodes.Count];			for (int i = 0; i < userid_t.Length; i++)			{				int j = 0;				for (; j < itemcodes.Count; j++)				{					if (itemcodes[j] == itemcode_t[i])						break;				}				if (frequency[j] == null)				{					frequency[j] = new List<int>();					frequency[j].Add(userid_t[i]);				}				else if (!frequency[j].Contains(userid_t[i]))					frequency[j].Add(userid_t[i]);			}			// Display the result			for (int i = 0; i < itemcodes.Count; i++)			{				Console.WriteLine(itemcodes[i] + "-" + frequency[i].Count);			}			Console.Read();		}	}}

With List<int>[] frequency = new List<int>[itemcodes.Count];, I have declared an array of list of integers. It stores the userids that selected the itemcode at the corresponding index in the sorted itemcodes list. Before adding the userid to the list, we check if it has been already added and skip the step if it is. At the end we can obtain the distinct users by retrieving the count property of the each of the lists.

Share this post


Link to post
Share on other sites

It looks so much easier in C#, and probably similarly easy on Java (my preferred language)... I'll see if I can translate that back to C since that's what I'm learning this semester. I'm actually hating C >.>".I'm learning C++ next semester so hopefully, it's gonna make it easier for someone who just loves programming with Java.xboxrulz

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.