Showing posts with label list. Show all posts
Showing posts with label list. Show all posts

Monday, December 18, 2017

Unique With Order Preserved

Remove duplicates from a list with order preserved.


function unique(m)=
     [for(i=[0:len(m)-1])if(search([m[i]],m,1)==[i])m[i]];
 

Friday, December 15, 2017

Clamp For List

Clamp every number in a list structure between two boundary numbers.

echo(clamplist([10,-.20,03,40,21,1,0,0.6,-2]));

function clamplist(v,b=0,c=1) = 
len(v) == undef ? min(max(v,min(b,c)),max(b,c)): 
len(v) == 0 ? [] : 
[for (i = v) clamplist(i,b,c)];

 

Tuesday, December 12, 2017

Select random item from a list

Select random item from a curated collection.

My_selection=sel_rnd( [1, 3, 0.5, 100, sin(2)]  );

function sel_rnd(v)=v[floor(rnd(len(v)))];

 

Wednesday, August 16, 2017

Randomly Shuffle A List

Put Items of a list in random sequence.

Deck=["Ace of Spades","Two of Spades","Three of Spades",
"Four of Spades","Five of Spades","Six of Spades","Seven of Spades",
"Eight of Spades","Nine of Spades","Ten of Spades","Jack of Spades",
"Queen of Spades","King of Spades","Ace of Hearts","Two of Hearts",
"Three of Hearts","Four of Hearts","Five of Hearts","Six of Hearts",
"Seven of Hearts","Eight of Hearts","Nine of Hearts","Ten of Hearts",
"Jack of Hearts","Queen of Hearts","King of Hearts","Ace of Clubs",
"Two of Clubs","Three of Clubs","Four of Clubs","Five of Clubs",
"Six of Clubs","Seven of Clubs","Eight of Clubs","Nine of Clubs",
"Ten of Clubs","Jack of Clubs","Queen of Clubs","King of Clubs",
"Ace of Diamonds","Two of Diamonds","Three of Diamonds",
"Four of Diamonds","Five of Diamonds","Six of Diamonds",
"Seven of Diamonds","Eight of Diamonds","Nine of Diamonds",
"Ten of Diamonds","Jack of Diamonds","Queen of Diamonds",
"King of Diamonds"];

echo( shuffle(Deck));
function shuffle(il,repeat=2)=len(il)<=1?il:
let(l=repeat>0?shuffle(il,repeat-1):il, p=split(l))
round(rands(0,2,1)[0])==0?
concat( shuffle(p[0]),shuffle(p[1])):
concat( shuffle(p[1]),shuffle(p[0]));

function split(l)=[
[for(i=[0:2:len(l)-1])l[i]],
[for(i=[1:2:len(l)-1])l[i]]]  ;
 

Friday, July 14, 2017

List Lerp

Linear interpolation over the items in a list

list=[[1,6],[3,5],[5,3],[1,2],[0,6]];

for(x=[0.01:0.05:4]){
echo(listlerp (list,x));
translate(listlerp (list,x))sphere(0.1);}

function listlerp (l,I)=let(
f=len(l)-1,start=max(0,min(f,floor(I))),
end=max(0,min(f,ceil(I))),bias=I%1) 
(l[end]* bias + l[start] * (1 - bias));

 

Friday, June 23, 2017

Named Colors Indexable List

Generate a list of OpenScad named colors.

function colors()= [
"Lavender", 
"Thistle", 
"Plum", 
"Violet", 
"Orchid", 
"Fuchsia", 
"Magenta", 
"MediumOrchid", 
"MediumPurple", 
"BlueViolet", 
"DarkViolet", 
"DarkOrchid", 
"DarkMagenta", 
"Purple", 
"Indigo", 
"DarkSlateBlue", 
"SlateBlue", 
"MediumSlateBlue", 
"Pink", 
"LightPink", 
"HotPink", 
"DeepPink", 
"MediumVioletRed", 
"PaleVioletRed", 
"Aqua", 
"Cyan", 
"LightCyan", 
"PaleTurquoise", 
"Aquamarine", 
"Turquoise", 
"MediumTurquoise", 
"DarkTurquoise", 
"CadetBlue", 
"SteelBlue", 
"LightSteelBlue", 
"PowderBlue", 
"LightBlue", 
"SkyBlue", 
"LightSkyBlue", 
"DeepSkyBlue", 
"DodgerBlue", 
"CornflowerBlue", 
"RoyalBlue", 
"Blue", 
"MediumBlue", 
"DarkBlue", 
"Navy", 
"MidnightBlue", 
"IndianRed", 
"LightCoral", 
"Salmon", 
"DarkSalmon", 
"LightSalmon", 
"Red", 
"Crimson", 
"FireBrick", 
"DarkRed", 
"GreenYellow", 
"Chartreuse", 
"LawnGreen", 
"Lime", 
"LimeGreen", 
"PaleGreen", 
"LightGreen", 
"MediumSpringGreen", 
"SpringGreen", 
"MediumSeaGreen", 
"SeaGreen", 
"ForestGreen", 
"Green", 
"DarkGreen", 
"YellowGreen", 
"OliveDrab", 
"Olive", 
"DarkOliveGreen", 
"MediumAquamarine", 
"DarkSeaGreen", 
"LightSeaGreen", 
"DarkCyan", 
"Teal", 
"LightSalmon", 
"Coral", 
"Tomato", 
"OrangeRed", 
"DarkOrange", 
"Orange", 
"Gold", 
"Yellow", 
"LightYellow", 
"LemonChiffon", 
"LightGoldenrodYellow", 
"PapayaWhip", 
"Moccasin", 
"PeachPuff", 
"PaleGoldenrod", 
"Khaki", 
"DarkKhaki", 
"Cornsilk", 
"BlanchedAlmond", 
"Bisque", 
"NavajoWhite", 
"Wheat", 
"BurlyWood", 
"Tan", 
"RosyBrown", 
"SandyBrown", 
"Goldenrod", 
"DarkGoldenrod", 
"Peru", 
"Chocolate", 
"SaddleBrown", 
"Sienna", 
"Brown", 
"Maroon", 
"White", 
"Snow", 
"Honeydew", 
"MintCream", 
"Azure", 
"AliceBlue", 
"GhostWhite", 
"WhiteSmoke", 
"Seashell", 
"Beige", 
"OldLace", 
"FloralWhite", 
"Ivory", 
"AntiqueWhite", 
"Linen", 
"LavenderBlush", 
"MistyRose", 
"Gainsboro", 
"LightGrey", 
"Silver", 
"DarkGray", 
"Gray", 
"DimGray", 
"LightSlateGray", 
"SlateGray", 
"DarkSlateGray", 
"Black"
];
 

Saturday, June 3, 2017

Merge sort

Contributed by arpruss.
Usage:
 



function _slice(a,start,end=undef) =
    let(end = end==undef ? len(a) : end)
        start>=end ? [] : [for (i=[start:end-1]) a[i]];

function _mergeLists(a,b,merged=[]) =
    len(a)==0 ? concat(merged,b) :
    len(b)==0 ? concat(merged,a) :
    a[0] < b[0] ? _mergeLists(_slice(a,1), b, merged=concat(merged,[a[0]])) : 
        _mergeLists(_slice(b,1), a, merged=concat(merged,[b[0]]));

function mergeSort(a) =
    let(l=len(a))
        l <= 1 ? a :
        let(split=floor(l/2),
            b=_slice(a,0,end=split),
            c=_slice(a,split))
            _mergeLists(mergeSort(b),mergeSort(c));

echo(mergeSort(rands(0,100,1000)));

 

Friday, May 12, 2017

Quick Sort

Basic quick sort.
Recursively sorts a list of elements


function quicksort(list)= 
!(len(list)>0)?[]:
let(
pivot = list[floor(len(list)/2)],
list_lesser =[ for(i=[0:len(list)-1])if( y[i] < pivot )y[i] ],
list_equal  =[ for(i=[0:len(list)-1])if( y[i]== pivot )y[i] ],
list_greater=[ for(i=[0:len(list)-1])if( y[i] > pivot )y[i] ])

concat(
quicksort(list_lesser),
list_equal,
quicksort(list_greater));

Sorts a list of elements by column col.

function quicksort_col(list,col=0)= 
!(len(list)>0)?[]:
let(
pivot = list[floor(len(list)/2)][col],
list_lesser =[ for(i=[0:len(list)-1])if( list[i][col] < pivot )list[i] ],
list_equal  =[ for(i=[0:len(list)-1])if( list[i][col]== pivot )list[i] ],
list_greater=[ for(i=[0:len(list)-1])if( list[i][col] > pivot )list[i] ])

concat(
quicksort_col(list_lesser),
list_equal,
quicksort_col(list_greater));