How to sort by number in data validation - sorting

I have currently a problem where I am trying to filter based on numbers that I have put on in data validation:
As you can see the numbers goes from 1 to 11 and when I am using the filter to sort A-Z, it goes by 1, 10, 11, 2, 3, 4, 5 ...
and I wonder how can I filter it by going numberic? meaning 1,2,3,4,5,6,7,8,9,10,11 ?

one way would be to use 01 prefix instead of 1
this would then sort:
01
02
10
11

Related

Extract which ID appears in consecutive years

i am having troubles trying to extract from my data only the IDs that appear at least on two consecutive years. I couldn't find similar questions for solutions, much sorry if this question it's a duplicate of some sort.
I'll create a data example:
ID= c(1, 2, 1, 3, 4, 2, 1, 5, 4, 1, 2, 6, 7, 3, 1, 2,6,9,5)
Year= c(2006, 2006, 2006, 2006, 2006, 2006, 2006,2006, 2007,2007,2007,2007,2007,2007,2007,2007,2008,2008,2008)
DF<- data.frame(ID, Year)
I would like to get a result which shows me which IDs appear in consecutive years only, namely IDs 1,2,3, 4 and 6, as 5 also appears twice, but not consecutively, and the others are unique entries.
Surely there are several ways to do this. One fairly simple way (which assumes the years are already sorted as in your data example) is to first compute the minimum of the differences of the unique years for each ID:
mdu = aggregate(Year~ID, DF, function(y) suppressWarnings(min(diff(unique(y)))))
This yields (with your data example):
ID Year
1 1 1
2 2 1
3 3 1
4 4 1
5 5 2
6 6 1
7 7 Inf
8 9 Inf
(The suppressWarnings serves to silence a warning from min for an empty diff list, coming from IDs only appearing in one year.)
Now, the wanted IDs are those where the minimum year difference is 1 (which means the ID appears in consecutive years); they can be easily extracted by:
mdu$ID[mdu$Year==1]

Euler18 dynamic algorithm

Given the array [5, 4, 12, 3, 11, 7, 2, 8, 1, 9] that forms a triangle like so:
5
4 12
3 11 7
2 8 1 9
Result should be 5 + 12 + 7 + 9 = 31.
Write a function that will traverse the triangle and find the largest possible sum of values when you can go from one point to either directly bottom left, or bottom right.
Refering to the dynamic algorithm in that link:
http://www.mathblog.dk/project-euler-18/
Result is 36.
5
4 12
3 11 7
2 8 1 9
5
4 12
11 19 16
5
23 31
36
Where is my mistake ??
The description of Problem 18 starts with an example where the optimal path is “left-right-right”. So you get a new choice of direction after every step, which means that after taking the first step to the right, you are still free to take the second step to the left and eventually come up with 5+12+11+8=36 as the optimal solution in your example, larger than the 31 you assumed. So the computation is correct in solving the problem as described. Your assumption about choosing a direction only once and then sticking with that choice would lead to a different (and rather boring) problem.

algorithms- stack and queue

I am doing revision for my exams and I came across this question which I need to find the contents of Q1 after the following code are executed.
The datas
5, 7, 12, 4, 0, 4, 6, 8, 67, 34, 23, 5, 0, 44, 33, 22, 6, 0
The pusedo-code
Q1=createQueue
S1=createStack
loop (not end of file)
read number
if (number not 0)
pushStack (S1,number)
else
popStack (S1,x)
popStack (S1,x)
loop (not empty S1)
popStack (S1,x)
enqueue (Q1,x)
end loop
end if
end loop
Here is my solutions
if number is not 0, push the numbers to the stack
so now the stack becomes
6
22
33
44
5
23
34
67
8
6
4
4
12
7
5
else pop the first 2 elements of the stack
so now the stack becomes
33
44
5
23
34
67
8
6
4
4
12
7
5
3.loop stack !empty, pop the stack and enqueue in Q1.so now the stack is empty and the queue becomes
5,7,12,4,4,6,8,67,34,23,5,44,33
33 is the first in the queue and 5 is the last of the queue.
I double checked with the answer provided and found out that my answers is different
The provided answer
7,5,34,67,8,6,4,33,44
I am not sure who is correct. please help.
You reasoning is in the correct direction, but there are three zeros in the input and you only seem to do the else logic for the last one. Keep in mind that the else clause is within the loop too. Try processing the input until each of them one by one.

Binary Search in C++ with array

Here is an array with exactly 15 elements:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Suppose that we are doing a binary search for an element. Indicate any elements that will be found by examining two or fewer numbers from the array.
What I've got: as we are doing binary search, so the number found by only one comparison will be 7th element = 7. For two comparison, this leads to second division of array. That is, number found can be either 3 or 11.
Am I right or not?
You are almost right, the first number is not seven but eight.
The others 2 will then be 4 and 12.
The correct answer would be 4, 8, 12
`I found the answer to be 8 that is the 7th element, the other elements found were 3.5th and 10.5th element of the sorted array. So, the next two numbers delved are 4 and 11.
explanation on how i got the answers.
given array is 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
head=1
tail=15
middle= 0+14/2=7th element **0 is the index value of 1 and 14 is of 15**
middle value turns to be 8 as it is the 7th element.
solving value for first half
head=1
tail=8
middle= 0+7/2=3.5 or 3rd element **0 is the index value of 1 and 7 is of 8**
middle value now turns to be 4 as it is the 3rd element.
solving value for second half
head=8
tail=15
middle= 7+14/2=10.5 or 10th element **7 is the index value of 8 and 14 is
of 15**
middle value now turns to be 11 as it is the 10th element of the array`
Blockquote

Reordering items with multiple order criteria

Scenario:
list of photos
every photo has the following properties
id
sequence_number
main_photo_bit
the first photo has the main_photo_bit set to 1 (all others are 0)
photos are ordered by sequence_number (which is arbitrary)
the main photo does not necessarily have the lowest sequence_number (before sorting)
See the following table:
id, sequence_number, main_photo_bit
1 10 1
2 5 0
3 20 0
Now you want to change the order by changing the sequence number and main photo bit.
Requirements after sorting:
the sequence_number of the first photo is not changed
the sequence_number of the first photo is the lowest
as less changes as possible
Examples:
Example #1 (second photo goes to the first position):
id, sequence_number, main_photo_bit
2 10 1
1 15 0
3 20 0
This is what happened:
id 1: new sequence_number and main_photo_bit set to 0
id 2: old first photo (id 2) sequence_number and main_photo_bit set to 1
id 3: nothing happens
Example #2 (third photo to first position):
id, sequence_number, main_photo_bit
3 10 1
1 20 0
2 30 0
This is what happened:
id 1: new sequence_number bigger than first photo and main_photo_bit to 0
id 2: new sequence_number bigger than newly generated second sequence_number
id 3: old first photo sequence_number and main_photo_bit set to 1
What is the best approach to calculate the steps needed to save the new order?
Edit:
The reason that I want as less updates as possible is because I want to sync it to an external service, which is a quite costly operation.
I already got a working prototype of the algorithm, but it fails in some edge cases. So instead of patching it up (which might work -- but it will become even more complex than it is already), I want to know if there are other (better) ways to do it.
In my version (in short) it orders the photos (changing sequence_number's), and swaps the main_photo_bit, but it isn't sufficient to solve every scenario.
From what I understood, a good solution would not only minimize changes (since updating is the costly operation), but also try to minimize future changes, as more and more photos are reordered. I'd start by adding a temporary field dirty, to indicate if the row must change or not:
id, sequence_number, main_photo_bit, dirty
1 10 1 false
2 5 0 false
3 20 0 false
4 30 0 false
5 31 0 false
6 33 0 false
If there are rows which sequence_number is smaller than the first, they will surely have to change (either to get a higher number, or to become the first). Let's mark them as dirty:
id, sequence_number, main_photo_bit, dirty
2 5 0 true
(skip this step if it's not really important that the first has the lowest sequence_number)
Now let's see the list of photos, as they should be in the result (as per the question, only one photo changed places, from anywhere to anywhere). Dirty ones in bold:
[1, 2, 3, 4, 5, 6] # Original ordering
[2, 1, 3, 4, 5, 6] # Example 1: 2nd to 1st place
[3, 1, 2, 4, 5, 6] # Example 2: 3rd to 1st place
[1, 2, 4, 3, 5, 6] # Example 3: 3rd to 4th place
[1, 3, 2, 4, 5, 6] # Example 4: 3rd to 2nd place
The first thing to do is ensure the first element has the lowest sequence_number. If it hasn't changed places, then it has by definition, otherwise the old first should be marked as dirty, have its main_photo_bit cleared, and the new one should receive those values to itself.
At this point, the first element should have a fixed sequence_number, and every dirty element can have its value changed at will (since it will have to change anyway, so it's better to change for an useful value). Before proceeding, we must ensure that it's possible to solve it with only changing the dirty rows, or if more rows will have to be dirtied as well. This is simply a matter of determining if the interval between every pair of clean rows is big enough to fit the number of dirty rows between them:
[10, D, 20, 30, 31, 33] # Original ordering (the first is dirty, but fixed)
[10, D, 20, 30, 31, 33] # Example 1: 2nd to 1st place (ok: 10 < ? < 20)
[10, D, D, 30, 31, 33] # Example 2: 3rd to 1st place (ok: 10 < ? < ? < 30)
[10, D, 30, D, 31, 33] # Example 3: 3rd to 4th place (NOT OK: 30 < ? < 31)
[10, D, 30, D, D, 33] # must mark 5th as dirty too (ok: 30 < ? < ? < 33)
[10, D, D, 30, 31, 33] # Example 4: 3rd to 2nd place (ok)
Now it's just a matter of assigning new sequence_numbers to the dirty rows. A naïve solution would be to just increment the previous one, but a better approach would be setting them as equally spaced as possible. This way, there are better odds that a future reorder would require less changes (in other words, to avoid problems like Example 3, where more rows than necessary had to be updated since some sequence_numbers were too close to each other):
[10, 15, 20, 30, 31, 33] # Example 1: 2nd to 1st place
[10, 16, 23, 30, 31, 33] # Example 2: 3rd to 1st place
[10, 20, 30, 31, 32, 33] # Example 3: 3rd to 4th place
[10, 16, 23, 30, 31, 33] # Example 4: 3rd to 2nd place
Bonus: if you really want to push the solution to its limits, do the computation twice - one moving the photo, other having it fixed and moving the surrounding photos - and see which one resulted in less changes. Take example 3A, where instead of "3rd to 4th place" we treat it as "4th to 3rd place" (same sorting results, but different changes):
[1, 2, 4, 3, 5, 6] # Example 3A: 4th to 3rd place
[10, D, D, 20, 31, 33] # (ok: 10 < ? < ? < 20)
[10, 13, 16, 20, 31, 33] # One less change
In most cases it can be done (ex.: 2nd to 4th position == 3rd/4th to 2nd/3rd position), whether or not the added complexity is worth the small gain, it's up to you to decide.
Use a linked list instead of sequence numbers. Then you can remove a picture from anywhere in the list and reinsert it anywhere in the list, and you only need to change 3 lines in your database file. Main photo bit should be unneccessary, the first photo being implicitly defined by not having any pointers to it.
id next
1 3
2 1
3
the order is: 2, 1, 3
user moves picture 3 to position 1:
id next
1
2 1
3 2
new order is: 3, 2, 1

Resources