Sortperm for matrix sorting in Julia lang - sorting

I am using Julia 1.6.1.
B is a matrix. For example,
B =
[ 2 4 4 4 5 ;
1 2 2 3 5 ;
1 2 3 3 3 ;
1 2 2 5 6 ;
1 3 4 4 4 ; ]
I wanted to sort it forcsing on each row.
sortedB = sortslices( B, dims=1, rev=true)
Then, we get sorted B
sortedB =
[ 2 4 4 4 5 ; # 1st row of the original matrix B
1 3 4 4 4 ; # 5th row of the original matrix B
1 2 3 3 3 ; # 3rd row of the original matrix B
1 2 2 5 6 ; # 4th row of the original matrix B
1 2 2 3 5 ;] # 2nd row of the original matrix B
I would like to get the array [1 5 3 4 2].
How can I do that ?
It seems that sortperm does not work.
sortperm( sortslices( B, dims=1, rev=true) )
# ERROR: MethodError; no method matching sortperm(::Matrix{Int64})

If performance is an issue use a non-allocating version.
julia> sortperm(view.(Ref(B), 1:size(B,1), :), rev=true)
5-element Vector{Int64}:
1
5
3
4
2
Here are some benchmarks using BenchmarkTools:
julia> #btime sortperm(view.(Ref($B), 1:size($B,1), :),rev=true);
376.471 ns (3 allocations: 432 bytes)
julia> #btime sortperm(collect(eachslice($B,dims=1)),rev=true)
642.683 ns (6 allocations: 496 bytes);

you can use eachrow or eachslice:
julia> C = collect(eachslice(B,dims=1))
5-element Vector{SubArray{Int64, 1, Matrix{Int64}, Tuple{Int64, Base.Slice{Base.OneTo{Int64}}}, true}}:
[2, 4, 4, 4, 5]
[1, 2, 2, 3, 5]
[1, 2, 3, 3, 3]
[1, 2, 2, 5, 6]
[1, 3, 4, 4, 4]
julia> sortperm(C,rev=true)
5-element Vector{Int64}:
1
5
3
4
2
although this will allocate more than necessary (collect is needed apparently)

Related

How to find the count of the maximum pair of a window

Today I got an interview question about finding the maximum pair window but I felt like I could solve it more efficient. I was wondering if theres any way to solve it more efficiently.
Input: nums = [1,3,-1,-3,5,3,6,7], k = 3, n = 7
Output: 3
Explanation:
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 4 (1,3) < 7 false
1 [3 -1 -3] 5 3 6 7 2 (3, -1) < 7 false
1 3 [-1 -3 5] 3 6 7 4 (5, -1) < 7 false
1 3 -1 [-3 5 3] 6 7 8 (5,3) > 7 true
1 3 -1 -3 [5 3 6] 7 11 (6,5) > 7 true
1 3 -1 -3 5 [3 6 7] 13 (6,7) > 7 true
just solved it by getting the subarray -> sorting then getting the last 2 element but I think theres a more efficient solution?

Confused about the depth-first-search result when using Golang

I tried to solve the 'Combination Sum' on leetcode, and the result is wrong when using test case:
[7,3,2] 18
I used C++ with the same logic and passed, but when using Golang, my result is:
[[2,2,2,2,2,2,2,2,2],[2,2,2,2,2,7,3,3],[2,2,2,2,3,7],[2,2,2,3,3,3,3],[2,2,7,7],[2,3,3,3,7],[3,3,3,3,3,3]]
and the correct one should be
[[2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,3,3],[2,2,2,2,3,7],[2,2,2,3,3,3,3],[2,2,7,7],[2,3,3,3,7],[3,3,3,3,3,3]]
the code is shown below:
import "sort"
func combinationSum(candidates []int, target int) [][]int {
result := make([][]int, 0, 0)
resultp := &result
sort.Ints(candidates)
helper(candidates, 0, target, make([]int, 0, 0), resultp, len(candidates))
return *resultp
}
func helper(nums []int, index int, target int, list []int, resultp *[][]int, length int) {
if target == 0 {
*resultp = append(*resultp, list)
return
}
for i := index; i < length; i++ {
if i != index && nums[i] == nums[i - 1] {
continue
}
if (nums[i] > target) {
break
}
helper(nums, i, target - nums[i], append(list, nums[i]), resultp, length)
}
}
Can anyone tell me why the result is incorrect, I am just confused about the [2,2,2,2,2,7,3,3] in my answer, why the 7 is before the 3 since the array has been sorted? Or anyone can tell me what mistake I have made in my code
append function may or may not modify the underlying array that your slice refers to. So you are not creating a completely new list when using append. I changed helper to match your desired behavior.
for i := index; i < length; i++ {
if i != index && nums[i] == nums[i - 1] {
continue
}
if nums[i] > target {
break
}
var newList []int
newList = append(newList, list...)
newList = append(newList, nums[i])
helper(nums, i, target - nums[i], newList, resultp, length)
}
If list has capacity, then it will be modified and therefore you are modifying your argument. Instead make a copy of list, and then append nums[i] to it.
See Go Slices: usage and internals
The line
helper(nums, i, target - nums[i], append(list, nums[i]), resultp, length)
may not perform as expected. It is called within the loop, and you are probably assuming that in the each iteration the append will always add the new member to the existing slice. If has more complex behavior that you seem not caring about enough:
If the new value fits into the current capacity of the backing array withing the slice, it is added to the current backing array. All variables assigned to that slice now report the updated content with the added new value present.
If the value does not fit, a new array is allocated. In this case further modifications of the returned slice will not change the content of the initial slice if that old value is also retained.
I am under impression that you may not expect value/content disagreement between the value returned by append and the parameter list you pass to it.
This behavior is described here (scroll to "gotcha").
So you can see the behavior a bit better by adding some print output:
https://play.golang.org/p/JPmqoAJE4S
Importantly, you can see it at this point:
0694 helper [2 3 7] 1 1 [2 2 2 2 2 2 2 3] [[2 2 2 2 2 2 2 2 2]] 3
4425 calling down 1 6 [2 2 2 2 2 2] 3
8511 helper [2 3 7] 1 3 [2 2 2 2 2 2 3] [[2 2 2 2 2 2 2 2 2]] 3
8511 calling down 1 3 [2 2 2 2 2 2 3] 3
8162 helper [2 3 7] 1 0 [2 2 2 2 2 2 3 3] [[2 2 2 2 2 2 2 2 2]] 3
8162 solution [2 2 2 2 2 2 3 3] [[2 2 2 2 2 2 2 2 2] [2 2 2 2 2 2 3 3]]
1318 calling down 1 8 [2 2 2 2 2] 3
5089 helper [2 3 7] 1 5 [2 2 2 2 2 3] [[2 2 2 2 2 2 2 2 2] [2 2 2 2 2 3 3 3]] 3
5089 calling down 1 5 [2 2 2 2 2 3] 3
4728 helper [2 3 7] 1 2 [2 2 2 2 2 3 3] [[2 2 2 2 2 2 2 2 2] [2 2 2 2 2 3 3 3]] 3
1318 calling down 2 8 [2 2 2 2 2] 7
3274 helper [2 3 7] 2 1 [2 2 2 2 2 7] [[2 2 2 2 2 2 2 2 2] [2 2 2 2 2 7 3 3]] 3
This is the sequence of actions:
You recursively call with [2 2 2 2 2 2 3] and append 3. You find that this is a valid solution and add [2 2 2 2 2 2 3 3] to the result slice.
You return up a few levels until you're back to [2 2 2 2 2] (before adding the 6th 2) and start trying to add 3s. You recursively call with [2 2 2 2 2] and append 3. Unfortunately, this overwrites your existing solution [2 2 2 2 2 2 3 3]. Since it's using the same backing array, you append 3 to the first 5 items in that slice, overwriting the 6th index in the slice you previously added to your solution set. Your second solution becomes [2 2 2 2 2 3 3 3] (note the 3 in the 6th slot)
You find that this solution set isn't going to work after a couple iterations (at [2 2 2 2 2 3 3]) because the remaining target (2) is less than the last number added (3), so you return up.
You repeat this sequence with a 7 in the 6th slot, overwriting the underlying array index again. Your second solution becomes [2 2 2 2 2 7 3 3], because you're still using the same underlying array. You find this solution also won't work, and return up.
After this point, you return up to before the list slice was greater than 4 in length (which is when the slice grew, by default it grows by doubling in size), meaning you're using a different (previous) backing array, which is why further iterations do not further change the existing solutions. By luck, none of the remaining solutions collide in a similar fashion.
This alternative print version shows you where the backing array changes (by showing where the address of the first entry changes): https://play.golang.org/p/nrgtMyqwow. As you can see, it changes when you grow beyond lengths 2, 4, and 8, but as you return upwards, you end up reverting back to different backing arrays.
The easiest solution to fix your specific problem is to copy the list slice before adding it to the solution set:
if target == 0 {
sol := make([]int, len(list))
copy(sol, list)
*resultp = append(*resultp, sol)
return
}
https://play.golang.org/p/3qTKoAumj0
[[2 2 2 2 2 2 2 2 2] [2 2 2 2 2 2 3 3] [2 2 2 2 3 7] [2 2 2 3 3 3 3] [2 2 7 7] [2 3 3 3 7] [3 3 3 3 3 3]]

How sort this list in netlogo

I am trying to order the following list in netoglo:
[[0.1 [1 2 5 3 1]] [0.3 [1 2 1 3 1]] [0.2 [1 2 1 3 1]]]
Actually they are list lists and what I want is to sort by the first element of each sublist:
When I use sort [[0.1 [1 2 5 3 1]] [0.3 [1 2 1 3 1]] [0.2 [1 2 1 3 1]]]
It returns empty I suppose because it only sees lists and not anything so I sort.
Any ideas.
Thanks to alan the answer was show sort-by [first ?1 > first ?2][[0.1 [1 2 5 3 1]] [0.3 [1 2 1 3 1]] [0.2 [1 2 1 3 1]]]

How to construct matrix from row/column vectors in Julia

Given a function/expression that yields a single column, how to build a matrix from those columns in Julia? I tried the following (simplified example):
column(j) = [j, j, j] # for example
my_matrix = Float64[column(j)[i] for i in 1:3, j in 1:4]
==> 3x4 Array{Float64,2}:
1.0 2.0 3.0 4.0
1.0 2.0 3.0 4.0
1.0 2.0 3.0 4.0
The result is correct, but this is not what I want because the column-vector-expression is evaluated unnecessarily (once for every row).
I also tried this alternative approach:
[column(j) for j in 1:4]
==> 4-element Array{Array{Float64,1},1}:
[1.0,1.0,1.0]
[2.0,2.0,2.0]
[3.0,3.0,3.0]
[4.0,4.0,4.0]
But I found no way to convert or reshape this into the form I want (matrix with two dimensions instead of array of arrays).
How to achieve this in Julia?
Have you tried hcat?:
julia> column(j) = [j, j ,j]
column (generic function with 1 method)
julia> my_matrix = hcat([column(j) for j=1:4]...)
3x4 Array{Int64,2}:
1 2 3 4
1 2 3 4
1 2 3 4
See hcat in Julia docs
Looking at answer #Benoit it is actually faster to use reduce(hcat, c):
julia> c = [[j,j,j] for j in 1:4]
4-element Vector{Vector{Int64}}:
[1, 1, 1]
[2, 2, 2]
[3, 3, 3]
[4, 4, 4]
julia> #btime reshape(collect(Iterators.flatten($c)), 3, 4)
232.791 ns (6 allocations: 448 bytes)
3×4 Matrix{Int64}:
1 2 3 4
1 2 3 4
1 2 3 4
julia> #btime reduce(hcat, $c)
70.825 ns (1 allocation: 176 bytes)
3×4 Matrix{Int64}:
1 2 3 4
1 2 3 4
1 2 3 4
But I found no way to convert or reshape this into the form I want (matrix with two dimensions instead of array of arrays).
You can obtain the matrix by reshaping the flatten'ed version of the vector of columns c:
julia> column(j) = [j, j, j]
column (generic function with 1 method)
julia> c = [column(j) for j in 1:4]
4-element Vector{Vector{Int64}}:
[1, 1, 1]
[2, 2, 2]
[3, 3, 3]
[4, 4, 4]
julia> v = collect(Iterators.flatten(c))
12-element Vector{Int64}:
1
1
1
2
2
2
3
3
3
4
4
4
julia> m = reshape(v, 3, 4)
3×4 Matrix{Int64}:
1 2 3 4
1 2 3 4
1 2 3 4
The alternative hcat(c...) suggested by the accepted answer works as well but as pointed out by a comment, this will not work for a large number of columns.
Indeed, if c is a vector of n columns then hcat(c...) will compile a method with n arguments. This first means that if your code does this for different number of columns then you will need to compile a new method every time. Secondly, compiling a method with too many arguments will make the stack memory overflow.
For this reason, when you don't need a one-line solution (e.g. like the one combining flatten and reshape above), it seems to me that most Julia users do:
m = Matrix{Int}(undef, 3, 4)
for j in 1:4
m[:, j] = column(j)
end
EDIT #Przemyslaw suggested a better solution: https://stackoverflow.com/a/68976092/1189815
stack, which will be in Julia 1.9, and can be used in the meanwhile with Compat, will construct matrix from row/column vectors (returned from function/expression).
column(j) = [j, j, j]
using Compat #for stack which will be in Julia 1.9
stack(column(j) for j=1:4)
#3×4 Matrix{Int64}:
# 1 2 3 4
# 1 2 3 4
# 1 2 3 4
What is fast and memory efficient.
using BenchmarkTools, Compat
column(j) = [j, j, j]
#btime stack(column(j) for j=1:4)
# 193.315 ns (5 allocations: 480 bytes)
#3×4 Matrix{Int64}:
# 1 2 3 4
# 1 2 3 4
# 1 2 3 4
#btime begin ##Benoît Legat
m = Matrix{Int}(undef, 3, 4)
for j in 1:4
m[:, j] = column(j)
end
m
end
# 201.942 ns (5 allocations: 480 bytes)
#3×4 Matrix{Int64}:
# 1 2 3 4
# 1 2 3 4
# 1 2 3 4
#btime reduce(hcat, [column(j) for j=1:4]) ##Przemyslaw Szufel
# 249.676 ns (6 allocations: 560 bytes)
#3×4 Matrix{Int64}:
# 1 2 3 4
# 1 2 3 4
# 1 2 3 4
#btime reshape(collect(Iterators.flatten([column(j) for j in 1:4])), 3, 4) ##Benoît Legat
# 392.325 ns (10 allocations: 976 bytes)
#3×4 Matrix{Int64}:
# 1 2 3 4
# 1 2 3 4
# 1 2 3 4
#btime Int[column(j)[i] for i in 1:3, j in 1:4] ##radioflash
# 440.211 ns (13 allocations: 1.09 KiB)
#3×4 Matrix{Int64}:
# 1 2 3 4
# 1 2 3 4
# 1 2 3 4
#btime hcat([column(j) for j=1:4]...) ##spencerlyon2
# 644.676 ns (10 allocations: 784 bytes)
#3×4 Matrix{Int64}:
# 1 2 3 4
# 1 2 3 4
# 1 2 3 4
In case you have already a vector.
c = [[j,j,j] for j in 1:4]
#4-element Vector{Vector{Int64}}:
# [1, 1, 1]
# [2, 2, 2]
# [3, 3, 3]
# [4, 4, 4]
using Compat
stack(c)
#3×4 Matrix{Int64}:
# 1 2 3 4
# 1 2 3 4
# 1 2 3 4
using BenchmarkTools
#btime stack($c)
# 63.204 ns (1 allocation: 160 bytes)
#3×4 Matrix{Int64}:
# 1 2 3 4
# 1 2 3 4
# 1 2 3 4
#btime reduce(hcat, $c) ##Przemyslaw Szufel
# 68.758 ns (1 allocation: 160 bytes)
#3×4 Matrix{Int64}:
# 1 2 3 4
# 1 2 3 4
# 1 2 3 4
#btime begin ##Benoît Legat
m = Matrix{Int}(undef, 3, 4)
for j in 1:4
m[:, j] = $c[j]
end
m
end
# 75.586 ns (1 allocation: 160 bytes)
#3×4 Matrix{Int64}:
# 1 2 3 4
# 1 2 3 4
# 1 2 3 4
#btime reshape(collect(Iterators.flatten($c)), 3, 4) ##Benoît Legat
# 210.752 ns (5 allocations: 576 bytes)
#3×4 Matrix{Int64}:
# 1 2 3 4
# 1 2 3 4
# 1 2 3 4
#btime hcat($c...) ##spencerlyon2
# 453.213 ns (5 allocations: 384 bytes)
#3×4 Matrix{Int64}:
# 1 2 3 4
# 1 2 3 4
# 1 2 3 4

Balanced layout of n items in a grid

I have a list of n logos to display in a grid, with a maximum of 3 per row.
What's an algorithm to decide how many to display per row such that the number of logos per row is as balanced as possible without using more than the minimum possible number of rows?
For example:
n -> number in each row
1 -> 1
2 -> 2
3 -> 3
4 -> 2, 2
5 -> 3, 2
6 -> 3, 3
7 -> 3, 2, 2
8 -> 3, 3, 2
9 -> 3, 3, 3
10 -> 3, 3, 2, 2
For N <= 3 just use N.
If N is exactly divisible by 3 then use: 3 3 ... 3
If N when divided by 3 has remainder 1 then use: 3 3 ... 2 2
If N when divided by 3 has remainder 2 then use: 3 3 ... 3 2
AS confusing as your question is, I think what you need to do is first determine:
number_of_rows = ceil(number_of_logos / 3.0)
Then add a logo to each row, one at a time.
Python:
import math
def partition_logos(count, lsize):
num_lines = int(math.ceil(count / float(lsize)))
partition = [0] * num_lines
for i in xrange(count):
partition[i%num_lines] += 1
return partition
>>> for i in xrange(1,11):
... print partition_logos(i, 3)
[1]
[2]
[3]
[2, 2]
[3, 2]
[3, 3]
[3, 2, 2]
[3, 3, 2]
[3, 3, 3]
[3, 3, 2, 2]
A recursive solution, in Python:
def logos_by_row(N, rows):
width = 0
if N > 4 or N == 3:
width = 3
elif N == 4 or N == 2:
width = 2
elif N == 1:
width = 1
if width != 0:
rows.append(width)
logos_by_row(N - width, rows)
answer = []
for i in range(10):
logos_by_row(i+1, answer)
print answer
just use n/3 to calculate the row and n%3 to calculate the column
edit: ok i saw you edited your question.... i din't saw that you want to display 2 in each row if the are 4 logos. but then you can use n mod 3 to calculate if their is a reminder as others already suggested
if n%3 = 0 then just put 3 logos in each row
if n%3 = 1 then put the last 4 logos in two rows
if n%3 = 2 then put 3 logos in n row and the last 2 logos in a separate row

Resources