Reputation: 555
This is a simple problem but nonetheless, I am stuck. So let's say I have an input vector with negatives, zeros and positive numbers.
[-1 -1 -2 -1 -4 0 -6 -1 -5 -2 -10 **4** -12 -4 -6 -1 -16 **3** -18 **2** -10 -8 -22 **12** -19
-10 -14 0 -28 **12** -30 -1 -18 -14 -22 **19** -36 -16 -22 **10** -40 **12** -42 -4 -12 -20 -46 **28** -41 -7]
And I want to return a vector of the indices of the previous vectors, where the value is a positive.
So the return value would be
(11 17 19 23 29 35 39 41 47)
11 is the index of where the 4 sits, 17 is the index of where the 3 is, etc
I am using .indexOF
and this is what it is returning: (11 17 19 23 23 35 39 23 47)
I figured that b/c at index 23, the positive value is 12, and at index 29, the value is also 12, so it is just returning the index where it first sees the positive value of "12" But how can I get around this?
I've read through the thread How do I find the index of an item in a vector? But I am still struggling since that one seems to talk about how to find the indices of "two" in a vector if you are looking for "two" specifically.
Upvotes: 1
Views: 636
Reputation:
If you are searching the index numbers of the positive arguments of your vector you might also use map-indexed
which gives you the arguments indexed from 0 to (dec (count your-vector))
in combination with (filter pos?)
to filter out the positive entries of your vector. filter
function goes through your vector and filters the numbers which fill the prerequisites of its predicate (in this case they should be positives), then you can easily ask for their index which you had just created using the map-indexed
:
(defn indexof [a]
(->> a
(map-indexed vector)
(filter #(pos? (second %)))
(map first)))
(indexof [-1 -1 -2 -1 -4 0 -6 -1 -5 -2 -10 4 -12 -4 -6 -1 -16 3 -18 2 -10 -8 -22 12 -19
-10 -14 0 -28 12 -30 -1 -18 -14 -22 19 -36 -16 -22 10 -40 12 -42 -4 -12 -20 -46 28 -41 -7])
=> (11 17 19 23 29 35 39 41 47)
If you search for zero's, just change the predicate of the filter
to zero?
to search for zero's:
(defn indexof-zero [a]
(->> a
(map-indexed vector)
(filter #(zero? (second %)))
(map first)))
(indexof-zero [-1 -1 -2 -1 -4 0 -6 -1 -5 -2 -10 4 -12 -4 -6 -1 -16 3 -18 2 -10 -8 -22 12 -19
-10 -14 0 -28 12 -30 -1 -18 -14 -22 19 -36 -16 -22 10 -40 12 -42 -4 -12 -20 -46 28 -41 -7])
=> (5 27)
Upvotes: 1
Reputation: 144136
You can use keep-indexed
:
(keep-indexed (fn [idx v] (if (pos? v) idx)) input-vector)
Upvotes: 6