Split Mark's marks
Challenge
Mark is a student who receives his N
marks in a concatenated way in a one single line.
The challenge is to separate his marks, knowing that each mark can only be 0
or 1
or 2
or 3
or 4
or 5
or 6
or 7
or 8
or 9
or 10
.
Input
N
natural number and one line.
Output
A set of natural numbers.
Example
N, One line------------------> Set of marks
3, '843'---------------------> [8, 4, 3]
1, '0'-----------------------> [0]
2, '1010'--------------------> [10,10]
3, '1010'--------------------> [1,0,10] or [10,1,0]
4, '1010'--------------------> [1,0,1,0]
9, '23104441070'-------------> [2, 3, 10, 4, 4, 4, 10, 7, 0]
12,'499102102121103'---------> [4, 9, 9, 10, 2, 10, 2, 1, 2, 1, 10, 3]
5, '71061'-------------------> [7, 1, 0, 6, 1]
11,'476565010684'------------> [4, 7, 6, 5, 6, 5, 0, 10, 6, 8, 4]
4, '1306'--------------------> [1, 3, 0, 6]
9, '51026221084'-------------> [5, 10, 2, 6, 2, 2, 10, 8, 4]
14,'851089085685524'---------> [8, 5, 10, 8, 9, 0, 8, 5, 6, 8, 5, 5, 2, 4]
11,'110840867780'------------> [1, 10, 8, 4, 0, 8, 6, 7, 7, 8, 0]
9, '4359893510'--------------> [4, 3, 5, 9, 8, 9, 3, 5, 10]
7, '99153710'----------------> [9, 9, 1, 5, 3, 7, 10]
14,'886171092313495'---------> [8, 8, 6, 1, 7, 10, 9, 2, 3, 1, 3, 4, 9, 5]
2, '44'----------------------> [4, 4]
4, '9386'--------------------> [9, 3, 8, 6]
Rules
- When several outputs are possible give only one output.
- Only mark of value
10
is on two decimal, others are on one decimal. - The input and output can be given in any convenient format
- No need to handle invalid input
- Either a full program or a function are acceptable. If a function, you can return the output rather than printing it.
- If possible, please include a link to an online testing environment so other people can try out your code!
Standard loopholes are forbidden.- This is code-golf so all usual golfing rules apply, and the shortest code (in bytes) wins.
code-golf string array-manipulation
|
show 3 more comments
Challenge
Mark is a student who receives his N
marks in a concatenated way in a one single line.
The challenge is to separate his marks, knowing that each mark can only be 0
or 1
or 2
or 3
or 4
or 5
or 6
or 7
or 8
or 9
or 10
.
Input
N
natural number and one line.
Output
A set of natural numbers.
Example
N, One line------------------> Set of marks
3, '843'---------------------> [8, 4, 3]
1, '0'-----------------------> [0]
2, '1010'--------------------> [10,10]
3, '1010'--------------------> [1,0,10] or [10,1,0]
4, '1010'--------------------> [1,0,1,0]
9, '23104441070'-------------> [2, 3, 10, 4, 4, 4, 10, 7, 0]
12,'499102102121103'---------> [4, 9, 9, 10, 2, 10, 2, 1, 2, 1, 10, 3]
5, '71061'-------------------> [7, 1, 0, 6, 1]
11,'476565010684'------------> [4, 7, 6, 5, 6, 5, 0, 10, 6, 8, 4]
4, '1306'--------------------> [1, 3, 0, 6]
9, '51026221084'-------------> [5, 10, 2, 6, 2, 2, 10, 8, 4]
14,'851089085685524'---------> [8, 5, 10, 8, 9, 0, 8, 5, 6, 8, 5, 5, 2, 4]
11,'110840867780'------------> [1, 10, 8, 4, 0, 8, 6, 7, 7, 8, 0]
9, '4359893510'--------------> [4, 3, 5, 9, 8, 9, 3, 5, 10]
7, '99153710'----------------> [9, 9, 1, 5, 3, 7, 10]
14,'886171092313495'---------> [8, 8, 6, 1, 7, 10, 9, 2, 3, 1, 3, 4, 9, 5]
2, '44'----------------------> [4, 4]
4, '9386'--------------------> [9, 3, 8, 6]
Rules
- When several outputs are possible give only one output.
- Only mark of value
10
is on two decimal, others are on one decimal. - The input and output can be given in any convenient format
- No need to handle invalid input
- Either a full program or a function are acceptable. If a function, you can return the output rather than printing it.
- If possible, please include a link to an online testing environment so other people can try out your code!
Standard loopholes are forbidden.- This is code-golf so all usual golfing rules apply, and the shortest code (in bytes) wins.
code-golf string array-manipulation
Here's a Python snippet I used to get then, 'string'
pairs from the copypasted example text block:spl = [item.split('-')[0] for item in text.split('n')]
– Gigaflop
Nov 28 '18 at 19:22
3
Plz some comments for down-votes...
– mdahmoune
Nov 29 '18 at 10:24
Downvotes don't require leaving comments for a reason. There is nothing that can be improved about this challenge.
– user202729
Nov 29 '18 at 14:55
So don't worry about it.
– user202729
Nov 29 '18 at 15:13
Are the outputs required to be in the same order as the input?
– Mnemonic
Nov 29 '18 at 20:01
|
show 3 more comments
Challenge
Mark is a student who receives his N
marks in a concatenated way in a one single line.
The challenge is to separate his marks, knowing that each mark can only be 0
or 1
or 2
or 3
or 4
or 5
or 6
or 7
or 8
or 9
or 10
.
Input
N
natural number and one line.
Output
A set of natural numbers.
Example
N, One line------------------> Set of marks
3, '843'---------------------> [8, 4, 3]
1, '0'-----------------------> [0]
2, '1010'--------------------> [10,10]
3, '1010'--------------------> [1,0,10] or [10,1,0]
4, '1010'--------------------> [1,0,1,0]
9, '23104441070'-------------> [2, 3, 10, 4, 4, 4, 10, 7, 0]
12,'499102102121103'---------> [4, 9, 9, 10, 2, 10, 2, 1, 2, 1, 10, 3]
5, '71061'-------------------> [7, 1, 0, 6, 1]
11,'476565010684'------------> [4, 7, 6, 5, 6, 5, 0, 10, 6, 8, 4]
4, '1306'--------------------> [1, 3, 0, 6]
9, '51026221084'-------------> [5, 10, 2, 6, 2, 2, 10, 8, 4]
14,'851089085685524'---------> [8, 5, 10, 8, 9, 0, 8, 5, 6, 8, 5, 5, 2, 4]
11,'110840867780'------------> [1, 10, 8, 4, 0, 8, 6, 7, 7, 8, 0]
9, '4359893510'--------------> [4, 3, 5, 9, 8, 9, 3, 5, 10]
7, '99153710'----------------> [9, 9, 1, 5, 3, 7, 10]
14,'886171092313495'---------> [8, 8, 6, 1, 7, 10, 9, 2, 3, 1, 3, 4, 9, 5]
2, '44'----------------------> [4, 4]
4, '9386'--------------------> [9, 3, 8, 6]
Rules
- When several outputs are possible give only one output.
- Only mark of value
10
is on two decimal, others are on one decimal. - The input and output can be given in any convenient format
- No need to handle invalid input
- Either a full program or a function are acceptable. If a function, you can return the output rather than printing it.
- If possible, please include a link to an online testing environment so other people can try out your code!
Standard loopholes are forbidden.- This is code-golf so all usual golfing rules apply, and the shortest code (in bytes) wins.
code-golf string array-manipulation
Challenge
Mark is a student who receives his N
marks in a concatenated way in a one single line.
The challenge is to separate his marks, knowing that each mark can only be 0
or 1
or 2
or 3
or 4
or 5
or 6
or 7
or 8
or 9
or 10
.
Input
N
natural number and one line.
Output
A set of natural numbers.
Example
N, One line------------------> Set of marks
3, '843'---------------------> [8, 4, 3]
1, '0'-----------------------> [0]
2, '1010'--------------------> [10,10]
3, '1010'--------------------> [1,0,10] or [10,1,0]
4, '1010'--------------------> [1,0,1,0]
9, '23104441070'-------------> [2, 3, 10, 4, 4, 4, 10, 7, 0]
12,'499102102121103'---------> [4, 9, 9, 10, 2, 10, 2, 1, 2, 1, 10, 3]
5, '71061'-------------------> [7, 1, 0, 6, 1]
11,'476565010684'------------> [4, 7, 6, 5, 6, 5, 0, 10, 6, 8, 4]
4, '1306'--------------------> [1, 3, 0, 6]
9, '51026221084'-------------> [5, 10, 2, 6, 2, 2, 10, 8, 4]
14,'851089085685524'---------> [8, 5, 10, 8, 9, 0, 8, 5, 6, 8, 5, 5, 2, 4]
11,'110840867780'------------> [1, 10, 8, 4, 0, 8, 6, 7, 7, 8, 0]
9, '4359893510'--------------> [4, 3, 5, 9, 8, 9, 3, 5, 10]
7, '99153710'----------------> [9, 9, 1, 5, 3, 7, 10]
14,'886171092313495'---------> [8, 8, 6, 1, 7, 10, 9, 2, 3, 1, 3, 4, 9, 5]
2, '44'----------------------> [4, 4]
4, '9386'--------------------> [9, 3, 8, 6]
Rules
- When several outputs are possible give only one output.
- Only mark of value
10
is on two decimal, others are on one decimal. - The input and output can be given in any convenient format
- No need to handle invalid input
- Either a full program or a function are acceptable. If a function, you can return the output rather than printing it.
- If possible, please include a link to an online testing environment so other people can try out your code!
Standard loopholes are forbidden.- This is code-golf so all usual golfing rules apply, and the shortest code (in bytes) wins.
code-golf string array-manipulation
code-golf string array-manipulation
edited Nov 28 '18 at 19:32
Giuseppe
16.6k31052
16.6k31052
asked Nov 28 '18 at 18:09
mdahmoune
1,4701723
1,4701723
Here's a Python snippet I used to get then, 'string'
pairs from the copypasted example text block:spl = [item.split('-')[0] for item in text.split('n')]
– Gigaflop
Nov 28 '18 at 19:22
3
Plz some comments for down-votes...
– mdahmoune
Nov 29 '18 at 10:24
Downvotes don't require leaving comments for a reason. There is nothing that can be improved about this challenge.
– user202729
Nov 29 '18 at 14:55
So don't worry about it.
– user202729
Nov 29 '18 at 15:13
Are the outputs required to be in the same order as the input?
– Mnemonic
Nov 29 '18 at 20:01
|
show 3 more comments
Here's a Python snippet I used to get then, 'string'
pairs from the copypasted example text block:spl = [item.split('-')[0] for item in text.split('n')]
– Gigaflop
Nov 28 '18 at 19:22
3
Plz some comments for down-votes...
– mdahmoune
Nov 29 '18 at 10:24
Downvotes don't require leaving comments for a reason. There is nothing that can be improved about this challenge.
– user202729
Nov 29 '18 at 14:55
So don't worry about it.
– user202729
Nov 29 '18 at 15:13
Are the outputs required to be in the same order as the input?
– Mnemonic
Nov 29 '18 at 20:01
Here's a Python snippet I used to get the
n, 'string'
pairs from the copypasted example text block: spl = [item.split('-')[0] for item in text.split('n')]
– Gigaflop
Nov 28 '18 at 19:22
Here's a Python snippet I used to get the
n, 'string'
pairs from the copypasted example text block: spl = [item.split('-')[0] for item in text.split('n')]
– Gigaflop
Nov 28 '18 at 19:22
3
3
Plz some comments for down-votes...
– mdahmoune
Nov 29 '18 at 10:24
Plz some comments for down-votes...
– mdahmoune
Nov 29 '18 at 10:24
Downvotes don't require leaving comments for a reason. There is nothing that can be improved about this challenge.
– user202729
Nov 29 '18 at 14:55
Downvotes don't require leaving comments for a reason. There is nothing that can be improved about this challenge.
– user202729
Nov 29 '18 at 14:55
So don't worry about it.
– user202729
Nov 29 '18 at 15:13
So don't worry about it.
– user202729
Nov 29 '18 at 15:13
Are the outputs required to be in the same order as the input?
– Mnemonic
Nov 29 '18 at 20:01
Are the outputs required to be in the same order as the input?
– Mnemonic
Nov 29 '18 at 20:01
|
show 3 more comments
17 Answers
17
active
oldest
votes
Perl 6, 25 bytes
->a,b{b~~/(10|.)**{a}/}
Try it online!
Anonymous code block that takes a number and a string and returns as a Match object.
Explanation:
->a,b{ } # Anonymous code block taking params a and b
b~~/ / # Match using b
(10|.) # 10 or a single digit
**{a} # Exactly a times, being greedy
add a comment |
Python 3, 47 bytes
lambda s,n:[*s.replace(b'1',b'n',len(s)-n)]
Try it online!
Takes the "one line" as a bytestring with raw bytes x00 - x09
. If it's not acceptable:
Python 3, 56 bytes
lambda s,n:[x-48for x in s.replace(b'10',b':',len(s)-n)]
Try it online!
Takes "one line" as bytestring.
add a comment |
Brachylog, 23 21 bytes
-2 bytes thanks to Fatalize
h~c.{ịℕ≤10&ịṫ?∧}ᵛ&t~l
Try it online!
The input is a pair [Line, N]
.
This is my first Brachylog program, so there is probably a lot room for improvement.
It is very slow when the length of the line > 7.
Explanation:
h~c.{ịℕ≤10&ịṫ?∧}ᵛ&t~l
h The first element in the input
~c is formed by concatenating
. the elements in the output array
.{ ∧}ᵛ AND For every element in the output array holds that
ị The element converted to an integer
ℕ is a natural number
≤10 and less than or equal to 10
&ịṫ? and it has no leading zeroes (*)
&t AND The second element of the input
~l is the length of the output
(*) ịṫ?
checks that there are no leading zeroes. It converts the string to integer and then back to string and compares to the original string.
You don't need to input the number as a string, just use an integer. This alleviates the need for all thoseị
and for the leading zero check:h~c.{ℕ≤10}ᵛ&t~l
. This is probably slower though as deconcatenation on integers must work even for unknown integers through constraints, which makes it inefficient.
– Fatalize
Nov 29 '18 at 7:46
(Also note that usingh
andt
to get the first/last element is more efficient than using∋
for both (which in most programs will not even work)).
– Fatalize
Nov 29 '18 at 7:47
@Fatalize I understood that the input line can contain leading zeroes, so it would not be possible to use an integer as the input.
– fergusq
Nov 29 '18 at 10:50
Right, that's annoying…
– Fatalize
Nov 29 '18 at 11:35
add a comment |
V, 17, 12 bytes
ÓòÀGjí1î
0
Try it online!
I was content with 17 bytes, but than 05AB1E came along with 13, and I couldn't let a challenge go unanswered. :D
Ó " Put each character on it's own line
ò " Recursively (repeat until an error happens)...
ÀG " Go to the "n"th line
j " Move down a line (this will error if there are exactly "n" lines)
í " Remove...
1 " a '1'
<0x93> " START THE MATCH HERE
î " a newline
<0x85> " END THE MATCH HERE
0 " a '0'
Hexdump:
00000000: 5cd3 f2c0 476a ed31 93ee 8530 ...Gj.1...0
Alternate solution:
ÓòÀGjç1î0/J
Unfortunately, this replaces 10
with 1 0
add a comment |
Ruby, 57 bytes
->n,m{m.sub!"10",?A while m[n];m.chars.map{|c|c.to_i 16}}
Try it online!
This may turn out to be not the golfiest approach, but it looks like a fun idea to temporarily substitute 10
for a hex A
, which incidentally is also a high mark (if we consider A-F grading system :))
add a comment |
Haskell, 68 bytes
n!('1':'0':x)|n-2<length x=10:(n-1)!x
n!(s:x)=read[s]:(n-1)!x
n!_=
Try it online!
Greedily take 10s as long as there are more digits than marks remaining.
add a comment |
JavaScript, 57 52 bytes
n=>g=s=>s[n]?g(s.replace(x=10,`x`)):[...s].map(eval)
Try It Online
add a comment |
Python 3, 71 68 59 bytes
down another 9 bytes thanks to ovs.
lambda n,s:[int(c,11)for c in s.replace('10','a',len(s)-n)]
Try it online!
I was iniitially trying to use str.partition()
recursively, but using replace
smacked me in the face not too long after. Can anyone improve on this?
Also, here's a TIO link that I used to make the test cases into something more copy/pasteable
1
-3 bytes: drop space between: [c
and'x' else
and10 for
– mdahmoune
Nov 28 '18 at 19:12
@mdahmoune Thanks for noticing, I have a hard time remembering what can be squished together.
– Gigaflop
Nov 28 '18 at 19:16
8
General rule of thumb: Basically anything except for two letters can be squished together. If you get a syntax error, add random spaces until it works :)
– Quintec
Nov 28 '18 at 19:21
There are some exceptions such as<number>e
,<letter><number>
,f'
.
– user202729
Nov 29 '18 at 5:37
3
59 bytes by replacing 10 with a and reading each character as a base 11 int:lambda n,s:[int(c,11)for c in s.replace('10','a',len(s)-n)]
.
– ovs
Nov 29 '18 at 6:47
|
show 1 more comment
Haskell, 98 bytes
n!x=[y|y<-s x,y==take n y]!!0
s('1':'0':x)=do y<-s x;[1:0:y,10:y]
s(x:y)=(read[x]:)<$>s y
s _=[]
Try it online or test all!
Explanation
The function s
does all possible splits, for example: "1010"
becomes [[1,0,1,0],[10,1,0],[1,0,10],[10,10]]
, note how the longest splits end up at the beginning (because 1:0:y
comes before 10:y
).
With that in mind, we can take all these values and filter the y
s out where y == take n y
which keeps also splits that are shorter than required. For example with 4
we leave the list the same [[1,0,1,0],[10,1,0],[1,0,10],[10,10]]
.
Now we can just get the first element in that list because the inputs will always be valid (eg. 5!"1010"
would give [1,0,1,0]
too, but we don't need to handle it).
Note: I somehow miscounted.. y==take n y
is the same length as length y==n
:S
add a comment |
Perl 5 -plF
, 39 bytes
$a=<>;$_="@F";s/1 0/10/ while$a-1<y/ //
Try it online!
add a comment |
Clean, 128 bytes
import StdEnv
@=[]
@['10':t]=[u++v\u<-[[10],[1,0]],v<- @t];@[h:t]=[[digitToInt h:v]\v<- @t]
?n l=hd[e\e<- @l|length e==n]
Try it online!
add a comment |
05AB1E, 13 bytes
.œsù.ΔïTÝÃJ¹Q
Try it online!
or as a Test Suite
Explanation
.œ # partitions of the first input
sù # of a length equal to the second input
.Δ # find the first partition that returns true when:
ï # each element is converted to integer
TÝÃ # and only numbers in [0 ... 10] are kept
J # then join it together
¹Q # and compare it to the first input for equality
add a comment |
JavaScript (Babel Node), 70 69 59 bytes
Takes input as (n)(line)
.
n=>s=>(a=s.match(/10|./g)).flatMap(x=>x>9&&!a[--n]?[1,0]:x)
Try it online!
Commented
n => s => // given n and s
(a = s.match(/10|./g)) // split s into marks; a '1' followed by a '0' is always
// interpreted as '10'
.flatMap(x => // for each mark x:
x > 9 && // if x = '10',
!a[--n] ? // then decrement n; if a[n] is undefined:
[1, 0] // yield [1, 0]
: // else:
x // yield the mark unchanged
) // end of flatMap()
JavaScript (ES6), 64 59 bytes
Saved 5 bytes thanks to @guest271314
Takes input as (n)(line)
.
n=>g=([...s])=>1/s[n]?g(eval(`[${s}]`.replace('1,0',10))):s
Try it online!
Commented
n => // main function, taking n
g = ([...s]) => // g = recursive function, taking s
// (which is either a string or an array)
1 / s[n] ? // if s[n] is defined (i.e. we have too many marks):
g( // do a recursive call to g:
eval( // build a new array by evaluating ...
`[${s}]` // ... the string representation of s where the
.replace('1,0', 10) // first occurrence of '1,0' is replaced with '10'
) // end of eval()
) // end of recursive call
: // else:
s // return s
Why the output for N=3 and line='1010' is with mixed types [ 1, 0, '10' ]?
– mdahmoune
Nov 28 '18 at 18:57
s.match()
returns an array of strings but a"10"
may be split into[1,0]
(2 integers) in the callback function offlatMap()
.
– Arnauld
Nov 28 '18 at 18:59
1
We can coerce everything to integers for +1 byte.
– Arnauld
Nov 28 '18 at 18:59
59 byteseval(`[${s}]`.replace('1,0',10))
– guest271314
Nov 29 '18 at 7:49
@guest271314 Thanks! Nice catch.
– Arnauld
Nov 29 '18 at 8:22
add a comment |
Java (OpenJDK 8), 78 bytes
A nice one-liner using the streams API.
(n,l)->l.join(":",l.split("10",l.length()-n+1)).chars().map(i->i-48).toArray()
Try it online!
How it works
(n,l)-> // Lambda function taking int and string
l.join(":", // Join the following array with colons
l.split("10", // Split the original string on "10"...
l.length()-n+1)) // But limit the parts to the difference between the length
// and expected length, to only remove required number of 10s
.chars() // Convert to an intstream of codepoints
.map(i->i-48) // Remove 48 to get the numeric value of each codepoint
.toArray() // Return an int array
add a comment |
R, 63 bytes
While the length of the string is larger than n
, substitute the next 10 you reach for a ":"
(the ASCII character after 9). Then split into numbers by taking the ASCII value of each char in the string.
function(n,x){while(nchar(x)>n)x=sub(10,":",x);utf8ToInt(x)-48}
Try it online!
add a comment |
Red, 91 bytes
func[n s][while[n < length? s][replace s"10""a"]foreach c s[prin[either c =#"a"[10][c]""]]]
Try it online!
add a comment |
Jelly, 18 bytes
Ḍ⁵⁻ƊƝr1ŒpS‘⁼ɗƇḢk⁸Ḍ
Try it online!
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%2f176735%2fsplit-marks-marks%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
Perl 6, 25 bytes
->a,b{b~~/(10|.)**{a}/}
Try it online!
Anonymous code block that takes a number and a string and returns as a Match object.
Explanation:
->a,b{ } # Anonymous code block taking params a and b
b~~/ / # Match using b
(10|.) # 10 or a single digit
**{a} # Exactly a times, being greedy
add a comment |
Perl 6, 25 bytes
->a,b{b~~/(10|.)**{a}/}
Try it online!
Anonymous code block that takes a number and a string and returns as a Match object.
Explanation:
->a,b{ } # Anonymous code block taking params a and b
b~~/ / # Match using b
(10|.) # 10 or a single digit
**{a} # Exactly a times, being greedy
add a comment |
Perl 6, 25 bytes
->a,b{b~~/(10|.)**{a}/}
Try it online!
Anonymous code block that takes a number and a string and returns as a Match object.
Explanation:
->a,b{ } # Anonymous code block taking params a and b
b~~/ / # Match using b
(10|.) # 10 or a single digit
**{a} # Exactly a times, being greedy
Perl 6, 25 bytes
->a,b{b~~/(10|.)**{a}/}
Try it online!
Anonymous code block that takes a number and a string and returns as a Match object.
Explanation:
->a,b{ } # Anonymous code block taking params a and b
b~~/ / # Match using b
(10|.) # 10 or a single digit
**{a} # Exactly a times, being greedy
edited Nov 29 '18 at 0:04
answered Nov 28 '18 at 23:36
Jo King
20.9k248110
20.9k248110
add a comment |
add a comment |
Python 3, 47 bytes
lambda s,n:[*s.replace(b'1',b'n',len(s)-n)]
Try it online!
Takes the "one line" as a bytestring with raw bytes x00 - x09
. If it's not acceptable:
Python 3, 56 bytes
lambda s,n:[x-48for x in s.replace(b'10',b':',len(s)-n)]
Try it online!
Takes "one line" as bytestring.
add a comment |
Python 3, 47 bytes
lambda s,n:[*s.replace(b'1',b'n',len(s)-n)]
Try it online!
Takes the "one line" as a bytestring with raw bytes x00 - x09
. If it's not acceptable:
Python 3, 56 bytes
lambda s,n:[x-48for x in s.replace(b'10',b':',len(s)-n)]
Try it online!
Takes "one line" as bytestring.
add a comment |
Python 3, 47 bytes
lambda s,n:[*s.replace(b'1',b'n',len(s)-n)]
Try it online!
Takes the "one line" as a bytestring with raw bytes x00 - x09
. If it's not acceptable:
Python 3, 56 bytes
lambda s,n:[x-48for x in s.replace(b'10',b':',len(s)-n)]
Try it online!
Takes "one line" as bytestring.
Python 3, 47 bytes
lambda s,n:[*s.replace(b'1',b'n',len(s)-n)]
Try it online!
Takes the "one line" as a bytestring with raw bytes x00 - x09
. If it's not acceptable:
Python 3, 56 bytes
lambda s,n:[x-48for x in s.replace(b'10',b':',len(s)-n)]
Try it online!
Takes "one line" as bytestring.
answered Nov 29 '18 at 8:08
Bubbler
6,312759
6,312759
add a comment |
add a comment |
Brachylog, 23 21 bytes
-2 bytes thanks to Fatalize
h~c.{ịℕ≤10&ịṫ?∧}ᵛ&t~l
Try it online!
The input is a pair [Line, N]
.
This is my first Brachylog program, so there is probably a lot room for improvement.
It is very slow when the length of the line > 7.
Explanation:
h~c.{ịℕ≤10&ịṫ?∧}ᵛ&t~l
h The first element in the input
~c is formed by concatenating
. the elements in the output array
.{ ∧}ᵛ AND For every element in the output array holds that
ị The element converted to an integer
ℕ is a natural number
≤10 and less than or equal to 10
&ịṫ? and it has no leading zeroes (*)
&t AND The second element of the input
~l is the length of the output
(*) ịṫ?
checks that there are no leading zeroes. It converts the string to integer and then back to string and compares to the original string.
You don't need to input the number as a string, just use an integer. This alleviates the need for all thoseị
and for the leading zero check:h~c.{ℕ≤10}ᵛ&t~l
. This is probably slower though as deconcatenation on integers must work even for unknown integers through constraints, which makes it inefficient.
– Fatalize
Nov 29 '18 at 7:46
(Also note that usingh
andt
to get the first/last element is more efficient than using∋
for both (which in most programs will not even work)).
– Fatalize
Nov 29 '18 at 7:47
@Fatalize I understood that the input line can contain leading zeroes, so it would not be possible to use an integer as the input.
– fergusq
Nov 29 '18 at 10:50
Right, that's annoying…
– Fatalize
Nov 29 '18 at 11:35
add a comment |
Brachylog, 23 21 bytes
-2 bytes thanks to Fatalize
h~c.{ịℕ≤10&ịṫ?∧}ᵛ&t~l
Try it online!
The input is a pair [Line, N]
.
This is my first Brachylog program, so there is probably a lot room for improvement.
It is very slow when the length of the line > 7.
Explanation:
h~c.{ịℕ≤10&ịṫ?∧}ᵛ&t~l
h The first element in the input
~c is formed by concatenating
. the elements in the output array
.{ ∧}ᵛ AND For every element in the output array holds that
ị The element converted to an integer
ℕ is a natural number
≤10 and less than or equal to 10
&ịṫ? and it has no leading zeroes (*)
&t AND The second element of the input
~l is the length of the output
(*) ịṫ?
checks that there are no leading zeroes. It converts the string to integer and then back to string and compares to the original string.
You don't need to input the number as a string, just use an integer. This alleviates the need for all thoseị
and for the leading zero check:h~c.{ℕ≤10}ᵛ&t~l
. This is probably slower though as deconcatenation on integers must work even for unknown integers through constraints, which makes it inefficient.
– Fatalize
Nov 29 '18 at 7:46
(Also note that usingh
andt
to get the first/last element is more efficient than using∋
for both (which in most programs will not even work)).
– Fatalize
Nov 29 '18 at 7:47
@Fatalize I understood that the input line can contain leading zeroes, so it would not be possible to use an integer as the input.
– fergusq
Nov 29 '18 at 10:50
Right, that's annoying…
– Fatalize
Nov 29 '18 at 11:35
add a comment |
Brachylog, 23 21 bytes
-2 bytes thanks to Fatalize
h~c.{ịℕ≤10&ịṫ?∧}ᵛ&t~l
Try it online!
The input is a pair [Line, N]
.
This is my first Brachylog program, so there is probably a lot room for improvement.
It is very slow when the length of the line > 7.
Explanation:
h~c.{ịℕ≤10&ịṫ?∧}ᵛ&t~l
h The first element in the input
~c is formed by concatenating
. the elements in the output array
.{ ∧}ᵛ AND For every element in the output array holds that
ị The element converted to an integer
ℕ is a natural number
≤10 and less than or equal to 10
&ịṫ? and it has no leading zeroes (*)
&t AND The second element of the input
~l is the length of the output
(*) ịṫ?
checks that there are no leading zeroes. It converts the string to integer and then back to string and compares to the original string.
Brachylog, 23 21 bytes
-2 bytes thanks to Fatalize
h~c.{ịℕ≤10&ịṫ?∧}ᵛ&t~l
Try it online!
The input is a pair [Line, N]
.
This is my first Brachylog program, so there is probably a lot room for improvement.
It is very slow when the length of the line > 7.
Explanation:
h~c.{ịℕ≤10&ịṫ?∧}ᵛ&t~l
h The first element in the input
~c is formed by concatenating
. the elements in the output array
.{ ∧}ᵛ AND For every element in the output array holds that
ị The element converted to an integer
ℕ is a natural number
≤10 and less than or equal to 10
&ịṫ? and it has no leading zeroes (*)
&t AND The second element of the input
~l is the length of the output
(*) ịṫ?
checks that there are no leading zeroes. It converts the string to integer and then back to string and compares to the original string.
edited Nov 29 '18 at 10:57
answered Nov 28 '18 at 19:59
fergusq
4,65211036
4,65211036
You don't need to input the number as a string, just use an integer. This alleviates the need for all thoseị
and for the leading zero check:h~c.{ℕ≤10}ᵛ&t~l
. This is probably slower though as deconcatenation on integers must work even for unknown integers through constraints, which makes it inefficient.
– Fatalize
Nov 29 '18 at 7:46
(Also note that usingh
andt
to get the first/last element is more efficient than using∋
for both (which in most programs will not even work)).
– Fatalize
Nov 29 '18 at 7:47
@Fatalize I understood that the input line can contain leading zeroes, so it would not be possible to use an integer as the input.
– fergusq
Nov 29 '18 at 10:50
Right, that's annoying…
– Fatalize
Nov 29 '18 at 11:35
add a comment |
You don't need to input the number as a string, just use an integer. This alleviates the need for all thoseị
and for the leading zero check:h~c.{ℕ≤10}ᵛ&t~l
. This is probably slower though as deconcatenation on integers must work even for unknown integers through constraints, which makes it inefficient.
– Fatalize
Nov 29 '18 at 7:46
(Also note that usingh
andt
to get the first/last element is more efficient than using∋
for both (which in most programs will not even work)).
– Fatalize
Nov 29 '18 at 7:47
@Fatalize I understood that the input line can contain leading zeroes, so it would not be possible to use an integer as the input.
– fergusq
Nov 29 '18 at 10:50
Right, that's annoying…
– Fatalize
Nov 29 '18 at 11:35
You don't need to input the number as a string, just use an integer. This alleviates the need for all those
ị
and for the leading zero check: h~c.{ℕ≤10}ᵛ&t~l
. This is probably slower though as deconcatenation on integers must work even for unknown integers through constraints, which makes it inefficient.– Fatalize
Nov 29 '18 at 7:46
You don't need to input the number as a string, just use an integer. This alleviates the need for all those
ị
and for the leading zero check: h~c.{ℕ≤10}ᵛ&t~l
. This is probably slower though as deconcatenation on integers must work even for unknown integers through constraints, which makes it inefficient.– Fatalize
Nov 29 '18 at 7:46
(Also note that using
h
and t
to get the first/last element is more efficient than using ∋
for both (which in most programs will not even work)).– Fatalize
Nov 29 '18 at 7:47
(Also note that using
h
and t
to get the first/last element is more efficient than using ∋
for both (which in most programs will not even work)).– Fatalize
Nov 29 '18 at 7:47
@Fatalize I understood that the input line can contain leading zeroes, so it would not be possible to use an integer as the input.
– fergusq
Nov 29 '18 at 10:50
@Fatalize I understood that the input line can contain leading zeroes, so it would not be possible to use an integer as the input.
– fergusq
Nov 29 '18 at 10:50
Right, that's annoying…
– Fatalize
Nov 29 '18 at 11:35
Right, that's annoying…
– Fatalize
Nov 29 '18 at 11:35
add a comment |
V, 17, 12 bytes
ÓòÀGjí1î
0
Try it online!
I was content with 17 bytes, but than 05AB1E came along with 13, and I couldn't let a challenge go unanswered. :D
Ó " Put each character on it's own line
ò " Recursively (repeat until an error happens)...
ÀG " Go to the "n"th line
j " Move down a line (this will error if there are exactly "n" lines)
í " Remove...
1 " a '1'
<0x93> " START THE MATCH HERE
î " a newline
<0x85> " END THE MATCH HERE
0 " a '0'
Hexdump:
00000000: 5cd3 f2c0 476a ed31 93ee 8530 ...Gj.1...0
Alternate solution:
ÓòÀGjç1î0/J
Unfortunately, this replaces 10
with 1 0
add a comment |
V, 17, 12 bytes
ÓòÀGjí1î
0
Try it online!
I was content with 17 bytes, but than 05AB1E came along with 13, and I couldn't let a challenge go unanswered. :D
Ó " Put each character on it's own line
ò " Recursively (repeat until an error happens)...
ÀG " Go to the "n"th line
j " Move down a line (this will error if there are exactly "n" lines)
í " Remove...
1 " a '1'
<0x93> " START THE MATCH HERE
î " a newline
<0x85> " END THE MATCH HERE
0 " a '0'
Hexdump:
00000000: 5cd3 f2c0 476a ed31 93ee 8530 ...Gj.1...0
Alternate solution:
ÓòÀGjç1î0/J
Unfortunately, this replaces 10
with 1 0
add a comment |
V, 17, 12 bytes
ÓòÀGjí1î
0
Try it online!
I was content with 17 bytes, but than 05AB1E came along with 13, and I couldn't let a challenge go unanswered. :D
Ó " Put each character on it's own line
ò " Recursively (repeat until an error happens)...
ÀG " Go to the "n"th line
j " Move down a line (this will error if there are exactly "n" lines)
í " Remove...
1 " a '1'
<0x93> " START THE MATCH HERE
î " a newline
<0x85> " END THE MATCH HERE
0 " a '0'
Hexdump:
00000000: 5cd3 f2c0 476a ed31 93ee 8530 ...Gj.1...0
Alternate solution:
ÓòÀGjç1î0/J
Unfortunately, this replaces 10
with 1 0
V, 17, 12 bytes
ÓòÀGjí1î
0
Try it online!
I was content with 17 bytes, but than 05AB1E came along with 13, and I couldn't let a challenge go unanswered. :D
Ó " Put each character on it's own line
ò " Recursively (repeat until an error happens)...
ÀG " Go to the "n"th line
j " Move down a line (this will error if there are exactly "n" lines)
í " Remove...
1 " a '1'
<0x93> " START THE MATCH HERE
î " a newline
<0x85> " END THE MATCH HERE
0 " a '0'
Hexdump:
00000000: 5cd3 f2c0 476a ed31 93ee 8530 ...Gj.1...0
Alternate solution:
ÓòÀGjç1î0/J
Unfortunately, this replaces 10
with 1 0
edited Nov 29 '18 at 17:22
answered Nov 28 '18 at 18:17
DJMcMayhem♦
40.9k11145309
40.9k11145309
add a comment |
add a comment |
Ruby, 57 bytes
->n,m{m.sub!"10",?A while m[n];m.chars.map{|c|c.to_i 16}}
Try it online!
This may turn out to be not the golfiest approach, but it looks like a fun idea to temporarily substitute 10
for a hex A
, which incidentally is also a high mark (if we consider A-F grading system :))
add a comment |
Ruby, 57 bytes
->n,m{m.sub!"10",?A while m[n];m.chars.map{|c|c.to_i 16}}
Try it online!
This may turn out to be not the golfiest approach, but it looks like a fun idea to temporarily substitute 10
for a hex A
, which incidentally is also a high mark (if we consider A-F grading system :))
add a comment |
Ruby, 57 bytes
->n,m{m.sub!"10",?A while m[n];m.chars.map{|c|c.to_i 16}}
Try it online!
This may turn out to be not the golfiest approach, but it looks like a fun idea to temporarily substitute 10
for a hex A
, which incidentally is also a high mark (if we consider A-F grading system :))
Ruby, 57 bytes
->n,m{m.sub!"10",?A while m[n];m.chars.map{|c|c.to_i 16}}
Try it online!
This may turn out to be not the golfiest approach, but it looks like a fun idea to temporarily substitute 10
for a hex A
, which incidentally is also a high mark (if we consider A-F grading system :))
answered Nov 28 '18 at 21:10
Kirill L.
3,6551319
3,6551319
add a comment |
add a comment |
Haskell, 68 bytes
n!('1':'0':x)|n-2<length x=10:(n-1)!x
n!(s:x)=read[s]:(n-1)!x
n!_=
Try it online!
Greedily take 10s as long as there are more digits than marks remaining.
add a comment |
Haskell, 68 bytes
n!('1':'0':x)|n-2<length x=10:(n-1)!x
n!(s:x)=read[s]:(n-1)!x
n!_=
Try it online!
Greedily take 10s as long as there are more digits than marks remaining.
add a comment |
Haskell, 68 bytes
n!('1':'0':x)|n-2<length x=10:(n-1)!x
n!(s:x)=read[s]:(n-1)!x
n!_=
Try it online!
Greedily take 10s as long as there are more digits than marks remaining.
Haskell, 68 bytes
n!('1':'0':x)|n-2<length x=10:(n-1)!x
n!(s:x)=read[s]:(n-1)!x
n!_=
Try it online!
Greedily take 10s as long as there are more digits than marks remaining.
answered Nov 29 '18 at 3:03
Nitrodon
6,8511820
6,8511820
add a comment |
add a comment |
JavaScript, 57 52 bytes
n=>g=s=>s[n]?g(s.replace(x=10,`x`)):[...s].map(eval)
Try It Online
add a comment |
JavaScript, 57 52 bytes
n=>g=s=>s[n]?g(s.replace(x=10,`x`)):[...s].map(eval)
Try It Online
add a comment |
JavaScript, 57 52 bytes
n=>g=s=>s[n]?g(s.replace(x=10,`x`)):[...s].map(eval)
Try It Online
JavaScript, 57 52 bytes
n=>g=s=>s[n]?g(s.replace(x=10,`x`)):[...s].map(eval)
Try It Online
edited Nov 29 '18 at 8:09
answered Nov 29 '18 at 0:00
Shaggy
19k21666
19k21666
add a comment |
add a comment |
Python 3, 71 68 59 bytes
down another 9 bytes thanks to ovs.
lambda n,s:[int(c,11)for c in s.replace('10','a',len(s)-n)]
Try it online!
I was iniitially trying to use str.partition()
recursively, but using replace
smacked me in the face not too long after. Can anyone improve on this?
Also, here's a TIO link that I used to make the test cases into something more copy/pasteable
1
-3 bytes: drop space between: [c
and'x' else
and10 for
– mdahmoune
Nov 28 '18 at 19:12
@mdahmoune Thanks for noticing, I have a hard time remembering what can be squished together.
– Gigaflop
Nov 28 '18 at 19:16
8
General rule of thumb: Basically anything except for two letters can be squished together. If you get a syntax error, add random spaces until it works :)
– Quintec
Nov 28 '18 at 19:21
There are some exceptions such as<number>e
,<letter><number>
,f'
.
– user202729
Nov 29 '18 at 5:37
3
59 bytes by replacing 10 with a and reading each character as a base 11 int:lambda n,s:[int(c,11)for c in s.replace('10','a',len(s)-n)]
.
– ovs
Nov 29 '18 at 6:47
|
show 1 more comment
Python 3, 71 68 59 bytes
down another 9 bytes thanks to ovs.
lambda n,s:[int(c,11)for c in s.replace('10','a',len(s)-n)]
Try it online!
I was iniitially trying to use str.partition()
recursively, but using replace
smacked me in the face not too long after. Can anyone improve on this?
Also, here's a TIO link that I used to make the test cases into something more copy/pasteable
1
-3 bytes: drop space between: [c
and'x' else
and10 for
– mdahmoune
Nov 28 '18 at 19:12
@mdahmoune Thanks for noticing, I have a hard time remembering what can be squished together.
– Gigaflop
Nov 28 '18 at 19:16
8
General rule of thumb: Basically anything except for two letters can be squished together. If you get a syntax error, add random spaces until it works :)
– Quintec
Nov 28 '18 at 19:21
There are some exceptions such as<number>e
,<letter><number>
,f'
.
– user202729
Nov 29 '18 at 5:37
3
59 bytes by replacing 10 with a and reading each character as a base 11 int:lambda n,s:[int(c,11)for c in s.replace('10','a',len(s)-n)]
.
– ovs
Nov 29 '18 at 6:47
|
show 1 more comment
Python 3, 71 68 59 bytes
down another 9 bytes thanks to ovs.
lambda n,s:[int(c,11)for c in s.replace('10','a',len(s)-n)]
Try it online!
I was iniitially trying to use str.partition()
recursively, but using replace
smacked me in the face not too long after. Can anyone improve on this?
Also, here's a TIO link that I used to make the test cases into something more copy/pasteable
Python 3, 71 68 59 bytes
down another 9 bytes thanks to ovs.
lambda n,s:[int(c,11)for c in s.replace('10','a',len(s)-n)]
Try it online!
I was iniitially trying to use str.partition()
recursively, but using replace
smacked me in the face not too long after. Can anyone improve on this?
Also, here's a TIO link that I used to make the test cases into something more copy/pasteable
edited Nov 29 '18 at 13:21
answered Nov 28 '18 at 19:09
Gigaflop
22117
22117
1
-3 bytes: drop space between: [c
and'x' else
and10 for
– mdahmoune
Nov 28 '18 at 19:12
@mdahmoune Thanks for noticing, I have a hard time remembering what can be squished together.
– Gigaflop
Nov 28 '18 at 19:16
8
General rule of thumb: Basically anything except for two letters can be squished together. If you get a syntax error, add random spaces until it works :)
– Quintec
Nov 28 '18 at 19:21
There are some exceptions such as<number>e
,<letter><number>
,f'
.
– user202729
Nov 29 '18 at 5:37
3
59 bytes by replacing 10 with a and reading each character as a base 11 int:lambda n,s:[int(c,11)for c in s.replace('10','a',len(s)-n)]
.
– ovs
Nov 29 '18 at 6:47
|
show 1 more comment
1
-3 bytes: drop space between: [c
and'x' else
and10 for
– mdahmoune
Nov 28 '18 at 19:12
@mdahmoune Thanks for noticing, I have a hard time remembering what can be squished together.
– Gigaflop
Nov 28 '18 at 19:16
8
General rule of thumb: Basically anything except for two letters can be squished together. If you get a syntax error, add random spaces until it works :)
– Quintec
Nov 28 '18 at 19:21
There are some exceptions such as<number>e
,<letter><number>
,f'
.
– user202729
Nov 29 '18 at 5:37
3
59 bytes by replacing 10 with a and reading each character as a base 11 int:lambda n,s:[int(c,11)for c in s.replace('10','a',len(s)-n)]
.
– ovs
Nov 29 '18 at 6:47
1
1
-3 bytes: drop space between
: [c
and 'x' else
and 10 for
– mdahmoune
Nov 28 '18 at 19:12
-3 bytes: drop space between
: [c
and 'x' else
and 10 for
– mdahmoune
Nov 28 '18 at 19:12
@mdahmoune Thanks for noticing, I have a hard time remembering what can be squished together.
– Gigaflop
Nov 28 '18 at 19:16
@mdahmoune Thanks for noticing, I have a hard time remembering what can be squished together.
– Gigaflop
Nov 28 '18 at 19:16
8
8
General rule of thumb: Basically anything except for two letters can be squished together. If you get a syntax error, add random spaces until it works :)
– Quintec
Nov 28 '18 at 19:21
General rule of thumb: Basically anything except for two letters can be squished together. If you get a syntax error, add random spaces until it works :)
– Quintec
Nov 28 '18 at 19:21
There are some exceptions such as
<number>e
, <letter><number>
, f'
.– user202729
Nov 29 '18 at 5:37
There are some exceptions such as
<number>e
, <letter><number>
, f'
.– user202729
Nov 29 '18 at 5:37
3
3
59 bytes by replacing 10 with a and reading each character as a base 11 int:
lambda n,s:[int(c,11)for c in s.replace('10','a',len(s)-n)]
.– ovs
Nov 29 '18 at 6:47
59 bytes by replacing 10 with a and reading each character as a base 11 int:
lambda n,s:[int(c,11)for c in s.replace('10','a',len(s)-n)]
.– ovs
Nov 29 '18 at 6:47
|
show 1 more comment
Haskell, 98 bytes
n!x=[y|y<-s x,y==take n y]!!0
s('1':'0':x)=do y<-s x;[1:0:y,10:y]
s(x:y)=(read[x]:)<$>s y
s _=[]
Try it online or test all!
Explanation
The function s
does all possible splits, for example: "1010"
becomes [[1,0,1,0],[10,1,0],[1,0,10],[10,10]]
, note how the longest splits end up at the beginning (because 1:0:y
comes before 10:y
).
With that in mind, we can take all these values and filter the y
s out where y == take n y
which keeps also splits that are shorter than required. For example with 4
we leave the list the same [[1,0,1,0],[10,1,0],[1,0,10],[10,10]]
.
Now we can just get the first element in that list because the inputs will always be valid (eg. 5!"1010"
would give [1,0,1,0]
too, but we don't need to handle it).
Note: I somehow miscounted.. y==take n y
is the same length as length y==n
:S
add a comment |
Haskell, 98 bytes
n!x=[y|y<-s x,y==take n y]!!0
s('1':'0':x)=do y<-s x;[1:0:y,10:y]
s(x:y)=(read[x]:)<$>s y
s _=[]
Try it online or test all!
Explanation
The function s
does all possible splits, for example: "1010"
becomes [[1,0,1,0],[10,1,0],[1,0,10],[10,10]]
, note how the longest splits end up at the beginning (because 1:0:y
comes before 10:y
).
With that in mind, we can take all these values and filter the y
s out where y == take n y
which keeps also splits that are shorter than required. For example with 4
we leave the list the same [[1,0,1,0],[10,1,0],[1,0,10],[10,10]]
.
Now we can just get the first element in that list because the inputs will always be valid (eg. 5!"1010"
would give [1,0,1,0]
too, but we don't need to handle it).
Note: I somehow miscounted.. y==take n y
is the same length as length y==n
:S
add a comment |
Haskell, 98 bytes
n!x=[y|y<-s x,y==take n y]!!0
s('1':'0':x)=do y<-s x;[1:0:y,10:y]
s(x:y)=(read[x]:)<$>s y
s _=[]
Try it online or test all!
Explanation
The function s
does all possible splits, for example: "1010"
becomes [[1,0,1,0],[10,1,0],[1,0,10],[10,10]]
, note how the longest splits end up at the beginning (because 1:0:y
comes before 10:y
).
With that in mind, we can take all these values and filter the y
s out where y == take n y
which keeps also splits that are shorter than required. For example with 4
we leave the list the same [[1,0,1,0],[10,1,0],[1,0,10],[10,10]]
.
Now we can just get the first element in that list because the inputs will always be valid (eg. 5!"1010"
would give [1,0,1,0]
too, but we don't need to handle it).
Note: I somehow miscounted.. y==take n y
is the same length as length y==n
:S
Haskell, 98 bytes
n!x=[y|y<-s x,y==take n y]!!0
s('1':'0':x)=do y<-s x;[1:0:y,10:y]
s(x:y)=(read[x]:)<$>s y
s _=[]
Try it online or test all!
Explanation
The function s
does all possible splits, for example: "1010"
becomes [[1,0,1,0],[10,1,0],[1,0,10],[10,10]]
, note how the longest splits end up at the beginning (because 1:0:y
comes before 10:y
).
With that in mind, we can take all these values and filter the y
s out where y == take n y
which keeps also splits that are shorter than required. For example with 4
we leave the list the same [[1,0,1,0],[10,1,0],[1,0,10],[10,10]]
.
Now we can just get the first element in that list because the inputs will always be valid (eg. 5!"1010"
would give [1,0,1,0]
too, but we don't need to handle it).
Note: I somehow miscounted.. y==take n y
is the same length as length y==n
:S
edited Nov 28 '18 at 21:43
answered Nov 28 '18 at 21:28
BMO
11.5k22187
11.5k22187
add a comment |
add a comment |
Perl 5 -plF
, 39 bytes
$a=<>;$_="@F";s/1 0/10/ while$a-1<y/ //
Try it online!
add a comment |
Perl 5 -plF
, 39 bytes
$a=<>;$_="@F";s/1 0/10/ while$a-1<y/ //
Try it online!
add a comment |
Perl 5 -plF
, 39 bytes
$a=<>;$_="@F";s/1 0/10/ while$a-1<y/ //
Try it online!
Perl 5 -plF
, 39 bytes
$a=<>;$_="@F";s/1 0/10/ while$a-1<y/ //
Try it online!
answered Nov 28 '18 at 21:46
Xcali
5,188520
5,188520
add a comment |
add a comment |
Clean, 128 bytes
import StdEnv
@=[]
@['10':t]=[u++v\u<-[[10],[1,0]],v<- @t];@[h:t]=[[digitToInt h:v]\v<- @t]
?n l=hd[e\e<- @l|length e==n]
Try it online!
add a comment |
Clean, 128 bytes
import StdEnv
@=[]
@['10':t]=[u++v\u<-[[10],[1,0]],v<- @t];@[h:t]=[[digitToInt h:v]\v<- @t]
?n l=hd[e\e<- @l|length e==n]
Try it online!
add a comment |
Clean, 128 bytes
import StdEnv
@=[]
@['10':t]=[u++v\u<-[[10],[1,0]],v<- @t];@[h:t]=[[digitToInt h:v]\v<- @t]
?n l=hd[e\e<- @l|length e==n]
Try it online!
Clean, 128 bytes
import StdEnv
@=[]
@['10':t]=[u++v\u<-[[10],[1,0]],v<- @t];@[h:t]=[[digitToInt h:v]\v<- @t]
?n l=hd[e\e<- @l|length e==n]
Try it online!
answered Nov 28 '18 at 23:19
Οurous
6,48211033
6,48211033
add a comment |
add a comment |
05AB1E, 13 bytes
.œsù.ΔïTÝÃJ¹Q
Try it online!
or as a Test Suite
Explanation
.œ # partitions of the first input
sù # of a length equal to the second input
.Δ # find the first partition that returns true when:
ï # each element is converted to integer
TÝÃ # and only numbers in [0 ... 10] are kept
J # then join it together
¹Q # and compare it to the first input for equality
add a comment |
05AB1E, 13 bytes
.œsù.ΔïTÝÃJ¹Q
Try it online!
or as a Test Suite
Explanation
.œ # partitions of the first input
sù # of a length equal to the second input
.Δ # find the first partition that returns true when:
ï # each element is converted to integer
TÝÃ # and only numbers in [0 ... 10] are kept
J # then join it together
¹Q # and compare it to the first input for equality
add a comment |
05AB1E, 13 bytes
.œsù.ΔïTÝÃJ¹Q
Try it online!
or as a Test Suite
Explanation
.œ # partitions of the first input
sù # of a length equal to the second input
.Δ # find the first partition that returns true when:
ï # each element is converted to integer
TÝÃ # and only numbers in [0 ... 10] are kept
J # then join it together
¹Q # and compare it to the first input for equality
05AB1E, 13 bytes
.œsù.ΔïTÝÃJ¹Q
Try it online!
or as a Test Suite
Explanation
.œ # partitions of the first input
sù # of a length equal to the second input
.Δ # find the first partition that returns true when:
ï # each element is converted to integer
TÝÃ # and only numbers in [0 ... 10] are kept
J # then join it together
¹Q # and compare it to the first input for equality
edited Nov 29 '18 at 7:43
answered Nov 29 '18 at 7:30
Emigna
45.4k432138
45.4k432138
add a comment |
add a comment |
JavaScript (Babel Node), 70 69 59 bytes
Takes input as (n)(line)
.
n=>s=>(a=s.match(/10|./g)).flatMap(x=>x>9&&!a[--n]?[1,0]:x)
Try it online!
Commented
n => s => // given n and s
(a = s.match(/10|./g)) // split s into marks; a '1' followed by a '0' is always
// interpreted as '10'
.flatMap(x => // for each mark x:
x > 9 && // if x = '10',
!a[--n] ? // then decrement n; if a[n] is undefined:
[1, 0] // yield [1, 0]
: // else:
x // yield the mark unchanged
) // end of flatMap()
JavaScript (ES6), 64 59 bytes
Saved 5 bytes thanks to @guest271314
Takes input as (n)(line)
.
n=>g=([...s])=>1/s[n]?g(eval(`[${s}]`.replace('1,0',10))):s
Try it online!
Commented
n => // main function, taking n
g = ([...s]) => // g = recursive function, taking s
// (which is either a string or an array)
1 / s[n] ? // if s[n] is defined (i.e. we have too many marks):
g( // do a recursive call to g:
eval( // build a new array by evaluating ...
`[${s}]` // ... the string representation of s where the
.replace('1,0', 10) // first occurrence of '1,0' is replaced with '10'
) // end of eval()
) // end of recursive call
: // else:
s // return s
Why the output for N=3 and line='1010' is with mixed types [ 1, 0, '10' ]?
– mdahmoune
Nov 28 '18 at 18:57
s.match()
returns an array of strings but a"10"
may be split into[1,0]
(2 integers) in the callback function offlatMap()
.
– Arnauld
Nov 28 '18 at 18:59
1
We can coerce everything to integers for +1 byte.
– Arnauld
Nov 28 '18 at 18:59
59 byteseval(`[${s}]`.replace('1,0',10))
– guest271314
Nov 29 '18 at 7:49
@guest271314 Thanks! Nice catch.
– Arnauld
Nov 29 '18 at 8:22
add a comment |
JavaScript (Babel Node), 70 69 59 bytes
Takes input as (n)(line)
.
n=>s=>(a=s.match(/10|./g)).flatMap(x=>x>9&&!a[--n]?[1,0]:x)
Try it online!
Commented
n => s => // given n and s
(a = s.match(/10|./g)) // split s into marks; a '1' followed by a '0' is always
// interpreted as '10'
.flatMap(x => // for each mark x:
x > 9 && // if x = '10',
!a[--n] ? // then decrement n; if a[n] is undefined:
[1, 0] // yield [1, 0]
: // else:
x // yield the mark unchanged
) // end of flatMap()
JavaScript (ES6), 64 59 bytes
Saved 5 bytes thanks to @guest271314
Takes input as (n)(line)
.
n=>g=([...s])=>1/s[n]?g(eval(`[${s}]`.replace('1,0',10))):s
Try it online!
Commented
n => // main function, taking n
g = ([...s]) => // g = recursive function, taking s
// (which is either a string or an array)
1 / s[n] ? // if s[n] is defined (i.e. we have too many marks):
g( // do a recursive call to g:
eval( // build a new array by evaluating ...
`[${s}]` // ... the string representation of s where the
.replace('1,0', 10) // first occurrence of '1,0' is replaced with '10'
) // end of eval()
) // end of recursive call
: // else:
s // return s
Why the output for N=3 and line='1010' is with mixed types [ 1, 0, '10' ]?
– mdahmoune
Nov 28 '18 at 18:57
s.match()
returns an array of strings but a"10"
may be split into[1,0]
(2 integers) in the callback function offlatMap()
.
– Arnauld
Nov 28 '18 at 18:59
1
We can coerce everything to integers for +1 byte.
– Arnauld
Nov 28 '18 at 18:59
59 byteseval(`[${s}]`.replace('1,0',10))
– guest271314
Nov 29 '18 at 7:49
@guest271314 Thanks! Nice catch.
– Arnauld
Nov 29 '18 at 8:22
add a comment |
JavaScript (Babel Node), 70 69 59 bytes
Takes input as (n)(line)
.
n=>s=>(a=s.match(/10|./g)).flatMap(x=>x>9&&!a[--n]?[1,0]:x)
Try it online!
Commented
n => s => // given n and s
(a = s.match(/10|./g)) // split s into marks; a '1' followed by a '0' is always
// interpreted as '10'
.flatMap(x => // for each mark x:
x > 9 && // if x = '10',
!a[--n] ? // then decrement n; if a[n] is undefined:
[1, 0] // yield [1, 0]
: // else:
x // yield the mark unchanged
) // end of flatMap()
JavaScript (ES6), 64 59 bytes
Saved 5 bytes thanks to @guest271314
Takes input as (n)(line)
.
n=>g=([...s])=>1/s[n]?g(eval(`[${s}]`.replace('1,0',10))):s
Try it online!
Commented
n => // main function, taking n
g = ([...s]) => // g = recursive function, taking s
// (which is either a string or an array)
1 / s[n] ? // if s[n] is defined (i.e. we have too many marks):
g( // do a recursive call to g:
eval( // build a new array by evaluating ...
`[${s}]` // ... the string representation of s where the
.replace('1,0', 10) // first occurrence of '1,0' is replaced with '10'
) // end of eval()
) // end of recursive call
: // else:
s // return s
JavaScript (Babel Node), 70 69 59 bytes
Takes input as (n)(line)
.
n=>s=>(a=s.match(/10|./g)).flatMap(x=>x>9&&!a[--n]?[1,0]:x)
Try it online!
Commented
n => s => // given n and s
(a = s.match(/10|./g)) // split s into marks; a '1' followed by a '0' is always
// interpreted as '10'
.flatMap(x => // for each mark x:
x > 9 && // if x = '10',
!a[--n] ? // then decrement n; if a[n] is undefined:
[1, 0] // yield [1, 0]
: // else:
x // yield the mark unchanged
) // end of flatMap()
JavaScript (ES6), 64 59 bytes
Saved 5 bytes thanks to @guest271314
Takes input as (n)(line)
.
n=>g=([...s])=>1/s[n]?g(eval(`[${s}]`.replace('1,0',10))):s
Try it online!
Commented
n => // main function, taking n
g = ([...s]) => // g = recursive function, taking s
// (which is either a string or an array)
1 / s[n] ? // if s[n] is defined (i.e. we have too many marks):
g( // do a recursive call to g:
eval( // build a new array by evaluating ...
`[${s}]` // ... the string representation of s where the
.replace('1,0', 10) // first occurrence of '1,0' is replaced with '10'
) // end of eval()
) // end of recursive call
: // else:
s // return s
edited Nov 29 '18 at 8:22
answered Nov 28 '18 at 18:53
Arnauld
72.5k689305
72.5k689305
Why the output for N=3 and line='1010' is with mixed types [ 1, 0, '10' ]?
– mdahmoune
Nov 28 '18 at 18:57
s.match()
returns an array of strings but a"10"
may be split into[1,0]
(2 integers) in the callback function offlatMap()
.
– Arnauld
Nov 28 '18 at 18:59
1
We can coerce everything to integers for +1 byte.
– Arnauld
Nov 28 '18 at 18:59
59 byteseval(`[${s}]`.replace('1,0',10))
– guest271314
Nov 29 '18 at 7:49
@guest271314 Thanks! Nice catch.
– Arnauld
Nov 29 '18 at 8:22
add a comment |
Why the output for N=3 and line='1010' is with mixed types [ 1, 0, '10' ]?
– mdahmoune
Nov 28 '18 at 18:57
s.match()
returns an array of strings but a"10"
may be split into[1,0]
(2 integers) in the callback function offlatMap()
.
– Arnauld
Nov 28 '18 at 18:59
1
We can coerce everything to integers for +1 byte.
– Arnauld
Nov 28 '18 at 18:59
59 byteseval(`[${s}]`.replace('1,0',10))
– guest271314
Nov 29 '18 at 7:49
@guest271314 Thanks! Nice catch.
– Arnauld
Nov 29 '18 at 8:22
Why the output for N=3 and line='1010' is with mixed types [ 1, 0, '10' ]?
– mdahmoune
Nov 28 '18 at 18:57
Why the output for N=3 and line='1010' is with mixed types [ 1, 0, '10' ]?
– mdahmoune
Nov 28 '18 at 18:57
s.match()
returns an array of strings but a "10"
may be split into [1,0]
(2 integers) in the callback function of flatMap()
.– Arnauld
Nov 28 '18 at 18:59
s.match()
returns an array of strings but a "10"
may be split into [1,0]
(2 integers) in the callback function of flatMap()
.– Arnauld
Nov 28 '18 at 18:59
1
1
We can coerce everything to integers for +1 byte.
– Arnauld
Nov 28 '18 at 18:59
We can coerce everything to integers for +1 byte.
– Arnauld
Nov 28 '18 at 18:59
59 bytes
eval(`[${s}]`.replace('1,0',10))
– guest271314
Nov 29 '18 at 7:49
59 bytes
eval(`[${s}]`.replace('1,0',10))
– guest271314
Nov 29 '18 at 7:49
@guest271314 Thanks! Nice catch.
– Arnauld
Nov 29 '18 at 8:22
@guest271314 Thanks! Nice catch.
– Arnauld
Nov 29 '18 at 8:22
add a comment |
Java (OpenJDK 8), 78 bytes
A nice one-liner using the streams API.
(n,l)->l.join(":",l.split("10",l.length()-n+1)).chars().map(i->i-48).toArray()
Try it online!
How it works
(n,l)-> // Lambda function taking int and string
l.join(":", // Join the following array with colons
l.split("10", // Split the original string on "10"...
l.length()-n+1)) // But limit the parts to the difference between the length
// and expected length, to only remove required number of 10s
.chars() // Convert to an intstream of codepoints
.map(i->i-48) // Remove 48 to get the numeric value of each codepoint
.toArray() // Return an int array
add a comment |
Java (OpenJDK 8), 78 bytes
A nice one-liner using the streams API.
(n,l)->l.join(":",l.split("10",l.length()-n+1)).chars().map(i->i-48).toArray()
Try it online!
How it works
(n,l)-> // Lambda function taking int and string
l.join(":", // Join the following array with colons
l.split("10", // Split the original string on "10"...
l.length()-n+1)) // But limit the parts to the difference between the length
// and expected length, to only remove required number of 10s
.chars() // Convert to an intstream of codepoints
.map(i->i-48) // Remove 48 to get the numeric value of each codepoint
.toArray() // Return an int array
add a comment |
Java (OpenJDK 8), 78 bytes
A nice one-liner using the streams API.
(n,l)->l.join(":",l.split("10",l.length()-n+1)).chars().map(i->i-48).toArray()
Try it online!
How it works
(n,l)-> // Lambda function taking int and string
l.join(":", // Join the following array with colons
l.split("10", // Split the original string on "10"...
l.length()-n+1)) // But limit the parts to the difference between the length
// and expected length, to only remove required number of 10s
.chars() // Convert to an intstream of codepoints
.map(i->i-48) // Remove 48 to get the numeric value of each codepoint
.toArray() // Return an int array
Java (OpenJDK 8), 78 bytes
A nice one-liner using the streams API.
(n,l)->l.join(":",l.split("10",l.length()-n+1)).chars().map(i->i-48).toArray()
Try it online!
How it works
(n,l)-> // Lambda function taking int and string
l.join(":", // Join the following array with colons
l.split("10", // Split the original string on "10"...
l.length()-n+1)) // But limit the parts to the difference between the length
// and expected length, to only remove required number of 10s
.chars() // Convert to an intstream of codepoints
.map(i->i-48) // Remove 48 to get the numeric value of each codepoint
.toArray() // Return an int array
answered Nov 29 '18 at 14:12
Luke Stevens
744214
744214
add a comment |
add a comment |
R, 63 bytes
While the length of the string is larger than n
, substitute the next 10 you reach for a ":"
(the ASCII character after 9). Then split into numbers by taking the ASCII value of each char in the string.
function(n,x){while(nchar(x)>n)x=sub(10,":",x);utf8ToInt(x)-48}
Try it online!
add a comment |
R, 63 bytes
While the length of the string is larger than n
, substitute the next 10 you reach for a ":"
(the ASCII character after 9). Then split into numbers by taking the ASCII value of each char in the string.
function(n,x){while(nchar(x)>n)x=sub(10,":",x);utf8ToInt(x)-48}
Try it online!
add a comment |
R, 63 bytes
While the length of the string is larger than n
, substitute the next 10 you reach for a ":"
(the ASCII character after 9). Then split into numbers by taking the ASCII value of each char in the string.
function(n,x){while(nchar(x)>n)x=sub(10,":",x);utf8ToInt(x)-48}
Try it online!
R, 63 bytes
While the length of the string is larger than n
, substitute the next 10 you reach for a ":"
(the ASCII character after 9). Then split into numbers by taking the ASCII value of each char in the string.
function(n,x){while(nchar(x)>n)x=sub(10,":",x);utf8ToInt(x)-48}
Try it online!
answered Dec 1 '18 at 10:11
J.Doe
2,279212
2,279212
add a comment |
add a comment |
Red, 91 bytes
func[n s][while[n < length? s][replace s"10""a"]foreach c s[prin[either c =#"a"[10][c]""]]]
Try it online!
add a comment |
Red, 91 bytes
func[n s][while[n < length? s][replace s"10""a"]foreach c s[prin[either c =#"a"[10][c]""]]]
Try it online!
add a comment |
Red, 91 bytes
func[n s][while[n < length? s][replace s"10""a"]foreach c s[prin[either c =#"a"[10][c]""]]]
Try it online!
Red, 91 bytes
func[n s][while[n < length? s][replace s"10""a"]foreach c s[prin[either c =#"a"[10][c]""]]]
Try it online!
answered Nov 29 '18 at 7:43
Galen Ivanov
6,35711032
6,35711032
add a comment |
add a comment |
Jelly, 18 bytes
Ḍ⁵⁻ƊƝr1ŒpS‘⁼ɗƇḢk⁸Ḍ
Try it online!
add a comment |
Jelly, 18 bytes
Ḍ⁵⁻ƊƝr1ŒpS‘⁼ɗƇḢk⁸Ḍ
Try it online!
add a comment |
Jelly, 18 bytes
Ḍ⁵⁻ƊƝr1ŒpS‘⁼ɗƇḢk⁸Ḍ
Try it online!
Jelly, 18 bytes
Ḍ⁵⁻ƊƝr1ŒpS‘⁼ɗƇḢk⁸Ḍ
Try it online!
answered Nov 30 '18 at 13:23
Erik the Outgolfer
31.4k429103
31.4k429103
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).
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f176735%2fsplit-marks-marks%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
Here's a Python snippet I used to get the
n, 'string'
pairs from the copypasted example text block:spl = [item.split('-')[0] for item in text.split('n')]
– Gigaflop
Nov 28 '18 at 19:22
3
Plz some comments for down-votes...
– mdahmoune
Nov 29 '18 at 10:24
Downvotes don't require leaving comments for a reason. There is nothing that can be improved about this challenge.
– user202729
Nov 29 '18 at 14:55
So don't worry about it.
– user202729
Nov 29 '18 at 15:13
Are the outputs required to be in the same order as the input?
– Mnemonic
Nov 29 '18 at 20:01