Offset in split text contentTips for golfing in <all languages>Justify a text by adding spacesRectangle of textHide one message inside anotherIn a string or not?KeyPad Code to Text!Is one string contained within the other?Read ASCII-Art TextSplit me in halfSplit string on first occurrence of each characterSorting out redundant text from screen scraper application
Do native speakers use "ultima" and "proxima" frequently in spoken English?
Can you describe someone as luxurious? As in someone who likes luxurious things?
Started in 1987 vs. Starting in 1987
Center page as a whole without centering each element individually
Can you take a "free object interaction" while incapacitated?
Sort with assumptions
How can I, as DM, avoid the Conga Line of Death occurring when implementing some form of flanking rule?
Friend wants my recommendation but I don't want to give it to him
How to get directions in deep space?
Does capillary rise violate hydrostatic paradox?
How do you justify more code being written by following clean code practices?
Not hide and seek
What properties make a magic weapon befit a Rogue more than a DEX-based Fighter?
Should I be concerned about student access to a test bank?
A seasonal riddle
Extract substring according to regexp with sed or grep
Should I warn a new PhD Student?
Why didn’t Eve recognize the little cockroach as a living organism?
What is the tangent at a sharp point on a curve?
Is there a distance limit for minecart tracks?
What is this high flying aircraft over Pennsylvania?
categorizing a variable turns it from insignificant to significant
Would a primitive species be able to learn English from reading books alone?
Unfrosted light bulb
Offset in split text content
Tips for golfing in <all languages>Justify a text by adding spacesRectangle of textHide one message inside anotherIn a string or not?KeyPad Code to Text!Is one string contained within the other?Read ASCII-Art TextSplit me in halfSplit string on first occurrence of each characterSorting out redundant text from screen scraper application
$begingroup$
You will be given an ASCII text. This text may have zero, one or many appearances of a split a character (let's assume it will be the space character )
You will also be given an offset integer value (zero based) anywhere between zero and the length of the text.
You should calculate the relative offset inside the one split text, which contains the absolute offset, as well as the index of that very split text.
If the given offset is at the split character the last split should have the offset
It's hard to describe (comments welcome), so here are some examples
text: Lorem ipsum dolor sit amet
offset: 20
Lorem ipsum dolor sit amet
enter code here
--------------------^
000000000011111111112
012345678901234567890
Result: 2, 3
Explanation: If we split the text along the space characters, the split text, where the offset is in, is the fragment `sit` which is the 3rd split (count starts at zero) and the offset inside this fragment is 2
sit
--^
012
Some more examples
Text Offset Result
Lorem ipsum dolor sit amet 0 0, 0
^
Lorem ipsum dolor sit amet 1 1, 0
-^
Lorem ipsum dolor sit amet 2 2, 0
--^
Lorem ipsum dolor sit amet 3 3, 0
---^
Lorem ipsum dolor sit amet 4 4, 0
----^
Lorem ipsum dolor sit amet 5 5, 0
-----^
Lorem ipsum dolor sit amet 6 0, 1
------^
Lorem ipsum dolor sit amet 7 1, 1
-------^
Lorem ipsum dolor sit amet 8 2, 1
--------^
Lorem ipsum dolor sit amet 9 3, 1
---------^
Lorem ipsum dolor sit amet 10 4, 1
----------^
Lorem ipsum dolor sit amet 11 5, 1
-----------^
Lorem ipsum dolor sit amet 12 0, 2
------------^
Lorem ipsum dolor sit amet 13 1, 2
-------------^
Lorem ipsum dolor sit amet 14 2, 2
--------------^
Lorem ipsum dolor sit amet 15 3, 2
---------------^
Lorem ipsum dolor sit amet 16 4, 2
----------------^
Lorem ipsum dolor sit amet 17 5, 2
-----------------^
Lorem ipsum dolor sit amet 18 0, 3
------------------^
Lorem ipsum dolor sit amet 19 1, 3
-------------------^
Lorem ipsum dolor sit amet 20 2, 3
--------------------^
Lorem ipsum dolor sit amet 21 3, 3
---------------------^
Lorem ipsum dolor sit amet 22 0, 4
----------------------^
Lorem ipsum dolor sit amet 23 1, 4
-----------------------^
Lorem ipsum dolor sit amet 24 2, 4
------------------------^
Lorem ipsum dolor sit amet 25 3, 4
-------------------------^
Lorem ipsum dolor sit amet 26 4, 4
--------------------------^
code-golf text-processing
New contributor
$endgroup$
|
show 2 more comments
$begingroup$
You will be given an ASCII text. This text may have zero, one or many appearances of a split a character (let's assume it will be the space character )
You will also be given an offset integer value (zero based) anywhere between zero and the length of the text.
You should calculate the relative offset inside the one split text, which contains the absolute offset, as well as the index of that very split text.
If the given offset is at the split character the last split should have the offset
It's hard to describe (comments welcome), so here are some examples
text: Lorem ipsum dolor sit amet
offset: 20
Lorem ipsum dolor sit amet
enter code here
--------------------^
000000000011111111112
012345678901234567890
Result: 2, 3
Explanation: If we split the text along the space characters, the split text, where the offset is in, is the fragment `sit` which is the 3rd split (count starts at zero) and the offset inside this fragment is 2
sit
--^
012
Some more examples
Text Offset Result
Lorem ipsum dolor sit amet 0 0, 0
^
Lorem ipsum dolor sit amet 1 1, 0
-^
Lorem ipsum dolor sit amet 2 2, 0
--^
Lorem ipsum dolor sit amet 3 3, 0
---^
Lorem ipsum dolor sit amet 4 4, 0
----^
Lorem ipsum dolor sit amet 5 5, 0
-----^
Lorem ipsum dolor sit amet 6 0, 1
------^
Lorem ipsum dolor sit amet 7 1, 1
-------^
Lorem ipsum dolor sit amet 8 2, 1
--------^
Lorem ipsum dolor sit amet 9 3, 1
---------^
Lorem ipsum dolor sit amet 10 4, 1
----------^
Lorem ipsum dolor sit amet 11 5, 1
-----------^
Lorem ipsum dolor sit amet 12 0, 2
------------^
Lorem ipsum dolor sit amet 13 1, 2
-------------^
Lorem ipsum dolor sit amet 14 2, 2
--------------^
Lorem ipsum dolor sit amet 15 3, 2
---------------^
Lorem ipsum dolor sit amet 16 4, 2
----------------^
Lorem ipsum dolor sit amet 17 5, 2
-----------------^
Lorem ipsum dolor sit amet 18 0, 3
------------------^
Lorem ipsum dolor sit amet 19 1, 3
-------------------^
Lorem ipsum dolor sit amet 20 2, 3
--------------------^
Lorem ipsum dolor sit amet 21 3, 3
---------------------^
Lorem ipsum dolor sit amet 22 0, 4
----------------------^
Lorem ipsum dolor sit amet 23 1, 4
-----------------------^
Lorem ipsum dolor sit amet 24 2, 4
------------------------^
Lorem ipsum dolor sit amet 25 3, 4
-------------------------^
Lorem ipsum dolor sit amet 26 4, 4
--------------------------^
code-golf text-processing
New contributor
$endgroup$
$begingroup$
If I am correctly interpreting your example, the second integer is the number of split characters in the firstoffset
characters, the first is the character that is hit inside the chunk counted from its own origin? Do the two have anything in common?
$endgroup$
– Jonathan Frech
19 hours ago
$begingroup$
@JonathanFrech I assume your thought on the second integer is correct. I don't understand your thought on the first character. And what do you mean byDo the two have anything in common?
?
$endgroup$
– yunzen
19 hours ago
$begingroup$
To me, the question appears to simply be two properties of a piece of text mangled together; I wanted to ask if there is any semantic connection between the two output integers.
$endgroup$
– Jonathan Frech
19 hours ago
$begingroup$
You want to now the reason why I ask this? It's inspired by this question on SE: stackoverflow.com/questions/55235774/… And I thought it would be an interesting question on CG
$endgroup$
– yunzen
18 hours ago
5
$begingroup$
"It's hard to describe (comments welcome)". This is why the Sandbox exists. When you want to create challenges, first lay them in the sandbox and let other people help you refine it.
$endgroup$
– Olivier Grégoire
15 hours ago
|
show 2 more comments
$begingroup$
You will be given an ASCII text. This text may have zero, one or many appearances of a split a character (let's assume it will be the space character )
You will also be given an offset integer value (zero based) anywhere between zero and the length of the text.
You should calculate the relative offset inside the one split text, which contains the absolute offset, as well as the index of that very split text.
If the given offset is at the split character the last split should have the offset
It's hard to describe (comments welcome), so here are some examples
text: Lorem ipsum dolor sit amet
offset: 20
Lorem ipsum dolor sit amet
enter code here
--------------------^
000000000011111111112
012345678901234567890
Result: 2, 3
Explanation: If we split the text along the space characters, the split text, where the offset is in, is the fragment `sit` which is the 3rd split (count starts at zero) and the offset inside this fragment is 2
sit
--^
012
Some more examples
Text Offset Result
Lorem ipsum dolor sit amet 0 0, 0
^
Lorem ipsum dolor sit amet 1 1, 0
-^
Lorem ipsum dolor sit amet 2 2, 0
--^
Lorem ipsum dolor sit amet 3 3, 0
---^
Lorem ipsum dolor sit amet 4 4, 0
----^
Lorem ipsum dolor sit amet 5 5, 0
-----^
Lorem ipsum dolor sit amet 6 0, 1
------^
Lorem ipsum dolor sit amet 7 1, 1
-------^
Lorem ipsum dolor sit amet 8 2, 1
--------^
Lorem ipsum dolor sit amet 9 3, 1
---------^
Lorem ipsum dolor sit amet 10 4, 1
----------^
Lorem ipsum dolor sit amet 11 5, 1
-----------^
Lorem ipsum dolor sit amet 12 0, 2
------------^
Lorem ipsum dolor sit amet 13 1, 2
-------------^
Lorem ipsum dolor sit amet 14 2, 2
--------------^
Lorem ipsum dolor sit amet 15 3, 2
---------------^
Lorem ipsum dolor sit amet 16 4, 2
----------------^
Lorem ipsum dolor sit amet 17 5, 2
-----------------^
Lorem ipsum dolor sit amet 18 0, 3
------------------^
Lorem ipsum dolor sit amet 19 1, 3
-------------------^
Lorem ipsum dolor sit amet 20 2, 3
--------------------^
Lorem ipsum dolor sit amet 21 3, 3
---------------------^
Lorem ipsum dolor sit amet 22 0, 4
----------------------^
Lorem ipsum dolor sit amet 23 1, 4
-----------------------^
Lorem ipsum dolor sit amet 24 2, 4
------------------------^
Lorem ipsum dolor sit amet 25 3, 4
-------------------------^
Lorem ipsum dolor sit amet 26 4, 4
--------------------------^
code-golf text-processing
New contributor
$endgroup$
You will be given an ASCII text. This text may have zero, one or many appearances of a split a character (let's assume it will be the space character )
You will also be given an offset integer value (zero based) anywhere between zero and the length of the text.
You should calculate the relative offset inside the one split text, which contains the absolute offset, as well as the index of that very split text.
If the given offset is at the split character the last split should have the offset
It's hard to describe (comments welcome), so here are some examples
text: Lorem ipsum dolor sit amet
offset: 20
Lorem ipsum dolor sit amet
enter code here
--------------------^
000000000011111111112
012345678901234567890
Result: 2, 3
Explanation: If we split the text along the space characters, the split text, where the offset is in, is the fragment `sit` which is the 3rd split (count starts at zero) and the offset inside this fragment is 2
sit
--^
012
Some more examples
Text Offset Result
Lorem ipsum dolor sit amet 0 0, 0
^
Lorem ipsum dolor sit amet 1 1, 0
-^
Lorem ipsum dolor sit amet 2 2, 0
--^
Lorem ipsum dolor sit amet 3 3, 0
---^
Lorem ipsum dolor sit amet 4 4, 0
----^
Lorem ipsum dolor sit amet 5 5, 0
-----^
Lorem ipsum dolor sit amet 6 0, 1
------^
Lorem ipsum dolor sit amet 7 1, 1
-------^
Lorem ipsum dolor sit amet 8 2, 1
--------^
Lorem ipsum dolor sit amet 9 3, 1
---------^
Lorem ipsum dolor sit amet 10 4, 1
----------^
Lorem ipsum dolor sit amet 11 5, 1
-----------^
Lorem ipsum dolor sit amet 12 0, 2
------------^
Lorem ipsum dolor sit amet 13 1, 2
-------------^
Lorem ipsum dolor sit amet 14 2, 2
--------------^
Lorem ipsum dolor sit amet 15 3, 2
---------------^
Lorem ipsum dolor sit amet 16 4, 2
----------------^
Lorem ipsum dolor sit amet 17 5, 2
-----------------^
Lorem ipsum dolor sit amet 18 0, 3
------------------^
Lorem ipsum dolor sit amet 19 1, 3
-------------------^
Lorem ipsum dolor sit amet 20 2, 3
--------------------^
Lorem ipsum dolor sit amet 21 3, 3
---------------------^
Lorem ipsum dolor sit amet 22 0, 4
----------------------^
Lorem ipsum dolor sit amet 23 1, 4
-----------------------^
Lorem ipsum dolor sit amet 24 2, 4
------------------------^
Lorem ipsum dolor sit amet 25 3, 4
-------------------------^
Lorem ipsum dolor sit amet 26 4, 4
--------------------------^
code-golf text-processing
code-golf text-processing
New contributor
New contributor
edited 18 hours ago
yunzen
New contributor
asked 20 hours ago
yunzenyunzen
1565
1565
New contributor
New contributor
$begingroup$
If I am correctly interpreting your example, the second integer is the number of split characters in the firstoffset
characters, the first is the character that is hit inside the chunk counted from its own origin? Do the two have anything in common?
$endgroup$
– Jonathan Frech
19 hours ago
$begingroup$
@JonathanFrech I assume your thought on the second integer is correct. I don't understand your thought on the first character. And what do you mean byDo the two have anything in common?
?
$endgroup$
– yunzen
19 hours ago
$begingroup$
To me, the question appears to simply be two properties of a piece of text mangled together; I wanted to ask if there is any semantic connection between the two output integers.
$endgroup$
– Jonathan Frech
19 hours ago
$begingroup$
You want to now the reason why I ask this? It's inspired by this question on SE: stackoverflow.com/questions/55235774/… And I thought it would be an interesting question on CG
$endgroup$
– yunzen
18 hours ago
5
$begingroup$
"It's hard to describe (comments welcome)". This is why the Sandbox exists. When you want to create challenges, first lay them in the sandbox and let other people help you refine it.
$endgroup$
– Olivier Grégoire
15 hours ago
|
show 2 more comments
$begingroup$
If I am correctly interpreting your example, the second integer is the number of split characters in the firstoffset
characters, the first is the character that is hit inside the chunk counted from its own origin? Do the two have anything in common?
$endgroup$
– Jonathan Frech
19 hours ago
$begingroup$
@JonathanFrech I assume your thought on the second integer is correct. I don't understand your thought on the first character. And what do you mean byDo the two have anything in common?
?
$endgroup$
– yunzen
19 hours ago
$begingroup$
To me, the question appears to simply be two properties of a piece of text mangled together; I wanted to ask if there is any semantic connection between the two output integers.
$endgroup$
– Jonathan Frech
19 hours ago
$begingroup$
You want to now the reason why I ask this? It's inspired by this question on SE: stackoverflow.com/questions/55235774/… And I thought it would be an interesting question on CG
$endgroup$
– yunzen
18 hours ago
5
$begingroup$
"It's hard to describe (comments welcome)". This is why the Sandbox exists. When you want to create challenges, first lay them in the sandbox and let other people help you refine it.
$endgroup$
– Olivier Grégoire
15 hours ago
$begingroup$
If I am correctly interpreting your example, the second integer is the number of split characters in the first
offset
characters, the first is the character that is hit inside the chunk counted from its own origin? Do the two have anything in common?$endgroup$
– Jonathan Frech
19 hours ago
$begingroup$
If I am correctly interpreting your example, the second integer is the number of split characters in the first
offset
characters, the first is the character that is hit inside the chunk counted from its own origin? Do the two have anything in common?$endgroup$
– Jonathan Frech
19 hours ago
$begingroup$
@JonathanFrech I assume your thought on the second integer is correct. I don't understand your thought on the first character. And what do you mean by
Do the two have anything in common?
?$endgroup$
– yunzen
19 hours ago
$begingroup$
@JonathanFrech I assume your thought on the second integer is correct. I don't understand your thought on the first character. And what do you mean by
Do the two have anything in common?
?$endgroup$
– yunzen
19 hours ago
$begingroup$
To me, the question appears to simply be two properties of a piece of text mangled together; I wanted to ask if there is any semantic connection between the two output integers.
$endgroup$
– Jonathan Frech
19 hours ago
$begingroup$
To me, the question appears to simply be two properties of a piece of text mangled together; I wanted to ask if there is any semantic connection between the two output integers.
$endgroup$
– Jonathan Frech
19 hours ago
$begingroup$
You want to now the reason why I ask this? It's inspired by this question on SE: stackoverflow.com/questions/55235774/… And I thought it would be an interesting question on CG
$endgroup$
– yunzen
18 hours ago
$begingroup$
You want to now the reason why I ask this? It's inspired by this question on SE: stackoverflow.com/questions/55235774/… And I thought it would be an interesting question on CG
$endgroup$
– yunzen
18 hours ago
5
5
$begingroup$
"It's hard to describe (comments welcome)". This is why the Sandbox exists. When you want to create challenges, first lay them in the sandbox and let other people help you refine it.
$endgroup$
– Olivier Grégoire
15 hours ago
$begingroup$
"It's hard to describe (comments welcome)". This is why the Sandbox exists. When you want to create challenges, first lay them in the sandbox and let other people help you refine it.
$endgroup$
– Olivier Grégoire
15 hours ago
|
show 2 more comments
13 Answers
13
active
oldest
votes
$begingroup$
Python 3.8, 56 bytes
lambda a,b:(x:=a[:b].split(' '))and[len(x[-1]),len(x)-1]
Try it online!
$endgroup$
add a comment |
$begingroup$
Perl 5, 47 44 bytes
$_=substr$_,0,<>;/.* /;$_=$'=~y///c.$".y/ //
$_=substr$_,0,<>;
to extract first n (from nextline <>) characters of input$_
and store in default argument./.* /
to match largest string ending with space$_=$'=~y///c.$".y/ //
default argument$_
(which will be printed-p
option) is set with concatenation.
of length of post-match :$'=~y///c
,$"
space (list argument delimiter when interpolated betwen double quotes), andy/ //
number of spaces in current default argument.
TIO
$endgroup$
$begingroup$
Oh wow! How very cryptic of you
$endgroup$
– yunzen
15 hours ago
add a comment |
$begingroup$
05AB1E, 9 bytes
£ð¡¤gsg<‚
Port of @JonasAusevicius's Python 3.8 answer, so make sure to upvote him as well!
Try it online or verify all test cases.
Explanation:
£ # Only leave the first (implicit) input-integer amount of characters
# of the (implicit) string-input
ð¡ # Split this by spaces
# (Note: builtin `#` doesn't work here, because a string without spaces
# would not be wrapped in a list)
¤g # Get the length of the last string in the list (without popping the list itself)
sg< # Swap to get the list again, and get it's length minus 1
‚ # Pair both integers (and output the result implicitly)
$endgroup$
add a comment |
$begingroup$
JavaScript (Node.js), 75 70 69 70 68 bytes
(t,o)=>[o+~(s=t.substr(0,o)).lastIndexOf(' '),s.split(' ').length-1]
Try it online!
No space parameter, only hardcoded (+ 2 bytes)
-2 bytes thanks to inspiration from the Java answer by Kevin Cruijssen
New contributor
$endgroup$
1
$begingroup$
o-s.lastIndexOf(d)-1
can beo+~s.lastIndexOf(d)
to save a byte (here the relevant tip).
$endgroup$
– Kevin Cruijssen
16 hours ago
$begingroup$
Also, you take the space as input, but all the others answers thus far hardcoded the space. So either the challenge should mention a third split-character is required as input, or this answer could also hardcode the space. Not sure which of the two you intended when you made the challenge?
$endgroup$
– Kevin Cruijssen
16 hours ago
$begingroup$
@KevinCruijssen I used a hardcoded space now
$endgroup$
– yunzen
16 hours ago
$begingroup$
You can dot=>o=>
instead of(t,o)=>
to save a byte and then take input asf(t)(o)
$endgroup$
– zevee
12 hours ago
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 79 77 75 63 60 bytes
a=>b=>((a=a.Substring(0,b).Split())[b=a.Length-1].Length,b);
8 bytes saved thanks to Kevin Cruijssen and a further 3 bytes saved thanks to Embodiment of Ignorance
Try it online!
New contributor
$endgroup$
$begingroup$
73 bytes
$endgroup$
– Kevin Cruijssen
15 hours ago
1
$begingroup$
Cut another 10 using dynamic and saving the extra split
$endgroup$
– Expired Data
15 hours ago
$begingroup$
Oh smart! I need to remember that.
$endgroup$
– Kevin Cruijssen
15 hours ago
$begingroup$
60 bytes: tio.run/…
$endgroup$
– Embodiment of Ignorance
12 hours ago
add a comment |
$begingroup$
Stax, 5 bytes
╓ò)♣*
Run and debug it
Unpacked, ungolfed, and commented, it looks like this.
( trim string to length
j split on spaces
N remove last element from array and push separately
W until the stack is empty...
%P pop element and print its length
Run this one
$endgroup$
add a comment |
$begingroup$
APL+WIN, 33 bytes
(1++/m),n-¯1↑(m←(m←n↑⎕)=' ')/⍳n←⎕
Try it online! Courtesy of Dyalog Classic
1 Indexed
Prompts for index of character in full text string followed by string
Outputs index of character in nth substring and substring number
$endgroup$
add a comment |
$begingroup$
Java 8, 83 75 bytes
n->s->n+~(s=s.substring(0,n)).lastIndexOf(" ")+","+~-s.split(" ",-1).length
-8 bytes implicitly thanks to inspiration from @yunzen's JavaScript answer.
Try it online.
Explanation:
n->s-> // Method with integer & String parameters and String return-type
n+~ // Return `n` minus 1, minus the following:
(s=s.substring(0,n))// Set `s` to the first `n` characters of input-String `s`
.lastIndexOf(" ") // and get the index of the last space
+"," // Appended with a comma-delimiter
s.split(" ", // Split the new `s` by spaces
-1) // and keep empty trailing items
+~- ... .length // And append the amount of items minus 1 to the result
$endgroup$
add a comment |
$begingroup$
Jelly, 7 bytes
ḣḲẈṪ,LƊ
A dyadic Link accepting a list of characters on the left and the 0-indexed index on the right which yields a list of integers, [character, word]
, again 0-indexed.
Try it online!
$endgroup$
$begingroup$
I think this should return[4, 4]
(10-byte fix).
$endgroup$
– Erik the Outgolfer
12 hours ago
$begingroup$
Thanks @EriktheOutgolfer if we go with 0-indexing it works out nicely for 7.
$endgroup$
– Jonathan Allan
11 hours ago
add a comment |
$begingroup$
JavaScript (ES6), 56 bytes
Takes input as (offset)(string)
.
n=>g=([c,...a],b=i=0)=>n--?g(a,b+!(i+=c!=' '||-i)):[i,b]
Try it online!
Commented
n => // n = desired offset in string
g = ( // g = recursive function taking:
[c, ...a], // c = current character; a[] = array of remaining characters
b = // b = index of current block
i = 0 // i = current position within the current block
) => //
n-- ? // if we haven't reached the desired offset:
g( // do a recursive call to g:
a, // pass the array of remaining characters
b + !( // update b:
i += // update i:
c != ' ' // increment i if the current character is not a space
|| -i // or set i to zero if it is
) // increment b if i was set to zero, or leave it unchanged otherwise
) // end of recursive call
: // else:
[i, b] // stop recursion and return [i, b]
$endgroup$
add a comment |
$begingroup$
Bash, 66 bytes
z=$(echo $i:0:o);echo $z|sed 's/^.* //g'|wc -c;set -- $z;echo $#
1-indexed. $i
contains the input, and $o
contains the offset
Explanation:
z=$(echo $LDS:0:o); # delete the rest of the word
echo $z|sed 's/^.* //g'|wc -c; # remove anything up to the last word, and count what's left.
set -- $z;echo $# # find the number of words after the stuff after the offset is deleted
$endgroup$
add a comment |
$begingroup$
Charcoal, 14 bytes
≔⪪…SN θI⟦L⊟θLθ
Try it online! Link is to verbose version of code. Explanation:
≔⪪…SN θ
Truncate the input string to the given length and split on spaces.
I⟦L⊟θLθ
Extract the last string and take its length, then take the number of remaining strings, cast both lengths to string and implicitly print them on separate lines.
$endgroup$
add a comment |
$begingroup$
Retina 0.8.2, 50 bytes
^.+
$*
^(1)*¶((?<-1>.)*).*
$2
^(.*? )*(.*)
$.2,$#1
Try it online! Takes input in the order offset, string. Explanation:
^.+
$*
Convert the offset to unary.
^(1)*¶((?<-1>.)*).*
$2
Truncate the string to the given length. This uses .NET's balancing groups to count the same number of characters in each string.
^(.*? )*(.*)
$.2,$#1
Count the number of spaces and the number of characters after the last space and output them in decimal in reverse order.
$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
);
);
yunzen is a new contributor. Be nice, and check out our Code of Conduct.
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%2f181794%2foffset-in-split-text-content%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
13 Answers
13
active
oldest
votes
13 Answers
13
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Python 3.8, 56 bytes
lambda a,b:(x:=a[:b].split(' '))and[len(x[-1]),len(x)-1]
Try it online!
$endgroup$
add a comment |
$begingroup$
Python 3.8, 56 bytes
lambda a,b:(x:=a[:b].split(' '))and[len(x[-1]),len(x)-1]
Try it online!
$endgroup$
add a comment |
$begingroup$
Python 3.8, 56 bytes
lambda a,b:(x:=a[:b].split(' '))and[len(x[-1]),len(x)-1]
Try it online!
$endgroup$
Python 3.8, 56 bytes
lambda a,b:(x:=a[:b].split(' '))and[len(x[-1]),len(x)-1]
Try it online!
answered 19 hours ago
Jonas AuseviciusJonas Ausevicius
1413
1413
add a comment |
add a comment |
$begingroup$
Perl 5, 47 44 bytes
$_=substr$_,0,<>;/.* /;$_=$'=~y///c.$".y/ //
$_=substr$_,0,<>;
to extract first n (from nextline <>) characters of input$_
and store in default argument./.* /
to match largest string ending with space$_=$'=~y///c.$".y/ //
default argument$_
(which will be printed-p
option) is set with concatenation.
of length of post-match :$'=~y///c
,$"
space (list argument delimiter when interpolated betwen double quotes), andy/ //
number of spaces in current default argument.
TIO
$endgroup$
$begingroup$
Oh wow! How very cryptic of you
$endgroup$
– yunzen
15 hours ago
add a comment |
$begingroup$
Perl 5, 47 44 bytes
$_=substr$_,0,<>;/.* /;$_=$'=~y///c.$".y/ //
$_=substr$_,0,<>;
to extract first n (from nextline <>) characters of input$_
and store in default argument./.* /
to match largest string ending with space$_=$'=~y///c.$".y/ //
default argument$_
(which will be printed-p
option) is set with concatenation.
of length of post-match :$'=~y///c
,$"
space (list argument delimiter when interpolated betwen double quotes), andy/ //
number of spaces in current default argument.
TIO
$endgroup$
$begingroup$
Oh wow! How very cryptic of you
$endgroup$
– yunzen
15 hours ago
add a comment |
$begingroup$
Perl 5, 47 44 bytes
$_=substr$_,0,<>;/.* /;$_=$'=~y///c.$".y/ //
$_=substr$_,0,<>;
to extract first n (from nextline <>) characters of input$_
and store in default argument./.* /
to match largest string ending with space$_=$'=~y///c.$".y/ //
default argument$_
(which will be printed-p
option) is set with concatenation.
of length of post-match :$'=~y///c
,$"
space (list argument delimiter when interpolated betwen double quotes), andy/ //
number of spaces in current default argument.
TIO
$endgroup$
Perl 5, 47 44 bytes
$_=substr$_,0,<>;/.* /;$_=$'=~y///c.$".y/ //
$_=substr$_,0,<>;
to extract first n (from nextline <>) characters of input$_
and store in default argument./.* /
to match largest string ending with space$_=$'=~y///c.$".y/ //
default argument$_
(which will be printed-p
option) is set with concatenation.
of length of post-match :$'=~y///c
,$"
space (list argument delimiter when interpolated betwen double quotes), andy/ //
number of spaces in current default argument.
TIO
edited 14 hours ago
answered 16 hours ago
Nahuel FouilleulNahuel Fouilleul
2,815210
2,815210
$begingroup$
Oh wow! How very cryptic of you
$endgroup$
– yunzen
15 hours ago
add a comment |
$begingroup$
Oh wow! How very cryptic of you
$endgroup$
– yunzen
15 hours ago
$begingroup$
Oh wow! How very cryptic of you
$endgroup$
– yunzen
15 hours ago
$begingroup$
Oh wow! How very cryptic of you
$endgroup$
– yunzen
15 hours ago
add a comment |
$begingroup$
05AB1E, 9 bytes
£ð¡¤gsg<‚
Port of @JonasAusevicius's Python 3.8 answer, so make sure to upvote him as well!
Try it online or verify all test cases.
Explanation:
£ # Only leave the first (implicit) input-integer amount of characters
# of the (implicit) string-input
ð¡ # Split this by spaces
# (Note: builtin `#` doesn't work here, because a string without spaces
# would not be wrapped in a list)
¤g # Get the length of the last string in the list (without popping the list itself)
sg< # Swap to get the list again, and get it's length minus 1
‚ # Pair both integers (and output the result implicitly)
$endgroup$
add a comment |
$begingroup$
05AB1E, 9 bytes
£ð¡¤gsg<‚
Port of @JonasAusevicius's Python 3.8 answer, so make sure to upvote him as well!
Try it online or verify all test cases.
Explanation:
£ # Only leave the first (implicit) input-integer amount of characters
# of the (implicit) string-input
ð¡ # Split this by spaces
# (Note: builtin `#` doesn't work here, because a string without spaces
# would not be wrapped in a list)
¤g # Get the length of the last string in the list (without popping the list itself)
sg< # Swap to get the list again, and get it's length minus 1
‚ # Pair both integers (and output the result implicitly)
$endgroup$
add a comment |
$begingroup$
05AB1E, 9 bytes
£ð¡¤gsg<‚
Port of @JonasAusevicius's Python 3.8 answer, so make sure to upvote him as well!
Try it online or verify all test cases.
Explanation:
£ # Only leave the first (implicit) input-integer amount of characters
# of the (implicit) string-input
ð¡ # Split this by spaces
# (Note: builtin `#` doesn't work here, because a string without spaces
# would not be wrapped in a list)
¤g # Get the length of the last string in the list (without popping the list itself)
sg< # Swap to get the list again, and get it's length minus 1
‚ # Pair both integers (and output the result implicitly)
$endgroup$
05AB1E, 9 bytes
£ð¡¤gsg<‚
Port of @JonasAusevicius's Python 3.8 answer, so make sure to upvote him as well!
Try it online or verify all test cases.
Explanation:
£ # Only leave the first (implicit) input-integer amount of characters
# of the (implicit) string-input
ð¡ # Split this by spaces
# (Note: builtin `#` doesn't work here, because a string without spaces
# would not be wrapped in a list)
¤g # Get the length of the last string in the list (without popping the list itself)
sg< # Swap to get the list again, and get it's length minus 1
‚ # Pair both integers (and output the result implicitly)
answered 18 hours ago
Kevin CruijssenKevin Cruijssen
40.8k566210
40.8k566210
add a comment |
add a comment |
$begingroup$
JavaScript (Node.js), 75 70 69 70 68 bytes
(t,o)=>[o+~(s=t.substr(0,o)).lastIndexOf(' '),s.split(' ').length-1]
Try it online!
No space parameter, only hardcoded (+ 2 bytes)
-2 bytes thanks to inspiration from the Java answer by Kevin Cruijssen
New contributor
$endgroup$
1
$begingroup$
o-s.lastIndexOf(d)-1
can beo+~s.lastIndexOf(d)
to save a byte (here the relevant tip).
$endgroup$
– Kevin Cruijssen
16 hours ago
$begingroup$
Also, you take the space as input, but all the others answers thus far hardcoded the space. So either the challenge should mention a third split-character is required as input, or this answer could also hardcode the space. Not sure which of the two you intended when you made the challenge?
$endgroup$
– Kevin Cruijssen
16 hours ago
$begingroup$
@KevinCruijssen I used a hardcoded space now
$endgroup$
– yunzen
16 hours ago
$begingroup$
You can dot=>o=>
instead of(t,o)=>
to save a byte and then take input asf(t)(o)
$endgroup$
– zevee
12 hours ago
add a comment |
$begingroup$
JavaScript (Node.js), 75 70 69 70 68 bytes
(t,o)=>[o+~(s=t.substr(0,o)).lastIndexOf(' '),s.split(' ').length-1]
Try it online!
No space parameter, only hardcoded (+ 2 bytes)
-2 bytes thanks to inspiration from the Java answer by Kevin Cruijssen
New contributor
$endgroup$
1
$begingroup$
o-s.lastIndexOf(d)-1
can beo+~s.lastIndexOf(d)
to save a byte (here the relevant tip).
$endgroup$
– Kevin Cruijssen
16 hours ago
$begingroup$
Also, you take the space as input, but all the others answers thus far hardcoded the space. So either the challenge should mention a third split-character is required as input, or this answer could also hardcode the space. Not sure which of the two you intended when you made the challenge?
$endgroup$
– Kevin Cruijssen
16 hours ago
$begingroup$
@KevinCruijssen I used a hardcoded space now
$endgroup$
– yunzen
16 hours ago
$begingroup$
You can dot=>o=>
instead of(t,o)=>
to save a byte and then take input asf(t)(o)
$endgroup$
– zevee
12 hours ago
add a comment |
$begingroup$
JavaScript (Node.js), 75 70 69 70 68 bytes
(t,o)=>[o+~(s=t.substr(0,o)).lastIndexOf(' '),s.split(' ').length-1]
Try it online!
No space parameter, only hardcoded (+ 2 bytes)
-2 bytes thanks to inspiration from the Java answer by Kevin Cruijssen
New contributor
$endgroup$
JavaScript (Node.js), 75 70 69 70 68 bytes
(t,o)=>[o+~(s=t.substr(0,o)).lastIndexOf(' '),s.split(' ').length-1]
Try it online!
No space parameter, only hardcoded (+ 2 bytes)
-2 bytes thanks to inspiration from the Java answer by Kevin Cruijssen
New contributor
edited 14 hours ago
New contributor
answered 17 hours ago
yunzenyunzen
1565
1565
New contributor
New contributor
1
$begingroup$
o-s.lastIndexOf(d)-1
can beo+~s.lastIndexOf(d)
to save a byte (here the relevant tip).
$endgroup$
– Kevin Cruijssen
16 hours ago
$begingroup$
Also, you take the space as input, but all the others answers thus far hardcoded the space. So either the challenge should mention a third split-character is required as input, or this answer could also hardcode the space. Not sure which of the two you intended when you made the challenge?
$endgroup$
– Kevin Cruijssen
16 hours ago
$begingroup$
@KevinCruijssen I used a hardcoded space now
$endgroup$
– yunzen
16 hours ago
$begingroup$
You can dot=>o=>
instead of(t,o)=>
to save a byte and then take input asf(t)(o)
$endgroup$
– zevee
12 hours ago
add a comment |
1
$begingroup$
o-s.lastIndexOf(d)-1
can beo+~s.lastIndexOf(d)
to save a byte (here the relevant tip).
$endgroup$
– Kevin Cruijssen
16 hours ago
$begingroup$
Also, you take the space as input, but all the others answers thus far hardcoded the space. So either the challenge should mention a third split-character is required as input, or this answer could also hardcode the space. Not sure which of the two you intended when you made the challenge?
$endgroup$
– Kevin Cruijssen
16 hours ago
$begingroup$
@KevinCruijssen I used a hardcoded space now
$endgroup$
– yunzen
16 hours ago
$begingroup$
You can dot=>o=>
instead of(t,o)=>
to save a byte and then take input asf(t)(o)
$endgroup$
– zevee
12 hours ago
1
1
$begingroup$
o-s.lastIndexOf(d)-1
can be o+~s.lastIndexOf(d)
to save a byte (here the relevant tip).$endgroup$
– Kevin Cruijssen
16 hours ago
$begingroup$
o-s.lastIndexOf(d)-1
can be o+~s.lastIndexOf(d)
to save a byte (here the relevant tip).$endgroup$
– Kevin Cruijssen
16 hours ago
$begingroup$
Also, you take the space as input, but all the others answers thus far hardcoded the space. So either the challenge should mention a third split-character is required as input, or this answer could also hardcode the space. Not sure which of the two you intended when you made the challenge?
$endgroup$
– Kevin Cruijssen
16 hours ago
$begingroup$
Also, you take the space as input, but all the others answers thus far hardcoded the space. So either the challenge should mention a third split-character is required as input, or this answer could also hardcode the space. Not sure which of the two you intended when you made the challenge?
$endgroup$
– Kevin Cruijssen
16 hours ago
$begingroup$
@KevinCruijssen I used a hardcoded space now
$endgroup$
– yunzen
16 hours ago
$begingroup$
@KevinCruijssen I used a hardcoded space now
$endgroup$
– yunzen
16 hours ago
$begingroup$
You can do
t=>o=>
instead of (t,o)=>
to save a byte and then take input as f(t)(o)
$endgroup$
– zevee
12 hours ago
$begingroup$
You can do
t=>o=>
instead of (t,o)=>
to save a byte and then take input as f(t)(o)
$endgroup$
– zevee
12 hours ago
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 79 77 75 63 60 bytes
a=>b=>((a=a.Substring(0,b).Split())[b=a.Length-1].Length,b);
8 bytes saved thanks to Kevin Cruijssen and a further 3 bytes saved thanks to Embodiment of Ignorance
Try it online!
New contributor
$endgroup$
$begingroup$
73 bytes
$endgroup$
– Kevin Cruijssen
15 hours ago
1
$begingroup$
Cut another 10 using dynamic and saving the extra split
$endgroup$
– Expired Data
15 hours ago
$begingroup$
Oh smart! I need to remember that.
$endgroup$
– Kevin Cruijssen
15 hours ago
$begingroup$
60 bytes: tio.run/…
$endgroup$
– Embodiment of Ignorance
12 hours ago
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 79 77 75 63 60 bytes
a=>b=>((a=a.Substring(0,b).Split())[b=a.Length-1].Length,b);
8 bytes saved thanks to Kevin Cruijssen and a further 3 bytes saved thanks to Embodiment of Ignorance
Try it online!
New contributor
$endgroup$
$begingroup$
73 bytes
$endgroup$
– Kevin Cruijssen
15 hours ago
1
$begingroup$
Cut another 10 using dynamic and saving the extra split
$endgroup$
– Expired Data
15 hours ago
$begingroup$
Oh smart! I need to remember that.
$endgroup$
– Kevin Cruijssen
15 hours ago
$begingroup$
60 bytes: tio.run/…
$endgroup$
– Embodiment of Ignorance
12 hours ago
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 79 77 75 63 60 bytes
a=>b=>((a=a.Substring(0,b).Split())[b=a.Length-1].Length,b);
8 bytes saved thanks to Kevin Cruijssen and a further 3 bytes saved thanks to Embodiment of Ignorance
Try it online!
New contributor
$endgroup$
C# (Visual C# Interactive Compiler), 79 77 75 63 60 bytes
a=>b=>((a=a.Substring(0,b).Split())[b=a.Length-1].Length,b);
8 bytes saved thanks to Kevin Cruijssen and a further 3 bytes saved thanks to Embodiment of Ignorance
Try it online!
New contributor
edited 12 hours ago
New contributor
answered 16 hours ago
Expired DataExpired Data
2665
2665
New contributor
New contributor
$begingroup$
73 bytes
$endgroup$
– Kevin Cruijssen
15 hours ago
1
$begingroup$
Cut another 10 using dynamic and saving the extra split
$endgroup$
– Expired Data
15 hours ago
$begingroup$
Oh smart! I need to remember that.
$endgroup$
– Kevin Cruijssen
15 hours ago
$begingroup$
60 bytes: tio.run/…
$endgroup$
– Embodiment of Ignorance
12 hours ago
add a comment |
$begingroup$
73 bytes
$endgroup$
– Kevin Cruijssen
15 hours ago
1
$begingroup$
Cut another 10 using dynamic and saving the extra split
$endgroup$
– Expired Data
15 hours ago
$begingroup$
Oh smart! I need to remember that.
$endgroup$
– Kevin Cruijssen
15 hours ago
$begingroup$
60 bytes: tio.run/…
$endgroup$
– Embodiment of Ignorance
12 hours ago
$begingroup$
73 bytes
$endgroup$
– Kevin Cruijssen
15 hours ago
$begingroup$
73 bytes
$endgroup$
– Kevin Cruijssen
15 hours ago
1
1
$begingroup$
Cut another 10 using dynamic and saving the extra split
$endgroup$
– Expired Data
15 hours ago
$begingroup$
Cut another 10 using dynamic and saving the extra split
$endgroup$
– Expired Data
15 hours ago
$begingroup$
Oh smart! I need to remember that.
$endgroup$
– Kevin Cruijssen
15 hours ago
$begingroup$
Oh smart! I need to remember that.
$endgroup$
– Kevin Cruijssen
15 hours ago
$begingroup$
60 bytes: tio.run/…
$endgroup$
– Embodiment of Ignorance
12 hours ago
$begingroup$
60 bytes: tio.run/…
$endgroup$
– Embodiment of Ignorance
12 hours ago
add a comment |
$begingroup$
Stax, 5 bytes
╓ò)♣*
Run and debug it
Unpacked, ungolfed, and commented, it looks like this.
( trim string to length
j split on spaces
N remove last element from array and push separately
W until the stack is empty...
%P pop element and print its length
Run this one
$endgroup$
add a comment |
$begingroup$
Stax, 5 bytes
╓ò)♣*
Run and debug it
Unpacked, ungolfed, and commented, it looks like this.
( trim string to length
j split on spaces
N remove last element from array and push separately
W until the stack is empty...
%P pop element and print its length
Run this one
$endgroup$
add a comment |
$begingroup$
Stax, 5 bytes
╓ò)♣*
Run and debug it
Unpacked, ungolfed, and commented, it looks like this.
( trim string to length
j split on spaces
N remove last element from array and push separately
W until the stack is empty...
%P pop element and print its length
Run this one
$endgroup$
Stax, 5 bytes
╓ò)♣*
Run and debug it
Unpacked, ungolfed, and commented, it looks like this.
( trim string to length
j split on spaces
N remove last element from array and push separately
W until the stack is empty...
%P pop element and print its length
Run this one
answered 12 hours ago
recursiverecursive
5,5691322
5,5691322
add a comment |
add a comment |
$begingroup$
APL+WIN, 33 bytes
(1++/m),n-¯1↑(m←(m←n↑⎕)=' ')/⍳n←⎕
Try it online! Courtesy of Dyalog Classic
1 Indexed
Prompts for index of character in full text string followed by string
Outputs index of character in nth substring and substring number
$endgroup$
add a comment |
$begingroup$
APL+WIN, 33 bytes
(1++/m),n-¯1↑(m←(m←n↑⎕)=' ')/⍳n←⎕
Try it online! Courtesy of Dyalog Classic
1 Indexed
Prompts for index of character in full text string followed by string
Outputs index of character in nth substring and substring number
$endgroup$
add a comment |
$begingroup$
APL+WIN, 33 bytes
(1++/m),n-¯1↑(m←(m←n↑⎕)=' ')/⍳n←⎕
Try it online! Courtesy of Dyalog Classic
1 Indexed
Prompts for index of character in full text string followed by string
Outputs index of character in nth substring and substring number
$endgroup$
APL+WIN, 33 bytes
(1++/m),n-¯1↑(m←(m←n↑⎕)=' ')/⍳n←⎕
Try it online! Courtesy of Dyalog Classic
1 Indexed
Prompts for index of character in full text string followed by string
Outputs index of character in nth substring and substring number
answered 18 hours ago
GrahamGraham
2,55678
2,55678
add a comment |
add a comment |
$begingroup$
Java 8, 83 75 bytes
n->s->n+~(s=s.substring(0,n)).lastIndexOf(" ")+","+~-s.split(" ",-1).length
-8 bytes implicitly thanks to inspiration from @yunzen's JavaScript answer.
Try it online.
Explanation:
n->s-> // Method with integer & String parameters and String return-type
n+~ // Return `n` minus 1, minus the following:
(s=s.substring(0,n))// Set `s` to the first `n` characters of input-String `s`
.lastIndexOf(" ") // and get the index of the last space
+"," // Appended with a comma-delimiter
s.split(" ", // Split the new `s` by spaces
-1) // and keep empty trailing items
+~- ... .length // And append the amount of items minus 1 to the result
$endgroup$
add a comment |
$begingroup$
Java 8, 83 75 bytes
n->s->n+~(s=s.substring(0,n)).lastIndexOf(" ")+","+~-s.split(" ",-1).length
-8 bytes implicitly thanks to inspiration from @yunzen's JavaScript answer.
Try it online.
Explanation:
n->s-> // Method with integer & String parameters and String return-type
n+~ // Return `n` minus 1, minus the following:
(s=s.substring(0,n))// Set `s` to the first `n` characters of input-String `s`
.lastIndexOf(" ") // and get the index of the last space
+"," // Appended with a comma-delimiter
s.split(" ", // Split the new `s` by spaces
-1) // and keep empty trailing items
+~- ... .length // And append the amount of items minus 1 to the result
$endgroup$
add a comment |
$begingroup$
Java 8, 83 75 bytes
n->s->n+~(s=s.substring(0,n)).lastIndexOf(" ")+","+~-s.split(" ",-1).length
-8 bytes implicitly thanks to inspiration from @yunzen's JavaScript answer.
Try it online.
Explanation:
n->s-> // Method with integer & String parameters and String return-type
n+~ // Return `n` minus 1, minus the following:
(s=s.substring(0,n))// Set `s` to the first `n` characters of input-String `s`
.lastIndexOf(" ") // and get the index of the last space
+"," // Appended with a comma-delimiter
s.split(" ", // Split the new `s` by spaces
-1) // and keep empty trailing items
+~- ... .length // And append the amount of items minus 1 to the result
$endgroup$
Java 8, 83 75 bytes
n->s->n+~(s=s.substring(0,n)).lastIndexOf(" ")+","+~-s.split(" ",-1).length
-8 bytes implicitly thanks to inspiration from @yunzen's JavaScript answer.
Try it online.
Explanation:
n->s-> // Method with integer & String parameters and String return-type
n+~ // Return `n` minus 1, minus the following:
(s=s.substring(0,n))// Set `s` to the first `n` characters of input-String `s`
.lastIndexOf(" ") // and get the index of the last space
+"," // Appended with a comma-delimiter
s.split(" ", // Split the new `s` by spaces
-1) // and keep empty trailing items
+~- ... .length // And append the amount of items minus 1 to the result
edited 15 hours ago
answered 15 hours ago
Kevin CruijssenKevin Cruijssen
40.8k566210
40.8k566210
add a comment |
add a comment |
$begingroup$
Jelly, 7 bytes
ḣḲẈṪ,LƊ
A dyadic Link accepting a list of characters on the left and the 0-indexed index on the right which yields a list of integers, [character, word]
, again 0-indexed.
Try it online!
$endgroup$
$begingroup$
I think this should return[4, 4]
(10-byte fix).
$endgroup$
– Erik the Outgolfer
12 hours ago
$begingroup$
Thanks @EriktheOutgolfer if we go with 0-indexing it works out nicely for 7.
$endgroup$
– Jonathan Allan
11 hours ago
add a comment |
$begingroup$
Jelly, 7 bytes
ḣḲẈṪ,LƊ
A dyadic Link accepting a list of characters on the left and the 0-indexed index on the right which yields a list of integers, [character, word]
, again 0-indexed.
Try it online!
$endgroup$
$begingroup$
I think this should return[4, 4]
(10-byte fix).
$endgroup$
– Erik the Outgolfer
12 hours ago
$begingroup$
Thanks @EriktheOutgolfer if we go with 0-indexing it works out nicely for 7.
$endgroup$
– Jonathan Allan
11 hours ago
add a comment |
$begingroup$
Jelly, 7 bytes
ḣḲẈṪ,LƊ
A dyadic Link accepting a list of characters on the left and the 0-indexed index on the right which yields a list of integers, [character, word]
, again 0-indexed.
Try it online!
$endgroup$
Jelly, 7 bytes
ḣḲẈṪ,LƊ
A dyadic Link accepting a list of characters on the left and the 0-indexed index on the right which yields a list of integers, [character, word]
, again 0-indexed.
Try it online!
edited 11 hours ago
answered 15 hours ago
Jonathan AllanJonathan Allan
53.2k535172
53.2k535172
$begingroup$
I think this should return[4, 4]
(10-byte fix).
$endgroup$
– Erik the Outgolfer
12 hours ago
$begingroup$
Thanks @EriktheOutgolfer if we go with 0-indexing it works out nicely for 7.
$endgroup$
– Jonathan Allan
11 hours ago
add a comment |
$begingroup$
I think this should return[4, 4]
(10-byte fix).
$endgroup$
– Erik the Outgolfer
12 hours ago
$begingroup$
Thanks @EriktheOutgolfer if we go with 0-indexing it works out nicely for 7.
$endgroup$
– Jonathan Allan
11 hours ago
$begingroup$
I think this should return
[4, 4]
(10-byte fix).$endgroup$
– Erik the Outgolfer
12 hours ago
$begingroup$
I think this should return
[4, 4]
(10-byte fix).$endgroup$
– Erik the Outgolfer
12 hours ago
$begingroup$
Thanks @EriktheOutgolfer if we go with 0-indexing it works out nicely for 7.
$endgroup$
– Jonathan Allan
11 hours ago
$begingroup$
Thanks @EriktheOutgolfer if we go with 0-indexing it works out nicely for 7.
$endgroup$
– Jonathan Allan
11 hours ago
add a comment |
$begingroup$
JavaScript (ES6), 56 bytes
Takes input as (offset)(string)
.
n=>g=([c,...a],b=i=0)=>n--?g(a,b+!(i+=c!=' '||-i)):[i,b]
Try it online!
Commented
n => // n = desired offset in string
g = ( // g = recursive function taking:
[c, ...a], // c = current character; a[] = array of remaining characters
b = // b = index of current block
i = 0 // i = current position within the current block
) => //
n-- ? // if we haven't reached the desired offset:
g( // do a recursive call to g:
a, // pass the array of remaining characters
b + !( // update b:
i += // update i:
c != ' ' // increment i if the current character is not a space
|| -i // or set i to zero if it is
) // increment b if i was set to zero, or leave it unchanged otherwise
) // end of recursive call
: // else:
[i, b] // stop recursion and return [i, b]
$endgroup$
add a comment |
$begingroup$
JavaScript (ES6), 56 bytes
Takes input as (offset)(string)
.
n=>g=([c,...a],b=i=0)=>n--?g(a,b+!(i+=c!=' '||-i)):[i,b]
Try it online!
Commented
n => // n = desired offset in string
g = ( // g = recursive function taking:
[c, ...a], // c = current character; a[] = array of remaining characters
b = // b = index of current block
i = 0 // i = current position within the current block
) => //
n-- ? // if we haven't reached the desired offset:
g( // do a recursive call to g:
a, // pass the array of remaining characters
b + !( // update b:
i += // update i:
c != ' ' // increment i if the current character is not a space
|| -i // or set i to zero if it is
) // increment b if i was set to zero, or leave it unchanged otherwise
) // end of recursive call
: // else:
[i, b] // stop recursion and return [i, b]
$endgroup$
add a comment |
$begingroup$
JavaScript (ES6), 56 bytes
Takes input as (offset)(string)
.
n=>g=([c,...a],b=i=0)=>n--?g(a,b+!(i+=c!=' '||-i)):[i,b]
Try it online!
Commented
n => // n = desired offset in string
g = ( // g = recursive function taking:
[c, ...a], // c = current character; a[] = array of remaining characters
b = // b = index of current block
i = 0 // i = current position within the current block
) => //
n-- ? // if we haven't reached the desired offset:
g( // do a recursive call to g:
a, // pass the array of remaining characters
b + !( // update b:
i += // update i:
c != ' ' // increment i if the current character is not a space
|| -i // or set i to zero if it is
) // increment b if i was set to zero, or leave it unchanged otherwise
) // end of recursive call
: // else:
[i, b] // stop recursion and return [i, b]
$endgroup$
JavaScript (ES6), 56 bytes
Takes input as (offset)(string)
.
n=>g=([c,...a],b=i=0)=>n--?g(a,b+!(i+=c!=' '||-i)):[i,b]
Try it online!
Commented
n => // n = desired offset in string
g = ( // g = recursive function taking:
[c, ...a], // c = current character; a[] = array of remaining characters
b = // b = index of current block
i = 0 // i = current position within the current block
) => //
n-- ? // if we haven't reached the desired offset:
g( // do a recursive call to g:
a, // pass the array of remaining characters
b + !( // update b:
i += // update i:
c != ' ' // increment i if the current character is not a space
|| -i // or set i to zero if it is
) // increment b if i was set to zero, or leave it unchanged otherwise
) // end of recursive call
: // else:
[i, b] // stop recursion and return [i, b]
edited 13 hours ago
answered 13 hours ago
ArnauldArnauld
79.2k796330
79.2k796330
add a comment |
add a comment |
$begingroup$
Bash, 66 bytes
z=$(echo $i:0:o);echo $z|sed 's/^.* //g'|wc -c;set -- $z;echo $#
1-indexed. $i
contains the input, and $o
contains the offset
Explanation:
z=$(echo $LDS:0:o); # delete the rest of the word
echo $z|sed 's/^.* //g'|wc -c; # remove anything up to the last word, and count what's left.
set -- $z;echo $# # find the number of words after the stuff after the offset is deleted
$endgroup$
add a comment |
$begingroup$
Bash, 66 bytes
z=$(echo $i:0:o);echo $z|sed 's/^.* //g'|wc -c;set -- $z;echo $#
1-indexed. $i
contains the input, and $o
contains the offset
Explanation:
z=$(echo $LDS:0:o); # delete the rest of the word
echo $z|sed 's/^.* //g'|wc -c; # remove anything up to the last word, and count what's left.
set -- $z;echo $# # find the number of words after the stuff after the offset is deleted
$endgroup$
add a comment |
$begingroup$
Bash, 66 bytes
z=$(echo $i:0:o);echo $z|sed 's/^.* //g'|wc -c;set -- $z;echo $#
1-indexed. $i
contains the input, and $o
contains the offset
Explanation:
z=$(echo $LDS:0:o); # delete the rest of the word
echo $z|sed 's/^.* //g'|wc -c; # remove anything up to the last word, and count what's left.
set -- $z;echo $# # find the number of words after the stuff after the offset is deleted
$endgroup$
Bash, 66 bytes
z=$(echo $i:0:o);echo $z|sed 's/^.* //g'|wc -c;set -- $z;echo $#
1-indexed. $i
contains the input, and $o
contains the offset
Explanation:
z=$(echo $LDS:0:o); # delete the rest of the word
echo $z|sed 's/^.* //g'|wc -c; # remove anything up to the last word, and count what's left.
set -- $z;echo $# # find the number of words after the stuff after the offset is deleted
answered 12 hours ago
zeveezevee
58029
58029
add a comment |
add a comment |
$begingroup$
Charcoal, 14 bytes
≔⪪…SN θI⟦L⊟θLθ
Try it online! Link is to verbose version of code. Explanation:
≔⪪…SN θ
Truncate the input string to the given length and split on spaces.
I⟦L⊟θLθ
Extract the last string and take its length, then take the number of remaining strings, cast both lengths to string and implicitly print them on separate lines.
$endgroup$
add a comment |
$begingroup$
Charcoal, 14 bytes
≔⪪…SN θI⟦L⊟θLθ
Try it online! Link is to verbose version of code. Explanation:
≔⪪…SN θ
Truncate the input string to the given length and split on spaces.
I⟦L⊟θLθ
Extract the last string and take its length, then take the number of remaining strings, cast both lengths to string and implicitly print them on separate lines.
$endgroup$
add a comment |
$begingroup$
Charcoal, 14 bytes
≔⪪…SN θI⟦L⊟θLθ
Try it online! Link is to verbose version of code. Explanation:
≔⪪…SN θ
Truncate the input string to the given length and split on spaces.
I⟦L⊟θLθ
Extract the last string and take its length, then take the number of remaining strings, cast both lengths to string and implicitly print them on separate lines.
$endgroup$
Charcoal, 14 bytes
≔⪪…SN θI⟦L⊟θLθ
Try it online! Link is to verbose version of code. Explanation:
≔⪪…SN θ
Truncate the input string to the given length and split on spaces.
I⟦L⊟θLθ
Extract the last string and take its length, then take the number of remaining strings, cast both lengths to string and implicitly print them on separate lines.
answered 6 hours ago
NeilNeil
81.8k745178
81.8k745178
add a comment |
add a comment |
$begingroup$
Retina 0.8.2, 50 bytes
^.+
$*
^(1)*¶((?<-1>.)*).*
$2
^(.*? )*(.*)
$.2,$#1
Try it online! Takes input in the order offset, string. Explanation:
^.+
$*
Convert the offset to unary.
^(1)*¶((?<-1>.)*).*
$2
Truncate the string to the given length. This uses .NET's balancing groups to count the same number of characters in each string.
^(.*? )*(.*)
$.2,$#1
Count the number of spaces and the number of characters after the last space and output them in decimal in reverse order.
$endgroup$
add a comment |
$begingroup$
Retina 0.8.2, 50 bytes
^.+
$*
^(1)*¶((?<-1>.)*).*
$2
^(.*? )*(.*)
$.2,$#1
Try it online! Takes input in the order offset, string. Explanation:
^.+
$*
Convert the offset to unary.
^(1)*¶((?<-1>.)*).*
$2
Truncate the string to the given length. This uses .NET's balancing groups to count the same number of characters in each string.
^(.*? )*(.*)
$.2,$#1
Count the number of spaces and the number of characters after the last space and output them in decimal in reverse order.
$endgroup$
add a comment |
$begingroup$
Retina 0.8.2, 50 bytes
^.+
$*
^(1)*¶((?<-1>.)*).*
$2
^(.*? )*(.*)
$.2,$#1
Try it online! Takes input in the order offset, string. Explanation:
^.+
$*
Convert the offset to unary.
^(1)*¶((?<-1>.)*).*
$2
Truncate the string to the given length. This uses .NET's balancing groups to count the same number of characters in each string.
^(.*? )*(.*)
$.2,$#1
Count the number of spaces and the number of characters after the last space and output them in decimal in reverse order.
$endgroup$
Retina 0.8.2, 50 bytes
^.+
$*
^(1)*¶((?<-1>.)*).*
$2
^(.*? )*(.*)
$.2,$#1
Try it online! Takes input in the order offset, string. Explanation:
^.+
$*
Convert the offset to unary.
^(1)*¶((?<-1>.)*).*
$2
Truncate the string to the given length. This uses .NET's balancing groups to count the same number of characters in each string.
^(.*? )*(.*)
$.2,$#1
Count the number of spaces and the number of characters after the last space and output them in decimal in reverse order.
answered 6 hours ago
NeilNeil
81.8k745178
81.8k745178
add a comment |
add a comment |
yunzen is a new contributor. Be nice, and check out our Code of Conduct.
yunzen is a new contributor. Be nice, and check out our Code of Conduct.
yunzen is a new contributor. Be nice, and check out our Code of Conduct.
yunzen is a new contributor. Be nice, and check out our Code of Conduct.
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%2f181794%2foffset-in-split-text-content%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
$begingroup$
If I am correctly interpreting your example, the second integer is the number of split characters in the first
offset
characters, the first is the character that is hit inside the chunk counted from its own origin? Do the two have anything in common?$endgroup$
– Jonathan Frech
19 hours ago
$begingroup$
@JonathanFrech I assume your thought on the second integer is correct. I don't understand your thought on the first character. And what do you mean by
Do the two have anything in common?
?$endgroup$
– yunzen
19 hours ago
$begingroup$
To me, the question appears to simply be two properties of a piece of text mangled together; I wanted to ask if there is any semantic connection between the two output integers.
$endgroup$
– Jonathan Frech
19 hours ago
$begingroup$
You want to now the reason why I ask this? It's inspired by this question on SE: stackoverflow.com/questions/55235774/… And I thought it would be an interesting question on CG
$endgroup$
– yunzen
18 hours ago
5
$begingroup$
"It's hard to describe (comments welcome)". This is why the Sandbox exists. When you want to create challenges, first lay them in the sandbox and let other people help you refine it.
$endgroup$
– Olivier Grégoire
15 hours ago