Desk Check Binary Search - search

I am here about a query that I am facing.
I was wondering on how I would go about desk checking the following code.
Data set
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
4 7 19 25 36 37 50 100 101 205 220 271 306 321 456 500 /* Numbers are a bit messed up */
Algorithm
binarySearch
SET found TO FALSE
SET bottom TO zero
SET top TO sizeOfList-1
WHILE ( NOT found AND bottom <= top )
SET middle TO (bottom+top) DIV 2
IF searchValue < list element middle THEN
SET top TO middle-1
ELSE
IF searchValue > list element middle THEN
SET bottom TO middle+1
ELSE
SET position TO middle
SET found TO TRUE
ENDIF
ENDIF
ENDWHILE
IF NOT found THEN
RETURN –1
ELSE
RETURN position
ENDIF

The best way to do this is to first draw up a table, with one column for each variable (found, bottom, top, etc.). Then, "be" the computer, step through your program code one line at a time (probably best to write down each line number that you visit to keep track), taking the conditional branches based on the values in your table. Every time you modify a variable, add a new row to your table with the updated values. Eventually, you should reach a return statement, and then you're done.

Related

excel incrementation by consecutive numbers

I have a form, thanks to user JamTay317, That lists data depending on folder number (bold number in form). I need to copy it for all 1500 folders (about 400 pages)
Form is divided on 4 labels on a page for easier printing
form overview
Form get it's folder number (nr teczki) from list with all folders from another sheet called "lista teczek":
list of folders
For first 4 folder numbers I use formula:
A2='lista teczek'!A1
J2='lista teczek'!A2
A21='lista teczek'!A3
J21='lista teczek'!A4
When I copy whole page underneath it increments by 36 (number of rows between)
A38='lista teczek'!A37
J38='lista teczek'!A38
A57='lista teczek'!A39
J38='lista teczek'!A40
Instead of A5, A6, etc.
Is there any way to override excel's incrementation to force it to use consecutive numbers? Or at least formula which will make it easier to follow folders list?
So I would use offset() to get the correct position
=A2=OFFSET('lista teczek'!$A$1;ROW(A1)-INT(ROW(A1)/36)*36+4*INT(ROW(A1)/36)-1;0)
So this will offset from A1 in the list sheet.
Below are row numbers a resultant lookup row numbers
Note the formula I used in the offset has an extra "-1" as this is an OFFSET so to get 1 from 1 we need to offset by 0
1 1
2 2
3 3
4 4
37 5
38 6
39 7
40 8
73 9
74 10
75 11
76 12
109 13
110 14
111 15
112 16
145 17
146 18
147 19
----LOGIC--- (edit)
So the idea is that you work out the occurrence you are on. Int(row()/36) gives us this. For example
int(1/36)=0
Int(363/36)=10
First part gives us the offset from the start of the occurrence
3-int(3/36)*36=3
378-Int(363/36)*36=3
Second part give the total of the previous occurrence
4*int(3/36)=0
4*Int(363/36)*36=40
So you need to change the 36 to the gap between the occurrences and the 4 to the length of occurrences Not sure if that helps to explain

Create a Histogram in Excel with three different level

Based on the picture that I have uploaded, how should I create it in Excel with three different level?
Thanks!
Grades Bins Frequency Intervals
9 9 0 0-9
6 19 2 10-19
1 29 1 20-29
7 39
5
5
2
4
6
2
10
11
15
18
20
21
23
25
26
27
29
To create a histogram in Excel you would use FREQUENCY
This is no diferent in using something such as the groups for colours.
The difference is that you would use an IF statement referring to the group column.
=FREQUENCY(IF(GroupRange="GroupName",DataRange),BinRange)
So if your data was in B2:B40 and your group delimiter in C2:C40 and your bin sizes was in E2:E12 you would use a formula such as:
=FREQUENCY(IF($C$2:$C$40="B",$B$2:$B$40),$E$2:$E$12)
Then pop each group next to each other changing the "B" (or whatever) as you go.
Hopefully this will get oyu on the right track.
(note: with FREQUENCY you must array enter into all cells in line with the bin range... [ctrl]+[shift]+[enter])

Find duplicates and count numbers at the same time

I have rows of data that contains numbers from 1 to 15, however these numbers can be in any sequence. For example here:
3 2 1 12 13 5 6 7 9 15 10 8 4 15 11
I know from a visual count these numbers above are all correct; as there are no duplicates, and all the numbers have values from 1 to 15. An example of a row of data I found to be wrong:
3 2 1 12 12 5 6 7 9 15 10 8 4 15
You can see this line has duplicated numbers i.e. 12, and number 11 is missing, so this row only has 14 elements in all.
However, I have many rows of data and it is impossible to visually check each row. I need to ensure in each row: there are 15 elements; there are no duplicates, and that the row contains values from 1 to 15 and find which rows are faulty to check these against the original paper data.
Is there a command or function that I can use in Excel to make this process easier?
You could find a set of conditions, each of which is true for rows that contain exactly those 15 numbers in any order and then test several of them. For example, if the row is in A5:O5:
=AND(COUNT(A5:O5)=15,SUM(A5:O5)=120,MIN(A5:O5)=1,MAX(A5:O5)=15,
AVERAGE(A5:O5)=8,ROUND(STDEV(A5:O5),3)=4.472)
This will show TRUE for a row that contains the integers 1 to 15 in any order, and is very unlikely (it could very well be impossible - I haven't checked) to show TRUE for a row that contains any different set of integers.
I'm pretty sure that the only way 15 positive integers less than 16 can add up to 120 other than by all being different is with duplication, so :
Check there are 15 numbers
Check their total is 120
Check the maximum is 15
Check not negative (nor zero):
=IF(OR(COUNT(A5:O5)<>15,SUM(A5:O5)<>120,MAX(A5:O5)>15,MIN(A5:O5)<1),"Error","Plausible")
then check for duplication with Conditional Formatting using a rule such as :
=COUNTIF($A5:$O5,A5)>1
and a distinctive format. Filter to select "Plausible" and then anything with a distnctive format is non compliant.

How in Excel change the direction of numbers (from right (0) to left (100))?

I have a line of numbers from 0 to 100. They follow each other like usual:
1 2 3 4 5 6 7 8 9 10 ... and so on --> ... 100
But I need to change their direction from right to left, like this:
100 <-- and so on ... 10 9 8 7 6 5 4 3 2 1 0
Is there some special tool for this operetion?
Each number is in its individual cell and horizontally: 1 in [A1], 2 in [B1], 3 in [C1], and so on.
If this is exaclty what you need, you can simply enter 100 in A1, 99 in B1, select the 2 cells and drag the formula to the right (by clicking on the little square at the bottom right of the selected range).
If your actual problem is a bit more complex and you need to sort the data in descending order, you could:
transpose it to make it vertical (Copy / Paste Special and select transpose)
sort the data set with the standard sorting feature
transpose it again back to its original range

A problem with connected points and determining geometry figures based on points' location analysis

In school we have a really hard problem, and still no one from the students has solved it yet. Take a look at the picture below:
http://d.imagehost.org/0422/mreza.gif
That's a kind of a network of connected points, which doesn't end and each point has its own number representing it. Let say the numbers are like this: 1-23-456-78910-etc. etc.. (You can't see the number 5 or 8,9... on the picture but they are there and their position is obvious, the point in middle of 4 and 6 is 5 and so on).
1 is connected to 2 and 3, 2 is connected to 1,3,5 and 4 etc.
The numbers 1-2-3 indicate they represent a triangle on the picture, but the numbers 1-4-6 do not because 4 is not directly connected with 6.
Let's look at 2-3-4-5, that's a parallelogram (you know why), but 4-6-7-9 is NOT a parallelogram because the in this problem there's a rule which says all the sides must be equal for all the figures - triangles and parallelograms.
Also there are hexagons, for ex. 4-5-7-9-13-12 is a hexagon - all sides must be equal here too.
12345 - that doesn't represent anything, so we ignore it.
I think i explained the problem well. The actual problem which is given to us by using an input of numbers like above to determine if that's a triangle/parallelogram/hexagon(according to the described rules).
For ex:
1 2 3 - triangle
11 13 24 26 -parallelogram
1 2 3 4 5 - nothing
11 23 13 25 - nothing
3 2 5 - triangle
I was reading computational geometry in order to solve this, but i gave up quickly, nothing seems to help here. One friend told me this site so i decided to give it a try.
If you have any ideas about how to solve this, please reply, you can use pseudo code or c++ whatever. Thank you very much.
Let's order the points like this:
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 26 27 28
You can store this in a matrix. Now let row[i] = the row number i is on and col[i] = the column number i is on. These can be computed more or less efficiently for each i.
First, sort your given numbers ascendingly. You will need exactly 3 points for a triangle, 4 for a parallelogram and 6 for a hexagon - anything else and you can dismiss it as no-figure.
Notice that we can only have right-angled triangles in this matrix, according to your rules. Label the three points A, B, C. You can check if these form a triangle by iterating from row[A] to row[B], then from col[B] to col[C] and then diagonally from row[C] to row[A] and checking to see if the distances are the same and if you get to the right positions. You can terminate this early, for example if B is 8 and A is 1, then you can tell you won't find it once you hit 11 on column 1.
For parallelograms a similar reasoning can be made. Label the 4 points A, B, C, D and remember to sort them ascendingly (remember, your points here are actually numbers). See if you can get from col[A] to col[B] on the same line, then from col[C] to col[D] on the same line and then diagonally or vertically-down from row[A] to row[C] and then (in the same direction you went the previous diagonal!) from row[B] to row[D].
Hexagons are also have a specific format you must test for. Here's how hexagons look like in this representation:
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 26 27 28
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 26 27 28
29 30 31 32 33 34 35 36
You can notice that every two pairs of points share the same column, and that the horizontal distance between the two middle points is twice the vertical distance between any two points and also twice the horizontal distance between any other two points.
You will also want to consider rotations, so you'll need to do more tests for each case.
You don't even really need the row and col arrays unless you plan on computing them efficiently. Just walk over your matrix until you identify the first point in sorted order and try to get to the others while following each of the rules.
Not exactly a nice way, but you will only need a 256x256 matrix for this, so while this does result in quite a lot of code, it's pretty efficient. I hope I made myself clear, if not please say what isn't clear. Anyway, maybe someone else will post a better solution, so wait a while longer if you can..

Resources