Split Mark's marks
up vote
10
down vote
favorite
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
10is 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
add a comment |
up vote
10
down vote
favorite
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
10is 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
6 hours ago
add a comment |
up vote
10
down vote
favorite
up vote
10
down vote
favorite
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
10is 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
10is 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 6 hours ago
Giuseppe
16.1k31052
16.1k31052
asked 7 hours ago
mdahmoune
1,3051722
1,3051722
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
6 hours ago
add a comment |
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
6 hours ago
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
6 hours ago
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
6 hours ago
add a comment |
10 Answers
10
active
oldest
votes
up vote
4
down vote
V, 17 bytes
òÀ|ló10/dòÓÍd/10
Try it online!
There are two inputs, s and n. s is in the buffer, and n is an argument.
òÀ|l ò " As long as the current line (s) is longer than n bytes...
ó " Replace...
10 " '10'
/ " With...
d " 'd'
"
Ó " Put each character on it's own line
Í " Replace (on every line)...
d " 'd'
/ " With...
10 " '10'
Hexdump:
00000000: f2c0 7c6c f331 302f 64f2 5cd3 cd64 2f31 ..|l.10/d...d/1
00000010: 30
add a comment |
up vote
3
down vote
Python 3, 71 68 bytes
lambda n,s:[c if c!='x'else 10for c in s.replace('10','x',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: [cand'x' elseand10 for
– mdahmoune
6 hours ago
@mdahmoune Thanks for noticing, I have a hard time remembering what can be squished together.
– Gigaflop
6 hours ago
6
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
6 hours ago
add a comment |
up vote
3
down vote
Brachylog, 23 bytes
∋~c.{ịℕ&ị≤10&ịṫ?∧}ᵛ&∋~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:
∋~c.{ịℕ&ị≤10&ịṫ?∧}ᵛ&∋~l
The input
∋ contains a string
~c that 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 its string repr is equal to the element (*)
& AND The input
∋ contains an element
~l that 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.
add a comment |
up vote
2
down vote
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 |
up vote
2
down vote
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 ys 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 |
up vote
2
down vote
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 bytes
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 ...
'[' + // ... the string representation of an array made by
(s + '') // coercing s to a string and
.replace('1,0', 10) // replacing the first occurrence of '1,0' with 10
+ ']' // with surrounding brackets
) // 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
7 hours ago
s.match()returns an array of strings but a"10"may be split into[1,0](2 integers) in the callback function offlatMap().
– Arnauld
7 hours ago
1
We can coerce everything to integers for +1 byte.
– Arnauld
7 hours ago
add a comment |
up vote
2
down vote
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 |
up vote
1
down vote
Perl 5 -plF, 39 bytes
$a=<>;$_="@F";s/1 0/10/ while$a-1<y/ //
Try it online!
add a comment |
up vote
1
down vote
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 |
up vote
0
down vote
JavaScript, 57 bytes
n=>g=s=>s[n]?g(s.replace(10,`x`)):[...s].map(x=>1/x?x:10)
Try it online
add a comment |
10 Answers
10
active
oldest
votes
10 Answers
10
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
V, 17 bytes
òÀ|ló10/dòÓÍd/10
Try it online!
There are two inputs, s and n. s is in the buffer, and n is an argument.
òÀ|l ò " As long as the current line (s) is longer than n bytes...
ó " Replace...
10 " '10'
/ " With...
d " 'd'
"
Ó " Put each character on it's own line
Í " Replace (on every line)...
d " 'd'
/ " With...
10 " '10'
Hexdump:
00000000: f2c0 7c6c f331 302f 64f2 5cd3 cd64 2f31 ..|l.10/d...d/1
00000010: 30
add a comment |
up vote
4
down vote
V, 17 bytes
òÀ|ló10/dòÓÍd/10
Try it online!
There are two inputs, s and n. s is in the buffer, and n is an argument.
òÀ|l ò " As long as the current line (s) is longer than n bytes...
ó " Replace...
10 " '10'
/ " With...
d " 'd'
"
Ó " Put each character on it's own line
Í " Replace (on every line)...
d " 'd'
/ " With...
10 " '10'
Hexdump:
00000000: f2c0 7c6c f331 302f 64f2 5cd3 cd64 2f31 ..|l.10/d...d/1
00000010: 30
add a comment |
up vote
4
down vote
up vote
4
down vote
V, 17 bytes
òÀ|ló10/dòÓÍd/10
Try it online!
There are two inputs, s and n. s is in the buffer, and n is an argument.
òÀ|l ò " As long as the current line (s) is longer than n bytes...
ó " Replace...
10 " '10'
/ " With...
d " 'd'
"
Ó " Put each character on it's own line
Í " Replace (on every line)...
d " 'd'
/ " With...
10 " '10'
Hexdump:
00000000: f2c0 7c6c f331 302f 64f2 5cd3 cd64 2f31 ..|l.10/d...d/1
00000010: 30
V, 17 bytes
òÀ|ló10/dòÓÍd/10
Try it online!
There are two inputs, s and n. s is in the buffer, and n is an argument.
òÀ|l ò " As long as the current line (s) is longer than n bytes...
ó " Replace...
10 " '10'
/ " With...
d " 'd'
"
Ó " Put each character on it's own line
Í " Replace (on every line)...
d " 'd'
/ " With...
10 " '10'
Hexdump:
00000000: f2c0 7c6c f331 302f 64f2 5cd3 cd64 2f31 ..|l.10/d...d/1
00000010: 30
edited 6 hours ago
answered 7 hours ago
DJMcMayhem♦
40.6k11145307
40.6k11145307
add a comment |
add a comment |
up vote
3
down vote
Python 3, 71 68 bytes
lambda n,s:[c if c!='x'else 10for c in s.replace('10','x',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: [cand'x' elseand10 for
– mdahmoune
6 hours ago
@mdahmoune Thanks for noticing, I have a hard time remembering what can be squished together.
– Gigaflop
6 hours ago
6
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
6 hours ago
add a comment |
up vote
3
down vote
Python 3, 71 68 bytes
lambda n,s:[c if c!='x'else 10for c in s.replace('10','x',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: [cand'x' elseand10 for
– mdahmoune
6 hours ago
@mdahmoune Thanks for noticing, I have a hard time remembering what can be squished together.
– Gigaflop
6 hours ago
6
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
6 hours ago
add a comment |
up vote
3
down vote
up vote
3
down vote
Python 3, 71 68 bytes
lambda n,s:[c if c!='x'else 10for c in s.replace('10','x',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 bytes
lambda n,s:[c if c!='x'else 10for c in s.replace('10','x',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 6 hours ago
answered 6 hours ago
Gigaflop
2116
2116
1
-3 bytes: drop space between: [cand'x' elseand10 for
– mdahmoune
6 hours ago
@mdahmoune Thanks for noticing, I have a hard time remembering what can be squished together.
– Gigaflop
6 hours ago
6
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
6 hours ago
add a comment |
1
-3 bytes: drop space between: [cand'x' elseand10 for
– mdahmoune
6 hours ago
@mdahmoune Thanks for noticing, I have a hard time remembering what can be squished together.
– Gigaflop
6 hours ago
6
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
6 hours ago
1
1
-3 bytes: drop space between
: [c and 'x' else and 10 for– mdahmoune
6 hours ago
-3 bytes: drop space between
: [c and 'x' else and 10 for– mdahmoune
6 hours ago
@mdahmoune Thanks for noticing, I have a hard time remembering what can be squished together.
– Gigaflop
6 hours ago
@mdahmoune Thanks for noticing, I have a hard time remembering what can be squished together.
– Gigaflop
6 hours ago
6
6
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
6 hours ago
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
6 hours ago
add a comment |
up vote
3
down vote
Brachylog, 23 bytes
∋~c.{ịℕ&ị≤10&ịṫ?∧}ᵛ&∋~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:
∋~c.{ịℕ&ị≤10&ịṫ?∧}ᵛ&∋~l
The input
∋ contains a string
~c that 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 its string repr is equal to the element (*)
& AND The input
∋ contains an element
~l that 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.
add a comment |
up vote
3
down vote
Brachylog, 23 bytes
∋~c.{ịℕ&ị≤10&ịṫ?∧}ᵛ&∋~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:
∋~c.{ịℕ&ị≤10&ịṫ?∧}ᵛ&∋~l
The input
∋ contains a string
~c that 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 its string repr is equal to the element (*)
& AND The input
∋ contains an element
~l that 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.
add a comment |
up vote
3
down vote
up vote
3
down vote
Brachylog, 23 bytes
∋~c.{ịℕ&ị≤10&ịṫ?∧}ᵛ&∋~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:
∋~c.{ịℕ&ị≤10&ịṫ?∧}ᵛ&∋~l
The input
∋ contains a string
~c that 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 its string repr is equal to the element (*)
& AND The input
∋ contains an element
~l that 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 bytes
∋~c.{ịℕ&ị≤10&ịṫ?∧}ᵛ&∋~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:
∋~c.{ịℕ&ị≤10&ịṫ?∧}ᵛ&∋~l
The input
∋ contains a string
~c that 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 its string repr is equal to the element (*)
& AND The input
∋ contains an element
~l that 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 4 hours ago
answered 6 hours ago
fergusq
4,63211036
4,63211036
add a comment |
add a comment |
up vote
2
down vote
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 |
up vote
2
down vote
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 |
up vote
2
down vote
up vote
2
down vote
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 4 hours ago
Kirill L.
3,2761118
3,2761118
add a comment |
add a comment |
up vote
2
down vote
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 ys 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 |
up vote
2
down vote
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 ys 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 |
up vote
2
down vote
up vote
2
down vote
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 ys 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 ys 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 4 hours ago
answered 4 hours ago
BMO
10.7k21880
10.7k21880
add a comment |
add a comment |
up vote
2
down vote
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 bytes
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 ...
'[' + // ... the string representation of an array made by
(s + '') // coercing s to a string and
.replace('1,0', 10) // replacing the first occurrence of '1,0' with 10
+ ']' // with surrounding brackets
) // 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
7 hours ago
s.match()returns an array of strings but a"10"may be split into[1,0](2 integers) in the callback function offlatMap().
– Arnauld
7 hours ago
1
We can coerce everything to integers for +1 byte.
– Arnauld
7 hours ago
add a comment |
up vote
2
down vote
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 bytes
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 ...
'[' + // ... the string representation of an array made by
(s + '') // coercing s to a string and
.replace('1,0', 10) // replacing the first occurrence of '1,0' with 10
+ ']' // with surrounding brackets
) // 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
7 hours ago
s.match()returns an array of strings but a"10"may be split into[1,0](2 integers) in the callback function offlatMap().
– Arnauld
7 hours ago
1
We can coerce everything to integers for +1 byte.
– Arnauld
7 hours ago
add a comment |
up vote
2
down vote
up vote
2
down vote
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 bytes
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 ...
'[' + // ... the string representation of an array made by
(s + '') // coercing s to a string and
.replace('1,0', 10) // replacing the first occurrence of '1,0' with 10
+ ']' // with surrounding brackets
) // 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 bytes
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 ...
'[' + // ... the string representation of an array made by
(s + '') // coercing s to a string and
.replace('1,0', 10) // replacing the first occurrence of '1,0' with 10
+ ']' // with surrounding brackets
) // end of eval()
) // end of recursive call
: // else:
s // return s
edited 3 hours ago
answered 7 hours ago
Arnauld
70.1k686295
70.1k686295
Why the output for N=3 and line='1010' is with mixed types [ 1, 0, '10' ]?
– mdahmoune
7 hours ago
s.match()returns an array of strings but a"10"may be split into[1,0](2 integers) in the callback function offlatMap().
– Arnauld
7 hours ago
1
We can coerce everything to integers for +1 byte.
– Arnauld
7 hours ago
add a comment |
Why the output for N=3 and line='1010' is with mixed types [ 1, 0, '10' ]?
– mdahmoune
7 hours ago
s.match()returns an array of strings but a"10"may be split into[1,0](2 integers) in the callback function offlatMap().
– Arnauld
7 hours ago
1
We can coerce everything to integers for +1 byte.
– Arnauld
7 hours ago
Why the output for N=3 and line='1010' is with mixed types [ 1, 0, '10' ]?
– mdahmoune
7 hours ago
Why the output for N=3 and line='1010' is with mixed types [ 1, 0, '10' ]?
– mdahmoune
7 hours ago
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
7 hours ago
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
7 hours ago
1
1
We can coerce everything to integers for +1 byte.
– Arnauld
7 hours ago
We can coerce everything to integers for +1 byte.
– Arnauld
7 hours ago
add a comment |
up vote
2
down vote
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 |
up vote
2
down vote
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 |
up vote
2
down vote
up vote
2
down vote
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 2 hours ago
answered 2 hours ago
Jo King
19.7k245103
19.7k245103
add a comment |
add a comment |
up vote
1
down vote
Perl 5 -plF, 39 bytes
$a=<>;$_="@F";s/1 0/10/ while$a-1<y/ //
Try it online!
add a comment |
up vote
1
down vote
Perl 5 -plF, 39 bytes
$a=<>;$_="@F";s/1 0/10/ while$a-1<y/ //
Try it online!
add a comment |
up vote
1
down vote
up vote
1
down vote
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 4 hours ago
Xcali
5,000520
5,000520
add a comment |
add a comment |
up vote
1
down vote
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 |
up vote
1
down vote
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 |
up vote
1
down vote
up vote
1
down vote
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 2 hours ago
Οurous
5,94311032
5,94311032
add a comment |
add a comment |
up vote
0
down vote
JavaScript, 57 bytes
n=>g=s=>s[n]?g(s.replace(10,`x`)):[...s].map(x=>1/x?x:10)
Try it online
add a comment |
up vote
0
down vote
JavaScript, 57 bytes
n=>g=s=>s[n]?g(s.replace(10,`x`)):[...s].map(x=>1/x?x:10)
Try it online
add a comment |
up vote
0
down vote
up vote
0
down vote
JavaScript, 57 bytes
n=>g=s=>s[n]?g(s.replace(10,`x`)):[...s].map(x=>1/x?x:10)
Try it online
JavaScript, 57 bytes
n=>g=s=>s[n]?g(s.replace(10,`x`)):[...s].map(x=>1/x?x:10)
Try it online
answered 2 hours ago
Shaggy
18.3k21663
18.3k21663
add a comment |
add a comment |
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
6 hours ago