Observable.switchMap does not interrupt Observable - rx-java2

I don't understand why switchMap doesn't work as expected.
At first case switchMap doesn't interrupt Observable but at second case it works as expected. Please, explain that behaviour.
val test = TestObserver<Int>()
Observable.fromArray(10, 20)
.switchMap({
// 1. switchMap does not interrupt generator
Observable.generate(
Callable{
generateSequence(it, { n -> if (n < it + 9) n + 1 else null}).iterator()
},
BiConsumer<Iterator<Int>, Emitter<Int>>(){ data, emitter ->
if (data.hasNext()) {
Thread.sleep(100)
emitter.onNext(data.next())
}else {
emitter.onComplete()
}
}
)
// 2. switchMap disposes observable
//Observable.just(it).delay(1, TimeUnit.SECONDS)
},2)
.subscribeWith(test)
test.await()
// 1. >> [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
// But I expect [20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
// 2. >> [20]
print(test.values())
What I want to do.
In reality my generator do hard work based on value from switchMap and I want interrupt this work if new value is appeared. At the beginning generator start transaction and at the end generator commits or cancels transaction.

Related

Flutter/Dart program to sort number in a list in descending order

I am designing flutter app application that find the position of pupils through their total scores, I have used sort method but is not giving me a valid result.
'''List totals = [90, 50, 10, 5, 30, 9, 45];''' my result if I use I sort method 10, 30, 45, 50, 5, 90, 9.
this is what I want to be my result / 5, 9, 10, 30, 45, 50, 90
please help me out
I think that your List is of type String, that's why the order is different than comparing int. To fix this you parse the String to an int by using the parse method.
List<String> totals = ["90", "50", "10", "5", "30", "9", "45"];
totals.sort((a, b) => int.parse(a).compareTo(int.parse(b)));
print(totals); // [5, 9, 10, 30, 45, 50, 90]
try totals.sort((a, b) => a.compareTo(b));

How do I get this simple pytest fixture to work correctly? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 months ago.
Improve this question
I'm attempting to get a simple pytest fixture to work.
#pytest.fixture
def two_cities_wind():
return {'bristol': [7, 7, 8, 8, 8, 8, 8, 8, 10, 10, 10, 10, 9, 9, 8, 8, 8, 8, 8, 8, 9, 8, 8, 8],
'bath': [14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 16, 16, 16, 16, 16, 15, 16, 16, 16, 16, 15, 15, 15]}
My test is
def test_city_with_high_wind_of_week():
windy_city = weather.city_with_high_wind_of_week(two_cities_wind)
assert windy_city == "bath"
I am expecting the fixture to provide the dict that should be iterable returned, but the test is throwing an exception.
windy_city = ""
wind_speed = 0
> for city in cities_weather:
E TypeError: 'function' object is not iterable
src/weather.py:52: TypeError
========================================================== short test summary info ===========================================================
FAILED tests/test_zweather.py::test_city_with_high_wind_of_week - TypeError: 'function' object is not iterable
======================================================== 1 failed, 10 passed in 0.49s ========================================================
The function under test is
def city_with_high_wind_of_week(cities_weather) -> str:
windy_city = ""
wind_speed = 0
for city in cities_weather:
highest_speed = cities_weather[city].sort()[-1]
if highest_speed > wind_speed:
windy_city = city
wind_speed = highest_speed
return windy_city
The way I am attempting this looks comparable with these examples
https://docs.pytest.org/en/latest/explanation/fixtures.html
https://levelup.gitconnected.com/how-to-make-your-pytest-tests-as-dry-as-bone-e9c1c35ddcb4
Answer supplied by #MrBeanBremen
I needed to pass the fixture to the test function.
The test should be called as follows:
def test_city_with_high_wind_of_week(two_cities_wind):
windy_city = weather.city_with_high_wind_of_week(two_cities_wind)
assert windy_city == "bath"

Using forall in minizinc with an array set (not contiguous)

I'm trying to use a forall instance to add a constraint but I got this error and I'm not sure which should I do.
(array slice must be contiguous)
in call 'forall'
in array comprehension expression
with i = {10,24}
in binary '<=' operator expression
in call 'slice_1d'
I am working in a schedulling problem, and I need to apply a restriction which determines that a set of tasks (determined by suc) can only starts after a specific task (determined by 1..nTasks) is already finished.
The model follows
include "globals.mzn";
int: n_res;
array [1..n_res] of int: res_cap;
int: n_tasks;
array [1..n_tasks] of int: duration;
array [1..n_res, 1..n_tasks] of int: res_req;
array [1..n_tasks] of set of int: suc;
int: t_max = sum(i in 1..n_tasks)(duration[i]);
array [1..n_tasks] of var 0..t_max: start;
array [1..n_tasks] of var 0..t_max: end;
var 0..t_max: makespan;
% constraint that I can't implement. this constraint should make every task[i] to start after a set of tasks{i} are finished. The set is defined by the array suc.
constraint forall (i in suc)(end[i] <= start[i]);
constraint cumulative(start, duration, row(res_req, 1), res_cap[1]);
constraint cumulative(start, duration, row(res_req, 2), res_cap[2]);
constraint cumulative(start, duration, row(res_req, 3), res_cap[3]);
constraint cumulative(start, duration, row(res_req, 4), res_cap[4]);
constraint forall(i in 1..n_tasks)(end[i] = start[i]+duration[i]);
constraint makespan = max(i in 1..n_tasks)(end[i]);
solve minimize makespan;
The arrays suc and 1..nTasks have the same number of lines.
I have a 1d array with the specific sets of tasks that can start after the task[i] is over.
In a smaller instance, for example:
suc = [{5, 15}, {17, 23, 28}, {10, 12}, {8}]
What i need to implement is:
end[i] | i in 1..nTasks <= start[i] | i in suc
For the specific set that I posted, it could be done manually like this:
end[1] <= start[5]
end[1] <= start[15]
end[2] <= start[17]
end[2] <= start[23]
end[2] <= start[28]
end[3] <= start[10]
end[3] <= start[12]
end[4] <= start[8]
I just start using minizinc and something tells me that I'm missing something that may be simple, however, it's been a while and I can't implement it.
How can I write a forall instance that can do this?
The culprit is this constraint (as the error indicates):
constraint forall (i in suc)(end[i] <= start[i]);
where you trying use suc as a generator for the loop. The problem is that you want two things for this constraint: the start of the current task should be before the successor of that task. And this is not possible using your approach since i will have value such as { 10, 24 }, but there is no value (reference) of the current task (i.e. the value of start[i]).
Here is a way of solving this: Use i in n_res to loop through all the tasks (i is the i'th task), and then loop through suc[i] to get the successors of each task.
constraint forall (i in 1..n_res) (
forall(s in suc[i]) (
end[i] <= start[s]
)
);
Another way, which is perhaps simpler, is to combine the two forall loops into one loop:
constraint forall (i in 1..n_res, s in suc[i]) (
end[i] <= start[s]
);
When I ran the model it generated this solution:
% obj = 51
start = array1d(1..30, [7, 21, 4, 31, 6, 41, 34, 3, 35, 39, 21, 28, 47, 0, 38, 48, 44, 35, 28, 7, 10, 34, 11, 47, 41, 3, 11, 3, 22, 17]);
end = array1d(1..30, [17, 25, 5, 34, 11, 51, 35, 7, 41, 47, 28, 35, 51, 3, 48, 51, 48, 38, 35, 12, 11, 44, 19, 48, 47, 7, 18, 9, 31, 21]);
makespan = 51;
----------
% obj = 51
==========

Sort numbers inside string

I have a string that consists of few numbers. First number is the number of the row, remaining are numbers in this row.(Array but string, kind of). The problem is that remaining numbers are unsorted, and I want to find the clearest way of sorting them without creating new List and sorting everything there.
String unsorted = '9, 12, 14, 11, 2, 10';
print(unsorted.sort()); // '9, 2, 10, 11, 12, 14'
You cant really avoid converting to a list of numbers if you want to sort it.
void main() {
print(sortNumString('9, 12, 14, 11, 2, 10')); // 2, 9, 10, 11, 12, 14
}
String sortNumString(String numString, [String separator = ', ']) =>
(numString.split(separator).map(int.parse).toList()..sort())
.join(separator);
The .. means to return the previous thing, the list, since sort returns void.
I'm not exactly experienced when it comes to dart programming language but this is what i came up with.
void main() {
var unsorted = "9, 12, 14, 11, 2, 10";
var nums_int = unsorted.split(", ").map(int.parse).toList();
nums_int.sort();
for (var n in nums_int) {
stdout.write(n.toString() + ", ");
}
}
That should give the expected output: "2, 9, 10, 11, 12, 14"
Hope this helps.

Swap first and last columns in matrix

For example, we have a matrix.
1, 2, 3, 4, 5
6, 7, 8, 9, 10
11, 12, 13, 14, 15
16, 17, 18, 19, 20
21, 22, 23, 24, 25
Perhaps the simplest way to resolve the problem sounded in the title of the topic in Perl6 looks like
my #matrix = [1..5], [6..10], [11..15], [16..20], [21..25];
#matrix.map:{.[0,*-1] = .[*-1,0]};
Result
5, 2, 3, 4, 1
10, 7, 8, 9, 6
15, 12, 13, 14, 11
20, 17, 18, 19, 16
25, 22, 23, 24, 21
How to do the same is also beautiful in PowerShell?
Your code snippet translated to PowerShell would look like this:
$matrix = (1..5), (6..10), (11..15), (16..20), (21..25)
$matrix | ForEach-Object { $_[0], $_[-1] = $_[-1], $_[0] }