Run Length Encoding Issue - Have [ ]s need ( )s - encoding

I'm trying to do a run length encoding problem and I think I more or less have it thanks to other questions on this topic on this site and others, however the format its returning in is not being accepted by my Uni's IDE. Could anyone help me?
This is my program
count([],[]).
count([X|T],[[X,C1]|R]) :- count(T,[[X,C]|R]), !, C1 is C+1.
count([X|T],[[X,1]|R]) :- count(T,R).
And this returns
?- count([1,1,1,2,2,2,3,1,1],R).
R = [[1, 3], [2, 3], [3, 1], [1, 2]].
However what the system wants it to return is
?- count([1,1,1,2,2,2,3,1,1],R).
R = [(1, 3), (2, 3), (3, 1), (1, 2)].
It wants tuples instead of lists, does anyone have any idea how to make this happen?

To construct a tuple, simply write (A, B) wherever you have written a term of the form [A, B].

Related

pyspark RDD combine consecutive values into tuple

Seems like a simple problem but I can't figure it out.
Given an rdd
Input
[1, 5, 3, 2, 7]
Output
[(1,5), (5,3), (3,2), (2,7)]
I've tried this but with obvious error.
rdd.map(lambda x,y: (x,y))
I'm assuming I need a helper function of some sort.

Julia convert EnumerableGroupBy to array

I have the following code that does a groupby on an array of numbers and returns an array of tuples with the numbers and respective counts:
using Query
a = [1, 2, 1, 2, 3, 4, 6, 1, 5, 5, 5, 5]
key_counts = a |> #groupby(_) |> #map g -> (key(g), length(values(g)))
collect(key_counts)
Is there a way to complete the last step in the pipeline to convert the key_counts of type QueryOperators.EnumerableMap{Tuple{Int64, Int64}, QueryOperators.EnumerableIterable{Grouping{Int64, Int64}, QueryOperators.EnumerableGroupBy{Grouping{Int64, Int64}, Int64, Int64, QueryOperators.EnumerableIterable{Int64, Vector{Int64}}, var"#12#17", var"#13#18"}}, var"#15#20"} to Vector{Tuple{Int, Int}} directly by integrating the collect operation to the pipeline as one liner?
The question has been clarified. My answer is no longer intended as a solution but provides additional information.
Using key_counts |> collect instead of collect(key_counts) works on the second line, but |> collect at the end of the pipe line does not, which feels like unwanted behavior.
Below response no longer relevant
When I run your code I actually do receive a Vector{Tuple{Int, Int}} as output.
I'm using Julia v1.6.0 with Query v1.0.0.
using Query
a = [1, 2, 1, 2, 3, 4, 6, 1, 5, 5, 5, 5]
key_counts = a |> #groupby(_) |> #map g -> (key(g), length(values(g)))
output = collect(key_counts)
typeof(output) # Vector{Tuple{Int64, Int64}} (alias for Array{Tuple{Int64, Int64}, 1})

Merge outputs into one list in Prolog

I have a prolog assignment that requires us to make a list of all the pairs of numbers in the range of two givens. I can get it to output (using one function) the following, but I don't know how to merge all the outputs. Here is the outbut of calling the function:
?- i(L,5,7).
L = [(5, 5), (5, 6), (5, 7)] ;
L = [(6, 5), (6, 6), (6, 7)] ;
L = [(7, 5), (7, 6), (7, 7)] ;
And here is the code (the interval methods are prof defined and not allowed to modified):
interval(X,L,H) :-
number(X),
number(L),
number(H),
!,
X>=L,
X=<H.
interval(X,X,H) :-
number(X),
number(H),
X=<H.
interval(X,L,H) :-
number(L),
number(H),
L<H,
L1 is L+1,
interval(X,L1,H).
i(L,X,Y):-
interval(N2,X,Y),
setof((N2,N),interval(N,X,Y),L).
I am looking for the output to be this instead:
L = [ (5, 5), (5, 6), (5, 7), (6, 5), (6, 6), (6, 7), (7, 5), (7, 6), (7, 7)]
The problem is that:
i(L,X,Y):-
interval(N2,X,Y),
setof((N2,N),interval(N,X,Y),L).
Will first set N2 to a number in the interval, and next you ask to generate a set for that given number N2 and a variable number N.
You can however simply define a composite in the goal of setof/3:
i(L,X,Y) :-
setof((N2,N),(interval(N2,X,Y),interval(N,X,Y)),L).
Nevertheless, perhaps a more elegant way to do it (an probably more Prolog) is to define an interval_tuple/3 predicate:
interval_tuple(X,Y,(N,N2)) :-
interval(N,X,Y),
interval(N2,X,Y).
and then call your setof/3 or listof/3 on that predicate:
i(L,X,Y) :-
listof(Tup,interval_tuple(X,Y,Tup),L).
You can do this concisely with CLP(FD):
:- use_module(library(clpfd)]).
interval(Left, Right, (X,Y)) :- % Definition of one interval
[X, Y] ins Left .. Right,
label([X, Y]).
intervals(Left, Right, IntervalList) :-
Left #=< Right,
label([Left, Right]),
findall(Interval, interval(Left, Right, Interval), IntervalList). % Find all intervals
I'm using the more descriptive name, intervals/3 rather than simply i/3. I've also reordered the arguments a bit.

Swift: How does zip() handle two different sized collections?

The zip() function takes two sequences and returns a sequence of tuples following:
output[i] = (sequence1[i], sequence2[i])
However, the sequences can potentially be of different dimensions. My question is how does the Swift language deal with this?
The docs were utterly useless.
Seems to me, there are two possibilities (in Swift):
Stop at end of the shortest
Stop at end of longest, filling with default constructor or a predefined value for shorter's element type
Swift uses the first option, the resulting sequence will have a length equal to the shorter of the two inputs.
For example:
let a: [Int] = [1, 2, 3]
let b: [Int] = [4, 5, 6, 7]
let c: [(Int, Int)] = zip(a, b) // [(1, 4), (2, 5), (3, 6)]
Apple's zip(_:_:) documentation has since been updated to answer this question:
https://developer.apple.com/documentation/swift/1541125-zip
If the two sequences passed to zip(_:_:) are different lengths, the resulting sequence is the same length as the shorter sequence. In this example, the resulting array is the same length as words:
let words = ["one", "two", "three", "four"]
let naturalNumbers = 1...Int.max
let zipped = Array(zip(words, naturalNumbers))
// zipped == [("one", 1), ("two", 2), ("three", 3), ("four", 4)]

Is there any analog for Scala 'zip' function in Groovy?

In Scala I can write something like this:
val a = List(1, 2, 3)
val b = List(4, 5)
println(a zip b)
That would produce List((1,4), (2,5)) as output.
Now I have two collections in Groovy and want to zip them in similar fashion. What is the simplest way to do this?
Groovy's equivalent of Scala's zip is List#transpose, which can be called on a list of lists:
assert [[1, 2, 3], [4, 5]].transpose() == [[1, 4], [2, 5]]