Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Awk exercises Q:58/88 imprecisely defined #8

Open
FabijanC opened this issue May 21, 2024 · 1 comment
Open

Awk exercises Q:58/88 imprecisely defined #8

FabijanC opened this issue May 21, 2024 · 1 comment

Comments

@FabijanC
Copy link

The exercises reads:

The input file nums.txt contains a single column of numbers. Change positive numbers to negative and vice versa. Solution should use the sub function and shouldn't explicitly use the if-else control structure or the ternary operator.

The reference solution indeed relies on sub:

awk '!sub(/^-/, ""){sub(/^/, "-")} 1' nums.txt

But the most elegant solution would be simply:

awk '{ print -$0 }' nums.txt

In fact, the elegant solution would work correctly in case the input file contained a zero value (0), whereas the reference solution would substitute 0 with -0.

Perhaps the conditions of the exercise need to be defined differently if the point is to really rely on sub and its return value.

@FabijanC FabijanC changed the title Awk exercises Q:58/88 Awk exercises Q:58/88 imprecisely defined May 21, 2024
@learnbyexample
Copy link
Owner

Hmm, yeah. The main intention was to force the use of sub function and its return value. I'll probably try to come up with a different string manipulation data instead of relying on numbers that could be solved in a simpler way.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants