Skip to main content
added 1121 characters in body
Source Link
Cabbie407
  • 1.3k
  • 10
  • 19

These are not in any particular order, and some of them might even apply to other languages too, but not to a lot of them I think.

  • If there is no input and you use the BEGIN block, you can try to use the END block instead. Depending on the judge it works.

    You can sometimes use awk's simple string concatenation to assemble numbers

z=x*10+y and a=b*10

become

z=x y and a=b 0

  • You can swap two numbers in a single command

    You can use the ~ operator (match pattern on the right side) to compare numbers or strings if you know that the two parts meet certain conditions

for instance if you know that a will always be smaller than or equal to b, you can replace

a==b

with

a~b

or if you want to check for an empty string s (which means that s isn't allowed to be "0" either), and you haven's changed the variable X before, so that it is still undefined, you could use

X~s

instead of

s==""

if you want to check if i is an integer you can use

i!~/\./ (i doesn't contain a dot)

instead of

i==int(i)

  • You can swap two numbers in a single command

t=a;a=b;b=t

becomes

a+=b-(b=a)

saving one character

  • If you don't need them as input anymore, you can use the input variables $1, $2, $3, ... as an array. So instead of writing a[n] you can just write $n. Sometimes even while reading the input from these you can use the already handled ones as a stack for something.

  • You can make good use of the built-in separators FS (space, if not changed by you) and RS (newline) when concatenating strings

a=$1" "$2

becomes

a=$1FS$2

or even better

a=$1"\n"$2

becomes

a=$1RS$2

  • If there is no input and you use the BEGIN block, you can try to use the END block instead. Depending on the judge it works. If you test it, you have to press Ctrl-D (end of input) to access the END block.

  • If you want to skip the first line, you can use

If I can thinkC++

instead of more, I'll edit

NR>1

if you won't be using C anywhere in your program.

  • If there is no input and you use the BEGIN block, you can try to use the END block instead. Depending on the judge it works.

  • You can swap two numbers in a single command

t=a;a=b;b=t

becomes

a+=b-(b=a)

saving one character

  • If you don't need them as input anymore, you can use the input variables $1, $2, $3, ... as an array. So instead of writing a[n] you can just write $n. Sometimes even while reading the input from these you can use the already handled ones as a stack for something.

  • You can make good use of the built-in separators FS (space, if not changed by you) and RS (newline) when concatenating strings

a=$1" "$2

becomes

a=$1FS$2

or even better

a=$1"\n"$2

becomes

a=$1RS$2

If I can think of more, I'll edit

These are not in any particular order, and some of them might even apply to other languages too, but not to a lot of them I think.

  • You can sometimes use awk's simple string concatenation to assemble numbers

z=x*10+y and a=b*10

become

z=x y and a=b 0

  • You can use the ~ operator (match pattern on the right side) to compare numbers or strings if you know that the two parts meet certain conditions

for instance if you know that a will always be smaller than or equal to b, you can replace

a==b

with

a~b

or if you want to check for an empty string s (which means that s isn't allowed to be "0" either), and you haven's changed the variable X before, so that it is still undefined, you could use

X~s

instead of

s==""

if you want to check if i is an integer you can use

i!~/\./ (i doesn't contain a dot)

instead of

i==int(i)

  • You can swap two numbers in a single command

t=a;a=b;b=t

becomes

a+=b-(b=a)

saving one character

  • If you don't need them as input anymore, you can use the input variables $1, $2, $3, ... as an array. So instead of writing a[n] you can just write $n. Sometimes even while reading the input from these you can use the already handled ones as a stack for something.

  • You can make good use of the built-in separators FS (space, if not changed by you) and RS (newline) when concatenating strings

a=$1" "$2

becomes

a=$1FS$2

or even better

a=$1"\n"$2

becomes

a=$1RS$2

  • If there is no input and you use the BEGIN block, you can try to use the END block instead. Depending on the judge it works. If you test it, you have to press Ctrl-D (end of input) to access the END block.

  • If you want to skip the first line, you can use

C++

instead of

NR>1

if you won't be using C anywhere in your program.

added 8 characters in body
Source Link
Cabbie407
  • 1.3k
  • 10
  • 19
  • If there is no input and you use the BEGIN block, you can try to use the END block instead. Depending on the judge it works.

  • You can swap two numbers in a single command

    t=a;a=b;b=t

t=a;a=b;b=t

becomes

`a+=b-(b=a)`

a+=b-(b=a)

saving one character

  • If you don't need them as input anymore, you can use the input variables $1, $2, $3, ... as an array. So instead of writing a[n] you can just write $n. Sometimes even while reading the input from these you can use the already handled ones as a stack for something.

    If you don't need them as input anymore, you can use the input variables $1, $2, $3, ... as an array. So instead of writing a[n] you can just write $n. Sometimes even while reading the input from these you can use the already handled ones as a stack for something.

  • You can make good use of the built-in separators FS (space, if not changed by you) and RS (newline) when concatenating strings

a=$1" "$2

becomes

a=$1FS$2

or even better

a=$1"\n"$2

becomes

a=$1RS$2

If I can think of more, I'll edit

  • If there is no input and you use the BEGIN block, you can try to use the END block instead. Depending on the judge it works.

  • You can swap two numbers in a single command

    t=a;a=b;b=t

becomes

`a+=b-(b=a)`

saving one character

  • If you don't need them as input anymore, you can use the input variables $1, $2, $3, ... as an array. So instead of writing a[n] you can just write $n. Sometimes even while reading the input from these you can use the already handled ones as a stack for something.

If I can think of more, I'll edit

  • If there is no input and you use the BEGIN block, you can try to use the END block instead. Depending on the judge it works.

  • You can swap two numbers in a single command

t=a;a=b;b=t

becomes

a+=b-(b=a)

saving one character

  • If you don't need them as input anymore, you can use the input variables $1, $2, $3, ... as an array. So instead of writing a[n] you can just write $n. Sometimes even while reading the input from these you can use the already handled ones as a stack for something.

  • You can make good use of the built-in separators FS (space, if not changed by you) and RS (newline) when concatenating strings

a=$1" "$2

becomes

a=$1FS$2

or even better

a=$1"\n"$2

becomes

a=$1RS$2

If I can think of more, I'll edit

added 8 characters in body
Source Link
Cabbie407
  • 1.3k
  • 10
  • 19

If there is no input and you use the BEGIN block, you can try to use the END block instead. Depending on the judge it works.

  • If there is no input and you use the BEGIN block, you can try to use the END block instead. Depending on the judge it works.

  • You can swap two numbers in a single command

    t=a;a=b;b=t

You can swap two numbers in a single commandbecomes

t=a;a=b;b=t     becomes

a+=b`a+=b-(b=a)      saving one character`

If you don't need them as input anymore, you can use the input variables $1, $2, $3, ... as an array. So instead of writing a[n] you can just write $n. Sometimes even while reading the input from these you can use the already handled ones as a stack for something.saving one character

  • If you don't need them as input anymore, you can use the input variables $1, $2, $3, ... as an array. So instead of writing a[n] you can just write $n. Sometimes even while reading the input from these you can use the already handled ones as a stack for something.

If I can think of more, I'll edit

If there is no input and you use the BEGIN block, you can try to use the END block instead. Depending on the judge it works.

You can swap two numbers in a single command

t=a;a=b;b=t     becomes

a+=b-(b=a)      saving one character

If you don't need them as input anymore, you can use the input variables $1, $2, $3, ... as an array. So instead of writing a[n] you can just write $n. Sometimes even while reading the input from these you can use the already handled ones as a stack for something.

If I can think of more, I'll edit

  • If there is no input and you use the BEGIN block, you can try to use the END block instead. Depending on the judge it works.

  • You can swap two numbers in a single command

    t=a;a=b;b=t

becomes

`a+=b-(b=a)`

saving one character

  • If you don't need them as input anymore, you can use the input variables $1, $2, $3, ... as an array. So instead of writing a[n] you can just write $n. Sometimes even while reading the input from these you can use the already handled ones as a stack for something.

If I can think of more, I'll edit

added 274 characters in body
Source Link
Cabbie407
  • 1.3k
  • 10
  • 19
Loading
Source Link
Cabbie407
  • 1.3k
  • 10
  • 19
Loading