Rotate a column
$begingroup$
Given 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 k > 35 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).
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$
add a comment |
$begingroup$
Given 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 k > 35 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).
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
1 hour ago
1
$begingroup$
@KevinCruijssen Whoops :-)
$endgroup$
– Luis Mendo
1 hour 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
1 hour ago
$begingroup$
@jimmy23013 APL wants every column to be of the same length, otherwise they have to be enclosed
$endgroup$
– Ven
8 mins ago
add a comment |
$begingroup$
Given 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 k > 35 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).
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 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 k > 35 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).
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 1 hour ago
jimmy23013
asked 1 hour ago
jimmy23013jimmy23013
29.6k560127
29.6k560127
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
1 hour ago
1
$begingroup$
@KevinCruijssen Whoops :-)
$endgroup$
– Luis Mendo
1 hour 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
1 hour ago
$begingroup$
@jimmy23013 APL wants every column to be of the same length, otherwise they have to be enclosed
$endgroup$
– Ven
8 mins ago
add a comment |
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
1 hour ago
1
$begingroup$
@KevinCruijssen Whoops :-)
$endgroup$
– Luis Mendo
1 hour 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
1 hour ago
$begingroup$
@jimmy23013 APL wants every column to be of the same length, otherwise they have to be enclosed
$endgroup$
– Ven
8 mins 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
1 hour 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
1 hour ago
1
1
$begingroup$
@KevinCruijssen Whoops :-)
$endgroup$
– Luis Mendo
1 hour ago
$begingroup$
@KevinCruijssen Whoops :-)
$endgroup$
– Luis Mendo
1 hour 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
1 hour 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
1 hour ago
$begingroup$
@jimmy23013 APL wants every column to be of the same length, otherwise they have to be enclosed
$endgroup$
– Ven
8 mins ago
$begingroup$
@jimmy23013 APL wants every column to be of the same length, otherwise they have to be enclosed
$endgroup$
– Ven
8 mins ago
add a comment |
2 Answers
2
active
oldest
votes
$begingroup$
Python 2, 111 110 109 99 98 96 bytes
a,n=input()
x=[l[n]for l in a if l[n:]]
for l in a:print l[:n]+(l[n:]and x.pop(x>x[:1])+l[n+1:])
Try it online!
Takes input as a list of lines and 0-index column, and prints result.
Column is rotated up 1.
-11 bytes, thanks to Jo King
$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 |
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
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Python 2, 111 110 109 99 98 96 bytes
a,n=input()
x=[l[n]for l in a if l[n:]]
for l in a:print l[:n]+(l[n:]and x.pop(x>x[:1])+l[n+1:])
Try it online!
Takes input as a list of lines and 0-index column, and prints result.
Column is rotated up 1.
-11 bytes, thanks to Jo King
$endgroup$
add a comment |
$begingroup$
Python 2, 111 110 109 99 98 96 bytes
a,n=input()
x=[l[n]for l in a if l[n:]]
for l in a:print l[:n]+(l[n:]and x.pop(x>x[:1])+l[n+1:])
Try it online!
Takes input as a list of lines and 0-index column, and prints result.
Column is rotated up 1.
-11 bytes, thanks to Jo King
$endgroup$
add a comment |
$begingroup$
Python 2, 111 110 109 99 98 96 bytes
a,n=input()
x=[l[n]for l in a if l[n:]]
for l in a:print l[:n]+(l[n:]and x.pop(x>x[:1])+l[n+1:])
Try it online!
Takes input as a list of lines and 0-index column, and prints result.
Column is rotated up 1.
-11 bytes, thanks to Jo King
$endgroup$
Python 2, 111 110 109 99 98 96 bytes
a,n=input()
x=[l[n]for l in a if l[n:]]
for l in a:print l[:n]+(l[n:]and x.pop(x>x[:1])+l[n+1:])
Try it online!
Takes input as a list of lines and 0-index column, and prints result.
Column is rotated up 1.
-11 bytes, thanks to Jo King
edited 56 mins ago
answered 1 hour ago
TFeldTFeld
16.2k21449
16.2k21449
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 55 mins ago
answered 1 hour ago
Kevin CruijssenKevin Cruijssen
41.7k568217
41.7k568217
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
1 hour ago
1
$begingroup$
@KevinCruijssen Whoops :-)
$endgroup$
– Luis Mendo
1 hour 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
1 hour ago
$begingroup$
@jimmy23013 APL wants every column to be of the same length, otherwise they have to be enclosed
$endgroup$
– Ven
8 mins ago