Fibonnaci Search arrays algorithmsss - data-structures

the keys 34, 12, and 45 using the Fibonacci search algorithm. Showing the complete algorithm
Elements: 12 26 34 40 43 45 65 68 74 78 89 98 120 125

Related

Ranking of nodes provides unranked result

I have the following graphviz input:
digraph para
{
node [shape=record];
rankdir=BT;
0 [label=<0<SUB>0,0</SUB>>];
1 [label=<1<SUB>1,1</SUB>>];
2 [label=<2<SUB>0,2</SUB>>];
3 [label=<2<SUB>1,1</SUB>>];
4 [label=<2<SUB>2,0</SUB>>];
5 [label=<3<SUB>1,3</SUB>>];
6 [label=<3<SUB>2,2</SUB>>];
7 [label=<3<SUB>3,1</SUB>>];
8 [label=<4<SUB>0,4</SUB>>];
9 [label=<4<SUB>1,3</SUB>>];
10 [label=<4<SUB>2,2</SUB>>];
11 [label=<4<SUB>3,1</SUB>>];
12 [label=<4<SUB>4,0</SUB>>];
13 [label=<5<SUB>1,5</SUB>>];
14 [label=<5<SUB>2,4</SUB>>];
15 [label=<5<SUB>3,3</SUB>>];
16 [label=<5<SUB>4,2</SUB>>];
17 [label=<5<SUB>5,1</SUB>>];
18 [label=<6<SUB>0,6</SUB>>];
19 [label=<6<SUB>1,5</SUB>>];
20 [label=<6<SUB>2,4</SUB>>];
21 [label=<6<SUB>3,3</SUB>>];
22 [label=<6<SUB>4,2</SUB>>];
23 [label=<6<SUB>5,1</SUB>>];
24 [label=<6<SUB>6,0</SUB>>];
25 [label=<7<SUB>1,7</SUB>>];
26 [label=<7<SUB>2,6</SUB>>];
27 [label=<7<SUB>3,5</SUB>>];
28 [label=<7<SUB>4,4</SUB>>];
29 [label=<7<SUB>5,3</SUB>>];
30 [label=<7<SUB>6,2</SUB>>];
31 [label=<7<SUB>7,1</SUB>>];
32 [label=<8<SUB>0,8</SUB>>];
33 [label=<8<SUB>1,7</SUB>>];
34 [label=<8<SUB>2,6</SUB>>];
35 [label=<8<SUB>3,5</SUB>>];
36 [label=<8<SUB>4,4</SUB>>];
37 [label=<8<SUB>5,3</SUB>>];
38 [label=<8<SUB>6,2</SUB>>];
39 [label=<8<SUB>7,1</SUB>>];
40 [label=<10<SUB>1,9</SUB>>];
41 [label=<7<SUB>7,1</SUB>>];
42 [label=<8<SUB>8,0</SUB>>];
43 [label=<5<SUB>4,1</SUB>>];
44 [label=<8<SUB>3,6</SUB>>];
45 [label=<8<SUB>5,4</SUB>>];
46 [label=<8<SUB>7,2</SUB>>];
47 [label=<1<SUB>1,1</SUB>>];
48 [label=<2<SUB>2,0</SUB>>];
49 [label=<3<SUB>1,3</SUB>>];
50 [label=<3<SUB>2,2</SUB>>];
51 [label=<3<SUB>3,1</SUB>>];
52 [label=<4<SUB>4,0</SUB>>];
53 [label=<6<SUB>1,5</SUB>>];
54 [label=<6<SUB>2,4</SUB>>];
55 [label=<7<SUB>3,5</SUB>>];
56 [label=<7<SUB>6,2</SUB>>];
57 [label=<7<SUB>7,1</SUB>>];
58 [label=<8<SUB>0,8</SUB>>];
59 [label=<8<SUB>3,5</SUB>>];
60 [label=<9<SUB>4,6</SUB>>];
61 [label=<1<SUB>0,1</SUB>>];
62 [label=<4<SUB>1,4</SUB>>];
63 [label=<5<SUB>2,3</SUB>>];
64 [label=<6<SUB>2,5</SUB>>];
65 [label=<6<SUB>6,1</SUB>>];
66 [label=<7<SUB>2,5</SUB>>];
67 [label=<7<SUB>3,4</SUB>>];
68 [label=<7<SUB>6,1</SUB>>];
69 [label=<7<SUB>7,0</SUB>>];
70 [label=<8<SUB>3,6</SUB>>];
71 [label=<8<SUB>4,5</SUB>>];
72 [label=<9<SUB>3,6</SUB>>];
73 [label=<5<SUB>1,5</SUB>>];
74 [label=<5<SUB>3,3</SUB>>];
75 [label=<6<SUB>4,2</SUB>>];
76 [label=<6<SUB>5,1</SUB>>];
77 [label=<7<SUB>4,4</SUB>>];
78 [label=<8<SUB>0,8</SUB>>];
{rank=same 0 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 37 38 39 40}
{rank=same 41 42}
{rank=same 43 44 45 46}
{rank=same 47 48 49 50 51 52 53 54 55 56 57 58 59 60}
{rank=same 61 62 63 64 65 66 67 68 69 70 71 72}
{rank=same 73 74 75 76 77 78}
4->5;
10->13;
24->29;
17->22;
12->15;
25->20;
39->46;
30->76;
36->59;
4->47;
27->58;
27->44;
29->75;
37->56;
30->71;
18->43;
7->50;
35->78;
8->49;
38->57;
21->54;
20->53;
9->50;
6->49;
4->61;
14->43;
24->41;
29->70;
10->51;
20->73;
15->52;
31->69;
31->69;
35->70;
22->74;
16->64;
23->67;
29->45;
33->67;
30->68;
24->65;
12->63;
5->48;
40->60;
9->62;
25->54;
4->49;
40->72;
37->60;
22->55;
18->63;
24->76;
34->59;
3->48;
0->47;
19->54;
32->55;
27->67;
37->72;
31->46;
22->66;
5->50;
32->66;
2->49;
29->77;
0->61;
26->55;
18->53;
35->77;
15->54;
11->52;
28->59;
1->48;
29->56;
37->69;
31->42;
18->73;
30->57;
20->74;
25->58;
25->44;
8->43;
18->64;
4->51;
13->52;
34->71;
27->75;
27->78;
28->76;
}
where I am trying to rank the nodes as follows:
{rank=same 0 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 37 38 39 40}
{rank=same 41 42}
{rank=same 43 44 45 46}
{rank=same 47 48 49 50 51 52 53 54 55 56 57 58 59 60}
{rank=same 61 62 63 64 65 66 67 68 69 70 71 72}
{rank=same 73 74 75 76 77 78}
However, it seems to me that only the first rank is assigned properly (for nodes 0,1,2,...,40). Could you please help me what the problem is with the ranking of the other groups? Why won't they be separated from each other?
rank=same can only force same rank for those nodes listed together, there is no guarantee that two rank=same gets different rank.
Also it seems like the hierarchy in you graph does not goes well with chosen subgraphs
Try replace your ranking with clusters and/or node coloring to analyze where the problem is. e.g.:
subgraph cluster1 {0 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 37 38 39 40}
subgraph cluster2 {41 42}
subgraph cluster3 {43 44 45 46}
subgraph cluster4 {47 48 49 50 51 52 53 54 55 56 57 58 59 60}
subgraph cluster5 {61 62 63 64 65 66 67 68 69 70 71 72}
subgraph cluster6 {73 74 75 76 77 78}
or
{0 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 37 38 39 40}
node[style=filled]
{node[fillcolor=red] 41 42}
{node[fillcolor=blue] 43 44 45 46}
{node[fillcolor=salmon] 47 48 49 50 51 52 53 54 55 56 57 58 59 60}
{node[fillcolor=pink] 61 62 63 64 65 66 67 68 69 70 71 72}
{node[fillcolor=green] 73 74 75 76 77 78}

Is it possible to do a bash brace expansion using a variable bound without using eval?

Consider the following script.
#!/bin/bash
echo {00..99}
n=99
echo {00..$n}
The output of this script is:
00 01 02 03 04 05 06 07 08 09 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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
{00..99}
The desired output is:
00 01 02 03 04 05 06 07 08 09 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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
00 01 02 03 04 05 06 07 08 09 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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
One solution which produces the desired output is
eval echo {00..$n}
Unfortunately, this solution uses eval which I'd prefer to avoid if possible.
Does anyone know of a way to obtain the desired result using brace expansion but not eval?
From the bash manual:
The order of expansions is: brace expansion; tilde expansion, parameter and variable expansion, arithmetic expansion, and command substitution (done in a left-to-right fashion); word splitting; and pathname expansion.
Given that variable expansion comes after brace expansion, and that there is no way to induce a different order of operations without using eval, I would have to conclude that no, there is no way to avoid using eval.
Does anyone know of a way to obtain the desired without without eval?
You can use seq command,
seq -w -s ' ' 0 $n
Test:
sat $ seq -w -s " " 0 $n
00 01 02 03 04 05 06 07 08 09 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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
Not sure if this meets your requirements, as it doesn't use braces, but (with GNU seq at least) the following produces the desired output:
$ n=99
$ seq -f%02.0f -s' ' 00 "$n"
00 01 02 03 04 05 06 07 08 09 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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
The "-f" option produces the zero-padding, and the "-d" uses spaces to separate, rather than newlines.

Undefined method '[]' nilLNilClass for Adding columns in multiarrays

I am trying to solve the Projecteuler #11 but I am running into an error when I'm trying to create a function to calculate the multiplication of every 4 numbers in a column. I am getting an error:
Project11.rb:59:in `sumvertical': undefined method `[]' for nil:NilClass (NoMeth
odError)
I feel like there is something I am easily overlooking here. I appreciate the help!
#project #11 http://projecteuler.net/problem=11
grid="08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48"
grid=grid.split()
grid=grid.collect {|s| s.to_i}
multiarray=[]
i = 0
e = 19
until e > 400
multiarray << grid[i..e]
i+= 20
e+= 20
end
def sumhorizontal(x) #checks sum of all horizontal 4 elements
sum = 0
x.each {|a|
i=0
e=3
while e < a.length
if a[i..e].inject(:*) > sum
sum = a[i..e].inject(:*)
i += 1
e += 1
else
i += 1
e += 1
end
end
}
return sum
end
def sumvertical(x)
sum = 0
i=0
e=0
while e < x.length #Will break once the end point is longer than the length of an array
until i > 20 #Checks the first column
if x[i][e]*x[i+1][e]*x[i+2][e]*x[i+3][e] > sum #Error is here
sum = x[i][e]*x[i+1][e]*x[i+2][e]*x[i+3][e]
i += 1
else
i += 1
end
end
e += 1 #once you are out of the until statement, it increases e by 1 to check the next column
i = 0 #resets i so it can go back to the zero
end
return sum
end
print sumvertical(multiarray)
The grid has 20 rows. Your loop is actually trying to reach all the way to a 24rd row; that's because it goes through 21 iterations (i starts at 0, and goes until it equals 21), and each iteration reaches 3 beyond the current value of i (when you call x[i+3]). When i is 17, your code will break, because x[i+3][e] is trying to index into the 21st row of x. i+3 is 20, but the highest available index is 19. So what happens is, x[20] returns nil, and then the [] method is called on nil, which generates your error.
Also, the standard library has a transpose method that you can call on your array. If you use it, you just need one method (sumhorizontal). You can get the column sums with sumhorizontal(multiarray.transpose).
One more thing... it looks like you're coming from a procedural language. Ruby has an extensive standard library and coding constructs that can save you a lot of time and keystrokes. There is typically no need to iterate with while loops and index variables in Ruby. sumhorizontal, for instance, can be written like this (it should really be called producthorizontal, though if you're trying to solve Project Euler #11:
def sumhorizontal(x)
x.map { |r| r.each_slice(4).map { |s| s.reduce(:*) }.max }.max
end
Good luck with the rest of your Ruby learning journey!

Specific Bubble Sort Process

I'm reviewing for an exam, one of the practice questions is as follows:
give the contents of the array after two iterations of the bubble sort (assume the lowest values are selected first to the left of the array
43 16 99 12 48 14 62
The given answer is:
12 14 43 16 99 48 62
I have been reviewing my notes trying to figure out why this is the correct answer, but I have no idea why. I have found examples of the bubble sort on google and wikipedia and while those make sense to me, they are also very simple, this is more difficult.
Can someone please explain how 12 14 43 16 99 48 62 is the answer?
I puzzled about this for a minute because indeed, it was hard to see, but once you realize how they're doing it, it's simple enough. Still, it's dumb.
We're sorting so that the lowest numbers are on the left, but we're iterating from the right. So the very first test is comparing 14 and 62, and not swapping; then comparing 48 and 14, and swapping; then 12 and 14, and doing nothing, etc. Once you get to the left end, go back to the right end and do a second pass, and you'll end up with the given solution.
Ok, this does not give the same answer as your teacher, but this is (I hope) a clear explanation of a bubble sort:
Because a picture is often a lot better than words: http://www.algolist.net/Algorithms/Sorting/Bubble_sort
In your case, after the first iteration:
43 > 16 => 16 43 99 12 48 14 62
43 < 99 => 16 43 99 12 48 14 62
99 > 12 => 16 43 12 99 48 14 62
99 > 48 => 16 43 12 48 99 14 62
99 > 14 => 16 43 12 48 14 99 62
99 > 62 => 16 43 12 48 14 62 99
Second Iteration:
16 < 43 => 16 43 12 48 14 62 99
43 > 12 => 16 12 43 48 14 62 99
43 < 48 => 16 12 43 48 14 62 99
48 > 14 => 16 12 43 14 48 62 99
48 < 62 => 16 12 43 14 48 62 99
62 < 99 => 16 12 43 14 48 62 99

Fixed Object Id for System Objects and Small Integers in Ruby

Why do system objects like nil, true or false have a fixed object id in Ruby. Also I tried printing out the object ids of numbers, they are the same and follow an odd number sequence pattern. Any explanation for this?
[nil,true,false].each { |o| print o.object_id, ' '}
4 2 0 => [nil, true, false]
>> (0..50).each { |i| print i.object_id, ' ' }
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99 101 => 0..50
The following two links explain the concept behind Ruby's object IDs:
http://www.oreillynet.com/ruby/blog/2006/01/the_ruby_value_1.html
http://www.oreillynet.com/ruby/blog/2006/02/ruby_values_and_object_ids.html
The object ID is calculated from the objects value plus some additional information. From that calculation you can derive the values you are seeing in your examples.

Resources