MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Finding shared element in two lists

  • To: mathgroup at smc.vnet.net
  • Subject: [mg114120] Re: Finding shared element in two lists
  • From: "Ingolf Dahl" <ingolf.dahl at telia.com>
  • Date: Wed, 24 Nov 2010 06:59:11 -0500 (EST)

To Jason,
(the question and my answer has also appeared in another forum)

The function

lastcommon[a_List, b_List] /; (a[[1]] == b[[1]]) := 
 a[[NestWhile[(# + 1) &, 
   1, (And[Length[a] > #, Length[b] > #, 
      a[[# + 1]] == b[[# + 1]]]) &]]]
LongestCommonSequence[] does something else:

In[40]:= LongestCommonSequence[{1, 2, 3, 4, 5}, {1, 3, 4, 5, 6, 7}] 
 
Out[41]= {3, 4, 5} 

In[41]:= LongestCommonSubsequence[{1, 2, 3, 4, 5}, {1, 3, 4, 5, 6, 7}] 

Out[41]= {3, 4, 5} 

In[42]:= lastcommon[{1, 2, 3, 4, 5}, {1, 3, 4, 5, 6, 7}] 

Out[42]= 1 

I suggest another command, which does not require the first elements to be
equal. It returns all the common start elements as a list: 

commonstart[a_List, b_List] := 
 Take[a, NestWhile[(# + 1) &, 
   0, (And[Length[a] > #, Length[b] > #, 
      a[[# + 1]] == b[[# + 1]]]) &]]

In[47]:= commonstart[{1, 2, 3, 4, 5}, {1, 3, 4, 5, 6, 7}] 

Out[47]= {1} 

In[48]:= commonstart[aphid, friendshouse] 

Out[48]= {} 

In[49]:= commonstart[aphid, mosquito] 

Out[49]= {"Insecta", "Dicondylia", "Pterygota", "Neoptera"} 

With the additional definitions below, commonstart may take any number of
arguments:

commonstart[] := {}

commonstart[a_List] := a;

commonstart[a_List, b_List, c__List] := 
  commonstart[commonstart[a, b], c];

Best regards

Ingolf Dahl



 > -----Original Message-----
> From: Jason Ebaugh [mailto:ebaughjason at gmail.com]
> Sent: den 22 november 2010 13:41
> To: mathgroup at smc.vnet.net
> Subject: [mg114072] Finding shared element in two lists
> 
> I am trying to write a function that would take two lists of increasing
more
> precise location, and return the most specific level of geography that
they
> share.
> 
> myhouse={"Earth", "North America", "USA", "Illinois", "Champaign County",
> "Urbana"};
> friendshouse={"Earth", "North America", "USA", "Minnesota", "Ramsey
County",
> "St. Paul"};
> 
> In this case, the function should output USA.
> 
> I could do this procedurally well enough. But I am really trying to get
> comfortable with functional programming.
> 
> 
> 
> The above is just a more digestible version of what I am really trying to
> do. I want to find the lowest level of taxonomy that two species share.
> 
> Ex:
> aphid={"Insecta", "Dicondylia", "Pterygota", "Neoptera", "Paraneoptera",
> "Hemiptera", "Sternorrhyncha", "Aphidiformes", "Aphidomorpha",
"Aphidoidea",
> "Aphididae", "Aphidinae", "Macrosiphini", "Acyrthosiphon"};
> mosquito={"Insecta", "Dicondylia", "Pterygota", "Neoptera",
"Endopterygota",
> "Diptera", "Nematocera", "Culicimorpha", "Culicoidea"};
> 
> answer = Neoptera
> 
> 
> Thank a 10^6,
> Jason
> 
> 
> --
> Jason Ebaugh, PhD.
> 
> Personal communications: ebaughjason at gmail.com
> Business communications: jason.ebaugh at apisscientific.com
> Dissertation: http://hdl.handle.net/2142/13005
> Linkedin:
>
http://www.linkedin.com/profile?viewProfile=&key=22842228&locale=en_US&trk=t
ab_pr
> o
> Need bioinformatics help? www.apisscientific.com




  • Prev by Date: Re: Plot function at specific points
  • Next by Date: Re: Strange Nest behaviour with functions
  • Previous by thread: Re: Finding shared element in two lists
  • Next by thread: Question on FinancialDerivative in Ver 8