#PWC 241

1 messages · Page 1 of 1 (latest)

rancid ridge
rancid ridge
#
@assert task1([0, 1, 4, 6, 7, 10], 3) == 2
@assert task1([4, 5, 6, 7, 8, 9], 2) == 2
@assert task2([11, 8, 27, 4]) == [11, 4, 8, 27]
#

Free tests ^^

#

Task 1
||```julia
task1(nums, dif) = sum(firstindex(nums):lastindex(nums)-2) do i
sum(i+1:lastindex(nums)-1) do j
count(j+1:lastindex(nums)) do k
(nums[j]-nums[i] == dif) &&
(nums[k]-nums[j] == dif)
end
end
end

||This is just the dead simple version since the vectors given are small, this is O(n^3)||
#

Task 2
||```julia
task2(nums) = sort(nums, by = num -> (nFactors(num), num))
function nFactors(num)
num < 2 && return 0
factorsOf2 = trailing_zeros(num)
num >>= factorsOf2
factorsOf2 + sum(3:2:isqrt(num), init = 0) do k
acc = 0
while iszero(num%k)
num ÷= k
acc += 1
end
acc
end
end

#

Task 1 attempt 2
||```julia

assuming strictly increasing

task1(nums, dif) = count(firstindex(nums):lastindex(nums)-2) do i
js = searchsorted(view(nums, i+1:lastindex(nums)-1), nums[i]+dif)
length(js) == 1 && insorted(nums[i]+2dif, view(nums, only(js)+1:lastindex(nums)))
end