v=: ((1 2);(3 4);(0 5);(2 1))
diff=: ([{]) ,. ]
direction_vector=: <"1 #: (-"0 #:(-/"2 #: (>"0 #: (diff))))
distance=: +/"1 #: *: #: (>"2 #:(direction_vector))
I want to get a dissimilarity matrix that looks like
(0 distance v),. (1 distance v),. (2 distance v) ,. (3 distance v)
I tried
i.4 distance v
which gave me an index error
Anyone can help me on this?
Thank you!
You are close, but you have two issues to deal with. One is that you want to complete the calculation of i. 4 before you apply distance (which is why you get the index error). Parenthesis to change the order of calculation are the solution to this.
i. 4 distance v
|index error: diff
| i.4 distance v
(i. 4) distance v
0 0 0 0
The second issue is that you want to apply each atom of i.4 to the whole of v and you do this by using " (rank) to specify 0 (atoms) for the left argument and _ (infinity) for the whole of the right argument.
(i. 4) distance"0 _ v
0 8 10 2
8 0 10 10
10 10 0 20
2 10 20 0
Related
Problem
Isyana is given the number of visitors at her local theme park on N consecutive days. The number of visitors on the i-th day is Vi. A day is record breaking if it satisfies both of the following conditions:
The number of visitors on the day is strictly larger than the number of visitors on each of the previous days.
Either it is the last day, or the number of visitors on the day is strictly larger than the number of visitors on the following day.
Note that the very first day could be a record breaking day!
Please help Isyana find out the number of record breaking days.
Input
The first line of the input gives the number of test cases, T. T test cases follow. Each test case begins with a line containing the integer N. The second line contains N integers. The i-th integer is Vi.
Output
For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the number of record breaking days.
Limits
Time limit: 20 seconds per test set.
Memory limit: 1GB.
1 ≤ T ≤ 100.
0 ≤ Vi ≤ 2 × 105.
Test set 1
1 ≤ N ≤ 1000.
Test set 2
1 ≤ N ≤ 2 × 105 for at most 10 test cases.
For the remaining cases, 1 ≤ N ≤ 1000.
Sample
Input
Output
4
8
1 2 0 7 2 0 2 0
6
4 8 15 16 23 42
9
3 1 4 1 5 9 2 6 5
6
9 9 9 9 9 9
Case #1: 2
Case #2: 1
Case #3: 3
Case #4: 0
In Sample Case #1, the bold and underlined numbers in the following represent the record breaking days: 1 2 0 7 2 0 2 0.
In Sample Case #2, only the last day is a record breaking day.
In Sample Case #3, the first, the third, and the sixth days are record breaking days.
In Sample Case #4, there is no record breaking day.
-------------------------------------------------------------------------
This python code which i submitted in 'Kickstart Round D problem 1: Record Broker'. I executed this code on my local machine and there wasn't any run time error on top of that I couldn't brute force any test case that could break the code or give wrong answer. But while doing submission in kickstart it gives me runtime error. What could be the issue for getting run time error on kickstart? Please help!
cases = int(input().strip())
for q in range(1, cases + 1):
l = int(input().strip())
ls = list(map(int, input().split(" ")))
l = len(ls)
local_max = 0
count = 0
for i in range(l):
if i == 0:
if ls[1] < ls[0]:
local_max = ls[0]
count += 1
continue
if i == l - 1:
if ls[i] > ls[i - 1] and ls[i] > local_max:
count += 1
break
if ls[i] > ls[i - 1] and ls[i] > ls[i + 1] and ls[i] > local_max:
count += 1
local_max = ls[i]
continue
print("Case #{}: {}".format(q, count))
this line:
if ls[1] < ls[0]:
What if ls only has 1 element?
After fixing that your code still returns the wrong answer if there's only 1 element.
Also, consider this test case:
"65 87 87 34 64 59 93 20 95 85 24 99 62 100 60 19 100"
64 is not valid, but you count it.
Consider:
x =. 0 1 2 3 4 1 3 4 99
v =. [ {.~ (>: # i.&1 # (##~. = #\))
v x NB. => 0 1 2 3 4 1
The behavior is correct. But as you can see, v is shamefully verbose. Is there a better solution?
You want the monad ~: (nub sieve):
v =: {.~ 1 + 0 i.~ ~:
x =: 0 1 2 3 4 1 3 4 99
v x
0 1 2 3 4 1
Code review:
Outside code-golf contexts, don't use #\ in place of i.##. It's too cutesy, hard to maintain, and won't be recognized by the special-code optimizer.
Don't assign to the names x, y, u, v, m, or n (except in special circumstances, and always locally in an explicit context).
Here's the table:
Should not they have the same result mathematically? (the average score of the per column and per row average)
The missing cells mean that your cells aren't all weighted evenly.
For example, row 11 has only two cells 82.67 and 90. So for your row average for row 11 they are weighted much more heavily than in your column averages where they are 1/13 and 1/14 of a column instead of 1/2 of a row.
Try filling up all the empty cells with 0 and the averages should match.
Taking a more extreme version of Ruslan Karaev's example:
5 5 5 | 5
1 | 1 Average of Average of Rows = (5 + 1 + 0) / 3 = 2
0 | 0
-----
2 5 5
Average of Average of Columns = (2 + 5 + 5) / 3 = 4
Yes, for example, the following two expressions:
/ a + b X + Y \ / a + X b + Y \
( ----- + ----- ) ( ----- + ----- )
\ 2 2 / \ 2 2 /
------------------- -------------------
2 2
are indeed mathematically equivalent, both coming out to be (a + b + X + Y) / 4.
However, short of having enough sufficient precision to store values, you may find that rounding errors accumulate differently depending on the order of operations.
You can see this sort of effect in a much simpler example if you assume a 3-digit precision and divide one by three, then multiply the result of that by three again:
1 / 3 -> 0.333, 0.333 x 3 -> 0.999
Contrast that with doing the operations in the oppisite order:
1 x 3 = 3, 3 / 1 = 1
The i. primitive produces a list of integers:
i. 10
0 1 2 3 4 5 6 7 8 9
If I want to produce several short lists in a row, I do this:
;i."0 each [ 2 3 4
0 1 0 1 2 0 1 2 3
(the result I want)
Boxing (that each) is a crutch here, because without it, i."0 produces a matrix.
i."0 [ 2 3 4
0 1 0 0
0 1 2 0
0 1 2 3
(the result I don't want)
Is there a better way to not have i."0 format the output to a matrix, but an array?
No, I believe you can't do any better than your current solution. There is no way for i."0 to return a vector.
The "0 adverb forces i. to accept scalars, and i. returns vectors. i. has no way of knowing that your input was a vector rather than a scalar. According to The J primer the result shape is the concatenation of the frame of the argument and the result.
The shortest "box-less" solution I've found so far is
(*#$"0~#&,i."0) 2 3 4
which is still longer than just using ;i. each 2 3 4
Say I have a list of 2 colours, Black and White. It's only possible to have 1 combination using these colours, because you can't have two of the same.
If I have 3 colours (Black, White and Red), there are 3 possible combination (Black+White, Black+Red, White+Red).
If I have 4 colours, there are 5 possible combinations and if I have 5 colours there are 10 possible combinations.
I'm trying to work out the relationship between the number of colours and the possible combinations. Here is some data:
Colours Combinations
0 0
1 0
2 1
3 3
4 5
5 10
6 14
You want the binomial coefficients.
The formula for the number of pairs from a set of size n is n * (n - 1) / 2.
Your values are incorrect. The correct values are:
n nCr (r=2)
2 1
3 3
4 6
5 10
6 15
This sequence is also known as the triangular numbers.
Read about Combinations
The formula to calculate the value is:
(n!)/(k!(n-k)!)
Where n is the total amount of possible colors and k is how many colors will you pick, so
"1 out of 3" = 3! / 1!*2! => 3*2*1/1*2*1 = 3
and so on...
Combinations of num_colors taken 2 at a time:
C(n, k) = n!/(k!*(n - k!))
C(0, 2) = C(1, 2) = 0 by definition in your case
C(2, 2) = 2!/(2!*0!) = 2!/2! = 1 (0! is usually 1)
C(6, 2) = 6!/(2!*4!) = 15 (is your 14 a mistake?)
This simplifies to n*(n - 1) / 2 when k = 2.