jesuskwice
BAN USERdef mostOnesRow(matrix)
mostOnesRow = 0
soonestOneCol = matrix[0].size
0.step(matrix.size - 1, 1) do |row|
0.step(matrix[row].size - 1, 1) do |col|
next if col > soonestOneCol
if matrix[row][col] == 1
soonestOneCol = col
mostOnesRow = row
end
end
end
return mostOnesRow
end
sub sumDiv3or5 {
$n = $_[0] - 1;
$sum = 0;
while ( $n > 0 ) {
if ( $n%3 == 0 || $n%5 == 0) {
$sum = $sum + $n;
}
$n--;
}
return $sum;
}
Break it down into two functions -- the first to breadth traverse the tree and store its values, and the other to print the stored values.
values[][];
int i = 0;
traverseTree (T, i) {
push(values[i], T.value);
traverseTree(T.leftChild, i+1) if T.leftChild;
traverseTree(T.rightChild, i+1) if T.rightChild;
}
printTreeVals (values) {
int i=0;
while(values[i]) {
foreach v in values[i] {
print v;
}
print "\n";
}
i++;
}
- jesuskwice February 19, 2018