Notifications
Clear all

[Closed] mini-challenge #5

here is a little problem … how can you make an unique and sorted array from an array that contains any possible iteratable number(!) values…
here is a sample:
[left]

#(7, point3 7 1.6 12, red, x_axis, #{87..100}, [1,17], #(#(#(1, "Hi!"))))  

[/left]

11 Replies
 lo1

You’ll have to define some logic, won’t you? Is 7 bigger than red? What is your intended output?

1 Reply
(@denist)
Joined: 11 months ago

Posts: 0

red is a color and can be define as (color 255 0 0 255)… for this task its 4 numbers

Agreed, what’s the sample output? Should the original values be kept or is the goal a sorted array of the extracted numbers, types unchanged (int, float etc.)? In that case, here’s the obvious solution (I guess the hint is they are all iterables but I can’t think of any universal approach):

mapped fn flatten arr buffer = case of
(
	(isKindOf arr Number) : append buffer arr
	(isKindOf arr BitArray) : join buffer arr
	(isKindOf arr Array) : flatten arr buffer
	(isKindOf arr Color) : join buffer #(arr.r, arr.g, arr.b, arr.a)
	(isProperty arr #x) :
	(
		join buffer #(arr.x, arr.y)
		if isProperty arr #z do append buffer arr.z 
		if isProperty arr #w do append buffer arr.w
	)
)

fn sortNumbers arr buffer:#() =
(
	flatten arr buffer
	makeUniqueArray (sort buffer)
)

sortNumbers #(7, point3 7 1.6 12, red, x_axis, #{87..100}, [1,17], #(#(#(1, "Hi!"))))
 lo1

so from #(red, 7) you want to get #(0,7,255)?

2 Replies
(@denist)
Joined: 11 months ago

Posts: 0

color has to be excluded from the list… it does’t have get function, which means the color[i] doesn’t work…
but matrix3 works for addition.

(@polytools3d)
Joined: 11 months ago

Posts: 0

Wouldn’t it be useful if you would put the expected output result for the given example?

#(7, point3 7 1.6 12, red, x_axis, #{87..100}, [1,17], #(#(#(1, "Hi!"))))
 lo1

I honestly have no idea what you’re looking to achieve. You’ll need to post sample output and explain it.

the final output for the sample is


 #(0.0, 1.0, 1.6, 7, 12.0, 17.0, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100)
 

red, and “Hi!” were wiped because one doesn’t have get method, and another returns not number


 red[1] -- not works
 "Hi!"[1] == "H" -- not number
 

for example (matrix3 1) is good:


 (matrix3 1)[1] == [1,0,0] -- OK
 ([1,0,0])[1] == 1.0 -- OK   
 --so 
 (matrix3 1) => 1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0 => #(0.0,1.0)
 

where has this task came from …
there are several methods that returns vertices as Point3, or bitarray, or array, or single index… you can handle every case individually but… maybe there is a universal solution

I’m still confused as to why you would need to sort this type of array in the first place?

1 Reply
(@denist)
Joined: 11 months ago

Posts: 0

the sorting is not really important and it’s easy to do. but it makes sense if you are talking about indexes for example… to make the final array unique is absolutely necessary.