Complex chain of optional values with implicit conversions

Swift Hard optionals

Task «Complex chain of optional values with implicit conversions»

Given a string containing integers separated by spaces and the word "nil" to indicate the absence of a value, it is necessary to parse the string into an array of optional integers (Int?). Then, for each element, perform the following sequence: if a value exists, square it; otherwise, leave it as nil. After that, for each element, if it is not nil, extract the value, convert it to a string, and write it to the resulting array of strings; otherwise, write "nil". Output the resulting array of strings separated by spaces. The solution must be implemented using optional chaining and nil-coalescing operators.

Input Format

One line containing integers and the word "nil", separated by a single space. The length of the line does not exceed 1000 characters.

Output Format

A string containing the processing result — numbers (converted to strings) or the word "nil", separated by a single space.

main.swift