#C7169. Get Responses Until the Third Positive
Get Responses Until the Third Positive
Get Responses Until the Third Positive
You are given a sequence of responses where each response is a string that can be either positive
, negative
, or neutral
. Your task is to print all the responses in order until and including the third occurrence of the response positive
. If fewer than three positive
responses appear, simply output all the responses.
Note: If the list contains at least three positive
responses, stop immediately after the third positive
has been encountered.
The problem can be mathematically described as follows. Let \( R = [r_1, r_2, \dots, r_n] \) be the input list. Let \( k \) be the smallest index satisfying \[ \sum_{i=1}^{k} I(r_i = \text{positive}) = 3, \] where \( I(\cdot) \) is the indicator function. If no such \( k \) exists, then let \( k = n \). Output the sublist \( [r_1, r_2, \dots, r_k] \).
inputFormat
The input is read from stdin and consists of:
- An integer
n
on the first line, representing the number of responses. - A single line containing
n
space separated strings, where each string ispositive
,negative
orneutral
.
outputFormat
Print the filtered list of responses (from the first until the third positive
encountered) as a sequence of space separated strings on stdout.
7
neutral positive negative positive neutral positive negative
neutral positive negative positive neutral positive