03 May 2022

Lot of ands and a strange grid

Sum of the ands

We are given a list of positive integers and asked to add together the results of and-ing all possible pairs.

The obvious (to me) way of doing this is two nested loops to generate the number pairs, 'and' them together and add the result to a successive sum.

The slightly trickier part is to output the result in the format Mohammad asks, but if we just add to the eventual output within the inner loop we can get a single 'say' for the output.

A strange grid

We are again given a list of positive numbers and asked to generate a triangular array according to certain rules. These rules don't seem quite to match the examples, so I took the examples as being definitive.

The first row of the output is just the input.

The nth row then has n - 1 blank columns, and subsequent columns contain the sum of the cell to the left and the cell above.

Again, there is the issue of producing pretty output, and I chose to output all the cells one character wider than the final cell, which is always going to be the largest number.


No comments:

Post a Comment