Rotate a column
$begingroup$
Given an integer k and either a block of text, or a 2d array that the inner arrays may have unequal lengths (that resembles a block of text), rotate every character or element in the k-th column up or down to the next position that exists.
Example
Rotate the 20th column of the following text (1-based):
A line with more than k characters.
A longer line with more than k character.
A short line.
Rotate here: ------v--
This is long enough.
This is not enough.
Wrapping around to the first line.
Output:
A line with more thtn k characters.
A longer line with aore than k character.
A short line.
Rotate here: ------m--
This is long enoughv
This is not enough.
Wrapping around to .he first line.
Rotating the k-th column of the same input where 35 < k < 42 would yield the input text unchanged.
Rules
- You may use raw text, an array of lines, a 2d array of characters, or any reasonable format to represent the data. You may also use data types other than characters.
- The number of possible values of the data type of the elements must be at least 20 if your code length depends on it, otherwise at least 2. This could be a subset of the characters or other values supported in the native type.
- Spaces and any kind of null values are just normal values, if you allow them in the input. You may also simply exclude them in the element type.
Rule change: You are allowed to pad the shorter arrays with a generic default value (such as spaces), if you prefer using arrays with equal lengths to store the data.
k could be 0-based or 1-based. It is guaranteed to be inside the longest line in the input (implying the input has at least one non-empty line).- You may choose whether it rotates up or down.
- Either just rotate one position, or rotate n positions where n is a positive integer given in the input.
- Shortest code wins.
code-golf string array-manipulation
$endgroup$
|
show 19 more comments
$begingroup$
Given an integer k and either a block of text, or a 2d array that the inner arrays may have unequal lengths (that resembles a block of text), rotate every character or element in the k-th column up or down to the next position that exists.
Example
Rotate the 20th column of the following text (1-based):
A line with more than k characters.
A longer line with more than k character.
A short line.
Rotate here: ------v--
This is long enough.
This is not enough.
Wrapping around to the first line.
Output:
A line with more thtn k characters.
A longer line with aore than k character.
A short line.
Rotate here: ------m--
This is long enoughv
This is not enough.
Wrapping around to .he first line.
Rotating the k-th column of the same input where 35 < k < 42 would yield the input text unchanged.
Rules
- You may use raw text, an array of lines, a 2d array of characters, or any reasonable format to represent the data. You may also use data types other than characters.
- The number of possible values of the data type of the elements must be at least 20 if your code length depends on it, otherwise at least 2. This could be a subset of the characters or other values supported in the native type.
- Spaces and any kind of null values are just normal values, if you allow them in the input. You may also simply exclude them in the element type.
Rule change: You are allowed to pad the shorter arrays with a generic default value (such as spaces), if you prefer using arrays with equal lengths to store the data.
k could be 0-based or 1-based. It is guaranteed to be inside the longest line in the input (implying the input has at least one non-empty line).- You may choose whether it rotates up or down.
- Either just rotate one position, or rotate n positions where n is a positive integer given in the input.
- Shortest code wins.
code-golf string array-manipulation
$endgroup$
1
$begingroup$
May the output contain additional trailing spaces (so the input is as in your example, but the output is a rectangle)?
$endgroup$
– Kevin Cruijssen
2 days ago
1
$begingroup$
@KevinCruijssen Whoops :-)
$endgroup$
– Luis Mendo
2 days ago
$begingroup$
@KevinCruijssen No. But edited to explicitly allow disallowing spaces in the input, so you could do easy trimming. I may consider changing it if there is a strong reason. But I think this should be easy enough for most practical languages. And for golfing languages, there are usually invisible padding values (someone should invent one if not).
$endgroup$
– jimmy23013
2 days ago
1
$begingroup$
@Adám I'd say allowing trailing garbage isn't a normal thing to do. At best I'll allow some "reasonable representation" of the data. The problem is created by repeatedly relaxing the rules, not that it should be like this. The difficulty of working with unequal lengths (of sections in a useful text, which isn't expected to be padded) is the reason why I wrote this challenge. Actually the APL answer solves the problem in the most ideal way I could think of. But better if you could split the lines in enclosed form. If not, I'll try to decide in a day and leave its validity unclear for a while.
$endgroup$
– jimmy23013
2 days ago
5
$begingroup$
My thought process: "Ha! Simple! Transpose, rotate rowk
, transpose back ... ah, goddamnit!"
$endgroup$
– Shaggy
2 days ago
|
show 19 more comments
$begingroup$
Given an integer k and either a block of text, or a 2d array that the inner arrays may have unequal lengths (that resembles a block of text), rotate every character or element in the k-th column up or down to the next position that exists.
Example
Rotate the 20th column of the following text (1-based):
A line with more than k characters.
A longer line with more than k character.
A short line.
Rotate here: ------v--
This is long enough.
This is not enough.
Wrapping around to the first line.
Output:
A line with more thtn k characters.
A longer line with aore than k character.
A short line.
Rotate here: ------m--
This is long enoughv
This is not enough.
Wrapping around to .he first line.
Rotating the k-th column of the same input where 35 < k < 42 would yield the input text unchanged.
Rules
- You may use raw text, an array of lines, a 2d array of characters, or any reasonable format to represent the data. You may also use data types other than characters.
- The number of possible values of the data type of the elements must be at least 20 if your code length depends on it, otherwise at least 2. This could be a subset of the characters or other values supported in the native type.
- Spaces and any kind of null values are just normal values, if you allow them in the input. You may also simply exclude them in the element type.
Rule change: You are allowed to pad the shorter arrays with a generic default value (such as spaces), if you prefer using arrays with equal lengths to store the data.
k could be 0-based or 1-based. It is guaranteed to be inside the longest line in the input (implying the input has at least one non-empty line).- You may choose whether it rotates up or down.
- Either just rotate one position, or rotate n positions where n is a positive integer given in the input.
- Shortest code wins.
code-golf string array-manipulation
$endgroup$
Given an integer k and either a block of text, or a 2d array that the inner arrays may have unequal lengths (that resembles a block of text), rotate every character or element in the k-th column up or down to the next position that exists.
Example
Rotate the 20th column of the following text (1-based):
A line with more than k characters.
A longer line with more than k character.
A short line.
Rotate here: ------v--
This is long enough.
This is not enough.
Wrapping around to the first line.
Output:
A line with more thtn k characters.
A longer line with aore than k character.
A short line.
Rotate here: ------m--
This is long enoughv
This is not enough.
Wrapping around to .he first line.
Rotating the k-th column of the same input where 35 < k < 42 would yield the input text unchanged.
Rules
- You may use raw text, an array of lines, a 2d array of characters, or any reasonable format to represent the data. You may also use data types other than characters.
- The number of possible values of the data type of the elements must be at least 20 if your code length depends on it, otherwise at least 2. This could be a subset of the characters or other values supported in the native type.
- Spaces and any kind of null values are just normal values, if you allow them in the input. You may also simply exclude them in the element type.
Rule change: You are allowed to pad the shorter arrays with a generic default value (such as spaces), if you prefer using arrays with equal lengths to store the data.
k could be 0-based or 1-based. It is guaranteed to be inside the longest line in the input (implying the input has at least one non-empty line).- You may choose whether it rotates up or down.
- Either just rotate one position, or rotate n positions where n is a positive integer given in the input.
- Shortest code wins.
code-golf string array-manipulation
code-golf string array-manipulation
edited 2 days ago
jimmy23013
asked 2 days ago
jimmy23013jimmy23013
29.8k561129
29.8k561129
1
$begingroup$
May the output contain additional trailing spaces (so the input is as in your example, but the output is a rectangle)?
$endgroup$
– Kevin Cruijssen
2 days ago
1
$begingroup$
@KevinCruijssen Whoops :-)
$endgroup$
– Luis Mendo
2 days ago
$begingroup$
@KevinCruijssen No. But edited to explicitly allow disallowing spaces in the input, so you could do easy trimming. I may consider changing it if there is a strong reason. But I think this should be easy enough for most practical languages. And for golfing languages, there are usually invisible padding values (someone should invent one if not).
$endgroup$
– jimmy23013
2 days ago
1
$begingroup$
@Adám I'd say allowing trailing garbage isn't a normal thing to do. At best I'll allow some "reasonable representation" of the data. The problem is created by repeatedly relaxing the rules, not that it should be like this. The difficulty of working with unequal lengths (of sections in a useful text, which isn't expected to be padded) is the reason why I wrote this challenge. Actually the APL answer solves the problem in the most ideal way I could think of. But better if you could split the lines in enclosed form. If not, I'll try to decide in a day and leave its validity unclear for a while.
$endgroup$
– jimmy23013
2 days ago
5
$begingroup$
My thought process: "Ha! Simple! Transpose, rotate rowk
, transpose back ... ah, goddamnit!"
$endgroup$
– Shaggy
2 days ago
|
show 19 more comments
1
$begingroup$
May the output contain additional trailing spaces (so the input is as in your example, but the output is a rectangle)?
$endgroup$
– Kevin Cruijssen
2 days ago
1
$begingroup$
@KevinCruijssen Whoops :-)
$endgroup$
– Luis Mendo
2 days ago
$begingroup$
@KevinCruijssen No. But edited to explicitly allow disallowing spaces in the input, so you could do easy trimming. I may consider changing it if there is a strong reason. But I think this should be easy enough for most practical languages. And for golfing languages, there are usually invisible padding values (someone should invent one if not).
$endgroup$
– jimmy23013
2 days ago
1
$begingroup$
@Adám I'd say allowing trailing garbage isn't a normal thing to do. At best I'll allow some "reasonable representation" of the data. The problem is created by repeatedly relaxing the rules, not that it should be like this. The difficulty of working with unequal lengths (of sections in a useful text, which isn't expected to be padded) is the reason why I wrote this challenge. Actually the APL answer solves the problem in the most ideal way I could think of. But better if you could split the lines in enclosed form. If not, I'll try to decide in a day and leave its validity unclear for a while.
$endgroup$
– jimmy23013
2 days ago
5
$begingroup$
My thought process: "Ha! Simple! Transpose, rotate rowk
, transpose back ... ah, goddamnit!"
$endgroup$
– Shaggy
2 days ago
1
1
$begingroup$
May the output contain additional trailing spaces (so the input is as in your example, but the output is a rectangle)?
$endgroup$
– Kevin Cruijssen
2 days ago
$begingroup$
May the output contain additional trailing spaces (so the input is as in your example, but the output is a rectangle)?
$endgroup$
– Kevin Cruijssen
2 days ago
1
1
$begingroup$
@KevinCruijssen Whoops :-)
$endgroup$
– Luis Mendo
2 days ago
$begingroup$
@KevinCruijssen Whoops :-)
$endgroup$
– Luis Mendo
2 days ago
$begingroup$
@KevinCruijssen No. But edited to explicitly allow disallowing spaces in the input, so you could do easy trimming. I may consider changing it if there is a strong reason. But I think this should be easy enough for most practical languages. And for golfing languages, there are usually invisible padding values (someone should invent one if not).
$endgroup$
– jimmy23013
2 days ago
$begingroup$
@KevinCruijssen No. But edited to explicitly allow disallowing spaces in the input, so you could do easy trimming. I may consider changing it if there is a strong reason. But I think this should be easy enough for most practical languages. And for golfing languages, there are usually invisible padding values (someone should invent one if not).
$endgroup$
– jimmy23013
2 days ago
1
1
$begingroup$
@Adám I'd say allowing trailing garbage isn't a normal thing to do. At best I'll allow some "reasonable representation" of the data. The problem is created by repeatedly relaxing the rules, not that it should be like this. The difficulty of working with unequal lengths (of sections in a useful text, which isn't expected to be padded) is the reason why I wrote this challenge. Actually the APL answer solves the problem in the most ideal way I could think of. But better if you could split the lines in enclosed form. If not, I'll try to decide in a day and leave its validity unclear for a while.
$endgroup$
– jimmy23013
2 days ago
$begingroup$
@Adám I'd say allowing trailing garbage isn't a normal thing to do. At best I'll allow some "reasonable representation" of the data. The problem is created by repeatedly relaxing the rules, not that it should be like this. The difficulty of working with unequal lengths (of sections in a useful text, which isn't expected to be padded) is the reason why I wrote this challenge. Actually the APL answer solves the problem in the most ideal way I could think of. But better if you could split the lines in enclosed form. If not, I'll try to decide in a day and leave its validity unclear for a while.
$endgroup$
– jimmy23013
2 days ago
5
5
$begingroup$
My thought process: "Ha! Simple! Transpose, rotate row
k
, transpose back ... ah, goddamnit!"$endgroup$
– Shaggy
2 days ago
$begingroup$
My thought process: "Ha! Simple! Transpose, rotate row
k
, transpose back ... ah, goddamnit!"$endgroup$
– Shaggy
2 days ago
|
show 19 more comments
17 Answers
17
active
oldest
votes
$begingroup$
Python 2, 111 110 109 99 98 96 94 bytes
lambda a,n:[l[:n]+(l[n:]and[L[n]for L in a[i:]+a if L[n:]][1]+l[n+1:])for i,l in enumerate(a)]
Try it online!
Takes input as a list of lines and 0-index column, and returns a list of strings.
Column is rotated up 1.
-11 bytes, thanks to Jo King
$endgroup$
add a comment |
$begingroup$
APL (Dyalog Extended), 9 bytesSBCS
Full program. Prompts stdin for 2D block of text, then k (0-based or 1-based, depending on APL's current setting), then n. Positive n rotate up, negative n rotate down.
The domain consists of either one of the following:
all Unicode characters, except spaces, leaving 1114111 allowed values, which is more than the required 20.
all numbers, except 0, leaving approximately 2129 allowed values, which is more than the required 20.
Since APL requires 2D blocks to be rectangular, the input must be padded with spaces/zeros. This can be done automatically by entering ↑
to the left of a list of strings/numerical lists.
⎕⌽@≠@⎕⍢⍉⎕
Try it online! (the apparent spaces are actually non-breaking spaces)
⎕
prompt for text block
⍢⍉
while transposed:
@⎕
apply the following on the input'th row:
@≠
at elements different from their prototype (space for characters, zero for numbers):
⎕⌽
rotate "input" steps left
APL (Dyalog Unicode), 22+ bytesSBCS
This version allows the full character set by using zeros as identifiable fill element.
0~¨⍨↓⍉⎕⌽@(0≠⊢)@⎕⍉↑0,¨⎕
Try it online!
This of course means that zeros are not allowed in numeric arguments. The corresponding program for all numbers would have the three occurrences of 0
replaced by ' '
and thus use space as fill:
' '~¨⍨↓⍉⎕⌽@(' '≠⊢)@⎕⍉↑' ',¨⎕
If we truly want the full ranges of (even a mixture of) both characters and numbers, we could use null as fill:
n~¨⍨↓⍉⎕⌽@(n≠⊢)@⎕⍉↑⎕,¨⍨n←⎕NULL
And finally, if we wanted to include nulls and objects in the input domain, we could define a fill class and use instances of this as fills:
~∘I¨⍨↓⍉⎕⌽@(~⊢∊I←⎕INSTANCES⊢∘C)@⎕⍉↑⎕,¨⍨⎕NEW⎕FIX':Class C' ':EndClass'
$endgroup$
$begingroup$
Now officially declare this (and the new answers like this) as a valid answer. Sorry if this has bothered you too much.
$endgroup$
– jimmy23013
2 days ago
$begingroup$
Shorter than your null version:n~¨⍨↓⍉⎕⌽@(≢¨)@⎕⍉↑⎕,¨⍨n←⊂⊂⍬
. Maybe you could add modded↑
↓
in your extension to help further golfing (but I'm not exactly sure how useful they are).
$endgroup$
– jimmy23013
2 days ago
$begingroup$
@jimmy23013 Yeah, I thought of that, but then it can't take lists of arbitrarily nested stuff. The last one is the ultimate solution, especially ifC
andI
are localised so they don't pollute:{~∘I¨⍨↓⍉⎕⌽@(~⊢∊I←⎕INSTANCES⊢∘C)@⎕⍉↑⎕,¨⍨⎕NEW⎕FIX I←C←':Class C' ':EndClass'}
$endgroup$
– Adám
2 days ago
add a comment |
$begingroup$
Java 8, 107 106 135 107 bytes
k->m->{int s=m.length,i=-1;for(char p=0,t;i<s;t=m[i%s][k],m[i%s][k]=p<1?t:p,p=t)for(;m[++i%s].length<=k;);}
+29 bytes for a bug-fix..
0-indexed; rotates down like the example.
Input as a character-matrix; modifies the char-matrix instead of returning a new one to save bytes.
Try it online.
Explanation:
k->m->{ // Method with integer and char-matrix parameters and no return-type
int s=m.length, // Amount of lines in the matrix `s`
i=-1; // Index-integer `i`, starting at -1
for(char p=0, // Previous-character, starting at 0
t; // Temp-char, uninitialized
i<s // Loop as long as `i` is smaller than `s`:
; // After every iteration:
t=m[i%s][k], // Set the temp to the `k`th character of the `i`'th line
m[i%s][k]= // Replace the `k`'th character of the `i`'th line with:
p<1? // If `p` is still 0:
t // Set it to the temp we just set
: // Else:
p, // Set it to the previous-character instead
p=t) // And then replace `p` with the temp for the next iteration
for(;m[++i // Increase `i` by 1 before every iteration with `++i`,
%s].length // And continue this loop until the length of the `i`'th line
<=k;);} // is smaller than or equal to the input `k`
$endgroup$
1
$begingroup$
One thing I noticed is that you make an assumption that the first line has enough characters -char p=m[0][k]
- if it doesn't, won't this throw an exception? Nice job btw. You are beating my C# attempt by a large margin :)
$endgroup$
– dana
2 days ago
$begingroup$
@dana Ah, shit, you're right.. I'm probably beating you by this large of a margin because I accidentally make that assumption.. Will try to fix it.
$endgroup$
– Kevin Cruijssen
2 days ago
1
$begingroup$
@dana Fixed, thanks for noticing.
$endgroup$
– Kevin Cruijssen
2 days ago
2
$begingroup$
@dana And back to 107 again. ;)
$endgroup$
– Kevin Cruijssen
2 days ago
1
$begingroup$
@EmbodimentofIgnorance Ah, forgot to change thatn
tok
. I usually usen
for integer-inputs, but since the question uses $k$ I later on changed it (but apparently not everywhere). As forint p=0,t;
then I will need a cast to char when assigningp
to a cell (m[i%s][k]=(char)(p<1?t:p)
). And although I could take the input as an integer-matrix, I personally think int-matrix =/= string/string-list/char-matrix..
$endgroup$
– Kevin Cruijssen
yesterday
|
show 1 more comment
$begingroup$
Zsh, 94 87 78 bytes
-7 bytes by changing to an arithmetic ternary, -9 bytes by changing the character in-place. (TIL)
i=$1
shift
for s;c+=($s[i])
c=($c[-1] $c)
for s;s[i]=$c[$[$#s<i?0:++j]]&&<<<$s
Try it online!
Try it online!
Try it online!
Input is index [ string ... ]
So, my previous comment about the subshell printing is now outdated, but here it is with the old last line for posterity:
for s;echo $s[0,i-1]$c[$[$#s<i?0:++j]]${s:$i}
Hey, Gamma, why did you use
echo
in that last command instead of<<<
?
Wouldn't<<<
save you two bytes?
I'm glad you asked! Go ahead and give it a try. See if you can figure out what's happening.
(Hint: Run echo $ZSH_SUBSHELL; <<< $ZSH_SUBSHELL
.)
You get 0
for echo
, but 1
for <<<
.
Here-strings are run in subshells because they are given as stdin to another program.
If there is no program given, it is implicitly given to cat
.
You can see this with <<< $_
.
<<< $ZSH_SUBSHELL
is similar to echo $ZSH_SUBSHELL | cat
.
Since we need to increment j
, we can't be in a subshell.
Anyway, on to the main event:
i=$1
shift
for s
c+=($s[i]) # without "", empty strings aren't added
c=($c[-1] $c) # add the last element onto the start
for s
echo $s[0,i-1]$c[$[$#s<i?0:++j]]${s:$i}
$s[0,i-1] ${s:$i} # before and after the replacement point
$c[ ] # insert from char array
$[$#s<i?0 ] # Sting is too small: at index 0: empty string
$[$#s<i :++j] # Increment j and insert from there
# j is unset, so ++j initially sets j to 1
$endgroup$
add a comment |
$begingroup$
R, 62 bytes
function(L,n){L[A,n]<-rep(L[A<-L[,n]!=' ',n],2)[1+1:sum(A)];L}
Try it online!
Takes input as a space-padded matrix of characters. Rotates upwards. All thanks to Kirill L.!
R, 74 bytes
function(L,n){substr(L[A],n,n)=rep(substr(L[A<-nchar(L)>=n],n,n),2)[-1]
L}
Try it online!
This submission predates the allowance of padded lines.
Aliasing substr
here won't work because we're calling substr
and substr<-
in the first line.
I/O as a list of non-padded lines; rotates upwards.
$endgroup$
$begingroup$
Since consensus allows modifying inputs, we can do this crazy thing
$endgroup$
– Kirill L.
yesterday
$begingroup$
Also, OP relaxed the rules, so that it is now possible to work with space-padded matrices, something like this
$endgroup$
– Kirill L.
yesterday
$begingroup$
@KirillL. I don't think that R passes by reference so we probably aren't allowed to do that; it strikes me that this only works if the argument is namedL
in the parent environment. I'll happily update to the space-padding, though!
$endgroup$
– Giuseppe
yesterday
$begingroup$
Ah, I see, you're right. I came up with this idea looking at my Ruby answer (which saves a whole 1 byte this way :)), but the languages indeed behave differently in this situation.
$endgroup$
– Kirill L.
yesterday
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 85 bytes
k=>a=>{var c='0';a.Where(b=>b.Count>k&&((b[k],c)=(c,b[k]))+""!="").ToList()[0][k]=c;}
Try it online!
Credit to @ASCIIOnly for suggesting foreach
which led to a 12 byte savings!
-8 bytes thanks to @someone!
-1 byte thanks to @EmbodimentofIgnorance!
$endgroup$
$begingroup$
184 bytes
$endgroup$
– Expired Data
2 days ago
$begingroup$
Maybe 183 if using ints instead of chars
$endgroup$
– Expired Data
2 days ago
$begingroup$
@ExpiredData ew int instead of char
$endgroup$
– ASCII-only
yesterday
$begingroup$
too bad you can't use aforeach
since it's the opposite order :(
$endgroup$
– ASCII-only
yesterday
1
$begingroup$
Usedynamic
to merge declarations and save 2 bytes (I've never seen this before!)Try it online!
$endgroup$
– someone
20 hours ago
|
show 10 more comments
$begingroup$
Ruby, 57 bytes
->a,k{b=a.map{|i|i[k]}-[p];c=-2;a.map{|i|i[k]&&=b[c+=1]}}
Try it online!
Takes input as an array of lines a
. Rotates the text down at 0-based position k
. Returns by modifying the input a
.
$endgroup$
add a comment |
$begingroup$
05AB1E, 21 bytes
ʒg‹}U¹εXyk©diX®<èIèIǝ
Can definitely be golfed some more..
0-indexed; input and output both as a list of strings.
It rotates down like the example, but <
can be replaced with >
to rotate up instead.
Try it online (footer joins the list by newlines, remove it to see the actual list output).
Explanation:
ʒ } # Filter the (implicit) input-list by:
g # Where length of the current string
‹ # is larger than the (implicit) input-integer
U # Pop and store this filtered list in variable `X`
¹ε # Map over the first input-list again:
Xyk # Get the index of the current string in variable `X`
© # Store it in the register (without popping)
di # If the index is not -1, so the current string is present in variable `X`
X®<è # Get the (index-1)'th string in variable `X`
Iè # Get the character at the index of the input-integer
Iǝ # And insert it at the index of the input-integer in the current string
$endgroup$
add a comment |
$begingroup$
Japt v2.0a0, 18 bytes
0-based with input & output as a multi-line string. Rotates up by 1.
There's gotta be a shorter method!
yÈrS_Y¦V?Z:°TgXrS
Try it
yÈrS_Y¦V?Z:°TgXrS :Implicit input of string U & integer V
y :Transpose
È :Pass each line X at 0-based index Y through the following function & transpose back
r : Replace
S : RegEx /S/g
_ : Pass each match Z through the following function
Y¦V : Test Y for inequality with V
?Z: : If true, return Z, else
°T : Increment T (initially 0)
g : Index into
XrS : X with spaces removed
$endgroup$
$begingroup$
I haven't looked closely at the comments and new rules (so perhaps it's somewhere mentioned it's allowed now, while it wasn't before), but is it allowed to skip spaces of the initial input? Let's say you change the input-index to6
, your answer would skip the space atline with
in the first line and atRotate here
in the fourth line, whereas most of the other answers would rotate that space as well.
$endgroup$
– Kevin Cruijssen
yesterday
$begingroup$
@KevinCruijssen, I'm a bit confused by all the additional rules & allowances but, if it is an issue, it looks like I can get around that by not allowing spaces in the input. I think.
$endgroup$
– Shaggy
yesterday
$begingroup$
"I think" This part is indeed how I would also respond, haha. I have no idea what is mandatory and optional anymore, but I think you can indeed choose to omit spaces in the in- and output; I think you can choose to count spaces or not; and I think you can use trialing spaces in the output or not; etc. All on all rather confusing. Ah well..
$endgroup$
– Kevin Cruijssen
yesterday
add a comment |
$begingroup$
Jelly, 16 bytes
z⁷⁹ịỴFṙ-ṁƲY⁸ṛ⁹¦"
A dyadic Link accepting a list of lines (lists of characters containing no newline characters) on the left and an integer on the right which returns a list of lines.
Try it online! (footer splits on newlines, calls the Link, and joins by newlines again)
How?
z⁷⁹ịỴFṙ-ṁƲY⁸ṛ⁹¦" - Link: lines L; index I e.g. example in question; 20
z⁷ - transpose L with filler 'n' ['AAARTnTW', ' ohnhr', ...]
⁹ị - Ith item 'amnv.nnt'
Ỵ - split at newlines ['am', 'v.', '', 't']
Ʋ - last four links as a monad - i.e. f(X):
F - flatten 'amv.t'
- - -1
ṙ - rotate left by 'tamv.'
ṁ - mould like X ['ta', 'mv', '', '.']
Y - join with newlines 'tanmvnn.'
- -- call this C
⁸ - chain's left argument, L
" - zip with - i.e. [f(L1,C1), f(L2,C2), ...]:
¦ - sparse application...
⁹ - ...to indices: chain's right argument, I
ṛ - ...of: right argument, Cn
$endgroup$
add a comment |
$begingroup$
Charcoal, 34 28 bytes
θF²«Jη⁰Fθ«≔KKκF¬¬κ«FιPζ≔κζ»↓
Try it online! Link is to verbose version of code. Takes an array of strings as input. 0-indexed. This was way too hard, because performing operations on the result of PeekDirection
is at best inconsistent. Explanation:
θ
Print the input strings.
F²«
Repeat twice.
Jη⁰
Jump to the top of the column to be rotated.
Fθ«
Loop over each row.
≔KKκ
Read the current cell.
F¬¬κ«
If it's not empty, then...
FιPζ
... only on the second pass, overwrite with the previous non-empty cell's value...
≔κζ
... and save the value for the next cell/pass.
»↓
Move to the next row.
$endgroup$
add a comment |
$begingroup$
K4, 41 bytes
Solution:
{.[x;i;:;.q.rotate[1;x . i:(&y<#:'x;y)]]}
Explanation:
Not sure if I'm missing something... 0 index, rotates up (change the 1
to -1
for rotate down)
{.[x;i;:;.q.rotate[1;x . i:(&y<#:'x;y)]]} / the solution
{ } / lambda taking implicit x and y
.[x; ;:; ] / index apply assignment back into x
.q.rotate[1; ] / left rotate 1 character
( ; ) / two item list
y / index to rotate
#:'x / count (#:) each (') input
y< / index less than ?
& / indexes where true
i: / assign to variable w
x . / index into x
i / indexes we saved as i earlier
$endgroup$
add a comment |
$begingroup$
perl 5 (-p
), 75 bytes
k is 0-indexed, rotate down
s/.*//;$r="^.{$&}";s/
//;/$r(.)/;$c=$1;s/$rK.(?=(?s:.)*?$r(.)|)/$1||$c/gme
TIO
$endgroup$
add a comment |
$begingroup$
Perl 6, 38 33 bytes
Modifies the array in place (rules don't disallow)
{@^a.grep(*>$^b+1)[*;$b].=rotate}
Try it online!
$endgroup$
$begingroup$
@joking At first I thought storing it rather than outputting it would be a bit cheating, but rereading the rules, there's no actual stipulation to output so … hooray. Doing a (…)(…) to get 31 like you have definitely seems to be cheating though, since it's effectively hoisting a call out of the block, but it can still be done with 33 passing them simultaneously with a single call so I'm going go with that.
$endgroup$
– guifa
yesterday
$begingroup$
ah oops, i pasted the wrong link. you got what I mean to comment though. Currying input is perfectly fine though (though it doesn't work in this case), and modifying an argument passed by reference is a standard forn of allowed input
$endgroup$
– Jo King
yesterday
add a comment |
$begingroup$
JavaScript (Node.js), 52 bytes
k=>a=>a.filter(b=>b[k]&&([b[k],a]=[a,b[k]]))[0][k]=a
Try it online!
-7 bytes thanks to Shaggy!
Didn't see a JavaScript answer yet! Port of my C# answer.
$endgroup$
$begingroup$
63 bytes. Can probably be golfed further.
$endgroup$
– Shaggy
13 hours ago
$begingroup$
@Shaggy - nice :) most of the time I see JS answers doing all sorts of crazy things. Figured this time I'd give it a shot since I had a strategy.
$endgroup$
– dana
13 hours ago
$begingroup$
Oh, nicely golfed! :)
$endgroup$
– Shaggy
2 hours ago
add a comment |
$begingroup$
Pip -rn
, 32 bytes
POgY#(g@_)>aFI,#gFiyAE@ySsg@i@ag
0-indexed, rotates down. Try it online!
Filters to find the indices of all rows that are long enough to participate in the rotation. Then loops over those rows, swapping the appropriate character on each row with a temp variable s
. Visiting the first row again at the end swaps the dummy value back out again.
$endgroup$
add a comment |
$begingroup$
Jelly, 19 bytes
ZnÄ×$ịḟ¹ṙ-;ɗɗʋ€⁹¦⁶Z
Try it online!
1-indexed. Rotates down. A monadic link that takes a right-padded list of Jelly strings (a list of lists of characters) as the first argument and k as the second. Spaces are forbidden in the input except as right padding, but all other characters are permitted.
As implemented on TIO, the footer splits a single string input into a list of strings and right-pads it, but this is for convenience; as I understand it, the result of that step is permitted input for the main link per the rules.
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "200"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f182363%2frotate-a-column%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
17 Answers
17
active
oldest
votes
17 Answers
17
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Python 2, 111 110 109 99 98 96 94 bytes
lambda a,n:[l[:n]+(l[n:]and[L[n]for L in a[i:]+a if L[n:]][1]+l[n+1:])for i,l in enumerate(a)]
Try it online!
Takes input as a list of lines and 0-index column, and returns a list of strings.
Column is rotated up 1.
-11 bytes, thanks to Jo King
$endgroup$
add a comment |
$begingroup$
Python 2, 111 110 109 99 98 96 94 bytes
lambda a,n:[l[:n]+(l[n:]and[L[n]for L in a[i:]+a if L[n:]][1]+l[n+1:])for i,l in enumerate(a)]
Try it online!
Takes input as a list of lines and 0-index column, and returns a list of strings.
Column is rotated up 1.
-11 bytes, thanks to Jo King
$endgroup$
add a comment |
$begingroup$
Python 2, 111 110 109 99 98 96 94 bytes
lambda a,n:[l[:n]+(l[n:]and[L[n]for L in a[i:]+a if L[n:]][1]+l[n+1:])for i,l in enumerate(a)]
Try it online!
Takes input as a list of lines and 0-index column, and returns a list of strings.
Column is rotated up 1.
-11 bytes, thanks to Jo King
$endgroup$
Python 2, 111 110 109 99 98 96 94 bytes
lambda a,n:[l[:n]+(l[n:]and[L[n]for L in a[i:]+a if L[n:]][1]+l[n+1:])for i,l in enumerate(a)]
Try it online!
Takes input as a list of lines and 0-index column, and returns a list of strings.
Column is rotated up 1.
-11 bytes, thanks to Jo King
edited 2 days ago
answered 2 days ago
TFeldTFeld
16.3k21450
16.3k21450
add a comment |
add a comment |
$begingroup$
APL (Dyalog Extended), 9 bytesSBCS
Full program. Prompts stdin for 2D block of text, then k (0-based or 1-based, depending on APL's current setting), then n. Positive n rotate up, negative n rotate down.
The domain consists of either one of the following:
all Unicode characters, except spaces, leaving 1114111 allowed values, which is more than the required 20.
all numbers, except 0, leaving approximately 2129 allowed values, which is more than the required 20.
Since APL requires 2D blocks to be rectangular, the input must be padded with spaces/zeros. This can be done automatically by entering ↑
to the left of a list of strings/numerical lists.
⎕⌽@≠@⎕⍢⍉⎕
Try it online! (the apparent spaces are actually non-breaking spaces)
⎕
prompt for text block
⍢⍉
while transposed:
@⎕
apply the following on the input'th row:
@≠
at elements different from their prototype (space for characters, zero for numbers):
⎕⌽
rotate "input" steps left
APL (Dyalog Unicode), 22+ bytesSBCS
This version allows the full character set by using zeros as identifiable fill element.
0~¨⍨↓⍉⎕⌽@(0≠⊢)@⎕⍉↑0,¨⎕
Try it online!
This of course means that zeros are not allowed in numeric arguments. The corresponding program for all numbers would have the three occurrences of 0
replaced by ' '
and thus use space as fill:
' '~¨⍨↓⍉⎕⌽@(' '≠⊢)@⎕⍉↑' ',¨⎕
If we truly want the full ranges of (even a mixture of) both characters and numbers, we could use null as fill:
n~¨⍨↓⍉⎕⌽@(n≠⊢)@⎕⍉↑⎕,¨⍨n←⎕NULL
And finally, if we wanted to include nulls and objects in the input domain, we could define a fill class and use instances of this as fills:
~∘I¨⍨↓⍉⎕⌽@(~⊢∊I←⎕INSTANCES⊢∘C)@⎕⍉↑⎕,¨⍨⎕NEW⎕FIX':Class C' ':EndClass'
$endgroup$
$begingroup$
Now officially declare this (and the new answers like this) as a valid answer. Sorry if this has bothered you too much.
$endgroup$
– jimmy23013
2 days ago
$begingroup$
Shorter than your null version:n~¨⍨↓⍉⎕⌽@(≢¨)@⎕⍉↑⎕,¨⍨n←⊂⊂⍬
. Maybe you could add modded↑
↓
in your extension to help further golfing (but I'm not exactly sure how useful they are).
$endgroup$
– jimmy23013
2 days ago
$begingroup$
@jimmy23013 Yeah, I thought of that, but then it can't take lists of arbitrarily nested stuff. The last one is the ultimate solution, especially ifC
andI
are localised so they don't pollute:{~∘I¨⍨↓⍉⎕⌽@(~⊢∊I←⎕INSTANCES⊢∘C)@⎕⍉↑⎕,¨⍨⎕NEW⎕FIX I←C←':Class C' ':EndClass'}
$endgroup$
– Adám
2 days ago
add a comment |
$begingroup$
APL (Dyalog Extended), 9 bytesSBCS
Full program. Prompts stdin for 2D block of text, then k (0-based or 1-based, depending on APL's current setting), then n. Positive n rotate up, negative n rotate down.
The domain consists of either one of the following:
all Unicode characters, except spaces, leaving 1114111 allowed values, which is more than the required 20.
all numbers, except 0, leaving approximately 2129 allowed values, which is more than the required 20.
Since APL requires 2D blocks to be rectangular, the input must be padded with spaces/zeros. This can be done automatically by entering ↑
to the left of a list of strings/numerical lists.
⎕⌽@≠@⎕⍢⍉⎕
Try it online! (the apparent spaces are actually non-breaking spaces)
⎕
prompt for text block
⍢⍉
while transposed:
@⎕
apply the following on the input'th row:
@≠
at elements different from their prototype (space for characters, zero for numbers):
⎕⌽
rotate "input" steps left
APL (Dyalog Unicode), 22+ bytesSBCS
This version allows the full character set by using zeros as identifiable fill element.
0~¨⍨↓⍉⎕⌽@(0≠⊢)@⎕⍉↑0,¨⎕
Try it online!
This of course means that zeros are not allowed in numeric arguments. The corresponding program for all numbers would have the three occurrences of 0
replaced by ' '
and thus use space as fill:
' '~¨⍨↓⍉⎕⌽@(' '≠⊢)@⎕⍉↑' ',¨⎕
If we truly want the full ranges of (even a mixture of) both characters and numbers, we could use null as fill:
n~¨⍨↓⍉⎕⌽@(n≠⊢)@⎕⍉↑⎕,¨⍨n←⎕NULL
And finally, if we wanted to include nulls and objects in the input domain, we could define a fill class and use instances of this as fills:
~∘I¨⍨↓⍉⎕⌽@(~⊢∊I←⎕INSTANCES⊢∘C)@⎕⍉↑⎕,¨⍨⎕NEW⎕FIX':Class C' ':EndClass'
$endgroup$
$begingroup$
Now officially declare this (and the new answers like this) as a valid answer. Sorry if this has bothered you too much.
$endgroup$
– jimmy23013
2 days ago
$begingroup$
Shorter than your null version:n~¨⍨↓⍉⎕⌽@(≢¨)@⎕⍉↑⎕,¨⍨n←⊂⊂⍬
. Maybe you could add modded↑
↓
in your extension to help further golfing (but I'm not exactly sure how useful they are).
$endgroup$
– jimmy23013
2 days ago
$begingroup$
@jimmy23013 Yeah, I thought of that, but then it can't take lists of arbitrarily nested stuff. The last one is the ultimate solution, especially ifC
andI
are localised so they don't pollute:{~∘I¨⍨↓⍉⎕⌽@(~⊢∊I←⎕INSTANCES⊢∘C)@⎕⍉↑⎕,¨⍨⎕NEW⎕FIX I←C←':Class C' ':EndClass'}
$endgroup$
– Adám
2 days ago
add a comment |
$begingroup$
APL (Dyalog Extended), 9 bytesSBCS
Full program. Prompts stdin for 2D block of text, then k (0-based or 1-based, depending on APL's current setting), then n. Positive n rotate up, negative n rotate down.
The domain consists of either one of the following:
all Unicode characters, except spaces, leaving 1114111 allowed values, which is more than the required 20.
all numbers, except 0, leaving approximately 2129 allowed values, which is more than the required 20.
Since APL requires 2D blocks to be rectangular, the input must be padded with spaces/zeros. This can be done automatically by entering ↑
to the left of a list of strings/numerical lists.
⎕⌽@≠@⎕⍢⍉⎕
Try it online! (the apparent spaces are actually non-breaking spaces)
⎕
prompt for text block
⍢⍉
while transposed:
@⎕
apply the following on the input'th row:
@≠
at elements different from their prototype (space for characters, zero for numbers):
⎕⌽
rotate "input" steps left
APL (Dyalog Unicode), 22+ bytesSBCS
This version allows the full character set by using zeros as identifiable fill element.
0~¨⍨↓⍉⎕⌽@(0≠⊢)@⎕⍉↑0,¨⎕
Try it online!
This of course means that zeros are not allowed in numeric arguments. The corresponding program for all numbers would have the three occurrences of 0
replaced by ' '
and thus use space as fill:
' '~¨⍨↓⍉⎕⌽@(' '≠⊢)@⎕⍉↑' ',¨⎕
If we truly want the full ranges of (even a mixture of) both characters and numbers, we could use null as fill:
n~¨⍨↓⍉⎕⌽@(n≠⊢)@⎕⍉↑⎕,¨⍨n←⎕NULL
And finally, if we wanted to include nulls and objects in the input domain, we could define a fill class and use instances of this as fills:
~∘I¨⍨↓⍉⎕⌽@(~⊢∊I←⎕INSTANCES⊢∘C)@⎕⍉↑⎕,¨⍨⎕NEW⎕FIX':Class C' ':EndClass'
$endgroup$
APL (Dyalog Extended), 9 bytesSBCS
Full program. Prompts stdin for 2D block of text, then k (0-based or 1-based, depending on APL's current setting), then n. Positive n rotate up, negative n rotate down.
The domain consists of either one of the following:
all Unicode characters, except spaces, leaving 1114111 allowed values, which is more than the required 20.
all numbers, except 0, leaving approximately 2129 allowed values, which is more than the required 20.
Since APL requires 2D blocks to be rectangular, the input must be padded with spaces/zeros. This can be done automatically by entering ↑
to the left of a list of strings/numerical lists.
⎕⌽@≠@⎕⍢⍉⎕
Try it online! (the apparent spaces are actually non-breaking spaces)
⎕
prompt for text block
⍢⍉
while transposed:
@⎕
apply the following on the input'th row:
@≠
at elements different from their prototype (space for characters, zero for numbers):
⎕⌽
rotate "input" steps left
APL (Dyalog Unicode), 22+ bytesSBCS
This version allows the full character set by using zeros as identifiable fill element.
0~¨⍨↓⍉⎕⌽@(0≠⊢)@⎕⍉↑0,¨⎕
Try it online!
This of course means that zeros are not allowed in numeric arguments. The corresponding program for all numbers would have the three occurrences of 0
replaced by ' '
and thus use space as fill:
' '~¨⍨↓⍉⎕⌽@(' '≠⊢)@⎕⍉↑' ',¨⎕
If we truly want the full ranges of (even a mixture of) both characters and numbers, we could use null as fill:
n~¨⍨↓⍉⎕⌽@(n≠⊢)@⎕⍉↑⎕,¨⍨n←⎕NULL
And finally, if we wanted to include nulls and objects in the input domain, we could define a fill class and use instances of this as fills:
~∘I¨⍨↓⍉⎕⌽@(~⊢∊I←⎕INSTANCES⊢∘C)@⎕⍉↑⎕,¨⍨⎕NEW⎕FIX':Class C' ':EndClass'
edited 2 days ago
answered 2 days ago
AdámAdám
28.8k276207
28.8k276207
$begingroup$
Now officially declare this (and the new answers like this) as a valid answer. Sorry if this has bothered you too much.
$endgroup$
– jimmy23013
2 days ago
$begingroup$
Shorter than your null version:n~¨⍨↓⍉⎕⌽@(≢¨)@⎕⍉↑⎕,¨⍨n←⊂⊂⍬
. Maybe you could add modded↑
↓
in your extension to help further golfing (but I'm not exactly sure how useful they are).
$endgroup$
– jimmy23013
2 days ago
$begingroup$
@jimmy23013 Yeah, I thought of that, but then it can't take lists of arbitrarily nested stuff. The last one is the ultimate solution, especially ifC
andI
are localised so they don't pollute:{~∘I¨⍨↓⍉⎕⌽@(~⊢∊I←⎕INSTANCES⊢∘C)@⎕⍉↑⎕,¨⍨⎕NEW⎕FIX I←C←':Class C' ':EndClass'}
$endgroup$
– Adám
2 days ago
add a comment |
$begingroup$
Now officially declare this (and the new answers like this) as a valid answer. Sorry if this has bothered you too much.
$endgroup$
– jimmy23013
2 days ago
$begingroup$
Shorter than your null version:n~¨⍨↓⍉⎕⌽@(≢¨)@⎕⍉↑⎕,¨⍨n←⊂⊂⍬
. Maybe you could add modded↑
↓
in your extension to help further golfing (but I'm not exactly sure how useful they are).
$endgroup$
– jimmy23013
2 days ago
$begingroup$
@jimmy23013 Yeah, I thought of that, but then it can't take lists of arbitrarily nested stuff. The last one is the ultimate solution, especially ifC
andI
are localised so they don't pollute:{~∘I¨⍨↓⍉⎕⌽@(~⊢∊I←⎕INSTANCES⊢∘C)@⎕⍉↑⎕,¨⍨⎕NEW⎕FIX I←C←':Class C' ':EndClass'}
$endgroup$
– Adám
2 days ago
$begingroup$
Now officially declare this (and the new answers like this) as a valid answer. Sorry if this has bothered you too much.
$endgroup$
– jimmy23013
2 days ago
$begingroup$
Now officially declare this (and the new answers like this) as a valid answer. Sorry if this has bothered you too much.
$endgroup$
– jimmy23013
2 days ago
$begingroup$
Shorter than your null version:
n~¨⍨↓⍉⎕⌽@(≢¨)@⎕⍉↑⎕,¨⍨n←⊂⊂⍬
. Maybe you could add modded ↑
↓
in your extension to help further golfing (but I'm not exactly sure how useful they are).$endgroup$
– jimmy23013
2 days ago
$begingroup$
Shorter than your null version:
n~¨⍨↓⍉⎕⌽@(≢¨)@⎕⍉↑⎕,¨⍨n←⊂⊂⍬
. Maybe you could add modded ↑
↓
in your extension to help further golfing (but I'm not exactly sure how useful they are).$endgroup$
– jimmy23013
2 days ago
$begingroup$
@jimmy23013 Yeah, I thought of that, but then it can't take lists of arbitrarily nested stuff. The last one is the ultimate solution, especially if
C
and I
are localised so they don't pollute: {~∘I¨⍨↓⍉⎕⌽@(~⊢∊I←⎕INSTANCES⊢∘C)@⎕⍉↑⎕,¨⍨⎕NEW⎕FIX I←C←':Class C' ':EndClass'}
$endgroup$
– Adám
2 days ago
$begingroup$
@jimmy23013 Yeah, I thought of that, but then it can't take lists of arbitrarily nested stuff. The last one is the ultimate solution, especially if
C
and I
are localised so they don't pollute: {~∘I¨⍨↓⍉⎕⌽@(~⊢∊I←⎕INSTANCES⊢∘C)@⎕⍉↑⎕,¨⍨⎕NEW⎕FIX I←C←':Class C' ':EndClass'}
$endgroup$
– Adám
2 days ago
add a comment |
$begingroup$
Java 8, 107 106 135 107 bytes
k->m->{int s=m.length,i=-1;for(char p=0,t;i<s;t=m[i%s][k],m[i%s][k]=p<1?t:p,p=t)for(;m[++i%s].length<=k;);}
+29 bytes for a bug-fix..
0-indexed; rotates down like the example.
Input as a character-matrix; modifies the char-matrix instead of returning a new one to save bytes.
Try it online.
Explanation:
k->m->{ // Method with integer and char-matrix parameters and no return-type
int s=m.length, // Amount of lines in the matrix `s`
i=-1; // Index-integer `i`, starting at -1
for(char p=0, // Previous-character, starting at 0
t; // Temp-char, uninitialized
i<s // Loop as long as `i` is smaller than `s`:
; // After every iteration:
t=m[i%s][k], // Set the temp to the `k`th character of the `i`'th line
m[i%s][k]= // Replace the `k`'th character of the `i`'th line with:
p<1? // If `p` is still 0:
t // Set it to the temp we just set
: // Else:
p, // Set it to the previous-character instead
p=t) // And then replace `p` with the temp for the next iteration
for(;m[++i // Increase `i` by 1 before every iteration with `++i`,
%s].length // And continue this loop until the length of the `i`'th line
<=k;);} // is smaller than or equal to the input `k`
$endgroup$
1
$begingroup$
One thing I noticed is that you make an assumption that the first line has enough characters -char p=m[0][k]
- if it doesn't, won't this throw an exception? Nice job btw. You are beating my C# attempt by a large margin :)
$endgroup$
– dana
2 days ago
$begingroup$
@dana Ah, shit, you're right.. I'm probably beating you by this large of a margin because I accidentally make that assumption.. Will try to fix it.
$endgroup$
– Kevin Cruijssen
2 days ago
1
$begingroup$
@dana Fixed, thanks for noticing.
$endgroup$
– Kevin Cruijssen
2 days ago
2
$begingroup$
@dana And back to 107 again. ;)
$endgroup$
– Kevin Cruijssen
2 days ago
1
$begingroup$
@EmbodimentofIgnorance Ah, forgot to change thatn
tok
. I usually usen
for integer-inputs, but since the question uses $k$ I later on changed it (but apparently not everywhere). As forint p=0,t;
then I will need a cast to char when assigningp
to a cell (m[i%s][k]=(char)(p<1?t:p)
). And although I could take the input as an integer-matrix, I personally think int-matrix =/= string/string-list/char-matrix..
$endgroup$
– Kevin Cruijssen
yesterday
|
show 1 more comment
$begingroup$
Java 8, 107 106 135 107 bytes
k->m->{int s=m.length,i=-1;for(char p=0,t;i<s;t=m[i%s][k],m[i%s][k]=p<1?t:p,p=t)for(;m[++i%s].length<=k;);}
+29 bytes for a bug-fix..
0-indexed; rotates down like the example.
Input as a character-matrix; modifies the char-matrix instead of returning a new one to save bytes.
Try it online.
Explanation:
k->m->{ // Method with integer and char-matrix parameters and no return-type
int s=m.length, // Amount of lines in the matrix `s`
i=-1; // Index-integer `i`, starting at -1
for(char p=0, // Previous-character, starting at 0
t; // Temp-char, uninitialized
i<s // Loop as long as `i` is smaller than `s`:
; // After every iteration:
t=m[i%s][k], // Set the temp to the `k`th character of the `i`'th line
m[i%s][k]= // Replace the `k`'th character of the `i`'th line with:
p<1? // If `p` is still 0:
t // Set it to the temp we just set
: // Else:
p, // Set it to the previous-character instead
p=t) // And then replace `p` with the temp for the next iteration
for(;m[++i // Increase `i` by 1 before every iteration with `++i`,
%s].length // And continue this loop until the length of the `i`'th line
<=k;);} // is smaller than or equal to the input `k`
$endgroup$
1
$begingroup$
One thing I noticed is that you make an assumption that the first line has enough characters -char p=m[0][k]
- if it doesn't, won't this throw an exception? Nice job btw. You are beating my C# attempt by a large margin :)
$endgroup$
– dana
2 days ago
$begingroup$
@dana Ah, shit, you're right.. I'm probably beating you by this large of a margin because I accidentally make that assumption.. Will try to fix it.
$endgroup$
– Kevin Cruijssen
2 days ago
1
$begingroup$
@dana Fixed, thanks for noticing.
$endgroup$
– Kevin Cruijssen
2 days ago
2
$begingroup$
@dana And back to 107 again. ;)
$endgroup$
– Kevin Cruijssen
2 days ago
1
$begingroup$
@EmbodimentofIgnorance Ah, forgot to change thatn
tok
. I usually usen
for integer-inputs, but since the question uses $k$ I later on changed it (but apparently not everywhere). As forint p=0,t;
then I will need a cast to char when assigningp
to a cell (m[i%s][k]=(char)(p<1?t:p)
). And although I could take the input as an integer-matrix, I personally think int-matrix =/= string/string-list/char-matrix..
$endgroup$
– Kevin Cruijssen
yesterday
|
show 1 more comment
$begingroup$
Java 8, 107 106 135 107 bytes
k->m->{int s=m.length,i=-1;for(char p=0,t;i<s;t=m[i%s][k],m[i%s][k]=p<1?t:p,p=t)for(;m[++i%s].length<=k;);}
+29 bytes for a bug-fix..
0-indexed; rotates down like the example.
Input as a character-matrix; modifies the char-matrix instead of returning a new one to save bytes.
Try it online.
Explanation:
k->m->{ // Method with integer and char-matrix parameters and no return-type
int s=m.length, // Amount of lines in the matrix `s`
i=-1; // Index-integer `i`, starting at -1
for(char p=0, // Previous-character, starting at 0
t; // Temp-char, uninitialized
i<s // Loop as long as `i` is smaller than `s`:
; // After every iteration:
t=m[i%s][k], // Set the temp to the `k`th character of the `i`'th line
m[i%s][k]= // Replace the `k`'th character of the `i`'th line with:
p<1? // If `p` is still 0:
t // Set it to the temp we just set
: // Else:
p, // Set it to the previous-character instead
p=t) // And then replace `p` with the temp for the next iteration
for(;m[++i // Increase `i` by 1 before every iteration with `++i`,
%s].length // And continue this loop until the length of the `i`'th line
<=k;);} // is smaller than or equal to the input `k`
$endgroup$
Java 8, 107 106 135 107 bytes
k->m->{int s=m.length,i=-1;for(char p=0,t;i<s;t=m[i%s][k],m[i%s][k]=p<1?t:p,p=t)for(;m[++i%s].length<=k;);}
+29 bytes for a bug-fix..
0-indexed; rotates down like the example.
Input as a character-matrix; modifies the char-matrix instead of returning a new one to save bytes.
Try it online.
Explanation:
k->m->{ // Method with integer and char-matrix parameters and no return-type
int s=m.length, // Amount of lines in the matrix `s`
i=-1; // Index-integer `i`, starting at -1
for(char p=0, // Previous-character, starting at 0
t; // Temp-char, uninitialized
i<s // Loop as long as `i` is smaller than `s`:
; // After every iteration:
t=m[i%s][k], // Set the temp to the `k`th character of the `i`'th line
m[i%s][k]= // Replace the `k`'th character of the `i`'th line with:
p<1? // If `p` is still 0:
t // Set it to the temp we just set
: // Else:
p, // Set it to the previous-character instead
p=t) // And then replace `p` with the temp for the next iteration
for(;m[++i // Increase `i` by 1 before every iteration with `++i`,
%s].length // And continue this loop until the length of the `i`'th line
<=k;);} // is smaller than or equal to the input `k`
edited yesterday
answered 2 days ago
Kevin CruijssenKevin Cruijssen
41.9k569217
41.9k569217
1
$begingroup$
One thing I noticed is that you make an assumption that the first line has enough characters -char p=m[0][k]
- if it doesn't, won't this throw an exception? Nice job btw. You are beating my C# attempt by a large margin :)
$endgroup$
– dana
2 days ago
$begingroup$
@dana Ah, shit, you're right.. I'm probably beating you by this large of a margin because I accidentally make that assumption.. Will try to fix it.
$endgroup$
– Kevin Cruijssen
2 days ago
1
$begingroup$
@dana Fixed, thanks for noticing.
$endgroup$
– Kevin Cruijssen
2 days ago
2
$begingroup$
@dana And back to 107 again. ;)
$endgroup$
– Kevin Cruijssen
2 days ago
1
$begingroup$
@EmbodimentofIgnorance Ah, forgot to change thatn
tok
. I usually usen
for integer-inputs, but since the question uses $k$ I later on changed it (but apparently not everywhere). As forint p=0,t;
then I will need a cast to char when assigningp
to a cell (m[i%s][k]=(char)(p<1?t:p)
). And although I could take the input as an integer-matrix, I personally think int-matrix =/= string/string-list/char-matrix..
$endgroup$
– Kevin Cruijssen
yesterday
|
show 1 more comment
1
$begingroup$
One thing I noticed is that you make an assumption that the first line has enough characters -char p=m[0][k]
- if it doesn't, won't this throw an exception? Nice job btw. You are beating my C# attempt by a large margin :)
$endgroup$
– dana
2 days ago
$begingroup$
@dana Ah, shit, you're right.. I'm probably beating you by this large of a margin because I accidentally make that assumption.. Will try to fix it.
$endgroup$
– Kevin Cruijssen
2 days ago
1
$begingroup$
@dana Fixed, thanks for noticing.
$endgroup$
– Kevin Cruijssen
2 days ago
2
$begingroup$
@dana And back to 107 again. ;)
$endgroup$
– Kevin Cruijssen
2 days ago
1
$begingroup$
@EmbodimentofIgnorance Ah, forgot to change thatn
tok
. I usually usen
for integer-inputs, but since the question uses $k$ I later on changed it (but apparently not everywhere). As forint p=0,t;
then I will need a cast to char when assigningp
to a cell (m[i%s][k]=(char)(p<1?t:p)
). And although I could take the input as an integer-matrix, I personally think int-matrix =/= string/string-list/char-matrix..
$endgroup$
– Kevin Cruijssen
yesterday
1
1
$begingroup$
One thing I noticed is that you make an assumption that the first line has enough characters -
char p=m[0][k]
- if it doesn't, won't this throw an exception? Nice job btw. You are beating my C# attempt by a large margin :)$endgroup$
– dana
2 days ago
$begingroup$
One thing I noticed is that you make an assumption that the first line has enough characters -
char p=m[0][k]
- if it doesn't, won't this throw an exception? Nice job btw. You are beating my C# attempt by a large margin :)$endgroup$
– dana
2 days ago
$begingroup$
@dana Ah, shit, you're right.. I'm probably beating you by this large of a margin because I accidentally make that assumption.. Will try to fix it.
$endgroup$
– Kevin Cruijssen
2 days ago
$begingroup$
@dana Ah, shit, you're right.. I'm probably beating you by this large of a margin because I accidentally make that assumption.. Will try to fix it.
$endgroup$
– Kevin Cruijssen
2 days ago
1
1
$begingroup$
@dana Fixed, thanks for noticing.
$endgroup$
– Kevin Cruijssen
2 days ago
$begingroup$
@dana Fixed, thanks for noticing.
$endgroup$
– Kevin Cruijssen
2 days ago
2
2
$begingroup$
@dana And back to 107 again. ;)
$endgroup$
– Kevin Cruijssen
2 days ago
$begingroup$
@dana And back to 107 again. ;)
$endgroup$
– Kevin Cruijssen
2 days ago
1
1
$begingroup$
@EmbodimentofIgnorance Ah, forgot to change that
n
to k
. I usually use n
for integer-inputs, but since the question uses $k$ I later on changed it (but apparently not everywhere). As for int p=0,t;
then I will need a cast to char when assigning p
to a cell (m[i%s][k]=(char)(p<1?t:p)
). And although I could take the input as an integer-matrix, I personally think int-matrix =/= string/string-list/char-matrix..$endgroup$
– Kevin Cruijssen
yesterday
$begingroup$
@EmbodimentofIgnorance Ah, forgot to change that
n
to k
. I usually use n
for integer-inputs, but since the question uses $k$ I later on changed it (but apparently not everywhere). As for int p=0,t;
then I will need a cast to char when assigning p
to a cell (m[i%s][k]=(char)(p<1?t:p)
). And although I could take the input as an integer-matrix, I personally think int-matrix =/= string/string-list/char-matrix..$endgroup$
– Kevin Cruijssen
yesterday
|
show 1 more comment
$begingroup$
Zsh, 94 87 78 bytes
-7 bytes by changing to an arithmetic ternary, -9 bytes by changing the character in-place. (TIL)
i=$1
shift
for s;c+=($s[i])
c=($c[-1] $c)
for s;s[i]=$c[$[$#s<i?0:++j]]&&<<<$s
Try it online!
Try it online!
Try it online!
Input is index [ string ... ]
So, my previous comment about the subshell printing is now outdated, but here it is with the old last line for posterity:
for s;echo $s[0,i-1]$c[$[$#s<i?0:++j]]${s:$i}
Hey, Gamma, why did you use
echo
in that last command instead of<<<
?
Wouldn't<<<
save you two bytes?
I'm glad you asked! Go ahead and give it a try. See if you can figure out what's happening.
(Hint: Run echo $ZSH_SUBSHELL; <<< $ZSH_SUBSHELL
.)
You get 0
for echo
, but 1
for <<<
.
Here-strings are run in subshells because they are given as stdin to another program.
If there is no program given, it is implicitly given to cat
.
You can see this with <<< $_
.
<<< $ZSH_SUBSHELL
is similar to echo $ZSH_SUBSHELL | cat
.
Since we need to increment j
, we can't be in a subshell.
Anyway, on to the main event:
i=$1
shift
for s
c+=($s[i]) # without "", empty strings aren't added
c=($c[-1] $c) # add the last element onto the start
for s
echo $s[0,i-1]$c[$[$#s<i?0:++j]]${s:$i}
$s[0,i-1] ${s:$i} # before and after the replacement point
$c[ ] # insert from char array
$[$#s<i?0 ] # Sting is too small: at index 0: empty string
$[$#s<i :++j] # Increment j and insert from there
# j is unset, so ++j initially sets j to 1
$endgroup$
add a comment |
$begingroup$
Zsh, 94 87 78 bytes
-7 bytes by changing to an arithmetic ternary, -9 bytes by changing the character in-place. (TIL)
i=$1
shift
for s;c+=($s[i])
c=($c[-1] $c)
for s;s[i]=$c[$[$#s<i?0:++j]]&&<<<$s
Try it online!
Try it online!
Try it online!
Input is index [ string ... ]
So, my previous comment about the subshell printing is now outdated, but here it is with the old last line for posterity:
for s;echo $s[0,i-1]$c[$[$#s<i?0:++j]]${s:$i}
Hey, Gamma, why did you use
echo
in that last command instead of<<<
?
Wouldn't<<<
save you two bytes?
I'm glad you asked! Go ahead and give it a try. See if you can figure out what's happening.
(Hint: Run echo $ZSH_SUBSHELL; <<< $ZSH_SUBSHELL
.)
You get 0
for echo
, but 1
for <<<
.
Here-strings are run in subshells because they are given as stdin to another program.
If there is no program given, it is implicitly given to cat
.
You can see this with <<< $_
.
<<< $ZSH_SUBSHELL
is similar to echo $ZSH_SUBSHELL | cat
.
Since we need to increment j
, we can't be in a subshell.
Anyway, on to the main event:
i=$1
shift
for s
c+=($s[i]) # without "", empty strings aren't added
c=($c[-1] $c) # add the last element onto the start
for s
echo $s[0,i-1]$c[$[$#s<i?0:++j]]${s:$i}
$s[0,i-1] ${s:$i} # before and after the replacement point
$c[ ] # insert from char array
$[$#s<i?0 ] # Sting is too small: at index 0: empty string
$[$#s<i :++j] # Increment j and insert from there
# j is unset, so ++j initially sets j to 1
$endgroup$
add a comment |
$begingroup$
Zsh, 94 87 78 bytes
-7 bytes by changing to an arithmetic ternary, -9 bytes by changing the character in-place. (TIL)
i=$1
shift
for s;c+=($s[i])
c=($c[-1] $c)
for s;s[i]=$c[$[$#s<i?0:++j]]&&<<<$s
Try it online!
Try it online!
Try it online!
Input is index [ string ... ]
So, my previous comment about the subshell printing is now outdated, but here it is with the old last line for posterity:
for s;echo $s[0,i-1]$c[$[$#s<i?0:++j]]${s:$i}
Hey, Gamma, why did you use
echo
in that last command instead of<<<
?
Wouldn't<<<
save you two bytes?
I'm glad you asked! Go ahead and give it a try. See if you can figure out what's happening.
(Hint: Run echo $ZSH_SUBSHELL; <<< $ZSH_SUBSHELL
.)
You get 0
for echo
, but 1
for <<<
.
Here-strings are run in subshells because they are given as stdin to another program.
If there is no program given, it is implicitly given to cat
.
You can see this with <<< $_
.
<<< $ZSH_SUBSHELL
is similar to echo $ZSH_SUBSHELL | cat
.
Since we need to increment j
, we can't be in a subshell.
Anyway, on to the main event:
i=$1
shift
for s
c+=($s[i]) # without "", empty strings aren't added
c=($c[-1] $c) # add the last element onto the start
for s
echo $s[0,i-1]$c[$[$#s<i?0:++j]]${s:$i}
$s[0,i-1] ${s:$i} # before and after the replacement point
$c[ ] # insert from char array
$[$#s<i?0 ] # Sting is too small: at index 0: empty string
$[$#s<i :++j] # Increment j and insert from there
# j is unset, so ++j initially sets j to 1
$endgroup$
Zsh, 94 87 78 bytes
-7 bytes by changing to an arithmetic ternary, -9 bytes by changing the character in-place. (TIL)
i=$1
shift
for s;c+=($s[i])
c=($c[-1] $c)
for s;s[i]=$c[$[$#s<i?0:++j]]&&<<<$s
Try it online!
Try it online!
Try it online!
Input is index [ string ... ]
So, my previous comment about the subshell printing is now outdated, but here it is with the old last line for posterity:
for s;echo $s[0,i-1]$c[$[$#s<i?0:++j]]${s:$i}
Hey, Gamma, why did you use
echo
in that last command instead of<<<
?
Wouldn't<<<
save you two bytes?
I'm glad you asked! Go ahead and give it a try. See if you can figure out what's happening.
(Hint: Run echo $ZSH_SUBSHELL; <<< $ZSH_SUBSHELL
.)
You get 0
for echo
, but 1
for <<<
.
Here-strings are run in subshells because they are given as stdin to another program.
If there is no program given, it is implicitly given to cat
.
You can see this with <<< $_
.
<<< $ZSH_SUBSHELL
is similar to echo $ZSH_SUBSHELL | cat
.
Since we need to increment j
, we can't be in a subshell.
Anyway, on to the main event:
i=$1
shift
for s
c+=($s[i]) # without "", empty strings aren't added
c=($c[-1] $c) # add the last element onto the start
for s
echo $s[0,i-1]$c[$[$#s<i?0:++j]]${s:$i}
$s[0,i-1] ${s:$i} # before and after the replacement point
$c[ ] # insert from char array
$[$#s<i?0 ] # Sting is too small: at index 0: empty string
$[$#s<i :++j] # Increment j and insert from there
# j is unset, so ++j initially sets j to 1
edited 10 hours ago
answered 2 days ago
GammaFunctionGammaFunction
2516
2516
add a comment |
add a comment |
$begingroup$
R, 62 bytes
function(L,n){L[A,n]<-rep(L[A<-L[,n]!=' ',n],2)[1+1:sum(A)];L}
Try it online!
Takes input as a space-padded matrix of characters. Rotates upwards. All thanks to Kirill L.!
R, 74 bytes
function(L,n){substr(L[A],n,n)=rep(substr(L[A<-nchar(L)>=n],n,n),2)[-1]
L}
Try it online!
This submission predates the allowance of padded lines.
Aliasing substr
here won't work because we're calling substr
and substr<-
in the first line.
I/O as a list of non-padded lines; rotates upwards.
$endgroup$
$begingroup$
Since consensus allows modifying inputs, we can do this crazy thing
$endgroup$
– Kirill L.
yesterday
$begingroup$
Also, OP relaxed the rules, so that it is now possible to work with space-padded matrices, something like this
$endgroup$
– Kirill L.
yesterday
$begingroup$
@KirillL. I don't think that R passes by reference so we probably aren't allowed to do that; it strikes me that this only works if the argument is namedL
in the parent environment. I'll happily update to the space-padding, though!
$endgroup$
– Giuseppe
yesterday
$begingroup$
Ah, I see, you're right. I came up with this idea looking at my Ruby answer (which saves a whole 1 byte this way :)), but the languages indeed behave differently in this situation.
$endgroup$
– Kirill L.
yesterday
add a comment |
$begingroup$
R, 62 bytes
function(L,n){L[A,n]<-rep(L[A<-L[,n]!=' ',n],2)[1+1:sum(A)];L}
Try it online!
Takes input as a space-padded matrix of characters. Rotates upwards. All thanks to Kirill L.!
R, 74 bytes
function(L,n){substr(L[A],n,n)=rep(substr(L[A<-nchar(L)>=n],n,n),2)[-1]
L}
Try it online!
This submission predates the allowance of padded lines.
Aliasing substr
here won't work because we're calling substr
and substr<-
in the first line.
I/O as a list of non-padded lines; rotates upwards.
$endgroup$
$begingroup$
Since consensus allows modifying inputs, we can do this crazy thing
$endgroup$
– Kirill L.
yesterday
$begingroup$
Also, OP relaxed the rules, so that it is now possible to work with space-padded matrices, something like this
$endgroup$
– Kirill L.
yesterday
$begingroup$
@KirillL. I don't think that R passes by reference so we probably aren't allowed to do that; it strikes me that this only works if the argument is namedL
in the parent environment. I'll happily update to the space-padding, though!
$endgroup$
– Giuseppe
yesterday
$begingroup$
Ah, I see, you're right. I came up with this idea looking at my Ruby answer (which saves a whole 1 byte this way :)), but the languages indeed behave differently in this situation.
$endgroup$
– Kirill L.
yesterday
add a comment |
$begingroup$
R, 62 bytes
function(L,n){L[A,n]<-rep(L[A<-L[,n]!=' ',n],2)[1+1:sum(A)];L}
Try it online!
Takes input as a space-padded matrix of characters. Rotates upwards. All thanks to Kirill L.!
R, 74 bytes
function(L,n){substr(L[A],n,n)=rep(substr(L[A<-nchar(L)>=n],n,n),2)[-1]
L}
Try it online!
This submission predates the allowance of padded lines.
Aliasing substr
here won't work because we're calling substr
and substr<-
in the first line.
I/O as a list of non-padded lines; rotates upwards.
$endgroup$
R, 62 bytes
function(L,n){L[A,n]<-rep(L[A<-L[,n]!=' ',n],2)[1+1:sum(A)];L}
Try it online!
Takes input as a space-padded matrix of characters. Rotates upwards. All thanks to Kirill L.!
R, 74 bytes
function(L,n){substr(L[A],n,n)=rep(substr(L[A<-nchar(L)>=n],n,n),2)[-1]
L}
Try it online!
This submission predates the allowance of padded lines.
Aliasing substr
here won't work because we're calling substr
and substr<-
in the first line.
I/O as a list of non-padded lines; rotates upwards.
edited yesterday
answered 2 days ago
GiuseppeGiuseppe
17.3k31152
17.3k31152
$begingroup$
Since consensus allows modifying inputs, we can do this crazy thing
$endgroup$
– Kirill L.
yesterday
$begingroup$
Also, OP relaxed the rules, so that it is now possible to work with space-padded matrices, something like this
$endgroup$
– Kirill L.
yesterday
$begingroup$
@KirillL. I don't think that R passes by reference so we probably aren't allowed to do that; it strikes me that this only works if the argument is namedL
in the parent environment. I'll happily update to the space-padding, though!
$endgroup$
– Giuseppe
yesterday
$begingroup$
Ah, I see, you're right. I came up with this idea looking at my Ruby answer (which saves a whole 1 byte this way :)), but the languages indeed behave differently in this situation.
$endgroup$
– Kirill L.
yesterday
add a comment |
$begingroup$
Since consensus allows modifying inputs, we can do this crazy thing
$endgroup$
– Kirill L.
yesterday
$begingroup$
Also, OP relaxed the rules, so that it is now possible to work with space-padded matrices, something like this
$endgroup$
– Kirill L.
yesterday
$begingroup$
@KirillL. I don't think that R passes by reference so we probably aren't allowed to do that; it strikes me that this only works if the argument is namedL
in the parent environment. I'll happily update to the space-padding, though!
$endgroup$
– Giuseppe
yesterday
$begingroup$
Ah, I see, you're right. I came up with this idea looking at my Ruby answer (which saves a whole 1 byte this way :)), but the languages indeed behave differently in this situation.
$endgroup$
– Kirill L.
yesterday
$begingroup$
Since consensus allows modifying inputs, we can do this crazy thing
$endgroup$
– Kirill L.
yesterday
$begingroup$
Since consensus allows modifying inputs, we can do this crazy thing
$endgroup$
– Kirill L.
yesterday
$begingroup$
Also, OP relaxed the rules, so that it is now possible to work with space-padded matrices, something like this
$endgroup$
– Kirill L.
yesterday
$begingroup$
Also, OP relaxed the rules, so that it is now possible to work with space-padded matrices, something like this
$endgroup$
– Kirill L.
yesterday
$begingroup$
@KirillL. I don't think that R passes by reference so we probably aren't allowed to do that; it strikes me that this only works if the argument is named
L
in the parent environment. I'll happily update to the space-padding, though!$endgroup$
– Giuseppe
yesterday
$begingroup$
@KirillL. I don't think that R passes by reference so we probably aren't allowed to do that; it strikes me that this only works if the argument is named
L
in the parent environment. I'll happily update to the space-padding, though!$endgroup$
– Giuseppe
yesterday
$begingroup$
Ah, I see, you're right. I came up with this idea looking at my Ruby answer (which saves a whole 1 byte this way :)), but the languages indeed behave differently in this situation.
$endgroup$
– Kirill L.
yesterday
$begingroup$
Ah, I see, you're right. I came up with this idea looking at my Ruby answer (which saves a whole 1 byte this way :)), but the languages indeed behave differently in this situation.
$endgroup$
– Kirill L.
yesterday
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 85 bytes
k=>a=>{var c='0';a.Where(b=>b.Count>k&&((b[k],c)=(c,b[k]))+""!="").ToList()[0][k]=c;}
Try it online!
Credit to @ASCIIOnly for suggesting foreach
which led to a 12 byte savings!
-8 bytes thanks to @someone!
-1 byte thanks to @EmbodimentofIgnorance!
$endgroup$
$begingroup$
184 bytes
$endgroup$
– Expired Data
2 days ago
$begingroup$
Maybe 183 if using ints instead of chars
$endgroup$
– Expired Data
2 days ago
$begingroup$
@ExpiredData ew int instead of char
$endgroup$
– ASCII-only
yesterday
$begingroup$
too bad you can't use aforeach
since it's the opposite order :(
$endgroup$
– ASCII-only
yesterday
1
$begingroup$
Usedynamic
to merge declarations and save 2 bytes (I've never seen this before!)Try it online!
$endgroup$
– someone
20 hours ago
|
show 10 more comments
$begingroup$
C# (Visual C# Interactive Compiler), 85 bytes
k=>a=>{var c='0';a.Where(b=>b.Count>k&&((b[k],c)=(c,b[k]))+""!="").ToList()[0][k]=c;}
Try it online!
Credit to @ASCIIOnly for suggesting foreach
which led to a 12 byte savings!
-8 bytes thanks to @someone!
-1 byte thanks to @EmbodimentofIgnorance!
$endgroup$
$begingroup$
184 bytes
$endgroup$
– Expired Data
2 days ago
$begingroup$
Maybe 183 if using ints instead of chars
$endgroup$
– Expired Data
2 days ago
$begingroup$
@ExpiredData ew int instead of char
$endgroup$
– ASCII-only
yesterday
$begingroup$
too bad you can't use aforeach
since it's the opposite order :(
$endgroup$
– ASCII-only
yesterday
1
$begingroup$
Usedynamic
to merge declarations and save 2 bytes (I've never seen this before!)Try it online!
$endgroup$
– someone
20 hours ago
|
show 10 more comments
$begingroup$
C# (Visual C# Interactive Compiler), 85 bytes
k=>a=>{var c='0';a.Where(b=>b.Count>k&&((b[k],c)=(c,b[k]))+""!="").ToList()[0][k]=c;}
Try it online!
Credit to @ASCIIOnly for suggesting foreach
which led to a 12 byte savings!
-8 bytes thanks to @someone!
-1 byte thanks to @EmbodimentofIgnorance!
$endgroup$
C# (Visual C# Interactive Compiler), 85 bytes
k=>a=>{var c='0';a.Where(b=>b.Count>k&&((b[k],c)=(c,b[k]))+""!="").ToList()[0][k]=c;}
Try it online!
Credit to @ASCIIOnly for suggesting foreach
which led to a 12 byte savings!
-8 bytes thanks to @someone!
-1 byte thanks to @EmbodimentofIgnorance!
edited 10 hours ago
answered 2 days ago
danadana
1,911167
1,911167
$begingroup$
184 bytes
$endgroup$
– Expired Data
2 days ago
$begingroup$
Maybe 183 if using ints instead of chars
$endgroup$
– Expired Data
2 days ago
$begingroup$
@ExpiredData ew int instead of char
$endgroup$
– ASCII-only
yesterday
$begingroup$
too bad you can't use aforeach
since it's the opposite order :(
$endgroup$
– ASCII-only
yesterday
1
$begingroup$
Usedynamic
to merge declarations and save 2 bytes (I've never seen this before!)Try it online!
$endgroup$
– someone
20 hours ago
|
show 10 more comments
$begingroup$
184 bytes
$endgroup$
– Expired Data
2 days ago
$begingroup$
Maybe 183 if using ints instead of chars
$endgroup$
– Expired Data
2 days ago
$begingroup$
@ExpiredData ew int instead of char
$endgroup$
– ASCII-only
yesterday
$begingroup$
too bad you can't use aforeach
since it's the opposite order :(
$endgroup$
– ASCII-only
yesterday
1
$begingroup$
Usedynamic
to merge declarations and save 2 bytes (I've never seen this before!)Try it online!
$endgroup$
– someone
20 hours ago
$begingroup$
184 bytes
$endgroup$
– Expired Data
2 days ago
$begingroup$
184 bytes
$endgroup$
– Expired Data
2 days ago
$begingroup$
Maybe 183 if using ints instead of chars
$endgroup$
– Expired Data
2 days ago
$begingroup$
Maybe 183 if using ints instead of chars
$endgroup$
– Expired Data
2 days ago
$begingroup$
@ExpiredData ew int instead of char
$endgroup$
– ASCII-only
yesterday
$begingroup$
@ExpiredData ew int instead of char
$endgroup$
– ASCII-only
yesterday
$begingroup$
too bad you can't use a
foreach
since it's the opposite order :($endgroup$
– ASCII-only
yesterday
$begingroup$
too bad you can't use a
foreach
since it's the opposite order :($endgroup$
– ASCII-only
yesterday
1
1
$begingroup$
Use
dynamic
to merge declarations and save 2 bytes (I've never seen this before!)Try it online!$endgroup$
– someone
20 hours ago
$begingroup$
Use
dynamic
to merge declarations and save 2 bytes (I've never seen this before!)Try it online!$endgroup$
– someone
20 hours ago
|
show 10 more comments
$begingroup$
Ruby, 57 bytes
->a,k{b=a.map{|i|i[k]}-[p];c=-2;a.map{|i|i[k]&&=b[c+=1]}}
Try it online!
Takes input as an array of lines a
. Rotates the text down at 0-based position k
. Returns by modifying the input a
.
$endgroup$
add a comment |
$begingroup$
Ruby, 57 bytes
->a,k{b=a.map{|i|i[k]}-[p];c=-2;a.map{|i|i[k]&&=b[c+=1]}}
Try it online!
Takes input as an array of lines a
. Rotates the text down at 0-based position k
. Returns by modifying the input a
.
$endgroup$
add a comment |
$begingroup$
Ruby, 57 bytes
->a,k{b=a.map{|i|i[k]}-[p];c=-2;a.map{|i|i[k]&&=b[c+=1]}}
Try it online!
Takes input as an array of lines a
. Rotates the text down at 0-based position k
. Returns by modifying the input a
.
$endgroup$
Ruby, 57 bytes
->a,k{b=a.map{|i|i[k]}-[p];c=-2;a.map{|i|i[k]&&=b[c+=1]}}
Try it online!
Takes input as an array of lines a
. Rotates the text down at 0-based position k
. Returns by modifying the input a
.
answered 2 days ago
Kirill L.Kirill L.
5,9531527
5,9531527
add a comment |
add a comment |
$begingroup$
05AB1E, 21 bytes
ʒg‹}U¹εXyk©diX®<èIèIǝ
Can definitely be golfed some more..
0-indexed; input and output both as a list of strings.
It rotates down like the example, but <
can be replaced with >
to rotate up instead.
Try it online (footer joins the list by newlines, remove it to see the actual list output).
Explanation:
ʒ } # Filter the (implicit) input-list by:
g # Where length of the current string
‹ # is larger than the (implicit) input-integer
U # Pop and store this filtered list in variable `X`
¹ε # Map over the first input-list again:
Xyk # Get the index of the current string in variable `X`
© # Store it in the register (without popping)
di # If the index is not -1, so the current string is present in variable `X`
X®<è # Get the (index-1)'th string in variable `X`
Iè # Get the character at the index of the input-integer
Iǝ # And insert it at the index of the input-integer in the current string
$endgroup$
add a comment |
$begingroup$
05AB1E, 21 bytes
ʒg‹}U¹εXyk©diX®<èIèIǝ
Can definitely be golfed some more..
0-indexed; input and output both as a list of strings.
It rotates down like the example, but <
can be replaced with >
to rotate up instead.
Try it online (footer joins the list by newlines, remove it to see the actual list output).
Explanation:
ʒ } # Filter the (implicit) input-list by:
g # Where length of the current string
‹ # is larger than the (implicit) input-integer
U # Pop and store this filtered list in variable `X`
¹ε # Map over the first input-list again:
Xyk # Get the index of the current string in variable `X`
© # Store it in the register (without popping)
di # If the index is not -1, so the current string is present in variable `X`
X®<è # Get the (index-1)'th string in variable `X`
Iè # Get the character at the index of the input-integer
Iǝ # And insert it at the index of the input-integer in the current string
$endgroup$
add a comment |
$begingroup$
05AB1E, 21 bytes
ʒg‹}U¹εXyk©diX®<èIèIǝ
Can definitely be golfed some more..
0-indexed; input and output both as a list of strings.
It rotates down like the example, but <
can be replaced with >
to rotate up instead.
Try it online (footer joins the list by newlines, remove it to see the actual list output).
Explanation:
ʒ } # Filter the (implicit) input-list by:
g # Where length of the current string
‹ # is larger than the (implicit) input-integer
U # Pop and store this filtered list in variable `X`
¹ε # Map over the first input-list again:
Xyk # Get the index of the current string in variable `X`
© # Store it in the register (without popping)
di # If the index is not -1, so the current string is present in variable `X`
X®<è # Get the (index-1)'th string in variable `X`
Iè # Get the character at the index of the input-integer
Iǝ # And insert it at the index of the input-integer in the current string
$endgroup$
05AB1E, 21 bytes
ʒg‹}U¹εXyk©diX®<èIèIǝ
Can definitely be golfed some more..
0-indexed; input and output both as a list of strings.
It rotates down like the example, but <
can be replaced with >
to rotate up instead.
Try it online (footer joins the list by newlines, remove it to see the actual list output).
Explanation:
ʒ } # Filter the (implicit) input-list by:
g # Where length of the current string
‹ # is larger than the (implicit) input-integer
U # Pop and store this filtered list in variable `X`
¹ε # Map over the first input-list again:
Xyk # Get the index of the current string in variable `X`
© # Store it in the register (without popping)
di # If the index is not -1, so the current string is present in variable `X`
X®<è # Get the (index-1)'th string in variable `X`
Iè # Get the character at the index of the input-integer
Iǝ # And insert it at the index of the input-integer in the current string
edited 2 days ago
answered 2 days ago
Kevin CruijssenKevin Cruijssen
41.9k569217
41.9k569217
add a comment |
add a comment |
$begingroup$
Japt v2.0a0, 18 bytes
0-based with input & output as a multi-line string. Rotates up by 1.
There's gotta be a shorter method!
yÈrS_Y¦V?Z:°TgXrS
Try it
yÈrS_Y¦V?Z:°TgXrS :Implicit input of string U & integer V
y :Transpose
È :Pass each line X at 0-based index Y through the following function & transpose back
r : Replace
S : RegEx /S/g
_ : Pass each match Z through the following function
Y¦V : Test Y for inequality with V
?Z: : If true, return Z, else
°T : Increment T (initially 0)
g : Index into
XrS : X with spaces removed
$endgroup$
$begingroup$
I haven't looked closely at the comments and new rules (so perhaps it's somewhere mentioned it's allowed now, while it wasn't before), but is it allowed to skip spaces of the initial input? Let's say you change the input-index to6
, your answer would skip the space atline with
in the first line and atRotate here
in the fourth line, whereas most of the other answers would rotate that space as well.
$endgroup$
– Kevin Cruijssen
yesterday
$begingroup$
@KevinCruijssen, I'm a bit confused by all the additional rules & allowances but, if it is an issue, it looks like I can get around that by not allowing spaces in the input. I think.
$endgroup$
– Shaggy
yesterday
$begingroup$
"I think" This part is indeed how I would also respond, haha. I have no idea what is mandatory and optional anymore, but I think you can indeed choose to omit spaces in the in- and output; I think you can choose to count spaces or not; and I think you can use trialing spaces in the output or not; etc. All on all rather confusing. Ah well..
$endgroup$
– Kevin Cruijssen
yesterday
add a comment |
$begingroup$
Japt v2.0a0, 18 bytes
0-based with input & output as a multi-line string. Rotates up by 1.
There's gotta be a shorter method!
yÈrS_Y¦V?Z:°TgXrS
Try it
yÈrS_Y¦V?Z:°TgXrS :Implicit input of string U & integer V
y :Transpose
È :Pass each line X at 0-based index Y through the following function & transpose back
r : Replace
S : RegEx /S/g
_ : Pass each match Z through the following function
Y¦V : Test Y for inequality with V
?Z: : If true, return Z, else
°T : Increment T (initially 0)
g : Index into
XrS : X with spaces removed
$endgroup$
$begingroup$
I haven't looked closely at the comments and new rules (so perhaps it's somewhere mentioned it's allowed now, while it wasn't before), but is it allowed to skip spaces of the initial input? Let's say you change the input-index to6
, your answer would skip the space atline with
in the first line and atRotate here
in the fourth line, whereas most of the other answers would rotate that space as well.
$endgroup$
– Kevin Cruijssen
yesterday
$begingroup$
@KevinCruijssen, I'm a bit confused by all the additional rules & allowances but, if it is an issue, it looks like I can get around that by not allowing spaces in the input. I think.
$endgroup$
– Shaggy
yesterday
$begingroup$
"I think" This part is indeed how I would also respond, haha. I have no idea what is mandatory and optional anymore, but I think you can indeed choose to omit spaces in the in- and output; I think you can choose to count spaces or not; and I think you can use trialing spaces in the output or not; etc. All on all rather confusing. Ah well..
$endgroup$
– Kevin Cruijssen
yesterday
add a comment |
$begingroup$
Japt v2.0a0, 18 bytes
0-based with input & output as a multi-line string. Rotates up by 1.
There's gotta be a shorter method!
yÈrS_Y¦V?Z:°TgXrS
Try it
yÈrS_Y¦V?Z:°TgXrS :Implicit input of string U & integer V
y :Transpose
È :Pass each line X at 0-based index Y through the following function & transpose back
r : Replace
S : RegEx /S/g
_ : Pass each match Z through the following function
Y¦V : Test Y for inequality with V
?Z: : If true, return Z, else
°T : Increment T (initially 0)
g : Index into
XrS : X with spaces removed
$endgroup$
Japt v2.0a0, 18 bytes
0-based with input & output as a multi-line string. Rotates up by 1.
There's gotta be a shorter method!
yÈrS_Y¦V?Z:°TgXrS
Try it
yÈrS_Y¦V?Z:°TgXrS :Implicit input of string U & integer V
y :Transpose
È :Pass each line X at 0-based index Y through the following function & transpose back
r : Replace
S : RegEx /S/g
_ : Pass each match Z through the following function
Y¦V : Test Y for inequality with V
?Z: : If true, return Z, else
°T : Increment T (initially 0)
g : Index into
XrS : X with spaces removed
answered 2 days ago
ShaggyShaggy
19.1k21768
19.1k21768
$begingroup$
I haven't looked closely at the comments and new rules (so perhaps it's somewhere mentioned it's allowed now, while it wasn't before), but is it allowed to skip spaces of the initial input? Let's say you change the input-index to6
, your answer would skip the space atline with
in the first line and atRotate here
in the fourth line, whereas most of the other answers would rotate that space as well.
$endgroup$
– Kevin Cruijssen
yesterday
$begingroup$
@KevinCruijssen, I'm a bit confused by all the additional rules & allowances but, if it is an issue, it looks like I can get around that by not allowing spaces in the input. I think.
$endgroup$
– Shaggy
yesterday
$begingroup$
"I think" This part is indeed how I would also respond, haha. I have no idea what is mandatory and optional anymore, but I think you can indeed choose to omit spaces in the in- and output; I think you can choose to count spaces or not; and I think you can use trialing spaces in the output or not; etc. All on all rather confusing. Ah well..
$endgroup$
– Kevin Cruijssen
yesterday
add a comment |
$begingroup$
I haven't looked closely at the comments and new rules (so perhaps it's somewhere mentioned it's allowed now, while it wasn't before), but is it allowed to skip spaces of the initial input? Let's say you change the input-index to6
, your answer would skip the space atline with
in the first line and atRotate here
in the fourth line, whereas most of the other answers would rotate that space as well.
$endgroup$
– Kevin Cruijssen
yesterday
$begingroup$
@KevinCruijssen, I'm a bit confused by all the additional rules & allowances but, if it is an issue, it looks like I can get around that by not allowing spaces in the input. I think.
$endgroup$
– Shaggy
yesterday
$begingroup$
"I think" This part is indeed how I would also respond, haha. I have no idea what is mandatory and optional anymore, but I think you can indeed choose to omit spaces in the in- and output; I think you can choose to count spaces or not; and I think you can use trialing spaces in the output or not; etc. All on all rather confusing. Ah well..
$endgroup$
– Kevin Cruijssen
yesterday
$begingroup$
I haven't looked closely at the comments and new rules (so perhaps it's somewhere mentioned it's allowed now, while it wasn't before), but is it allowed to skip spaces of the initial input? Let's say you change the input-index to
6
, your answer would skip the space at line with
in the first line and at Rotate here
in the fourth line, whereas most of the other answers would rotate that space as well.$endgroup$
– Kevin Cruijssen
yesterday
$begingroup$
I haven't looked closely at the comments and new rules (so perhaps it's somewhere mentioned it's allowed now, while it wasn't before), but is it allowed to skip spaces of the initial input? Let's say you change the input-index to
6
, your answer would skip the space at line with
in the first line and at Rotate here
in the fourth line, whereas most of the other answers would rotate that space as well.$endgroup$
– Kevin Cruijssen
yesterday
$begingroup$
@KevinCruijssen, I'm a bit confused by all the additional rules & allowances but, if it is an issue, it looks like I can get around that by not allowing spaces in the input. I think.
$endgroup$
– Shaggy
yesterday
$begingroup$
@KevinCruijssen, I'm a bit confused by all the additional rules & allowances but, if it is an issue, it looks like I can get around that by not allowing spaces in the input. I think.
$endgroup$
– Shaggy
yesterday
$begingroup$
"I think" This part is indeed how I would also respond, haha. I have no idea what is mandatory and optional anymore, but I think you can indeed choose to omit spaces in the in- and output; I think you can choose to count spaces or not; and I think you can use trialing spaces in the output or not; etc. All on all rather confusing. Ah well..
$endgroup$
– Kevin Cruijssen
yesterday
$begingroup$
"I think" This part is indeed how I would also respond, haha. I have no idea what is mandatory and optional anymore, but I think you can indeed choose to omit spaces in the in- and output; I think you can choose to count spaces or not; and I think you can use trialing spaces in the output or not; etc. All on all rather confusing. Ah well..
$endgroup$
– Kevin Cruijssen
yesterday
add a comment |
$begingroup$
Jelly, 16 bytes
z⁷⁹ịỴFṙ-ṁƲY⁸ṛ⁹¦"
A dyadic Link accepting a list of lines (lists of characters containing no newline characters) on the left and an integer on the right which returns a list of lines.
Try it online! (footer splits on newlines, calls the Link, and joins by newlines again)
How?
z⁷⁹ịỴFṙ-ṁƲY⁸ṛ⁹¦" - Link: lines L; index I e.g. example in question; 20
z⁷ - transpose L with filler 'n' ['AAARTnTW', ' ohnhr', ...]
⁹ị - Ith item 'amnv.nnt'
Ỵ - split at newlines ['am', 'v.', '', 't']
Ʋ - last four links as a monad - i.e. f(X):
F - flatten 'amv.t'
- - -1
ṙ - rotate left by 'tamv.'
ṁ - mould like X ['ta', 'mv', '', '.']
Y - join with newlines 'tanmvnn.'
- -- call this C
⁸ - chain's left argument, L
" - zip with - i.e. [f(L1,C1), f(L2,C2), ...]:
¦ - sparse application...
⁹ - ...to indices: chain's right argument, I
ṛ - ...of: right argument, Cn
$endgroup$
add a comment |
$begingroup$
Jelly, 16 bytes
z⁷⁹ịỴFṙ-ṁƲY⁸ṛ⁹¦"
A dyadic Link accepting a list of lines (lists of characters containing no newline characters) on the left and an integer on the right which returns a list of lines.
Try it online! (footer splits on newlines, calls the Link, and joins by newlines again)
How?
z⁷⁹ịỴFṙ-ṁƲY⁸ṛ⁹¦" - Link: lines L; index I e.g. example in question; 20
z⁷ - transpose L with filler 'n' ['AAARTnTW', ' ohnhr', ...]
⁹ị - Ith item 'amnv.nnt'
Ỵ - split at newlines ['am', 'v.', '', 't']
Ʋ - last four links as a monad - i.e. f(X):
F - flatten 'amv.t'
- - -1
ṙ - rotate left by 'tamv.'
ṁ - mould like X ['ta', 'mv', '', '.']
Y - join with newlines 'tanmvnn.'
- -- call this C
⁸ - chain's left argument, L
" - zip with - i.e. [f(L1,C1), f(L2,C2), ...]:
¦ - sparse application...
⁹ - ...to indices: chain's right argument, I
ṛ - ...of: right argument, Cn
$endgroup$
add a comment |
$begingroup$
Jelly, 16 bytes
z⁷⁹ịỴFṙ-ṁƲY⁸ṛ⁹¦"
A dyadic Link accepting a list of lines (lists of characters containing no newline characters) on the left and an integer on the right which returns a list of lines.
Try it online! (footer splits on newlines, calls the Link, and joins by newlines again)
How?
z⁷⁹ịỴFṙ-ṁƲY⁸ṛ⁹¦" - Link: lines L; index I e.g. example in question; 20
z⁷ - transpose L with filler 'n' ['AAARTnTW', ' ohnhr', ...]
⁹ị - Ith item 'amnv.nnt'
Ỵ - split at newlines ['am', 'v.', '', 't']
Ʋ - last four links as a monad - i.e. f(X):
F - flatten 'amv.t'
- - -1
ṙ - rotate left by 'tamv.'
ṁ - mould like X ['ta', 'mv', '', '.']
Y - join with newlines 'tanmvnn.'
- -- call this C
⁸ - chain's left argument, L
" - zip with - i.e. [f(L1,C1), f(L2,C2), ...]:
¦ - sparse application...
⁹ - ...to indices: chain's right argument, I
ṛ - ...of: right argument, Cn
$endgroup$
Jelly, 16 bytes
z⁷⁹ịỴFṙ-ṁƲY⁸ṛ⁹¦"
A dyadic Link accepting a list of lines (lists of characters containing no newline characters) on the left and an integer on the right which returns a list of lines.
Try it online! (footer splits on newlines, calls the Link, and joins by newlines again)
How?
z⁷⁹ịỴFṙ-ṁƲY⁸ṛ⁹¦" - Link: lines L; index I e.g. example in question; 20
z⁷ - transpose L with filler 'n' ['AAARTnTW', ' ohnhr', ...]
⁹ị - Ith item 'amnv.nnt'
Ỵ - split at newlines ['am', 'v.', '', 't']
Ʋ - last four links as a monad - i.e. f(X):
F - flatten 'amv.t'
- - -1
ṙ - rotate left by 'tamv.'
ṁ - mould like X ['ta', 'mv', '', '.']
Y - join with newlines 'tanmvnn.'
- -- call this C
⁸ - chain's left argument, L
" - zip with - i.e. [f(L1,C1), f(L2,C2), ...]:
¦ - sparse application...
⁹ - ...to indices: chain's right argument, I
ṛ - ...of: right argument, Cn
edited yesterday
answered yesterday
Jonathan AllanJonathan Allan
53.5k535172
53.5k535172
add a comment |
add a comment |
$begingroup$
Charcoal, 34 28 bytes
θF²«Jη⁰Fθ«≔KKκF¬¬κ«FιPζ≔κζ»↓
Try it online! Link is to verbose version of code. Takes an array of strings as input. 0-indexed. This was way too hard, because performing operations on the result of PeekDirection
is at best inconsistent. Explanation:
θ
Print the input strings.
F²«
Repeat twice.
Jη⁰
Jump to the top of the column to be rotated.
Fθ«
Loop over each row.
≔KKκ
Read the current cell.
F¬¬κ«
If it's not empty, then...
FιPζ
... only on the second pass, overwrite with the previous non-empty cell's value...
≔κζ
... and save the value for the next cell/pass.
»↓
Move to the next row.
$endgroup$
add a comment |
$begingroup$
Charcoal, 34 28 bytes
θF²«Jη⁰Fθ«≔KKκF¬¬κ«FιPζ≔κζ»↓
Try it online! Link is to verbose version of code. Takes an array of strings as input. 0-indexed. This was way too hard, because performing operations on the result of PeekDirection
is at best inconsistent. Explanation:
θ
Print the input strings.
F²«
Repeat twice.
Jη⁰
Jump to the top of the column to be rotated.
Fθ«
Loop over each row.
≔KKκ
Read the current cell.
F¬¬κ«
If it's not empty, then...
FιPζ
... only on the second pass, overwrite with the previous non-empty cell's value...
≔κζ
... and save the value for the next cell/pass.
»↓
Move to the next row.
$endgroup$
add a comment |
$begingroup$
Charcoal, 34 28 bytes
θF²«Jη⁰Fθ«≔KKκF¬¬κ«FιPζ≔κζ»↓
Try it online! Link is to verbose version of code. Takes an array of strings as input. 0-indexed. This was way too hard, because performing operations on the result of PeekDirection
is at best inconsistent. Explanation:
θ
Print the input strings.
F²«
Repeat twice.
Jη⁰
Jump to the top of the column to be rotated.
Fθ«
Loop over each row.
≔KKκ
Read the current cell.
F¬¬κ«
If it's not empty, then...
FιPζ
... only on the second pass, overwrite with the previous non-empty cell's value...
≔κζ
... and save the value for the next cell/pass.
»↓
Move to the next row.
$endgroup$
Charcoal, 34 28 bytes
θF²«Jη⁰Fθ«≔KKκF¬¬κ«FιPζ≔κζ»↓
Try it online! Link is to verbose version of code. Takes an array of strings as input. 0-indexed. This was way too hard, because performing operations on the result of PeekDirection
is at best inconsistent. Explanation:
θ
Print the input strings.
F²«
Repeat twice.
Jη⁰
Jump to the top of the column to be rotated.
Fθ«
Loop over each row.
≔KKκ
Read the current cell.
F¬¬κ«
If it's not empty, then...
FιPζ
... only on the second pass, overwrite with the previous non-empty cell's value...
≔κζ
... and save the value for the next cell/pass.
»↓
Move to the next row.
edited 2 days ago
answered 2 days ago
NeilNeil
82.1k745178
82.1k745178
add a comment |
add a comment |
$begingroup$
K4, 41 bytes
Solution:
{.[x;i;:;.q.rotate[1;x . i:(&y<#:'x;y)]]}
Explanation:
Not sure if I'm missing something... 0 index, rotates up (change the 1
to -1
for rotate down)
{.[x;i;:;.q.rotate[1;x . i:(&y<#:'x;y)]]} / the solution
{ } / lambda taking implicit x and y
.[x; ;:; ] / index apply assignment back into x
.q.rotate[1; ] / left rotate 1 character
( ; ) / two item list
y / index to rotate
#:'x / count (#:) each (') input
y< / index less than ?
& / indexes where true
i: / assign to variable w
x . / index into x
i / indexes we saved as i earlier
$endgroup$
add a comment |
$begingroup$
K4, 41 bytes
Solution:
{.[x;i;:;.q.rotate[1;x . i:(&y<#:'x;y)]]}
Explanation:
Not sure if I'm missing something... 0 index, rotates up (change the 1
to -1
for rotate down)
{.[x;i;:;.q.rotate[1;x . i:(&y<#:'x;y)]]} / the solution
{ } / lambda taking implicit x and y
.[x; ;:; ] / index apply assignment back into x
.q.rotate[1; ] / left rotate 1 character
( ; ) / two item list
y / index to rotate
#:'x / count (#:) each (') input
y< / index less than ?
& / indexes where true
i: / assign to variable w
x . / index into x
i / indexes we saved as i earlier
$endgroup$
add a comment |
$begingroup$
K4, 41 bytes
Solution:
{.[x;i;:;.q.rotate[1;x . i:(&y<#:'x;y)]]}
Explanation:
Not sure if I'm missing something... 0 index, rotates up (change the 1
to -1
for rotate down)
{.[x;i;:;.q.rotate[1;x . i:(&y<#:'x;y)]]} / the solution
{ } / lambda taking implicit x and y
.[x; ;:; ] / index apply assignment back into x
.q.rotate[1; ] / left rotate 1 character
( ; ) / two item list
y / index to rotate
#:'x / count (#:) each (') input
y< / index less than ?
& / indexes where true
i: / assign to variable w
x . / index into x
i / indexes we saved as i earlier
$endgroup$
K4, 41 bytes
Solution:
{.[x;i;:;.q.rotate[1;x . i:(&y<#:'x;y)]]}
Explanation:
Not sure if I'm missing something... 0 index, rotates up (change the 1
to -1
for rotate down)
{.[x;i;:;.q.rotate[1;x . i:(&y<#:'x;y)]]} / the solution
{ } / lambda taking implicit x and y
.[x; ;:; ] / index apply assignment back into x
.q.rotate[1; ] / left rotate 1 character
( ; ) / two item list
y / index to rotate
#:'x / count (#:) each (') input
y< / index less than ?
& / indexes where true
i: / assign to variable w
x . / index into x
i / indexes we saved as i earlier
answered 2 days ago
streetsterstreetster
2,584515
2,584515
add a comment |
add a comment |
$begingroup$
perl 5 (-p
), 75 bytes
k is 0-indexed, rotate down
s/.*//;$r="^.{$&}";s/
//;/$r(.)/;$c=$1;s/$rK.(?=(?s:.)*?$r(.)|)/$1||$c/gme
TIO
$endgroup$
add a comment |
$begingroup$
perl 5 (-p
), 75 bytes
k is 0-indexed, rotate down
s/.*//;$r="^.{$&}";s/
//;/$r(.)/;$c=$1;s/$rK.(?=(?s:.)*?$r(.)|)/$1||$c/gme
TIO
$endgroup$
add a comment |
$begingroup$
perl 5 (-p
), 75 bytes
k is 0-indexed, rotate down
s/.*//;$r="^.{$&}";s/
//;/$r(.)/;$c=$1;s/$rK.(?=(?s:.)*?$r(.)|)/$1||$c/gme
TIO
$endgroup$
perl 5 (-p
), 75 bytes
k is 0-indexed, rotate down
s/.*//;$r="^.{$&}";s/
//;/$r(.)/;$c=$1;s/$rK.(?=(?s:.)*?$r(.)|)/$1||$c/gme
TIO
answered yesterday
Nahuel FouilleulNahuel Fouilleul
2,965211
2,965211
add a comment |
add a comment |
$begingroup$
Perl 6, 38 33 bytes
Modifies the array in place (rules don't disallow)
{@^a.grep(*>$^b+1)[*;$b].=rotate}
Try it online!
$endgroup$
$begingroup$
@joking At first I thought storing it rather than outputting it would be a bit cheating, but rereading the rules, there's no actual stipulation to output so … hooray. Doing a (…)(…) to get 31 like you have definitely seems to be cheating though, since it's effectively hoisting a call out of the block, but it can still be done with 33 passing them simultaneously with a single call so I'm going go with that.
$endgroup$
– guifa
yesterday
$begingroup$
ah oops, i pasted the wrong link. you got what I mean to comment though. Currying input is perfectly fine though (though it doesn't work in this case), and modifying an argument passed by reference is a standard forn of allowed input
$endgroup$
– Jo King
yesterday
add a comment |
$begingroup$
Perl 6, 38 33 bytes
Modifies the array in place (rules don't disallow)
{@^a.grep(*>$^b+1)[*;$b].=rotate}
Try it online!
$endgroup$
$begingroup$
@joking At first I thought storing it rather than outputting it would be a bit cheating, but rereading the rules, there's no actual stipulation to output so … hooray. Doing a (…)(…) to get 31 like you have definitely seems to be cheating though, since it's effectively hoisting a call out of the block, but it can still be done with 33 passing them simultaneously with a single call so I'm going go with that.
$endgroup$
– guifa
yesterday
$begingroup$
ah oops, i pasted the wrong link. you got what I mean to comment though. Currying input is perfectly fine though (though it doesn't work in this case), and modifying an argument passed by reference is a standard forn of allowed input
$endgroup$
– Jo King
yesterday
add a comment |
$begingroup$
Perl 6, 38 33 bytes
Modifies the array in place (rules don't disallow)
{@^a.grep(*>$^b+1)[*;$b].=rotate}
Try it online!
$endgroup$
Perl 6, 38 33 bytes
Modifies the array in place (rules don't disallow)
{@^a.grep(*>$^b+1)[*;$b].=rotate}
Try it online!
edited yesterday
answered yesterday
guifaguifa
28115
28115
$begingroup$
@joking At first I thought storing it rather than outputting it would be a bit cheating, but rereading the rules, there's no actual stipulation to output so … hooray. Doing a (…)(…) to get 31 like you have definitely seems to be cheating though, since it's effectively hoisting a call out of the block, but it can still be done with 33 passing them simultaneously with a single call so I'm going go with that.
$endgroup$
– guifa
yesterday
$begingroup$
ah oops, i pasted the wrong link. you got what I mean to comment though. Currying input is perfectly fine though (though it doesn't work in this case), and modifying an argument passed by reference is a standard forn of allowed input
$endgroup$
– Jo King
yesterday
add a comment |
$begingroup$
@joking At first I thought storing it rather than outputting it would be a bit cheating, but rereading the rules, there's no actual stipulation to output so … hooray. Doing a (…)(…) to get 31 like you have definitely seems to be cheating though, since it's effectively hoisting a call out of the block, but it can still be done with 33 passing them simultaneously with a single call so I'm going go with that.
$endgroup$
– guifa
yesterday
$begingroup$
ah oops, i pasted the wrong link. you got what I mean to comment though. Currying input is perfectly fine though (though it doesn't work in this case), and modifying an argument passed by reference is a standard forn of allowed input
$endgroup$
– Jo King
yesterday
$begingroup$
@joking At first I thought storing it rather than outputting it would be a bit cheating, but rereading the rules, there's no actual stipulation to output so … hooray. Doing a (…)(…) to get 31 like you have definitely seems to be cheating though, since it's effectively hoisting a call out of the block, but it can still be done with 33 passing them simultaneously with a single call so I'm going go with that.
$endgroup$
– guifa
yesterday
$begingroup$
@joking At first I thought storing it rather than outputting it would be a bit cheating, but rereading the rules, there's no actual stipulation to output so … hooray. Doing a (…)(…) to get 31 like you have definitely seems to be cheating though, since it's effectively hoisting a call out of the block, but it can still be done with 33 passing them simultaneously with a single call so I'm going go with that.
$endgroup$
– guifa
yesterday
$begingroup$
ah oops, i pasted the wrong link. you got what I mean to comment though. Currying input is perfectly fine though (though it doesn't work in this case), and modifying an argument passed by reference is a standard forn of allowed input
$endgroup$
– Jo King
yesterday
$begingroup$
ah oops, i pasted the wrong link. you got what I mean to comment though. Currying input is perfectly fine though (though it doesn't work in this case), and modifying an argument passed by reference is a standard forn of allowed input
$endgroup$
– Jo King
yesterday
add a comment |
$begingroup$
JavaScript (Node.js), 52 bytes
k=>a=>a.filter(b=>b[k]&&([b[k],a]=[a,b[k]]))[0][k]=a
Try it online!
-7 bytes thanks to Shaggy!
Didn't see a JavaScript answer yet! Port of my C# answer.
$endgroup$
$begingroup$
63 bytes. Can probably be golfed further.
$endgroup$
– Shaggy
13 hours ago
$begingroup$
@Shaggy - nice :) most of the time I see JS answers doing all sorts of crazy things. Figured this time I'd give it a shot since I had a strategy.
$endgroup$
– dana
13 hours ago
$begingroup$
Oh, nicely golfed! :)
$endgroup$
– Shaggy
2 hours ago
add a comment |
$begingroup$
JavaScript (Node.js), 52 bytes
k=>a=>a.filter(b=>b[k]&&([b[k],a]=[a,b[k]]))[0][k]=a
Try it online!
-7 bytes thanks to Shaggy!
Didn't see a JavaScript answer yet! Port of my C# answer.
$endgroup$
$begingroup$
63 bytes. Can probably be golfed further.
$endgroup$
– Shaggy
13 hours ago
$begingroup$
@Shaggy - nice :) most of the time I see JS answers doing all sorts of crazy things. Figured this time I'd give it a shot since I had a strategy.
$endgroup$
– dana
13 hours ago
$begingroup$
Oh, nicely golfed! :)
$endgroup$
– Shaggy
2 hours ago
add a comment |
$begingroup$
JavaScript (Node.js), 52 bytes
k=>a=>a.filter(b=>b[k]&&([b[k],a]=[a,b[k]]))[0][k]=a
Try it online!
-7 bytes thanks to Shaggy!
Didn't see a JavaScript answer yet! Port of my C# answer.
$endgroup$
JavaScript (Node.js), 52 bytes
k=>a=>a.filter(b=>b[k]&&([b[k],a]=[a,b[k]]))[0][k]=a
Try it online!
-7 bytes thanks to Shaggy!
Didn't see a JavaScript answer yet! Port of my C# answer.
edited 9 hours ago
answered 18 hours ago
danadana
1,911167
1,911167
$begingroup$
63 bytes. Can probably be golfed further.
$endgroup$
– Shaggy
13 hours ago
$begingroup$
@Shaggy - nice :) most of the time I see JS answers doing all sorts of crazy things. Figured this time I'd give it a shot since I had a strategy.
$endgroup$
– dana
13 hours ago
$begingroup$
Oh, nicely golfed! :)
$endgroup$
– Shaggy
2 hours ago
add a comment |
$begingroup$
63 bytes. Can probably be golfed further.
$endgroup$
– Shaggy
13 hours ago
$begingroup$
@Shaggy - nice :) most of the time I see JS answers doing all sorts of crazy things. Figured this time I'd give it a shot since I had a strategy.
$endgroup$
– dana
13 hours ago
$begingroup$
Oh, nicely golfed! :)
$endgroup$
– Shaggy
2 hours ago
$begingroup$
63 bytes. Can probably be golfed further.
$endgroup$
– Shaggy
13 hours ago
$begingroup$
63 bytes. Can probably be golfed further.
$endgroup$
– Shaggy
13 hours ago
$begingroup$
@Shaggy - nice :) most of the time I see JS answers doing all sorts of crazy things. Figured this time I'd give it a shot since I had a strategy.
$endgroup$
– dana
13 hours ago
$begingroup$
@Shaggy - nice :) most of the time I see JS answers doing all sorts of crazy things. Figured this time I'd give it a shot since I had a strategy.
$endgroup$
– dana
13 hours ago
$begingroup$
Oh, nicely golfed! :)
$endgroup$
– Shaggy
2 hours ago
$begingroup$
Oh, nicely golfed! :)
$endgroup$
– Shaggy
2 hours ago
add a comment |
$begingroup$
Pip -rn
, 32 bytes
POgY#(g@_)>aFI,#gFiyAE@ySsg@i@ag
0-indexed, rotates down. Try it online!
Filters to find the indices of all rows that are long enough to participate in the rotation. Then loops over those rows, swapping the appropriate character on each row with a temp variable s
. Visiting the first row again at the end swaps the dummy value back out again.
$endgroup$
add a comment |
$begingroup$
Pip -rn
, 32 bytes
POgY#(g@_)>aFI,#gFiyAE@ySsg@i@ag
0-indexed, rotates down. Try it online!
Filters to find the indices of all rows that are long enough to participate in the rotation. Then loops over those rows, swapping the appropriate character on each row with a temp variable s
. Visiting the first row again at the end swaps the dummy value back out again.
$endgroup$
add a comment |
$begingroup$
Pip -rn
, 32 bytes
POgY#(g@_)>aFI,#gFiyAE@ySsg@i@ag
0-indexed, rotates down. Try it online!
Filters to find the indices of all rows that are long enough to participate in the rotation. Then loops over those rows, swapping the appropriate character on each row with a temp variable s
. Visiting the first row again at the end swaps the dummy value back out again.
$endgroup$
Pip -rn
, 32 bytes
POgY#(g@_)>aFI,#gFiyAE@ySsg@i@ag
0-indexed, rotates down. Try it online!
Filters to find the indices of all rows that are long enough to participate in the rotation. Then loops over those rows, swapping the appropriate character on each row with a temp variable s
. Visiting the first row again at the end swaps the dummy value back out again.
answered yesterday
DLoscDLosc
19.4k33990
19.4k33990
add a comment |
add a comment |
$begingroup$
Jelly, 19 bytes
ZnÄ×$ịḟ¹ṙ-;ɗɗʋ€⁹¦⁶Z
Try it online!
1-indexed. Rotates down. A monadic link that takes a right-padded list of Jelly strings (a list of lists of characters) as the first argument and k as the second. Spaces are forbidden in the input except as right padding, but all other characters are permitted.
As implemented on TIO, the footer splits a single string input into a list of strings and right-pads it, but this is for convenience; as I understand it, the result of that step is permitted input for the main link per the rules.
$endgroup$
add a comment |
$begingroup$
Jelly, 19 bytes
ZnÄ×$ịḟ¹ṙ-;ɗɗʋ€⁹¦⁶Z
Try it online!
1-indexed. Rotates down. A monadic link that takes a right-padded list of Jelly strings (a list of lists of characters) as the first argument and k as the second. Spaces are forbidden in the input except as right padding, but all other characters are permitted.
As implemented on TIO, the footer splits a single string input into a list of strings and right-pads it, but this is for convenience; as I understand it, the result of that step is permitted input for the main link per the rules.
$endgroup$
add a comment |
$begingroup$
Jelly, 19 bytes
ZnÄ×$ịḟ¹ṙ-;ɗɗʋ€⁹¦⁶Z
Try it online!
1-indexed. Rotates down. A monadic link that takes a right-padded list of Jelly strings (a list of lists of characters) as the first argument and k as the second. Spaces are forbidden in the input except as right padding, but all other characters are permitted.
As implemented on TIO, the footer splits a single string input into a list of strings and right-pads it, but this is for convenience; as I understand it, the result of that step is permitted input for the main link per the rules.
$endgroup$
Jelly, 19 bytes
ZnÄ×$ịḟ¹ṙ-;ɗɗʋ€⁹¦⁶Z
Try it online!
1-indexed. Rotates down. A monadic link that takes a right-padded list of Jelly strings (a list of lists of characters) as the first argument and k as the second. Spaces are forbidden in the input except as right padding, but all other characters are permitted.
As implemented on TIO, the footer splits a single string input into a list of strings and right-pads it, but this is for convenience; as I understand it, the result of that step is permitted input for the main link per the rules.
answered yesterday
Nick KennedyNick Kennedy
1,16649
1,16649
add a comment |
add a comment |
If this is an answer to a challenge…
…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.
…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.
More generally…
…Please make sure to answer the question and provide sufficient detail.
…Avoid asking for help, clarification or responding to other answers (use comments instead).
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f182363%2frotate-a-column%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
$begingroup$
May the output contain additional trailing spaces (so the input is as in your example, but the output is a rectangle)?
$endgroup$
– Kevin Cruijssen
2 days ago
1
$begingroup$
@KevinCruijssen Whoops :-)
$endgroup$
– Luis Mendo
2 days ago
$begingroup$
@KevinCruijssen No. But edited to explicitly allow disallowing spaces in the input, so you could do easy trimming. I may consider changing it if there is a strong reason. But I think this should be easy enough for most practical languages. And for golfing languages, there are usually invisible padding values (someone should invent one if not).
$endgroup$
– jimmy23013
2 days ago
1
$begingroup$
@Adám I'd say allowing trailing garbage isn't a normal thing to do. At best I'll allow some "reasonable representation" of the data. The problem is created by repeatedly relaxing the rules, not that it should be like this. The difficulty of working with unequal lengths (of sections in a useful text, which isn't expected to be padded) is the reason why I wrote this challenge. Actually the APL answer solves the problem in the most ideal way I could think of. But better if you could split the lines in enclosed form. If not, I'll try to decide in a day and leave its validity unclear for a while.
$endgroup$
– jimmy23013
2 days ago
5
$begingroup$
My thought process: "Ha! Simple! Transpose, rotate row
k
, transpose back ... ah, goddamnit!"$endgroup$
– Shaggy
2 days ago