How to apply same fetch x value to different cells in structured array - j

For a 3 row array of a boxed structure e.g. q
]q=: ,. (0;'At least one uppercase letter';'At least one special character from "#$%^"';'Must be 10 to 30 characters long') ; (1;'howaboutthat';2020 11 3 13 10 8.913) ; < (1;'123#gmail.com';20 11 3 13 10 8.913)
┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│┌─┬─────────────────────────────┬──────────────────────────────────────────┬────────────────────────────────┐│
││0│At least one uppercase letter│At least one special character from "#$%^"│Must be 10 to 30 characters long││
│└─┴─────────────────────────────┴──────────────────────────────────────────┴────────────────────────────────┘│
├─────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│┌─┬────────────┬─────────────────────┐ │
││1│howaboutthat│2020 11 3 13 10 8.913│ │
│└─┴────────────┴─────────────────────┘ │
├─────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│┌─┬─────────────┬───────────────────┐ │
││1│123#gmail.com│20 11 3 13 10 8.913│ │
│└─┴─────────────┴───────────────────┘ │
└─────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
(0 0;0){:: q will extract the value in q which is the first cell. (1 0;0) and (2 0;0) with {:: work for the other 2 rows as well.
I want to do something like (0 0;0){:: each q to get all three values out of the array. Is this possible with fetch? I tried different ranks and different enclosures on x and y to no avail.
Here is what I have now that works well but an "each" use of (<0 0;0) would be preferred. I'll review the provided solutions.
((<0 0;0),(<1 0;0),(<2 0;0)) {::every <finalcheck
0 1 1
New image of q/finalcheck below.

] data0=. (0 ; 'str01' ; 'str02') ; (1 ; 'str11' ; 1 2) ; < (2 ; 'str21' ; 2 2 42)
+---------------+-------------+----------------+
|+-+-----+-----+|+-+-----+---+|+-+-----+------+|
||0|str01|str02|||1|str11|1 2|||2|str21|2 2 42||
|+-+-----+-----+|+-+-----+---+|+-+-----+------+|
+---------------+-------------+----------------+
NB. case 0: a scalar is stored and a scalar will be extracted
(0 {:: {.) L: _1 data0
+-+-+-+
|0|1|2|
+-+-+-+
(0 {:: {.) S: _1 data0
0 1 2
] data1=. (0 ; 'str01' ; 'str02') ; (1 0 ; 'str11' ; 1 2) ; < (2 0 42 ; 'str21' ; 2 2 42)
+---------------+---------------+---------------------+
|+-+-----+-----+|+---+-----+---+|+------+-----+------+|
||0|str01|str02|||1 0|str11|1 2|||2 0 42|str21|2 2 42||
|+-+-----+-----+|+---+-----+---+|+------+-----+------+|
+---------------+---------------+---------------------+
NB. case 1: a vector is stored and a vector will be extracted
(0 {:: {.) L: _1 data1
+-+---+------+
|0|1 0|2 0 42|
+-+---+------+
NB. case 2: a vector is stored and a scalar will be extracted
((0 ; 0) {:: {.) L: _1 data1
+-+-+-+
|0|1|2|
+-+-+-+
((0 ; 0) {:: {.) S: _1 data1
0 1 2

Related

Print values and it's associated index when working with vectors / matrix in J

If I want to check how many values in a vector or matrix are less than a given value
I can use +/ (a < 20). But what if I wanted to know both the specific value and it's index.
Something like (2(value) 5(index)) as a table. I looked at i., i: (which give first and last position) and I. Does sorting first help?
A very common pattern in J is the creation of a mask from a filter and applying an action on and/or using the masked data in a hook or fork:
((actions) (filter)) (data)
For example:
NB. Random array
a =: ? 20 $ 10
6 3 9 0 3 3 0 6 2 9 2 4 6 8 7 4 6 1 7 1
NB. Filter and mask
f =: 5 < ]
m =: f a
1 0 1 0 0 0 0 1 0 1 0 0 1 1 1 0 1 0 1 0
NB. Values of a on m
m # a
6 9 6 9 6 8 7 6 7
NB. Indices of a on m
I. m
0 2 7 9 12 13 14 16 18
NB. Joint results
(I.m) ,: (m # a)
0 2 7 9 12 13 14 16 18
6 9 6 9 6 8 7 6 7
In other words, in this case you have m&# and f acting on a and I. acting on m. Notice that the final result can be derived from an action on m alone by commuting the arguments of copy #~:
(I. ,: (a #~ ]) m
0 2 7 9 12 13 14 16 18
6 9 6 9 6 8 7 6 7
and a can be pulled out from the action on m like so:
a ( (]I.) ,: (#~ ])) m
But since m itself is derived from an action (f) on a, we can write:
a ( (]I.) ,: (#~ ])) (f a)
which is a simple monadic hook y v (f y) → (v f) y.
Therefore:
action =: (]I.) ,: (#~ ])
filter =: 5 < ]
data =: a
(action filter) data
0 2 7 9 12 13 14 16 18
6 9 6 9 6 8 7 6 7

Last digit of a huge number

I'm solving Last digit of a huge number in Codewars.
I managed to find out a way to calculate odd number ^ odd number and odd number ^ even number. However I got stuck in even ^ even/odd as the relation between the number and its power's mod is not obvious.
Here's what I managed to get:
lastDigit :: [Integer] -> Integer
lastDigit = (`rem` 10) . go
where
go [] = 1
go [x] = x
go (x : y : r)
| odd x && odd y = x ^ (go (y : r) `rem` (x + 1))
| odd x && even y = x ^ (foldMod y (go r) (x + 1))
| otherwise = -- any hint?
foldMod :: Integer -> Integer -> Integer -> Integer
foldMod _ 0 _ = 1
foldMod base 1 modulo = base `rem` modulo
foldMod base power modulo = (base * foldMod base (power -1) modulo) `rem` modulo
Can anyone give some hints on how to do about the even cases?
I recommend rethinking your approach, and beginning with a more general function. Specifically, can you compute
powMod :: Integer -> [Integer] -> Integer
where powMod base exponents computes the exponential tower described in the problem mod base? Note that when you recurse, you will recurse with a different base -- because the cycle lengths of the various first exponents are not necessarily all divisors of base. For example, in base 10, any first exponent with a last digit of 7 cycles every four goes, not every ten; the last digit of the powers goes like this:
x 0 1 2 3 4 5 6 7
7^x `mod` 10 1 7 9 3 1 7 9 3
You'll also want to watch out for situations where the first exponent is not itself in the eventual cycle that gets reached; for example, in base 4, we have:
x 0 1 2 3 4 5 6 7
2^x `mod` 4 1 2 0 0 0 0 0 0
So you can't look at just x `mod` 1 and infer from it what 2^x `mod` 4 is, even though the cycle length is 1. (There are other examples, like 2^x `mod` 12, where the cycle is longer than 1 and still doesn't have the original 2 in it.)
You do not need to calculate the entire number to know the last digit, there are fast, and less fast approaches that can calculate the last digit without having to worry about all the other digits.
A simple approach that can calculate ab in O(log b) time is by making use of the following equivalence:
(a×b) mod m = ((a mod m) × (b mod m)) mod m.
This means we can each time calculate the last digit and work with that. It thus means that as long as we can represent all numbers up to 81, we can calculate the ab mod m powMod m a b:
powMod :: Int -> Int -> Int -> Int
powMod m = go
where go _ 0 = 1
go a b | even n = r
| otherwise = (x * r) `mod` m
where r = go ((a*a) `mod` m) (div b 2) `mod` m
If we assume we can calculate modulo, divide by two, check if a value is even, etc. in constant time, this runs in O(log b).
But we can even do this faster by looking for cycles. Imagine that we have to calculate a power of 7, then 7^0 is 1, 7^1 is 7, 7^2 mod 10 = 9, etc. We can make a table with multiplications:
× │ 0 1 2 3 4 5 6 7 8 9
──┼─────────────────────
0 │ 0 0 0 0 0 0 0 0 0 0
1 │ 1 2 3 4 5 6 7 8 9
2 │ 4 6 8 0 2 4 8 6
3 │ 9 2 5 8 1 4 7
4 │ 6 0 4 8 2 6
5 │ 5 0 5 0 5
6 │ 6 2 8 4
7 │ 9 6 3
8 &verbar; 4 2
9 &verbar; 1
If we thus look at the last digit for a power of 7, we see that for:
power │ │ last digit
──────────────────────────
0 │ │ 1
1 │ 7*1 │ 7
2 │ 7*7 │ 9
3 │ 7*9 │ 3
4 │ 7*3 │ 1
This thus means that there is a cycle, indeed, after each multiplication with 7, we move to the next state, and thus obtain the following graph:
1 &rightarrow; 7 &rightarrow; 9 &rightarrow; 3
&nwarr;_______________/
This thus means that the cycle has a length of four. It thus means that if we have to calculate 7 to the power of for example 374, then we know that these are 93 cycles of length four that have thus no impact, and two extra moves, we thus know that the last digit of 7393 is 9, without having to compute this number. Since such cycles has a maximum length of 10, this can thus done in constant time do determine the last digit of the decimal number.

How to recognize [1,X,X,X,1] repeating pattern in panda serie

I have a boolean column in a csv file for example:
1 1
2 0
3 0
4 0
5 1
6 1
7 1
8 0
9 0
10 1
11 0
12 0
13 1
14 0
15 1
You can see here 1 is reapting every 5 lines.
I want to recognize this repeating pattern [1,0,0,0] as soon as the repetition is above 10 in python (I have ~20.000 rows/file).
The pattern can start at any position
How could I manage this in python avoiding if .....
# Generate 20000 of 0s and 1s
data = pd.Series(np.random.randint(0, 2, 20000))
# Keep indices of 1s
idx = df[df > 0].index
# Check distance of current index with next index whether is 4 or not,
# Say if position 2 and position 6 is found as 1, so 6 - 2 = 4
found = []
for i, v in enumerate(idx):
if i == len(idx) - 1:
break
next_value = idx[i + 1]
if (next_value - v) == 4:
found.append(v)
print(found)

Use # (Copy) as selection or filter on 2-d array

The J primitive Copy (#) can be used as a filter function, such as
k =: i.8
(k>3) # k
4 5 6 7
That's essentially
0 0 0 0 1 1 1 1 # i.8
The question is if the right-hand side of # is 2-d or higher rank shaped array, how to make a selection using #, if possible. For example:
k =: 2 4 $ i.8
(k > 3) # k
I got length error
What is the right way to make such a selection?
You can use the appropriate verb rank to get something like a 2d-selection:
(2 | k) #"1 1 k
1 3
5 7
but the requested axes have to be filled with 0s (or !.) to keep the correct shape:
(k > 3) #("1 1) k
0 0 0 0
4 5 6 7
(k > 2) #("1 1) k
3 0 0 0
4 5 6 7
You have to better define select for dimensions > 1 because now you have a structure. How do you discard values? Do you keep empty "cells"? Do you replace with 0s? Is structure important for the result?
If, for example, you only need the "values where" then just ravel , the array:
(,k > 2) # ,k
3 4 5 6 7
If you need to "replace where", then you can use amend }:
u =: 5 :'I. , 5 > y' NB. indices where 5 > y
0 u } k
0 0 0 0
0 5 6 7
z =: 3 2 4 $ i.25
u =: 4 :'I. , (5 > y) +. (0 = 3|y)' NB. indices where 5>y or 3 divides y
_999 u } z
_999 _999 _999 _999
_999 5 _999 7
8 _999 10 11
_999 13 14 _999
16 17 _999 19
20 _999 22 23

FoldList like primitive in J

Mathematica has a built-in function called FoldList FoldList function description. Is there a similar primitive verb in J?
(I know that J has a ^: verb, which is like Nest and FixedPoint.)
To clarify my question, J has dyadic verb, so usually u / x1 x2 x3 becomes x1 u (x2 u x3), which works just like FoldList, with reverse order.
Except if the function u takes y, in a different shape from x. In FoldList there is an initial x. In J, if x3 is a different shape, one has to rely on < to pack it together. For example, one has to pack and unpack
[list =. (;/ 3 3 4 3 3 34),(< 1 2)
+-+-+-+-+-+--+---+
|3|3|4|3|3|34|1 2|
+-+-+-+-+-+--+---+
tf =: 4 : '<((> x) , >y)'
tf/ list
+----------------+
|1 2 3 3 4 3 3 34|
+----------------+
tf/\ |. list
+---+------+--------+----------+------------+--------------+----------------+
|1 2|1 2 34|1 2 34 3|1 2 34 3 3|1 2 34 3 3 4|1 2 34 3 3 4 3|1 2 34 3 3 4 3 3|
+---+------+--------+----------+------------+--------------+----------------+
which is kind of inconvenient. Any better solutions?
u/\ comes very close (if you don't mind the right folding):
+/\ 1 2 3 4
1 3 6 10
*/\1+i.10
1 2 6 24 120 720 5040 ...
(+%)/\7#1. NB. continued fraction of phi
1 2 1.5 1.66667 1.6 1.625 1.61538
edit on your edit:
The first two elements of FoldList are x and f(x,a). In J those two have to be of the same "kind" (shape+type) if you want them on the same list. The inconvenience comes from J's data structures not from the lack of a FoldList verb. If you exclude x from the list, things are easier:
FoldListWithout_x =: 1 : 'u/ each }.<\y'
; FoldListWithout_x 1 2 3 4
┌─────┬───────┬─────────┐
│┌─┬─┐│┌─┬─┬─┐│┌─┬─┬─┬─┐│
││1│2│││1│2│3│││1│2│3│4││
│└─┴─┘│└─┴─┴─┘│└─┴─┴─┴─┘│
└─────┴───────┴─────────┘
>+ FoldListWithout_x 1 2 3 4
3 6 10
(+%) FoldListWithout_x 7#1
┌─┬───┬───────┬───┬─────┬───────┐
│2│1.5│1.66667│1.6│1.625│1.61538│
└─┴───┴───────┴───┴─────┴───────┘
The next logical step is to include a boxed x after making the folds, but that will either require more complex code or a case-by-case construction. Eg:
FoldList =: 1 :'({.y) ; u FoldListWithout_x y'
+ FoldList 1 2 3 4
┌─┬─┬─┬──┐
│1│3│6│10│
└─┴─┴─┴──┘
; FoldList 1 2 3 4
┌─┬─────┬───────┬─────────┐
│1│┌─┬─┐│┌─┬─┬─┐│┌─┬─┬─┬─┐│
│ ││1│2│││1│2│3│││1│2│3│4││
│ │└─┴─┘│└─┴─┴─┘│└─┴─┴─┴─┘│
└─┴─────┴───────┴─────────┘
vs
FoldList =: 1 :'(<{.y) ; u FoldListWithout_x y'
+ FoldList 1 2 3 4
┌───┬─┬─┬──┐
│┌─┐│3│6│10│
││1││ │ │ │
│└─┘│ │ │ │
└───┴─┴─┴──┘
; FoldList 1 2 3 4
┌───┬─────┬───────┬─────────┐
│┌─┐│┌─┬─┐│┌─┬─┬─┐│┌─┬─┬─┬─┐│
││1│││1│2│││1│2│3│││1│2│3│4││
│└─┘│└─┴─┘│└─┴─┴─┘│└─┴─┴─┴─┘│
└───┴─────┴───────┴─────────┘
I guess #Dan Bron's comment deserves an answer. It is discussed with some solutions in http://www.jsoftware.com/pipermail/programming/2006-May/002245.html
if we define an adverb (modified from the link above)
upd =: 1 : 0
:
u&.> /\ ( <"_ x),<"0 y
)
then
1 2 , upd |. 3 3 4 3 3 34
┌───┬──────┬────────┬──────────┬────────────┬──────────────┬────────────────┐
│1 2│1 2 34│1 2 34 3│1 2 34 3 3│1 2 34 3 3 4│1 2 34 3 3 4 3│1 2 34 3 3 4 3 3│
└───┴──────┴────────┴──────────┴────────────┴──────────────┴────────────────┘

Resources