Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

Commonmark migration
Source Link

##bash, sed and coreutils, (削除) 95 (削除ここまで) 89 bytes

bash, sed and coreutils, (削除) 95 (削除ここまで) 89 bytes

You can define a function like this

f(){ n=$[1ドル-2];yes \ |sed $[2ドル*n]q|tr -d \\n|fold -w$n|sed 's/^\|$/|/g;1!{$!b};s/ /-/g';}

Or in a more readable format:

f() { 
 n=$((1ドル-2))
 
 # The next couple of lines create a rectangle of spaces
 # matching the desired size
 yes ' ' |
 head -n$((2ドル*n)) |
 tr -d '\n' |
 fold -w$n |
 # Add the pipes and dashes
 sed '
 s/^\|$/|/g # Replace first and last character by a pipe
 1! {$!b } # Do nothing if not on first or last line
 s/ /-/g # Add the dashes
 '
 echo
}

You can now say f 4 3:

|--|
| |
|--|

If you care about trailing new-line, add an echo at the end of function.

##bash, sed and coreutils, (削除) 95 (削除ここまで) 89 bytes

You can define a function like this

f(){ n=$[1ドル-2];yes \ |sed $[2ドル*n]q|tr -d \\n|fold -w$n|sed 's/^\|$/|/g;1!{$!b};s/ /-/g';}

Or in a more readable format:

f() { 
 n=$((1ドル-2))
 
 # The next couple of lines create a rectangle of spaces
 # matching the desired size
 yes ' ' |
 head -n$((2ドル*n)) |
 tr -d '\n' |
 fold -w$n |
 # Add the pipes and dashes
 sed '
 s/^\|$/|/g # Replace first and last character by a pipe
 1! {$!b } # Do nothing if not on first or last line
 s/ /-/g # Add the dashes
 '
 echo
}

You can now say f 4 3:

|--|
| |
|--|

If you care about trailing new-line, add an echo at the end of function.

bash, sed and coreutils, (削除) 95 (削除ここまで) 89 bytes

You can define a function like this

f(){ n=$[1ドル-2];yes \ |sed $[2ドル*n]q|tr -d \\n|fold -w$n|sed 's/^\|$/|/g;1!{$!b};s/ /-/g';}

Or in a more readable format:

f() { 
 n=$((1ドル-2))
 
 # The next couple of lines create a rectangle of spaces
 # matching the desired size
 yes ' ' |
 head -n$((2ドル*n)) |
 tr -d '\n' |
 fold -w$n |
 # Add the pipes and dashes
 sed '
 s/^\|$/|/g # Replace first and last character by a pipe
 1! {$!b } # Do nothing if not on first or last line
 s/ /-/g # Add the dashes
 '
 echo
}

You can now say f 4 3:

|--|
| |
|--|

If you care about trailing new-line, add an echo at the end of function.

Added $[...] and head improvements suggested by sch
Source Link
Thor
  • 2.9k
  • 2
  • 21
  • 24

##bash, sed and coreutils, 95(削除) 95 (削除ここまで) 89 bytes

You can define a function like this

f(){ n=$((1ドルn=$[1ドル-2));yes2];yes \ |head|sed -n$((2ドル*n))|tr$[2ドル*n]q|tr -d \\n|fold -w$n|sed 's/^\|$/|/g;1!{$!b};s/ /-/g';}

Or in a more readable format:

f() { 
 n=$((1ドル-2))
 
 # The next couple of lines create a rectangle of spaces
 # matching the desired size
 yes ' ' |
 head -n$((2ドル*n)) |
 tr -d '\n' |
 fold -w$n |
 # Add the pipes and dashes
 sed '
 s/^\|$/|/g # Replace first and last character by a pipe
 1! {$!b } # Do nothing if not on first or last line
 s/ /-/g # Add the dashes
 '
 echo
}

You can now say f 4 3:

|--|
| |
|--|

If you care about trailing new-line, add an echo at the end of function.

##bash and coreutils, 95 bytes

You can define a function like this

f(){ n=$((1ドル-2));yes \ |head -n$((2ドル*n))|tr -d \\n|fold -w$n|sed 's/^\|$/|/g;1!{$!b};s/ /-/g';}

Or in a more readable format:

f() { 
 n=$((1ドル-2))
 
 # The next couple of lines create a rectangle of spaces
 # matching the desired size
 yes ' ' |
 head -n$((2ドル*n)) |
 tr -d '\n' |
 fold -w$n |
 # Add the pipes and dashes
 sed '
 s/^\|$/|/g # Replace first and last character by a pipe
 1! {$!b } # Do nothing if not on first or last line
 s/ /-/g # Add the dashes
 '
 echo
}

You can now say f 4 3:

|--|
| |
|--|

If you care about trailing new-line, add an echo at the end of function.

##bash, sed and coreutils, (削除) 95 (削除ここまで) 89 bytes

You can define a function like this

f(){ n=$[1ドル-2];yes \ |sed $[2ドル*n]q|tr -d \\n|fold -w$n|sed 's/^\|$/|/g;1!{$!b};s/ /-/g';}

Or in a more readable format:

f() { 
 n=$((1ドル-2))
 
 # The next couple of lines create a rectangle of spaces
 # matching the desired size
 yes ' ' |
 head -n$((2ドル*n)) |
 tr -d '\n' |
 fold -w$n |
 # Add the pipes and dashes
 sed '
 s/^\|$/|/g # Replace first and last character by a pipe
 1! {$!b } # Do nothing if not on first or last line
 s/ /-/g # Add the dashes
 '
 echo
}

You can now say f 4 3:

|--|
| |
|--|

If you care about trailing new-line, add an echo at the end of function.

Added a commented version of the answer
Source Link
Thor
  • 2.9k
  • 2
  • 21
  • 24

##bash and coreutils, 95 bytes

You can define a function like this

f(){ n=$((1ドル-2));yes \ |head -n$((2ドル*n))|tr -d \\n|fold -w$n|sed 's/^\|$/|/g;1!{$!b};s/ /-/g';}

Or in a more readable format:

f() { 
 n=$((1ドル-2))
 
 # The next couple of lines create a rectangle of spaces
 # matching the desired size
 yes ' ' |
 head -n$((2ドル*n)) |
 tr -d '\n' |
 fold -w$n |
 # Add the pipes and dashes
 sed '
 s/^\|$/|/g # Replace first and last character by a pipe
 1! {$!b } # Do nothing if not on first or last line
 s/ /-/g # Add the dashes
 '
 echo
}

You can now say f 4 3:

|--|
| |
|--|

If you care about trailing new-line, add an echo at the end of function.

##bash and coreutils, 95 bytes

You can define a function like this

f(){ n=$((1ドル-2));yes \ |head -n$((2ドル*n))|tr -d \\n|fold -w$n|sed 's/^\|$/|/g;1!{$!b};s/ /-/g';}

You can now say f 4 3:

|--|
| |
|--|

If you care about trailing new-line, add an echo at the end of function.

##bash and coreutils, 95 bytes

You can define a function like this

f(){ n=$((1ドル-2));yes \ |head -n$((2ドル*n))|tr -d \\n|fold -w$n|sed 's/^\|$/|/g;1!{$!b};s/ /-/g';}

Or in a more readable format:

f() { 
 n=$((1ドル-2))
 
 # The next couple of lines create a rectangle of spaces
 # matching the desired size
 yes ' ' |
 head -n$((2ドル*n)) |
 tr -d '\n' |
 fold -w$n |
 # Add the pipes and dashes
 sed '
 s/^\|$/|/g # Replace first and last character by a pipe
 1! {$!b } # Do nothing if not on first or last line
 s/ /-/g # Add the dashes
 '
 echo
}

You can now say f 4 3:

|--|
| |
|--|

If you care about trailing new-line, add an echo at the end of function.

Source Link
Thor
  • 2.9k
  • 2
  • 21
  • 24
Loading

AltStyle によって変換されたページ (->オリジナル) /