Mathematica 9 is now available
Student Support Forum
-----
Student Support Forum: 'What was the name of the function' topicStudent Support Forum > General > Archives > "What was the name of the function"

< Previous Comment | Next Comment >Help | Reply To Comment | Reply To Topic
Author Comment/Response
yehuda ben-shimol
10/02/12 11:08am

I'm not aware of a single function that would do that, but the logic of what you describe is as follows:
1. Collect all adjacent elements that are apart at most by 1 - you do that with Split and using the second argument
2. Map a function that would replace each of the elements by their average
(This means that the first two elements of your program need to be 3/2 rather then 1/2)


Functional Programming style:
Flatten[Table[#[[1]]/#[[2]], {#[[2]]}] & /@ ({Total[#], Length[#]} & /@
Split[{1, 2, 5, 6, 8, 9}, Abs[#1 - #2] <= 1 &])]

Pattern Matching style:

Flatten[Cases[Split[{1, 2, 5, 6, 8, 9}, Abs[#1 - #2] <= 1 &],
x_List :> Table[Mean[x], {Length[x]}]]]

URL: benshimo@bgu.ac.il,

Subject (listing for 'What was the name of the function')
Author Date Posted
What was the name of the function speleomania 09/28/12 12:15pm
Re: What was the name of the function Michael 10/01/12 7:19pm
Re: What was the name of the function yehuda ben-s... 10/02/12 11:08am
Re: What was the name of the function Peter Pein 10/03/12 09:22am
Re: What was the name of the function speleomania 10/03/12 11:25am
< Previous Comment | Next Comment >Help | Reply To Comment | Reply To Topic