Given two sets, how can I determine what was added or removed? - swift

let first = { 2, 4, 5, 9 };
let second = { 2, 8, 15, 53, 4 }
//removed = 5, 9
//added = 8, 15, 53
What is the simplest solution to determine what was added or removed?

Just use set differences.
To determine what was added, take the set subtraction
second.subtracting(first)
To determine what was removed, take
first.subtracting(second)

A very simple algorithm for determining the changes is similar to the merge algorithm for sorted lists:
Order both sets
Start iterating both ordered lists at the initial element
In a loop, if the current item from both lists is the same, skip it by moving to the next element of both lists
If the item in the original set is smaller, add it to the list of removed items, and move the original set to the next item
If the item in the new set is smaller, add it to the list of added items, and move the new set to the next item
If you reach the end of one of two lists before the other one, add the remaining elements to the corresponding output list (added or removed).

Related

How to change background color for each two rows in SSRS in a group

How can I write the expression in order to change background color for each two rows in SSRS?
I need something like that:
I tried expression
=IIF(Fields!Type.Value="2016 Submitted" , "LightBlue",
IIF(Fields!Type.Value="2015 Submitted" , "LightBlue",
Nothing))
But because some months dont have values it looks like this:
If I try this expression I get below:
=IIF(RunningValue(Fields!Count.Value, CountDistinct, Nothing) MOD 2 = 1, "White", "PaleTurquoise")
Dance-Henry I tried your code
=IIF(RowNumber(Nothing) Mod 4 = 1 or RowNumber(Nothing) Mod 4 = 2, "Aqua","White")
and this is what i got:
You can select the row in design pane and press F4 to setup property BackgroundColor as =IIF(RowNumber(Nothing) Mod 4 = 1 or RowNumber(Nothing) Mod 4 = 2, "Aqua","White")
Captures are attached. Do it accordingly.
RESULT is something like this
After going thru the link https://blogs.msdn.microsoft.com/chrishays/2004/08/30/green-bar-matrix/
Tested and it works well for the Green Bar Effect for Matrix. I will show it step by step here as for later reference.
Step 1: Create the Matrix and add one more column under the innermost row grouping in your matrix. (ColorNameTextbox here)
Step 2: Select the textbox of ColorNameTextbox and press F4 to setup BackgroundColor property as =Value shown below.
Step 3: Select the textbox of Matrix cell and press F4 to setup BackgroundColor property as =ReportItems!ColorNameTextbox.Value shown below.
Step 4: Drag the inner grouping header (ColorNameTextbox) to be as narrow as possible.
Step 5: Preview Pane to check the result.
If you could add a hidden color_group column and populate it with a new number at each point you want to change the color (in your case 1,1,2,2,3,3,4,4) then you could use something like the following (which works for varying size groups):
IIF(RunningValue(Fields!color_group.Value, CountDistinct, Nothing) MOD 2 = 1, "White", "PaleTurquoise")
I had a similar problem where I could not come up with alternating rows because my data has Column Groups that were the RowNumber(Nothing) method to fail. Learning from all the other posts here this is how I resolved it in steps.
I added the following code into the report properties, that provides a function to get the row number, each time it is called. The function increments the row count each time it is called. >>Right click space around the report >> Properties >> Code. Alternatively go to Code Properties Window, when the report is selected.
Add the following lines:
Public Row_Sum As Decimal = 0
Public Function Lookup_Sum( ) As integer
Row_Sum = Row_Sum + 1
Return Row_Sum
End Function
I added a new column at the beginning of the rows called No. that would calculate and show the row number.Right click the first column >>Insert Column>>Inside Group-Left.
On the expression for the new report add this line of code. Also take a note of the name of the TextBox that will have the No. Value. It will be needed in the next step. In my case the TextBox is called TextBox6 (Properties Window). >>Right Click Cell>>Expression.
Add the code:
=Code.Lookup_Sum()
I highlighted the entire row and went to the Background property and added the following expression to compute the row number.
Add the code(TextBox6 is the name Textbox name noted above):
=IIF(VAL(ReportItems!Textbox6.Value) MOD 2, "LIGHTBLUE", "WHITE")

Listbox multiple selection matlab

I created a listbox and enabled multiple selections. My listbox contain numbers from 1 to 10. When I select 3, 1, and 8, the function always put my selections in alphabetical order (1,3,8). Is there any way I could make it not put my selection in alphabetical order? So if I select 3, 1, and 8 the output of my selection is 3, 1, 8.
Thank you.
For the purpose of this answer I'm assuming you're using matlab-hg2.
From the docs for uicontrol:
'listbox' ... The Value property stores the row indexes of currently selected list box items, and is a vector value when you select multiple items. After any mouse button up event that changes the Value property, MATLAB evaluates the list box's callback routine. To delay action when multiple items can be selected, you can associate a "Done" push button with the list box. Use the callback for that button to evaluate the list box Value property.
From the above we learn that the information on the selected lines is returned in Value. From there, it's a matter of keeping track of what's selected. This can be quite easily done using a persistent variable inside the listbox' Callback as shown in the following example:
function LISTBOX_EXAMPLE
hFig = figure('Units','Pixels','Position', [200 200 100 200],'Menubar','none');
uicontrol(hFig, ...
'Style', 'listbox',...
'Units','Pixels',...
'Position', [20 20 80 150],...
'Max',3,...
'Min',0,...
'String',num2cell(1:10),...
'Callback',#selectionCallback);
function selectionCallback(hObject,~)
persistent previouslyChosenItems
%// Elements were added:
if numel(previouslyChosenItems) < numel(hObject.Value)
previouslyChosenItems = union(previouslyChosenItems,hObject.Value,'stable');
%// Elements were removed:
elseif numel(previouslyChosenItems) > numel(hObject.Value)
previouslyChosenItems = intersect(previouslyChosenItems,hObject.Value,'stable');
%// A different element was selected (single element):
elseif numel(previouslyChosenItems) == numel(hObject.Value) && numel(hObject.Value)==1
previouslyChosenItems = hObject.Value;
end
disp(['Currently selected items (in order): ' num2str(previouslyChosenItems(:)')]);
end
end
Example output:
Currently selected items (in order): 7
Currently selected items (in order): 3
Currently selected items (in order): 3 9
Currently selected items (in order): 3 9 1
Then it's up to you to assign the value of previouslyChosenItems somplace useful.

How to align a label versus its content?

I have a label (e.g. "A list of stuff") and some content (e.g. an actual list). When I add all of this to a PDF, I get:
A list of stuff: test A, test B, coconut, coconut, watermelons, apple, oranges, many more
fruites, carshow, monstertrucks thing
I want to change this so that the content is aligned like this:
A list of stuff: test A, test B, coconut, coconut, watermelons, apple, oranges, many more
fruites, carshow, monstertrucks thing, everything is startting on the
same point in the line now
In other words: I want the content to be aligned so that it every line starts at the same X position, no matter how many items are added to the list.
There are many different ways to achieve what you want: Take a look at the following screen shot:
This PDF was created using the IndentationOptions example.
In the first option, we use a List with the label ("A list of stuff: ") as the list symbol:
List list = new List();
list.setListSymbol(new Chunk(LABEL));
list.add(CONTENT);
document.add(list);
document.add(Chunk.NEWLINE);
In the second option, we use a paragraph of which we use the width of the LABEL as indentation, but we change the indentation of the first line to compensate for that indentation.
BaseFont bf = BaseFont.createFont();
Paragraph p = new Paragraph(LABEL + CONTENT, new Font(bf, 12));
float indentation = bf.getWidthPoint(LABEL, 12);
p.setIndentationLeft(indentation);
p.setFirstLineIndent(-indentation);
document.add(p);
document.add(Chunk.NEWLINE);
In the third option, we use a table with columns for which we define an absolute width. We use the previously calculated width for the first column, but we add 4, because the default padding (left and right) of a cell equals 2. (Obviously, you can change this padding.)
PdfPTable table = new PdfPTable(2);
table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
table.setTotalWidth(new float[]{indentation + 4, 519 - indentation});
table.setLockedWidth(true);
table.addCell(LABEL);
table.addCell(CONTENT);
document.add(table);
There may be other ways to achieve the same result, and you can always tweak the above options. It's up to you to decide which option fits best in your case.

Xssf merging Queries

I have this piece of code:::
XSSFWorkbook workbook1=new XSSFWorkbook(inputStream);
XSSFSheet sheet = workbook1.getSheetAt(1);
sheet.addMergedRegion(new CellRangeAddress(28,28,5,9));
Now , i have few straight Question.
a) how to Set border for the merged region.
b) how to set Color inside the merged region
c) how to Put some value inside this merged region.
NB::
All needs to be in xssf.
You can work with merged cells as one cell. After merging row 28, column 5 to 9 all of them will be one cell which is row 28, column 5. You can do all of your usual cell editing on this cell.
XSSFRow row = sheet.createRow(28);
XSSFCell cell = row.createCell(5);
...

jquery ui sortable temporary element swapping during drag

I need to implement temporary drag replacement in a size-limited jquery ui sortable list.
I have 2 sortable lists:
A "slot" with only 1 spot to hold a single element
A "bucket" that holds several elements from which the user can pick any to fill the "slot"
The user has to drag an element from the bucket into the slot. While the user is holding the element with the mouse over the slot, if the slot is already occupied, the current slotted element should be moved into the bucket to visually make room for the dragged element. Then the user has 2 options:
If the user drops the new element: insert the new element into the slot
If the user cancels the drop: move the previously slotted element back into the slot (as it was originally)
This behaviour should repeat until the user has either cancelled the drag or dropped the element into the slot or the bucket.
To visually limit the slot to a single element, I have limited it to the exact height of a single element and setted its overflow to hidden.
Unfortunately, I have not been able to produce the effect while keeping a single element in the slot at all time.
EDIT 1: Here is an example of what I have so far
$(".slot").bind("sortchange", function (event, ui) {
var slot = $(event.target);
var bucket = $(".bucket");
// Move any element already in the slot (other than the currently
// dragged element) into the bucket
var slotElements = slot.children(".item").not(ui.item);
if (slotElements.length > 0) {
for (var idx = 0; idx < slotElements.length; idx += 1) {
var element = $(slotElements[idx]);
moveAnimate(element, bucket);
}
}
});