#K46. Group Non-Negative Integers into Sublists

    ID: 27878 Type: Default 1000ms 256MiB

Group Non-Negative Integers into Sublists

Group Non-Negative Integers into Sublists

You are given a sequence of integers. Your task is to filter out all negative integers and group the remaining non-negative integers into sublists where each sublist has exactly 3 elements (except possibly the last one which may have fewer than 3 elements). The order of the non-negative integers must be preserved.

For example, given the input list:

4 -1 0 7 -3 9 2 -8

After filtering, we get: [4, 0, 7, 9, 2]. Grouping them in sublists of 3 results in: [[4, 0, 7], [9, 2]].

Formally, if ( L = [a_1, a_2, \dots, a_n] ) is the input list, let ( L' ) be the list containing only the non-negative integers in ( L ). Then partition ( L' ) into contiguous groups of 3 elements. Use LaTeX format for any formulas.

inputFormat

The input is given via standard input (stdin) as a single line containing zero or more integers separated by spaces. For example, an input might be:

4 -1 0 7 -3 9 2 -8

outputFormat

Output the resulting list-of-lists (in Python list format) to standard output (stdout). For example, for the above input, the output should be:

[[4, 0, 7], [9, 2]]## sample

4 -1 0 7 -3 9 2 -8
[[4, 0, 7], [9, 2]]