Splitting Observable into windows - system.reactive

I am looking for Observable.Window like operator with opening window selector
As example we can take a sequence of natural numbers.
I am wondering how to split this sequence into windows so every new window starts if number is greater than 4 or window size has reached 3
Input sequence is IObservable<int>
Output sequence is IObservable<IObservable<int>>
Sequence 1 2 5 3 1 1 2 3 1 5 0
will produce windows 1 2; 5 3 1; 1 2 3; 1; 5 0

Using C# this works:
var observable =
source
.Concat(Observable.Return(-1))
.Publish(sp =>
sp.Zip(sp.Skip(1), (x0, x1) => new { x0, x1 })
.Publish(zsp =>
zsp
.Window(zsp.Where(x => x.x1 >= 4))
.Select(xs => xs.Select(x => x.x0).Window(3))
.Merge()));
I get this result:

As per my comment on the question I am unsure whether the sequence 1, 2, 3, 5 should produce the windows 1 2 3 | 5 or 1 2 3 | | 5 (see the empty window which is what Enigmativity's answer produces). My answer does not produce the empty window:
public static IObservable<IObservable<T>> Window<T>(this IObservable<T> source, Func<T, bool> predicate, int maximumWindowSize)
{
return Observable.Create<IObservable<T>>(obs =>
{
var currentWindow = new Subject<T>();
obs.OnNext(currentWindow);
var count = 0;
return source.Subscribe(x =>
{
if (count == maximumWindowSize || predicate(x))
{
count = 0;
currentWindow.OnCompleted();
currentWindow = new Subject<T>();
obs.OnNext(currentWindow);
}
currentWindow.OnNext(x);
count++;
}, obs.OnError, () =>
{
obs.OnCompleted();
currentWindow.OnCompleted();
});
});
}
This can be used like so:
var windows = source.Window(x => x > 4, 3);

Related

Can't assign a big number to a variable out of the while loop in scala

I want to write a program that can find the N-th number,which only contains factor 2 , 3 or 5.
def method3(n:Int):Int = {
var q2 = mutable.Queue[Int](2)
var q3 = mutable.Queue[Int](3)
var q5 = mutable.Queue[Int](5)
var count = 1
var x:Int = 0
while(count != n){
val minVal = Seq(q2,q3,q5).map(_.head).min
if(minVal == q2.head){
x = q2.dequeue()
q2.enqueue(2*x)
q3.enqueue(3*x)
q5.enqueue(5*x)
}else if(minVal == q3.head){
x = q3.dequeue()
q3.enqueue(3*x)
q5.enqueue(5*x)
}else{
x = q5.dequeue()
q5.enqueue(5*x)
}
count+=1
}
return x
}
println(method3(1000))
println(method3(10000))
println(method3(100000))
The results
51200000
0
0
When the input number gets larger , I get 0 from the function.
But if I change the function to
def method3(n:Int):Int = {
...
q5.enqueue(5*x)
}
if(x > 1000000000) println(('-',x)) //note here!!!
count+=1
}
return x
}
The results
51200000
(-,1006632960)
(-,1007769600)
(-,1012500000)
(-,1019215872)
(-,1020366720)
(-,1024000000)
(-,1025156250)
(-,1033121304)
(-,1036800000)
(-,1048576000)
(-,1049760000)
(-,1054687500)
(-,1061683200)
(-,1062882000)
(-,1073741824)
0
.....
So I don't know why the result equals to 0 when the input number grows larger.
An Int is only 32 bits (4 bytes). You're hitting the limits of what an Int can hold.
Take that last number you encounter: 1073741824. Multiply that by 2 and the result is negative (-2147483648). Multiply it by 4 and the result is zero.
BTW, if you're working with numbers "which only contains factor 2, 3 or 5", in other words the numbers 2, 3, 4, 5, 6, 8, 9, 10, 12, 14, 15, ... etc., then the 1,000th number in that sequence shouldn't be that big. By my calculations the result should only be 1365.

DP Coin Change Algorithm - Retrieve coin combinations from table

To find how many ways we have of making change for the amount 4 given the coins [1,2,3], we can create a DP algorithm that produces the following table:
table[amount][coins.count]
0 1 2 3 4
-----------
(0) 1 | 1 1 1 1 1
(1) 2 | 1 1 2 2 3
(2) 3 | 1 1 2 3 4
The last position being our answer. The answer is 4 because we have the following combinations: [1,1,1,1],[2,1],[2,2],[3,1].
My question is, is it possible to retrieve these combinations from the table I just generated? How?
For completeness, here's my algorithm
func coinChange(coins: [Int], amount: Int) -> Int {
// int[amount+1][coins]
var table = Array<Array<Int>>(repeating: Array<Int>(repeating: 0, count: coins.count), count: amount + 1)
for i in 0..<coins.count {
table[0][i] = 1
}
for i in 1...amount {
for j in 0..<coins.count {
//solutions that include coins[j]
let x = i - coins[j] >= 0 ? table[i - coins[j]][j] : 0
//solutions that don't include coins[j]
let y = j >= 1 ? table[i][j-1] : 0
table[i][j] = x + y
}
}
return table[amount][coins.count - 1];
}
Thanks!
--
Solution
Here's an ugly function that retrieves the combinations, based on #Sayakiss 's explanation:
func getSolution(_ i: Int, _ j: Int) -> [[Int]] {
if j < 0 || i < 0 {
//not a solution
return []
}
if i == 0 && j == 0 {
//valid solution. return an empty array where the coins will be appended
return [[]]
}
return getSolution(i - coins[j], j).map{var a = $0; a.append(coins[j]);return a} + getSolution(i, j - 1)
}
getSolution(amount, coins.count-1)
Output:
[[1, 3], [2, 2], [1, 1, 2], [1, 1, 1, 1]]
Sure you can. We define a new function get_solution(i,j) which means all solution for your table[i][j].
You can think it returns an array of array, for example, the output of get_solution(4,3) is [[1,1,1,1],[2,1],[2,2],[3,1]]. Then:
Case 1. Any solution from get_solution(i - coins[j], j) plus coins[j] is a solution for table[i][j].
Case 2. Any solution from get_solution(i, j - 1) is a solution for table[i][j].
You can prove Case 1 + Case 2 is all possible solution for table[i][j](note you get table[i][j] by this way).
The only problem remains is to implement get_solution(i,j) and I think it's good for you to do it by yourself.
If you still got any question, please don't hesitate to leave a comment here.

Sum of Printed For Loop in Swift

For a project, I'm trying to find the sum of the multiples of both 3 and 5 under 10,000 using Swift. Insert NoobJokes.
Printing the multiples of both 3 and 5 was fairly easy using a ForLoop, but I'm wondering how I can..."sum" all of the items that I printed.
for i in 0...10000 {
if i % 3 == 0 || i % 5 == 0 {
print(i)
}
}
(468 individual numbers printed; how can they be summed?)
Just a little walk through about the process. First you will need a variable which can hold the value of your sum, whenever loop will get execute. You can define an optional variable of type Int or initialize it with a default value same as I have done in the first line. Every time the loop will execute, i which is either multiple of 3 or 5 will be added to the totalSum and after last iteration you ll get your result.
var totalSum = 0
for i in 0...10000 {
if i % 3 == 0 || i % 5 == 0
{
print(i)
totalSum = totalSum + i
}
}
print (totalSum)
In Swift you can do it without a repeat loop:
let numberOfDivisiblesBy3And5 = (0...10000).filter{ $0 % 3 == 0 || $0 % 5 == 0 }.count
Or to get the sum of the items:
let sumOfDivisiblesBy3And5 = (0...10000).filter{ $0 % 3 == 0 || $0 % 5 == 0 }.reduce(0, {$0 + $1})
range : to specify the range of numbers for operation to act on.
here we are using filter method to filter out numbers that are multiple of 3 and 5 and then sum the filtered values.
(reduce(0,+) does the job)
let sum = (3...n).filter({($0 % 3) * ($0 % 5) == 0}).reduce(0,+)
You just need to sum the resulting i like below
var sum = 0
for i in 0...10000 {
if i % 3 == 0 || i % 5 == 0 {
sum = sum + i
print(i)
}
}
Now sum contains the Sum of the values
Try this:
var sum = 0
for i in 0...10000 {
if i % 3 == 0 || i % 5 == 0 {
sum = sum + i
print(i)
}
}
print(sum)
In the Bottom line, this should to be working.
var sum = 0
for i in 0...10000 {
if i % 3 == 0 || i % 5 == 0 {
sum += i
print(i)
}
}
print(sum)

Check a multiple in Swift?

I am trying to find the odd numbers and a multiple of 7 between a 1 to 100 and append them into an array. I have got this far:
var results: [Int] = []
for n in 1...100 {
if n / 2 != 0 && 7 / 100 == 0 {
results.append(n)
}
}
Your conditions are incorrect. You want to use "modular arithmetic"
Odd numbers are not divisible by 2. To check this use:
if n % 2 != 0
The % is the mod function and it returns the remainder of the division (e.g. 5 / 2 is 2.5 but integers don't have decimals, so the integer result is 2 with a remainder of 1 and 5 / 2 => 2 and 5 % 2 => 1)
To check if it's divisible by 7, use the same principle:
if n % 7 == 0
The remainder is 0 if the dividend is divisible by the divisor. The complete if condition is:
if n % 2 != 0 && n % 7 == 0
You can also use n % 2 == 1 because the remainder is always 1. The result of any mod function, a % b, is always between 0 and b - 1.
Or, using the new function isMultiple(of:, that final condition would be:
if !n.isMultiple(of: 2) && n.isMultiple(of: 7)
Swift 5:
Since Swift 5 has been released, you could use isMultiple(of:) method.
In your case, you should check if it is not multiple of ... :
if !n.isMultiple(of: 2)
Swift 5 is coming with isMultiple(of:) method for integers , so you can try
let res = Array(1...100).filter { !$0.isMultiple(of:2) && $0.isMultiple(of:7) }
Here is an efficient and concise way of getting the odd multiples of 7 less than or equal to 100 :
let results: [Int] = Array(stride(from: 7, through: 100, by: 14))
You can also use the built-in filter to do an operation on only qualified members of an array. Here is how that'd go in your case for example
var result = Array(1...100).filter { (number) -> Bool in
return (number % 2 != 0 && number % 7 == 0)
}
print(result) // will print [7, 21, 35, 49, 63, 77, 91]
You can read more about filter in the doc but here is the basics: it goes through each element and collects elements that return true on the condition. So it filters the array and returns what you want

Specman On the Fly Generation: How to constrain a list whose values are differ from each other at least 2

I need to generate a random list values with the next constrain:
my_list[i] not in [my_list[i-1] .. my_list[i-1] + 1]
i.e. all values in the list are different and with at least difference of 2 between each other. All code variations I've tried failed, e.g.:
var prev_val : uint = 0;
gen my_list keeping {
it.size() == LIST_SIZE;
it.all_different(it);
for each (val) in it {
val not in [prev_val .. prev_val + 1];
prev_val = val;
};
};
How such list can be generated? Thank you for your help
I am not sure I fully understand the request but following your code:
gen my_list keeping {
it.size() == LIST_SIZE;
it.all_different(it);
keep for each (val) in it {
val != prev;
val != prev + 1;
};
};
This will generate a list (all items will be generate together) according to your rule:
my_list[i] not in [my_list[i-1] .. my_list[i-1] + 1]
But the following list is a valid solution: 0,2,1,3,5,4,6,8,7,9,11,10,12,...
which doesn't follow "the all values in the list are different and with at least difference of 2 between each other".
To generate a list according to the "text request", you must use double keep for each and abs:
gen my_list keeping {
it.size() == LIST_SIZE;
for each (val1) using index (i1) in it {
for each (val2) using index (i2) in it {
i1 < i2 => abs(val1-val2) >= 2;
};
};
If you want my_list to be sorted (and will be solved faster) :
gen my_list keeping {
it.size() == LIST_SIZE;
it.size() == LIST_SIZE;
it.all_different(it);
for each (val) in it {
val >= prev + 2;
};
};
You could try the following:
gen my_list keeping {
it.size() == 10;
it.all_different(it);
for each (val) in it {
index > 0 =>
val not in [value(it[index - 1]) .. value(it[index - 1]) + 1];
};
};
The solver requires the it[index - 1] expression in the constraint be "fixed" at the point of generation, hence the use of value(...). This means that the list will be generated element by element.
If that's a problem, you could change to:
index > 0 =>
val != it[index - 1] + 1;
This should be equivalent, since the all_different(...) constraint should make sure that an element doesn't have the same value as the previous one. Naturally, this won't work if you have a wider set.