Enumerate non-1 step in Power M List - powerbi

I know there is enumeration in list of Power M
for {1, 2, 3, ... 10} can be achieved by
{1..25}
What about {0, 25, 50, ... 250}
how can i enumerate every 25?

You can go with:
List.Generate(() => 0, each _ > 250, each _ +25)

Related

Grouping list elements based on values in list

i have a list of elements(tuples):
tuples(
[
(a,b,[1,3,5,7]),
(a,b,[9,11,13,15]),
(a,b,[17,19,21,23]),
(c,d,[0,2,4,6]),
(c,d,[8,10,12,14]),
(c,d,[16,18,20,22]),
(e,f,[100,200,300,400]),
(e,f,[500,600,700,800]),
(e,f,[900,1000,1100,1200])
]
).
How to group them so it becomes:
[
(a,b,[1,3,5,7,9,11,13,15,17,19,21,23]),
(c,d,[0,2,4,6,8,10,12,14,16,18,20,22]),
(e,f,[100,200,300,400,500,600,700,800,900,1000,1100,1200])
]
As we can see, we grouped the (a,b) (c,d) (e,f) and concatenated their respective lists.
thank you for the help.
How about this somewhat SQL-ish way:
Define a predicate to access data in the original deep and nasty datastructure (which uses conjunction (_,_) to create what looks like n-tuples but are actually "nearly" lists, except at the final position
of the backbone). On backtracking, it will pull out the individual
records one by one and present the information therein in the head variables:
some_tuple(V,W,Values) :-
tuples(Tuples),
member((V,W,Values), Tuples).
And then collect using a 2-level bagof/3 call.
solution(Bag) :-
bagof((V,W,FlatBagForVW), % will backtrack over V,W
SubBag^(bagof(L,some_tuple(V,W,L),BagForVW), % collect lists for a given V,W
flatten(BagForVW,FlatBagForVW)), % flatten it
Bag). % what we want
Done!
If you are in SWI-Prolog, first tell the toplevel printer to not elide long lists so much:
?-
set_prolog_flag(answer_write_options,[max_depth(100)]).
Then:
?-
solution(Bag).
Bag = [(a,b,[1,3,5,7,9,11,13,15,17,19,21,23]),
(c,d,[0,2,4,6,8,10,12,14,16,18,20,22]),
(e,f,[100,200,300,400,500,600,700,800,900,1000,1100,1200])].
A variation using foldl/4 and then library(yall):
'Grouping list elements based on values in list'(Gs) :-
tuples(Ts),
foldl([(X,Y,L),V0,V1]>>(
( append(N,[(X,Y,L0)|M],V0)
-> append(L0,L,L1)
; N=V0, L1=L
),
append(N,[(X,Y,L1)|M],V1)
)
,Ts,[],Gs).
a day after...
There is a bug going unnoticed, namely M remains unbound where a pair of keys (that is, X,Y in a tuple) is not present in V0 (have attempted to keep the variables naming coherent to the docs for foldl/4). A possible correction, that illustrates a single unification call (=/2) to perform multiple 'assignments' at once:
...
foldl([(X,Y,L),V0,V1]>>(
( append(N,[(X,Y,L0)|M],V0)
-> append(L0,L,L1)
; (N,M,L1)=(V0,[],L1)
),
append(N,[(X,Y,L1)|M],V1)
...
Still, it's not clear to me why the bug didn't materialized... for instance, here it's clearly visible...
?- append([1,2,3],[4,5,6|_],R).
R = [1, 2, 3, 4, 5, 6|_19156].
another way...
library(solution_sequences) is a recent addition to SWI-Prolog arsenal, providing more 'SQL like' constructs, for instance group_by/4:
by_group_by(Gs) :-
tuples(Ts),
findall((X,Y,All_xy), (
group_by([X,Y],L,member((X,Y,L),Ts),Lt),
append(Lt,All_xy)
),Gs).
The documentation is too much terse, but overall, the library is worth a try. There are some examples posted in SWI-Prolog discourse group, but I find the data used there boring and difficult to understand.
Note: I used the syntax [X,Y] for the free variables, to make clear the 'shape' of this important specification is unrelated to the pattern.
First we need to be able to find the list of unique A,B pairings in the list of tuples:
foo(X,A,B) :-
setof( p(A,B), C^member( (A,B,C), X ), T),
member( p(A,B), T).
/* 48 ?- tuples(_X), foo(_X,A,B).
A = a,
B = b ;
A = c,
B = d ;
A = e,
B = f. */
Next we collect the tuples for each pair of the A and B values:
bar(X,A,B,G) :-
foo(X,A,B), % for each unique (A,B,_) in X
findall( C, % find all Cs such that (A,B,C) is in X
member( (A,B,C), X),
CS),
append( CS,G). % and append them together
/* 66 ?- tuples(_X), bar(_X,A,B,G).
A = a,
B = b,
G = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23] ;
A = c,
B = d,
G = [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22] ;
A = e,
B = f,
G = [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200].*/
So that the grouping is done with
groups(X,GS):-
findall( (A,B,G), bar(X,A,B,G), GS).
/* 68 ?- tuples(_X), groups(_X, GS).
GS = [(a, b, [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23]),
(c, d, [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22]),
(e, f, [100, 200, 300, 400, 500, 600, 700, 800,
900, 1000, 1100, 1200])].
*/
update: from David Tonhofer's answer, we see that it can be done with just one nested bagof call,
solution( Sol ) :-
tuples(TS),
bagof( (A,B,FLS), % (* collect (A,B,FLS) for each (A,B) *)
LS^( % (* such that (A,B,L) is in TS *)
bagof( L, member((A,B,L), TS), LS ),
flatten( LS, FLS)
), % (* with the lists LS flattened *)
Sol ).
The outer bagof's goal predicate (i.e. the inner bagof call) is automatically backtracking to produce its LS result for each distinct pair of the A,B values, automagically achieving the same effect as we had "manually", in this answer.
group([],L,L).
group([(A,B,L)|T],Acc,Out):-
( append(L1,[(A,B,LL)|L2], Acc)
-> append(LL,L,LLL),
append(L1,[(A,B,LLL)|L2], NewAcc),
group(T,NewAcc,Out)
; append(Acc,[(A,B,L)],NewAcc),
group(T,NewAcc,Out)
).
?- tuples(L), group(L,[],G).
G = [
(a,b,[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23]),
(c,d,[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22]),
(e,f,[100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200])
],
L = [
(a,b,[1, 3, 5, 7]),
(a,b,[9, 11, 13, 15]),
(a,b,[17, 19, 21, 23]),
(c,d,[0, 2, 4, 6]),
(c,d,[8, 10, 12, 14]),
(c,d,[16, 18, 20, 22]),
(e,f,[100, 200, 300, 400]),
(e,f,[500, 600, 700, 800]),
(e,f,[900, 1000, 1100, 1200])
].
Basically I'm stripping an element from the (tuple) list ([(A,B,L)|T]) and look if I got a similar entry within my bag-list (2nd argument, append(L1,[(A,B,LL)|L2], Acc)). I use append/3 to do so because it can be used to find an element and at the same time dividing the original list into the list before and after the found element.
If I found an entry in the baglist, I append the two number lists (L and LL) to a new one (LLL) and exchange the values within the bag-list. Then I call the predicate again with the rest list and the altered bag-list.
If no element fits, I just add the element to the bag-list (as last element, append(Acc,[(A,B,L)],Acc2)) and call the predicate again. So each time I call the predicate group/3 it's first argument loses an element. Until there is no element left (group([],L,L).), in this case I state my bag-list is my ouput.
After playing a bit around with group_by/4 and/or bagof/3 as suggested by Guy coder, and using a variant of Davids "extraction" predicate here is another solution:
one_tuple(V,W,Value) :-
tuples(Tuples),
member((V,W,Values), Tuples),
member(Value, Values).
?- findall((A,B,Cs), bagof(C, one_tuple(A, B, C), Cs), O).
O = [(a,b,[1,3,5,7,9,11,13,15,17,19,21,23]),
(c,d,[0,2,4,6,8,10,12,14,16,18,20,22]),
(e,f,[100,200,300,400,500,600,700,800,900,1000,1100,1200])].

Removing elements that have consecutive dupes in Elixir list

I have a list of numbers in Elixir, and I want to remove the duplicates, but only for the consecutive dupes.
For the following input list: [1,1,2,2,1,1,1,1,3,3,2,2].
The result should be: [1,2,1,3,2].
Enum.dedup/1 does exactly what you want: it replaces consecutive duplicate elements with only one instance of it and returns the remaining elements in a list.
iex(1)> Enum.dedup([1, 1, 2, 2, 1, 1, 1, 1, 3, 3, 2, 2])
[1, 2, 1, 3, 2]
This works on all values that compare equal with ===, including maps:
iex(2)> Enum.dedup([%{a: 1}, %{a: 2}, %{a: 2}, %{a: 2}])
[%{a: 1}, %{a: 2}]

Haskell: How to show all elements in impair and pair indexes from a list?

It will need to get the next imput and output:
pospair [1, 3, 9, 2, 5, 7, 1, 11]
[1, 9, 5, 1]
posimpair [1, 3, 9, 2, 5, 7, 1, 11]
[3, 2, 7, 11]
This is the way to obtain the element on the specified index:
show_in_index::Ord a=>[a]->Int->a
show_in_index l n = l!!n
It shows a result like this:
*Main> show_in_index [1,4,2,7,9] 3
7
The most simple way to do this is using recursion:
pospair :: [a] -> [a]
pospair xs = aux xs [] True
where
aux [] acc _ = acc
aux (y:ys) acc True = aux ys (acc ++ [y]) False
aux (y:ys) acc False = aux ys acc True
Note how I use True or False value to keep track of what value to eliminate. If it's False, I don't include the value in acc (accumulator). If it's True, I include the value. Using the same idea, you can implement posimpair.
You could map the function for indexing
For pospair the following works:
map ([1, 3, 9, 2, 5, 7, 1, 11] !! ) [0,2..length [1, 3, 9, 2, 5, 7, 1, 11]-1]
For posimpair we only have to change the second list that is the one that holds the indexing numbers, previously we had the series of pairs and now we want the series of impairs, so instead of having 0,2,.. until the length of the list -1 we have to do it with 1,3,..until the length of the list-1.
map ([1, 3, 9, 2, 5, 7, 1, 11] !! ) [1,3..length [1, 3, 9, 2, 5, 7, 1, 11]-1]
The general implementation is
pospairs = map (list !!) [0,2..length list - 1]
posimpairs = map (list !!) [1,3..length list - 1]
I tested your example and works.

List of List in scala

I would like to know how can I create a List of List in the result of a reduce operation.
I've for example this lines
1,2,3,4
0,7,8,9
1,5,6,7
0,6,5,7
And I would like to get something like this
1, [[2,3,4],[5,6,7]]
0, [[7,8,9],[6,5,7]]
Thsi is my code
val parsedData = data.map { line =>
val parts = line.split(",")
val label = Integer.parseInt(parts(0))
(label, List(Integer.parseInt(parts(1)), Integer.parseInt(parts(2)), Integer.parseInt(parts(3)))
}
With this I get
1, [2,3,4]
0, [7,8,9]
1, [5,6,7]
0, [6,5,7]
But if I use a reduceByKey operation with a List.concat(_,_) I get one single List with all items concated.
parsedData.reduceByKey(List.concat(_,_))
I want a List of List, reduced by the Key.
Is there some other operation that i don't know?
Thanks a lot for your help!
Here is a working example:
val data = "1,2,3,4\n0,7,8,9\n1,5,6,7\n0,6,5,7".split("\n")
val parsedData = data.map{ line =>
val parts = line.split(",")
val label = Integer.parseInt(parts(0))
(label, List(Integer.parseInt(parts(1)), Integer.parseInt(parts(2)), Integer.parseInt(parts(3))))
}.toList
//parsedData: List[(Int, List[Int])] = List((1,List(2, 3, 4)), (0,List(7, 8, 9)), (1,List(5, 6, 7)), (0,List(6, 5, 7)))
parsedData.groupBy(_._1).mapValues(_.map(_._2))
// Map(1 -> List(List(2, 3, 4), List(5, 6, 7)), 0 -> List(List(7, 8, 9), List(6, 5, 7)))
I am not sure this is concat you are looking for.
Can you try with that:
parsedData.reduceByKey(_ :: _ :: Nil)
Which should literally create a new list with your elements inside

Multiply list elements in Haskell

I want a function which takes the product of the inits of a list and duplicates its elements.
For example the list is: [2, 3, 4, 5].
The product of its inits : [1, 2, 6, 24, 120].
At the end the list should look like this: [1, 1, 2, 2, 2, 6, 6, 6, 6, 24, 24, 24, 24, 24].
My problem is that the [1, 2, 6, 24, 120] should not vary, but i can't solve it, I'm pretty new to haskell. You don't need to modify this code, you can make a new one.
makeSystem :: Integral a => [a] -> [a]
makeSystem l= replicate (l !! 0) ((map product(inits l))!!0) ++ asd (tail l) where
inits [] = [[]]
inits (x:xs) = [[]] ++ map (x:) (inits xs)
An other example: makeSystem [5,2,5,2,5,2]
The result: [1, 1, 1, 1, 1, 5, 5, 10, 10, 10, 10, 10, 50, 50, 100, 100, 100, 100, 100, 500, 500]
For the first part, you can use the standard function scanl:
> scanl (*) 1 [2, 3, 4, 5]
[1,2,6,24,120]
For the second part, zipWith with replicate gets us most of the way there:
> zipWith replicate [2, 3, 4, 5] [1, 2, 6, 24, 120]
[[1,1],[2,2,2],[6,6,6,6],[24,24,24,24,24]]
then we just need to concat these lists.
Putting it all together:
> let makeSystem xs = concat $ zipWith replicate xs (scanl (*) 1 xs)
> makeSystem [2, 3, 4, 5]
[1,1,2,2,2,6,6,6,6,24,24,24,24,24]
> makeSystem [5, 2, 5, 2, 5, 2]
[1,1,1,1,1,5,5,10,10,10,10,10,50,50,100,100,100,100,100,500,500]