Im taking CompArch for the first time ever. I have to print out an array
list: .word 3, 0, 1, 2, 6, -2, 4, 7, 3, 7
I managed to print it out in two rows
R1 3 0 1 2 6
R2 -2 4 7 3 7
How can i create a loop or code to add all the numbers in the first row. Or in other words the first 5 ints from the array. Thanks for your help in advance.
Related
Since I am quite new with Swift, I was reading about Subscripts in Swift. It gave this example under Subscript Options: theExample
My question is how is the formula, grid[(row * columns) + column], is suppose to correspond to the right value in the grid array because when I calculate it it corresponds to the wrong value. Somebody please help.
0 1 2 3 4
--------------------
0 | 0, 1, 2, 3, 4,
1 | 5, 6, 7, 8, 9,
2 | 10, 11, 12, 13, 14
You have columns = 5.
If you want to get 8, row = 1 and column = 3.
row * columns + column
1 * 5 + 3 = 8.
In Ruby, given this array
[0,1,2,3,4,5]
how do i produce
> 0, 1
> 1, 2
> 2, 3
> 3, 4
> 4, 5
[0,1,2,3,4,5].each_cons(2){|a| puts a.join(", ")}
each_cons for sure, but another way:
enum = [0,1,2,3,4,5].to_enum
loop do
puts "#{enum.next}, #{enum.peek}"
end
0, 1
1, 2
2, 3
3, 4
4, 5
See Kernel#to_enum and Kernel#loop. Note the docs for all Kernel instance methods are shown at Object, whereas Kernel module methods are documented at Kernel.
How do you solve this problem in O(n) time?
Given an unsorted array of integers, design algorithms to transform the array such that the integers are replaced by the nearest bigger integer on their right. If there is no bigger integer on its right, the integer remains the same. For example, the following array of integers
2 1 4 5 3 6 7 9 4 8
should become
4 4 5 6 6 7 9 9 8 8
Init Stack of integers
From left to right.
Pop from stack while element is smaller, and replace it with the current
Add id of element to stack
Note that the stack will be strictly decreasing. And that each element is popped/added max 1 time. Hence O(N)
Example code in Python:
l = [2, 1, 4, 5, 3, 6, 7, 9, 4, 8]
s = [0]
for i in range(1, len(l)):
while s and l[i] > l[s[-1]]:
l[s.pop()] = l[i]
s.append(i)
I have two giant array which looks like:
A = [11, 11, 12, 3, 3, 4, 4, 4 ];
B = [ 12, 4; 3, 11; 11, 1; 4, 13 ];
I want to create an array which takes values from B and column 1 from A to look like:
C = [ 11, 1; 11, 1; 12, 4; 3, 11; 3, 11; 4, 13; 4, 13; 4, 13 ];
I don't want to use for or any other kind of loop to optimize the process.
Sorry for being terse.
I will search each element from column 1 of A in column 1 of B and pick the corresponding column 2 elements from B and create a new array with column 1 elements of A and discovered column 2 elements from B.
What you are doing in this problem is using A and searching the first column of B to see if there's a match. Once there's a match, extract out the row that corresponds to this matched location in B. Repeat this for the rest of the values in A.
Assuming that all values of A can be found in B and that the first column of B is distinct and that there are no duplicates, you can a unique call and sortrows call. The unique call is on A so that you can assign each value in A to be a unique label in sorted order. You would then use these labels to index into the sorted version of B to get your desired result:
[~,~,id] = unique(A);
Bs = sortrows(B);
C = Bs(id,:);
We get for C:
C =
11 1
11 1
12 4
3 11
3 11
4 13
4 13
4 13
Thanks to #rayryeng for clarifying the question to me.
Assuming each element from A is present in column 1 of B:
[~, ind] = max(bsxfun(#eq, A(:).', B(:,1)), [], 1);
C = B(ind,:);
If that assumption doesn't necessarily hold:
[val, ind] = max(bsxfun(#eq, A(:).', B(:,1)), [], 1);
C = B(ind(val),:);
So for example A = [11, 20, 12, 3, 3, 4, 4, 4 ]; would produce
C =
11 1
12 4
3 11
3 11
4 13
4 13
4 13
When I was trying to print a specific element in array, I have mistakenly typed the name of the same array inside [ ] as element and got some output as shown. I thought that it is taking the size of the array and printing that number of characters, which was proven to be wrong by the output.
#array = (0..10, 12);
print "#array[#array]";
prints
Use of uninitialized value in join or string at
/home/VAR121/Program/Practise_Perl/Arrays.pl line 9.
0 1 2 3 4 5 6 7 8 9 10
I went one step ahead and edited code as shown below
print "#array[#array[#array]]";
output as: `0 1 2 3 4 5 6 7 8 9 10 0` Use of uninitialized value in join or string at
/home/VAR121/Program/Practise_Perl/Arrays.pl line 9.
Now I tried to put a number inside the second array instead of again giving array name as below.
print "#array[#array[1,2,3]]";
output as
1 2 3
But no warning message this time.
What it is trying to print? and What is the reason behind this behavior.
You're creating an array slice. Start out by understanding that your array contains elements 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12.
That's a total of 12 elements (there is no value 11, but at index 11 you are storing the value 12).
Now when you use this construct: #array[#array] you're taking a slice, and requesting the values stored in indices 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, and 12. There is no element with an index of 12 in your array. The eleventh element has the value "12".
#array[] is array slice notation. For example:
my #array = (qw/apple banana cucumber date/)
#array[1,2] will return a list of (banana, cucumber) (index 1 and index 2 of #array).
Now, #array[#array] tries to take the values of the inner #array and use them as indexes of the outer #array. In your example, #array[#array] is equivalent to #array[0..10, 12]. Since one of the values of the inner array is 12, and the outer array has no index of 12, you get an undefined value warning.