Raindrops - suggestion to improve AWK problem

Raindrops in AWK

A student recently asked me about an idiomatic solution to a problem, but I believe the current formulation of the problem is not optimal. The AWK program and language are primarily designed for processing data streams, so it would be more correct to assume that we are dealing with a stream of numbers instead of passing a variable containing a number. This is the exact task for which the AWK program was developed.

Below, I provide one of the possible solutions for converting a stream of numbers.

    OFS = ""
    number = $1
number % 3 == 0 {
    $(NF++) = "Pling"
number % 5 == 0 {
    $(NF++) = "Plang"
number % 7 == 0 {
    $NF = "Plong"

If the community agrees with my point of view, then I can improve the formulation for this problem and at the same time fix the tests

Other awk exercises do read from STDIN. We explicitly opted to mix things up with this exercise to show some variety in how awk can be used.

1 Like