List integerList=[1,2,4,11,14,15,16,16,19,30,31,50,51,100,101,105]; //input
var subList=integerList.splitBetween((v1, v2) => (v2 - v1).abs() > 6);
print(subList); //([1, 2, 4], [11, 14, 15, 16, 16, 19], [30, 31], [50, 51], [100, 101, 105])
what is the logic splitBetween methods works here ?
check each pair of adjacent elements v1 and v2
lets use your data:
[1,2,4,11,14,15,16,16,19,30,31,50,51,100,101,105]
begin with index 0 and 1
we have : v1 = 1 , v2 = 2
then test with the function (v2 - v1).abs() > 6)
( 1-2).abs()>6 = false
index 1 and 2 : v1=2 , v2=4
(2 -4).abs() > 6 = false
index 2 and 3 : v1=4 , v2=11
(4 - 11).abs() > 6 absolute(-7) > 6 = true,
since its true : the elements since the previous chunk-splitting elements are emitted as a list
which means, index 1 - 3 emmited as a list.
current sublist = ([1,2,4])
and so on
index : 4 - 8 is false. and pair of index 8 and 9 is true
current sublist = ([1,2,4], [11,14,15,16,16,19])
repeat untin last index.
lastly :if at last index are false then we keep add to the list. because it says that : Any final elements are emitted at the end.
final result : ([1, 2, 4], [11, 14, 15, 16, 16, 19], [30, 31], [50, 51], [100, 101, 105])
Related
It seems like a combination of collect(every:on:skipEmpty:discardWhenCompleted:) and collect(count:) in ReactiveSwift.
The resulting signal would send an event every n seconds if the count of accumulated values doesn't reach max count during each time interval. But if in a specific time interval, the count of values has reached max count, it will send immediately.
For example, timeInterval = 2s, maxCount = 2
interval 1: received two values [1, 2], forward them at end of interval 1
interval 2: received one value [3], forward them at end of interval 2
interval 3: received three values [5, 6, 7] ( 3 values > maxCount), forward [5, 6] immediately when 7 is received and 7 is regarded as received value in interval 4 (interval 3 stopped early)
Question is hard to grasp, but I will try.
If you want to batch emitted values by count and time, you can use bufferTimeout method. See documentation here https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Flux.html#bufferTimeout-int-java.time.Duration-
Some example:
void bufferTimeoutTry() throws InterruptedException {
Flux.interval(Duration.ofMillis(157))
.filter(time -> time > 20 && time < 38 || time % 5 == 0 || time % 17 == 0)
.bufferTimeout(5, Duration.ofSeconds(1))
.doOnNext(list -> {
// we will get list of items buffered in 1-second period of time, or at most 5 items.
})
.subscribe(System.out::println);
Thread.sleep(30000);
}
Output will be list of items. Flux.interval is generating sequential long number (i.e. 1, 2, 3, 4, 5, ...), it is filtered on second line of method (to get some non interval behavior) and than buffer-ed. After buffer, there is no long on stream, but it has changed to list of longs.
[0, 5]
[10, 15]
[17, 20, 21, 22, 23]
[24, 25, 26, 27, 28]
[29, 30, 31, 32, 33]
[34, 35, 36, 37, 40]
[45, 50, 51]
[55, 60]
[65, 68, 70]
[75, 80]
[85, 90]
[95, 100]
[102, 105]
[110, 115]
[119, 120, 125]
[130, 135, 136]
[140, 145]
[150, 153, 155]
[160, 165]
[170, 175]
[180, 185]
Is this what you want?
I have a single trained classifier tested on 2 related multiclass classification tasks. As each trial of the classification tasks are related, the 2 sets of predictions constitute paired data. I would like to run a paired permutation test to find out if the difference in classification accuracy between the 2 prediction sets is significant.
So my data consists of 2 lists of predicted classes, where each prediction is related to the prediction in the other test set at the same index.
Example:
actual_classes = [1, 3, 6, 1, 22, 1, 11, 12, 9, 2]
predictions1 = [1, 3, 6, 1, 22, 1, 11, 12, 9 10] # 90% acc.
predictions2 = [1, 3, 7, 10, 22, 1, 7, 12, 2, 10] # 50% acc.
H0: There is no significant difference in classification accuracy.
How do I go about running a paired permutation test to test significance of the difference in classification accuracy?
I have been thinking about this and I'm going to post a proposed solution and see if someone approves or explains why I'm wrong.
actual_classes = [1, 3, 6, 1, 22, 1, 11, 12, 9, 2]
predictions1 = [1, 3, 6, 1, 22, 1, 11, 12, 9 10] # 90% acc.
predictions2 = [1, 3, 7, 10, 22, 1, 7, 12, 2, 10] # 50% acc.
paired_predictions = [[1,1], [3,3], [6,7], [1,10], [22,22], [1,1], [11,7], [12,12], [9,2], [10,10]]
actual_test_statistic = predictions1 - predictions2 # 90%-50%=40 # 0.9-0.5=0.4
all_simulations = [] # empty list
for number_of_iterations:
shuffle(paired_predictions) # only shuffle between pairs, not within
simulated_predictions1 = paired_predictions[first prediction of each pair]
simulated_predictions2 = paired_predictions[second prediction of each pair]
simulated_accuracy1 = proportion of times simulated_predictions1 equals actual_classes
simulated_accuracy2 = proportion of times simulated_predictions2 equals actual_classes
all_simulations.append(simulated_accuracy1 - simulated_accuracy2) # Put the simulated difference in the list
p = count(absolute(all_simulations) > absolute(actual_test_statistic ))/number_of_iterations
If you have any thoughts, let me know in the comments. Or better still, provide your own corrected version in your own answer. Thank you!
I have a master set generated by 5 random numbers from 1 to 52; I would like to compare this master set with 13 other sets, each containing 4 numbers between 1 and 52.
Is there a way to check if there are any 2 sets, each containing 2 numbers from the master set?
import UIKit
var firstCard = 0
var secondCard = 0
var thirdCard = 0
var fourthCard = 0
var fifthCard = 0
func generateRandomNumber(_ from:Int, _ to:Int, _ qut:Int?) -> [Int]
{
var myRandomNumbers = [Int]()
var numberOfNumbers = qut
let lower = UInt32(from)
let higher = UInt32(to+1)
if numberOfNumbers == nil || numberOfNumbers! > (to-from) + 1
{
numberOfNumbers = (to-from) + 1
}
while myRandomNumbers.count != numberOfNumbers
{
let myNumber = arc4random_uniform(higher - lower) + lower
if !myRandomNumbers.contains(Int(myNumber))
{
myRandomNumbers.append(Int(myNumber))
}
}
return myRandomNumbers
}
let myArray = generateRandomNumber(1, 53, 5)
firstCard = myArray[0]
secondCard = myArray[1]
thirdCard = myArray[2]
fourthCard = myArray[3]
fifthCard = myArray[4]
let mainSetA = Set([firstCard, secondCard, thirdCard, fourthCard, fifthCard])
let setB: Set = [1, 2, 3, 4]
let setC: Set = [5, 6, 7, 8]
let setD: Set = [9, 10, 11, 12]
let setE: Set = [13, 14, 15, 16]
let setF: Set = [17, 18, 19, 20]
let setG: Set = [21, 22, 23, 24]
let setH: Set = [25, 26, 27, 28]
let setI: Set = [29, 30, 31, 32]
let setJ: Set = [33, 34, 35, 36]
let setK: Set = [37, 38, 39, 40]
let setL: Set = [41, 42, 43, 44]
let setM: Set = [45, 46, 47, 48]
let setN: Set = [49, 50, 51, 52]
no clue what to do next...
Something like this might help:
let mainSet = Set([1, 2, 3, 4])
Here I have three sets out of four that contain at least two items in the main set:
let inputs: [Set<Int>] = [
Set([9, 8, 7, 1]),
Set([9, 8, 1, 2]),
Set([9, 1, 2, 3]),
Set([9, 0, 3, 4])
]
Filter the input sets array, to find any where the intersection between that set and the main set is at least 2:
let matchingSets = inputs.filter {
$0.intersection(mainSet).count >= 2
}
i want to know the pattern for the above series in order to write the code for above series.
I am thinking that the above series is mix of two different series 1,2,4,6,...and 1,2,2,..
Please help me with this sequence and also tell whether i am thinking in correct way or not.
logic :--
series 1-> Prime-1 i.e. [1, 2, 4, 6, 10, 12, 16, 18, 22, 28, 30, 36.....]
series 2-> Number Series i.e. [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5.....]
final output -> Alternate Series i.e. [1 1 2 2 4 2 6 3 10 3 12 3 16 4 18 4 22 4 28 4....]
Note : There might be another logic but by the given Question, this series can be identified by below program..
Please do not use for any competition Test/Exam
import math
global li_prime;global li_series;xp=0
def prime(size):
global li_prime;count = 2;
while len(li_prime)
isprime = True
for x in range(2, int(math.sqrt(count) + 1)):
if count % x == 0:
isprime = False
break
if isprime:
li_prime.append(count-1)
count += 1
def series(size):
global li_series
for i in range(size+1):
for j in range(i):
li_series.append(i)
if len(li_series)>size:
break
def main():
global xp
global li_prime
global li_series
testcase=int(input(''))
for I in range(testcase):
li_series=[]
li_prime=[]
size=int(input(''))
prime(size)
series(size)
li_prime=li_prime[:size]
li_series=li_series[:size]
lc=[]
for i in range(size//2+1):
lc.append(str(li_prime[i]))
lc.append(str(li_series[i]))
lc=lc[:size]
main()
It is series whose greatest common divisior (gcd) is 1 also known as Euler's Totient Function.
series format = {1 1 2 2 4 2 6 32 ..... 168 80 216 120 164 100}
Code:
public static void main(String[] args) {
//n is the input for the size of the series
for(int j=1;j<=n;j++){
System.out.print(calSeriesVal(j)+" ");
}
}
private static int calDivisor(int a, int b)
{
if (a == 0)
return b;
return calDivisor(b % a, a);
}
private static int calSeriesVal(int n)
{
int val = 1;
for (int i = 2; i < n; i++)
if (calDivisor(i, n) == 1)
val++;
return val;
}
I have 2 sets of Date, their 1st and last dates are the same respectively but their dates within might not be the same to each other. Both DateA and DateB contain different values on their each date, which are arrays A and B.
DateA= '2016-01-01'
'2016-01-02'
'2016-01-04'
'2016-01-05'
'2016-01-06'
'2016-01-07'
'2016-01-08'
'2016-01-09'
'2016-01-10'
'2016-01-12'
'2016-01-13'
'2016-01-14'
'2016-01-16'
'2016-01-17'
'2016-01-18'
'2016-01-19'
'2016-01-20'
DateB= '2016-01-01'
'2016-01-02'
'2016-01-03'
'2016-01-04'
'2016-01-05'
'2016-01-09'
'2016-01-10'
'2016-01-11'
'2016-01-12'
'2016-01-13'
'2016-01-15'
'2016-01-16'
'2016-01-17'
'2016-01-19'
'2016-01-20'
A = [5, 2, 3, 4, 6, 1, 7, 9, 3, 6, 1, 7, 9, 2, 1, 4, 6]
B = [4, 2, 7, 1, 8, 4, 9, 5, 3, 9, 3, 6, 7, 2, 9]
I have converted the dates into datenumber,ie
datenumberA= 736330
736331
736333
736334
736335
736336
736337
736338
736339
736341
736342
736343
736345
736346
736347
datenumberB= 736330
736331
736332
736333
736334
736338
736339
736340
736341
736342
736344
736345
736346
736348
736349
Now I want to compare the value of A on DateA(n) to that of B on DateB while DateB is the date that is closest to and before the date of DateA(n).
For example,
comparing the value of A on DateA '2016-01-12' to that of B on DateB '2016-01-11'.
Please help and thanks a lot.
It'll get you the desired output!
all_k=0;
out(1)=1; % not comparing the first index as you mentioned
for n=2:size(datenumberA,1)
j=0;
while 1
k=find(datenumberB+j==datenumberA(n)-1); %finding the index of DateB closest to and before DateA(n)
if size(k,1)==1 break; end %if found, come out of the while loop
j=j+1; % otherwise keep adding 1 in the values of datenumberB until found
end
if size(find(all_k==k),2) ~=1 % to avoid if any DateB is already compared
out(end+1)=A(n)> B(k); %Comparing Value in A with corresponding value in B
all_k(end+1)=k; end %Storing which indices of DateB are already compared
end
out' %Output
Output:-
ans =
1
0
0
1
0
0
1
0
0
1
0
0
1