A well linked challenge
$begingroup$
An activity I sometimes do when I'm bored is to write a couple of characters in matching pairs. I then draw lines (over the tops never below) to connect these characters. For example I might write $abcbac$ and then I would draw the lines as:
Or I might write $abbcac$
Once I've drawn these lines I try to draw closed loops around chunks so that my loop doesn't intersect any of the lines I just drew. For example, in the first one the only loop we can draw is around the entire thing, but in the second one we can draw a loop around just the $b$s (or everything else)
If we play around with this for a little while we will find that some strings can only be drawn so that closed loops contain all or none of the letters (like our first example). We will call such strings well linked strings.
Note that some strings can be drawn in multiple ways. For example $bbbb$ can be drawn in both of the following ways (and a third not included):
or
If one of these ways can be drawn such that a closed loop can be made to contain some of the characters without intersecting any of the lines, then the string is not well linked. (so $bbbb$ is not well linked)
Task
Your task is to write a program to identify strings that are well linked. Your input will consist of a string where every character appears an even number of times, and your output should be one of two distinct consistent values, one if the strings are well linked and the other otherwise.
In addition your program must be a well linked string meaning
Every character appears an even number of times in your program.
It should output the truthy value when passed itself.
Your program should be able to produce the correct output for any string consisting of characters from printable ASCII or your own program. With each character appearing an even number of times.
Answers will be scored as their lengths in bytes with fewer bytes being a better score.
Hint
A string is not well linked iff a contiguous non-empty strict substring exists such that each character appears an even number of times in that substring.
Test Cases
abcbac -> True
abbcac -> False
bbbb -> False
abacbc -> True
abcbabcb -> True
abcbca -> False
code-golf decision-problem restricted-source source-layout
$endgroup$
add a comment |
$begingroup$
An activity I sometimes do when I'm bored is to write a couple of characters in matching pairs. I then draw lines (over the tops never below) to connect these characters. For example I might write $abcbac$ and then I would draw the lines as:
Or I might write $abbcac$
Once I've drawn these lines I try to draw closed loops around chunks so that my loop doesn't intersect any of the lines I just drew. For example, in the first one the only loop we can draw is around the entire thing, but in the second one we can draw a loop around just the $b$s (or everything else)
If we play around with this for a little while we will find that some strings can only be drawn so that closed loops contain all or none of the letters (like our first example). We will call such strings well linked strings.
Note that some strings can be drawn in multiple ways. For example $bbbb$ can be drawn in both of the following ways (and a third not included):
or
If one of these ways can be drawn such that a closed loop can be made to contain some of the characters without intersecting any of the lines, then the string is not well linked. (so $bbbb$ is not well linked)
Task
Your task is to write a program to identify strings that are well linked. Your input will consist of a string where every character appears an even number of times, and your output should be one of two distinct consistent values, one if the strings are well linked and the other otherwise.
In addition your program must be a well linked string meaning
Every character appears an even number of times in your program.
It should output the truthy value when passed itself.
Your program should be able to produce the correct output for any string consisting of characters from printable ASCII or your own program. With each character appearing an even number of times.
Answers will be scored as their lengths in bytes with fewer bytes being a better score.
Hint
A string is not well linked iff a contiguous non-empty strict substring exists such that each character appears an even number of times in that substring.
Test Cases
abcbac -> True
abbcac -> False
bbbb -> False
abacbc -> True
abcbabcb -> True
abcbca -> False
code-golf decision-problem restricted-source source-layout
$endgroup$
1
$begingroup$
Test case:abcbca -> False
.
$endgroup$
– Ørjan Johansen
Feb 3 at 0:56
$begingroup$
I think your hint contains a superfluousthere
.
$endgroup$
– Jonathan Frech
Feb 3 at 1:05
2
$begingroup$
So to be clear: Whether a string has a total even number of every character is irrelevant to whether it's a well-linked string. That requirement applies only to programs' source code. This is of course only a matter of semantics, because programs are allowed to have undefined behavior for inputted strings having an odd total number of any character (and at least one submitted program takes advantage of this).
$endgroup$
– Deadcode
Feb 4 at 6:11
$begingroup$
What kinds of characters can be in the input?
$endgroup$
– xnor
Feb 4 at 6:43
$begingroup$
@xnor I added it to the challenge. Hopefully that clears that up.
$endgroup$
– TRITICIMAGVS
Feb 4 at 14:34
add a comment |
$begingroup$
An activity I sometimes do when I'm bored is to write a couple of characters in matching pairs. I then draw lines (over the tops never below) to connect these characters. For example I might write $abcbac$ and then I would draw the lines as:
Or I might write $abbcac$
Once I've drawn these lines I try to draw closed loops around chunks so that my loop doesn't intersect any of the lines I just drew. For example, in the first one the only loop we can draw is around the entire thing, but in the second one we can draw a loop around just the $b$s (or everything else)
If we play around with this for a little while we will find that some strings can only be drawn so that closed loops contain all or none of the letters (like our first example). We will call such strings well linked strings.
Note that some strings can be drawn in multiple ways. For example $bbbb$ can be drawn in both of the following ways (and a third not included):
or
If one of these ways can be drawn such that a closed loop can be made to contain some of the characters without intersecting any of the lines, then the string is not well linked. (so $bbbb$ is not well linked)
Task
Your task is to write a program to identify strings that are well linked. Your input will consist of a string where every character appears an even number of times, and your output should be one of two distinct consistent values, one if the strings are well linked and the other otherwise.
In addition your program must be a well linked string meaning
Every character appears an even number of times in your program.
It should output the truthy value when passed itself.
Your program should be able to produce the correct output for any string consisting of characters from printable ASCII or your own program. With each character appearing an even number of times.
Answers will be scored as their lengths in bytes with fewer bytes being a better score.
Hint
A string is not well linked iff a contiguous non-empty strict substring exists such that each character appears an even number of times in that substring.
Test Cases
abcbac -> True
abbcac -> False
bbbb -> False
abacbc -> True
abcbabcb -> True
abcbca -> False
code-golf decision-problem restricted-source source-layout
$endgroup$
An activity I sometimes do when I'm bored is to write a couple of characters in matching pairs. I then draw lines (over the tops never below) to connect these characters. For example I might write $abcbac$ and then I would draw the lines as:
Or I might write $abbcac$
Once I've drawn these lines I try to draw closed loops around chunks so that my loop doesn't intersect any of the lines I just drew. For example, in the first one the only loop we can draw is around the entire thing, but in the second one we can draw a loop around just the $b$s (or everything else)
If we play around with this for a little while we will find that some strings can only be drawn so that closed loops contain all or none of the letters (like our first example). We will call such strings well linked strings.
Note that some strings can be drawn in multiple ways. For example $bbbb$ can be drawn in both of the following ways (and a third not included):
or
If one of these ways can be drawn such that a closed loop can be made to contain some of the characters without intersecting any of the lines, then the string is not well linked. (so $bbbb$ is not well linked)
Task
Your task is to write a program to identify strings that are well linked. Your input will consist of a string where every character appears an even number of times, and your output should be one of two distinct consistent values, one if the strings are well linked and the other otherwise.
In addition your program must be a well linked string meaning
Every character appears an even number of times in your program.
It should output the truthy value when passed itself.
Your program should be able to produce the correct output for any string consisting of characters from printable ASCII or your own program. With each character appearing an even number of times.
Answers will be scored as their lengths in bytes with fewer bytes being a better score.
Hint
A string is not well linked iff a contiguous non-empty strict substring exists such that each character appears an even number of times in that substring.
Test Cases
abcbac -> True
abbcac -> False
bbbb -> False
abacbc -> True
abcbabcb -> True
abcbca -> False
code-golf decision-problem restricted-source source-layout
code-golf decision-problem restricted-source source-layout
edited Feb 4 at 14:34
TRITICIMAGVS
asked Feb 3 at 0:18
TRITICIMAGVSTRITICIMAGVS
34.6k10157369
34.6k10157369
1
$begingroup$
Test case:abcbca -> False
.
$endgroup$
– Ørjan Johansen
Feb 3 at 0:56
$begingroup$
I think your hint contains a superfluousthere
.
$endgroup$
– Jonathan Frech
Feb 3 at 1:05
2
$begingroup$
So to be clear: Whether a string has a total even number of every character is irrelevant to whether it's a well-linked string. That requirement applies only to programs' source code. This is of course only a matter of semantics, because programs are allowed to have undefined behavior for inputted strings having an odd total number of any character (and at least one submitted program takes advantage of this).
$endgroup$
– Deadcode
Feb 4 at 6:11
$begingroup$
What kinds of characters can be in the input?
$endgroup$
– xnor
Feb 4 at 6:43
$begingroup$
@xnor I added it to the challenge. Hopefully that clears that up.
$endgroup$
– TRITICIMAGVS
Feb 4 at 14:34
add a comment |
1
$begingroup$
Test case:abcbca -> False
.
$endgroup$
– Ørjan Johansen
Feb 3 at 0:56
$begingroup$
I think your hint contains a superfluousthere
.
$endgroup$
– Jonathan Frech
Feb 3 at 1:05
2
$begingroup$
So to be clear: Whether a string has a total even number of every character is irrelevant to whether it's a well-linked string. That requirement applies only to programs' source code. This is of course only a matter of semantics, because programs are allowed to have undefined behavior for inputted strings having an odd total number of any character (and at least one submitted program takes advantage of this).
$endgroup$
– Deadcode
Feb 4 at 6:11
$begingroup$
What kinds of characters can be in the input?
$endgroup$
– xnor
Feb 4 at 6:43
$begingroup$
@xnor I added it to the challenge. Hopefully that clears that up.
$endgroup$
– TRITICIMAGVS
Feb 4 at 14:34
1
1
$begingroup$
Test case:
abcbca -> False
.$endgroup$
– Ørjan Johansen
Feb 3 at 0:56
$begingroup$
Test case:
abcbca -> False
.$endgroup$
– Ørjan Johansen
Feb 3 at 0:56
$begingroup$
I think your hint contains a superfluous
there
.$endgroup$
– Jonathan Frech
Feb 3 at 1:05
$begingroup$
I think your hint contains a superfluous
there
.$endgroup$
– Jonathan Frech
Feb 3 at 1:05
2
2
$begingroup$
So to be clear: Whether a string has a total even number of every character is irrelevant to whether it's a well-linked string. That requirement applies only to programs' source code. This is of course only a matter of semantics, because programs are allowed to have undefined behavior for inputted strings having an odd total number of any character (and at least one submitted program takes advantage of this).
$endgroup$
– Deadcode
Feb 4 at 6:11
$begingroup$
So to be clear: Whether a string has a total even number of every character is irrelevant to whether it's a well-linked string. That requirement applies only to programs' source code. This is of course only a matter of semantics, because programs are allowed to have undefined behavior for inputted strings having an odd total number of any character (and at least one submitted program takes advantage of this).
$endgroup$
– Deadcode
Feb 4 at 6:11
$begingroup$
What kinds of characters can be in the input?
$endgroup$
– xnor
Feb 4 at 6:43
$begingroup$
What kinds of characters can be in the input?
$endgroup$
– xnor
Feb 4 at 6:43
$begingroup$
@xnor I added it to the challenge. Hopefully that clears that up.
$endgroup$
– TRITICIMAGVS
Feb 4 at 14:34
$begingroup$
@xnor I added it to the challenge. Hopefully that clears that up.
$endgroup$
– TRITICIMAGVS
Feb 4 at 14:34
add a comment |
11 Answers
11
active
oldest
votes
$begingroup$
Regex (ECMAScript 2018 or .NET), 140 126 118 100 98 82 bytes
^(?!(^.*)(.+)(.*$)(?<!^2|^1(?=(|(<?(|(?!8).)*(8|3$){1}){2})*$).*(.)+3$)!?=*)
This is much slower than the 98 byte version, because the ^1
is left of the lookahead and is thus evaluated after it. See below for a simple switcheroo that regains the speed. But due to this, the two TIOs below are limited to completing a smaller test case set than before, and the .NET one is too slow to check its own regex.
Try it online! (ECMAScript 2018)
Try it online! (.NET)
To drop 18 bytes (118 → 100), I shamelessly stole a really nice optimization from Neil's regex that avoids the need to put a lookahead inside the negative lookbehind (yielding an 80 byte unrestricted regex). Thank you, Neil!
That became obsolete when it dropped an incredible 16 more bytes (98 → 82) thanks to jaytea's ideas which led to a 69 byte unrestricted regex! It's much slower, but that's golf!
Note that the (|(
no-ops for making the regex well-linked have the result of making the it evaluate very slowly under .NET. They do not have this effect in ECMAScript because zero-width matches in a loop are treated as non-matches.
ECMAScript prohibits quantifiers on assertions, so this makes golfing the restricted-source requirements harder. However, at this point it's so well-golfed that I don't think lifting that particular restriction would open up any further golfing possibilities.
Without the extra characters needed to make it pass the restrictions (101 69 bytes):
^(?!(.*)(.+)(.*$)(?<!^2|^1(?=((((?!8).)*(8|3$)){2})*$).*(.)+3))
It's slow, but this simple switcheroo (for 74 bytes) regains all the lost speed and more:
^(?!(.*)(.+)(.*$)(?<!^2|(?=((((?!8).)*(8|3$)){2})*$)(?<=^1).*(.)+3))
^
(?!
(.*) # cycle through all starting points of substrings;
# 1 = part to exclude from the start
(.+) # cycle through all ending points of non-empty substrings;
# 2 = the substring
(.*$) # 3 = part to exclude from the end
(?<! # Assert that every character in the substring appears a total
# even number of times.
^2 # Assert that our substring is not the whole string. We don't
# need a $ anchor because we were already at the end before
# entering this lookbehind.
| # Note that the following steps are evaluated right to left,
# so please read them from bottom to top.
^1 # Do not look further left than the start of our substring.
(?=
# Assert that the number of times the character 8 appears in our
# substring is odd.
(
(
((?!8).)*
(8|3$) # This is the best part. Until the very last iteration
# of the loop outside the {2} loop, this alternation
# can only match 8, and once it reaches the end of the
# substring, it can match 3$ only once. This guarantees
# that it will match 8 an odd number of times, in matched
# pairs until finding one more at the end of the substring,
# which is paired with the 3$ instead of another 8.
){2}
)*$
)
.*(.)+ # 8 = cycle through all characters in this substring
# Assert (within this context) that at least one character appears an odd
# number of times within our substring. (Outside this negative lookbehind,
# that is equivalent to asserting that no character appears an odd number
# of times in our substring.)
3 # Skip to our substring (do not look further right than its end)
)
)
I wrote it using molecular lookahead (103 69 bytes) before converting it to variable-length lookbehind:
^(?!.*(?*(.+)(.*$))(?!^1$|(?*(.)+.*2$)((((?!3).)*(3|2$)){2})*$))
^
(?!
.*(?*(.+)(.*$)) # cycle through all non-empty substrings;
# 1 = the current substring;
# 2 = the part to exclude from the end
(?! # Assert that no character in the substring appears a
# total even number of times.
^1$ # Assert that our substring is not the whole string
# (i.e. it's a strict substring)
|
(?*(.)+.*2$) # 3 = Cycle through all characters that appear in this
# substring.
# Assert (within this context) that this character appears an odd number
# of times within our substring.
(
(
((?!3).)*
(3|2$)
){2}
)*$
)
)
And to aid in making my regex itself well-linked, I've been using a variation of the above regex:
(?*(.+)(.*$))(?!^1$|(?*(.)+.*2$)((((?!3).)*(3|2$)){2})*$)1
When used with regex -xml,rs -o
, this identifies a strict substring of the input that contains an even number of every character (if one exists). Sure, I could have written a non-regex program to do this for me, but where would be the fun in that?
$endgroup$
8
$begingroup$
wtf it's still being golfed
$endgroup$
– ASCII-only
Feb 4 at 8:51
$begingroup$
@ASCII-only and still being golfed...
$endgroup$
– Quintec
Feb 5 at 14:27
add a comment |
$begingroup$
Jelly, 20 bytes
ĠẈḂẸẆṖÇ€Ạ
ĠẈḂẸ
ẆṖÇ€Ạ
Try it online!
The first line is ignored. It's only there to satisfy the condition that every character appear an even number of times.
The next line first Ġ
roups indices by their value. If we then take the length of each sublist in the resulting list (Ẉ
), we get the number of times each character appears. To check whether any of these are non-even, we get the last Ḃ
it of each count and ask whether there Ẹ
xists a truthy (nonzero) value.
Therefore, this helper link returns whether a substring cannot be circled.
In the main link, we take all substrings of the input (Ẇ
), Ṗ
op off the last one (so that we don't check whether the entire string can be circled), and run the helper link (Ç
) on €
ach substring. The result is then whether Ạ
ll substrings cannot be circled.
$endgroup$
$begingroup$
So, yeah, this would be my solution too, but, unfortunately, it's boring... :(
$endgroup$
– Erik the Outgolfer
Feb 3 at 9:58
add a comment |
$begingroup$
J, 42 bytes
(*#'.,012&|@~#')=1#.[:,([:*/0=2&|@#/.~).
Try it online!
explanation
(*#'.,012&|@~#') = 1 #. [: , ([: */ 0 = 2&|@#/.~).
(*#'.,012&|@~#') NB. this evaluates to 1
NB. while supplying extra
NB. chars we need. hence...
= NB. does 1 equal...
1 #. NB. the sum of...
[: , NB. the flatten of...
( ). NB. the verb in parens applied
NB. to every suffix of every
NB. prefix, ie, all contiguous
NB. substrings
([: */ 0 = 2&|@#/.~) NB. def of verb in paren:
/.~ NB. when we group by each
NB. distinct char...
[: */ NB. is it the case that
NB. every group...
@# NB. has a length...
0 = 2&| NB. divisible by 2...
$endgroup$
1
$begingroup$
@Deadcode Since handling that amounts to doing the opposite test for the whole string as for every other substring, it seems a safe bet that most solutions would leave that out. Testing withabc
, only the Perl entry doesn't "fail" on it. (It has other problems though.)
$endgroup$
– Ørjan Johansen
Feb 4 at 3:12
1
$begingroup$
@ØrjanJohansen You misunderstood. I said strings with an odd total number of any character (which only disqualifies programs' source code, not well-linked strings) can be well-linked, and this program returns falsey for some of those well-linked strings. The question explicitly allows this undefined behavior, so the program is valid. Jonah, I think it's really interesting that your program does this and I admire that you figured out a method that works this way. I would love an explanation. This kind of programming is completely alien to me so I don't understand the comments and code.
$endgroup$
– Deadcode
Feb 4 at 9:12
add a comment |
$begingroup$
Python 2, 74 bytes
bmn0=f=lambda s,P={0},d=:s<" "if(P in d)else+f(s[f<s:],P^{s[0^0]},[P]+d)
Try it online!
Iterates through the string, keeping track in P
of the set of characters seen an odd number of times so far. The list d
stores all past values of P
, and if see the current P
already in d
, this means that in the characters seen since that time, each character has appeared an even number of times. If so, check if we've gone through the entire input: if we have, accept because the whole string is paired as expected, and otherwise reject.
Now about the source restriction. Characters needing pairing are stuffed into various harmless places, underlined below:
bmn0=f=lambda s,P={0},d=:s<" "if(P in d)else+f(s[f<s:],P^{s[0^0]},[P]+d)
_____ _ _ _ _ ___ ___
The f<s
evaluates to 0
while pairing off an f
, taking advantage of the function name also being f
so that it's defined (by the time the function is called.) The 0^0
absorbs an ^
symbol.
The 0
in P={0}
is unfortunate: in Python {}
evaluates to an empty dict rather than an empty set as we want, and here we can put in any non-character element and it will be harmless. I don't see anything spare to put in though, and have put in a 0
and duplicated it in bmn0
, costing 2 bytes. Note that initial arguments are evaluated when the function is defined, so variables we define ourselves can't be put in here.
$endgroup$
add a comment |
$begingroup$
Perl 6, 76 bytes
*.comb[^*X+(^*).map(^*)].grep({$_&[&]($_)}).none.Bag{*}.none%2#*^+XBob2rec%#
Try it online!
A Whatever lambda that returns a None Junction of None Junctions that can be boolified to a truthy/falsey value. I would recommend not removing the ?
that boolifies the return result though, otherwise the output gets rather large.
This solution is a little more complex than needed, due to several involved functions being unlinked, e.g. ..
, all
, >>
, %%
etc. Without the source restriction, this could be 43 bytes:
*.comb[^*X.. ^*].grep(?*).one.Bag{*}.all%%2
Try it online!
Explanation:
*.comb # Split the string to a list of characters
[^*X+(^*).map(^*)] # Get all substrings, alongside some garbage
.grep({$_&[&]($_)}) # Filter out the garbage (empty lists, lists with Nil values)
.none # Are none of
.Bag{*} # The count of characters in each substring
.none%2 # All not divisible by 2
#*^+XBob2rec%# And garbage to even out character counts
$endgroup$
add a comment |
$begingroup$
Perl 5 -p
, 94, 86, 78 bytes
m-.+(?{$Q|=@q&grp,$|=$&eq$_^!grep+/^/&(@m=$g=~/Q$_/g),($g=$&)=~/./g})(?!)-}{
ouput 0 if well-linked 1 otherwise.
78 bytes
86 bytes
94 bytes
How it works
-p
with}{
ending trick to output$
at the end
m-.+(?{
..})(?!)-
, to execute code over all non-empty substring (.+
matches the whole string first, and after executing code between(?{
..})
backtracks because of failed forced(?!)
$Q|=@q&grp,
garbage because of source restriction
$|=
integer bitwise or assignment, if there's almost one 1,$
will be 1 (true), by default it is empty (false)
$&eq$_
the case where the sbustring is the whole string is bitwise xored^
with "no odd character occurence"
($g=$&)=~/./g
to copy the matched substring into$g
(because will be overwirtten after next regex match) and return the array of character of substring.
/^/
garbage which evaluates to 1
grep
1&(@m=$g=~/Q$_/g),
for each character in substring get the array of character in$g
matching itself, array in scalar evaluates to its size andgrep
to filter the chracters with odd occurence1&x
is equivalent tox%2==1
$endgroup$
$begingroup$
I don't think this satisfies the source restriction: I count an odd number of open parentheses, for example
$endgroup$
– msh210
Feb 3 at 21:14
$begingroup$
@msh210 Isn't that the point? If there's an even number, it's not well linked
$endgroup$
– Quintec
Feb 3 at 23:58
$begingroup$
@Quintec One of the requirements for being well linked is that there are an even number of each character.
$endgroup$
– Ørjan Johansen
Feb 4 at 0:31
$begingroup$
my first answer had the requirement but after trying to golf, lost it. updated, but can be golfed.
$endgroup$
– Nahuel Fouilleul
Feb 4 at 5:32
1
$begingroup$
all the sources here satisfy source restriction, also the code returns 0 if well-linked and even number of each character
$endgroup$
– Nahuel Fouilleul
Feb 4 at 9:48
|
show 3 more comments
$begingroup$
Retina, 150 96 bytes
^(?!(.*)(.+)(.*)$(?<!^2|^1(?:(^?(?:(?!6).)*6){2})*(|(?!6).)*(?!.+6.*3$)(.){1,}3)(?!,<)?)
Try it online! Link includes test cases, including itself. Edit: Golfed down the original regex a fair bit with help from @Deadcode, then padded back up slightly less extravagently to maintain the source layout. Explanation:
^(?!(.*)(.+)(.*)$
Assert that no substring 3
exists that matches the following constraints.
(?<!^2|
Assert that the substring is not the whole original string.
^1(?:(^?(?:(?!6).)*6){2})*(|(?!6).)*(?!.+6.*3$)(.){1,}3)(?!,<)?)
Assert that there is no character 6
such that:
- it does not appear between the character itself (exclusive) and the end of the substring
- it appears an even number of times between the start of the substring and itself (exclusive)
In order to pass the source layout constraint, I replaced ((((
with (?:(^?(?:(
and ((
with (|(
. I still had one source constraint ))
left and the characters !()1<{}
left over, so I changed a +
into {1,}
and inserted the useless (?!,<)?
to consume the rest.
$endgroup$
2
$begingroup$
This seems not to satisfy the restricted source requirements.
$endgroup$
– Ørjan Johansen
Feb 3 at 2:00
$begingroup$
@ØrjanJohansen Finally, I've come up with a valid solution. Lots of junk in there though, so there might be something shorter available...
$endgroup$
– Neil
Feb 3 at 23:55
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 208 206 200 198 bytes
x=>!x.GroupBy(c=>c).Any(g=>g.Count()%2>0)&!Enumerable.Repeat(x.Count,x.Count*x.Count).Where(
(l,i)=>i%l>0&!x.Skip(i/l).Take(i%l).GroupBy(c=>c).Any(g=>g.Count()%2>0)
).Any()/*>!oyAnC0EmeablR*WhiS/T*/
Try it online!
-2 bytes thanks to @KevinCruijssen!
Finally got it below 200, so I might be done golfing for now :) I ended up creating a second TIO to test things out based on a previous answer.
Try it online!
Things that made this task tricky:
- Equality operator
==
was not allowed - Increment/assign operator
++
was not allowed - Linq
All()
function was not allowed
Commented code below:
// anonymous function that takes an IList as input
x=>
// the first condition makes sure the string even
// counts of each character
!x.GroupBy(c=>c).Any(g=>g.Count()%2>0)&
// the second condition generates all proper substrings of x
// and tests for any that contain even character counts
// the string length `l` is repeated `l*l` times
!Enumerable.Repeat(x.Count,x.Count*x.Count)
.Where((l,i)=>
// check that the substring length is greater than 0
i%l>0&
// use skip/take to generate a substring
// and check for a character count thats odd
// negate the result meaning we have found
// a substring that invalidates the input
!x.Skip(i/l).Take(i%l)
.GroupBy(c=>c).Any(g=>g.Count()%2>0)
)
// if any invalid substrings are found
// then the result in invalid
// the comment string at the end is needed
// to make the program well-linked
.Any()/*>!oyAnC0EmeablR*WhiS/T*/
$endgroup$
$begingroup$
You can remove the two spaces in your trailing comment.
$endgroup$
– Kevin Cruijssen
Feb 4 at 10:56
$begingroup$
@KevinCruijssen - good one :) I had forgotten that I added a space already. I had to throw another one into the source.
$endgroup$
– dana
Feb 4 at 11:04
add a comment |
$begingroup$
Python 2, 108 bytes
lambda d,e=enumerate:min(max(d[l:l+b].count(k)%2for(k)in d)for b,c in e(d[2:],2)for l,f in e(d) )#b =:x+.%2#
Try it online!
-2 thanks to Ørjan Johansen.
$endgroup$
$begingroup$
@ØrjanJohansen Huh, nice. EDIT: Looks like it saves only two bytes overall.
$endgroup$
– Erik the Outgolfer
Feb 3 at 12:47
add a comment |
$begingroup$
Python 3.8.0a1, 66 bytes
lambda l,b={id}:len({str(b:=b^{c})for(c)in l})<len(l)#,<^fmnost{}#
The Era of Assignment Expressions is upon us. With PEP 572 included in Python 3.8, golfing will never be the same. You can install the early developer preview 3.8.0a1 here.
Assignment expressions let you use :=
to assign to a variable inline while evaluating to that value. For example, (a:=2, a+1)
gives (2, 3)
. This can of course be used to store variables for reuse, but here we go a step further and use it as an accumulator in a comprehension.
For example, this code computes the cumulative sums [1, 3, 6]
t=0
l=[1,2,3]
print([t:=t+x for x in l])
Note how with each pass through the list comprehension, the cumulative sum t
is increased by x
and the new value is stored in the list produced by the comprehension.
Similarly, b:=b^{c}
updates the set of characters b
to toggle whether it includes character c
, and evaluates to the new value of b
. So, the code [b:=b^{c}for c in l]
iterates over characters c
in l
and accumulates the set of characters seen an odd number of times in each non-empty prefix.
This list is checked for duplicates by making it a set comprehension instead and seeing if its length is smaller than that of s
, which means that some repeats were collapsed. If so, the repeat means that in the portion of s
seen in between those times every character encountered an even number of numbers, making the string non-well-linked. Python doesn't allow sets of sets for being unhashable, so the inner sets are converted to strings instead.
The set b
is initialized as an optional arguments, and successfully gets modified in the function scope. I was worried this would make the function non-reusable, but it seems to reset between runs.
For the source restriction, unpaired characters are stuffed in a comment at the end. Writing for(c)in l
rather than for c in l
cancels the extra parens for free. We put id
into the initial set b
, which is harmless since it can start as any set, but the empty set can't be written as {}
because Python will make an empty dictionary. Since the letters i
and d
are among those needing pairing, we can put the function id
there.
Note that the code outputs negated booleans, so it will correctly give False
on itself.
$endgroup$
add a comment |
$begingroup$
05AB1E, 22 20 bytes
Œε¢Pà}KŒIKεSIS¢ÈP}àÈ
Outputs 1
if the string is well-linked, and 0
if the string is not well-linked.
Try it online or verify all test cases.
Explanation:
The base program is ŒsKεsS¢ÈP}à
(11 bytes), which outputs 0
if well-linked and 1
if not well-linked. The trailing È
(is_even) is a semi no-op that inverts the output, so 1
for well-linked strings and 0
for not well-linked strings. The other parts are no-ops to comply to the challenge rules.
Œε¢Pà}K # No-ops: substrings, map, count, product, maximum, close map, remove
# Due to the last remove, we're back at the (implicit) input again
Œ # Take the substrings of the input
IK # Remove the input itself from that list of substrings
ε # Map each substring to:
S # No-op: transform the substring into a list of characters
IS # Get the input-string as a list of characters
¢ # Count the occurrence of each character in the current substring
È # Check which counts are even (1 if truthy; 0 if falsey)
P # Take the product of that
}à # After the map: check if any are truthy by taking the maximum
È # Semi no-op: check if this maximum is even (0 becomes 1; 1 becomes 0)
# (and output the result implicitly)
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "200"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f179422%2fa-well-linked-challenge%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
11 Answers
11
active
oldest
votes
11 Answers
11
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Regex (ECMAScript 2018 or .NET), 140 126 118 100 98 82 bytes
^(?!(^.*)(.+)(.*$)(?<!^2|^1(?=(|(<?(|(?!8).)*(8|3$){1}){2})*$).*(.)+3$)!?=*)
This is much slower than the 98 byte version, because the ^1
is left of the lookahead and is thus evaluated after it. See below for a simple switcheroo that regains the speed. But due to this, the two TIOs below are limited to completing a smaller test case set than before, and the .NET one is too slow to check its own regex.
Try it online! (ECMAScript 2018)
Try it online! (.NET)
To drop 18 bytes (118 → 100), I shamelessly stole a really nice optimization from Neil's regex that avoids the need to put a lookahead inside the negative lookbehind (yielding an 80 byte unrestricted regex). Thank you, Neil!
That became obsolete when it dropped an incredible 16 more bytes (98 → 82) thanks to jaytea's ideas which led to a 69 byte unrestricted regex! It's much slower, but that's golf!
Note that the (|(
no-ops for making the regex well-linked have the result of making the it evaluate very slowly under .NET. They do not have this effect in ECMAScript because zero-width matches in a loop are treated as non-matches.
ECMAScript prohibits quantifiers on assertions, so this makes golfing the restricted-source requirements harder. However, at this point it's so well-golfed that I don't think lifting that particular restriction would open up any further golfing possibilities.
Without the extra characters needed to make it pass the restrictions (101 69 bytes):
^(?!(.*)(.+)(.*$)(?<!^2|^1(?=((((?!8).)*(8|3$)){2})*$).*(.)+3))
It's slow, but this simple switcheroo (for 74 bytes) regains all the lost speed and more:
^(?!(.*)(.+)(.*$)(?<!^2|(?=((((?!8).)*(8|3$)){2})*$)(?<=^1).*(.)+3))
^
(?!
(.*) # cycle through all starting points of substrings;
# 1 = part to exclude from the start
(.+) # cycle through all ending points of non-empty substrings;
# 2 = the substring
(.*$) # 3 = part to exclude from the end
(?<! # Assert that every character in the substring appears a total
# even number of times.
^2 # Assert that our substring is not the whole string. We don't
# need a $ anchor because we were already at the end before
# entering this lookbehind.
| # Note that the following steps are evaluated right to left,
# so please read them from bottom to top.
^1 # Do not look further left than the start of our substring.
(?=
# Assert that the number of times the character 8 appears in our
# substring is odd.
(
(
((?!8).)*
(8|3$) # This is the best part. Until the very last iteration
# of the loop outside the {2} loop, this alternation
# can only match 8, and once it reaches the end of the
# substring, it can match 3$ only once. This guarantees
# that it will match 8 an odd number of times, in matched
# pairs until finding one more at the end of the substring,
# which is paired with the 3$ instead of another 8.
){2}
)*$
)
.*(.)+ # 8 = cycle through all characters in this substring
# Assert (within this context) that at least one character appears an odd
# number of times within our substring. (Outside this negative lookbehind,
# that is equivalent to asserting that no character appears an odd number
# of times in our substring.)
3 # Skip to our substring (do not look further right than its end)
)
)
I wrote it using molecular lookahead (103 69 bytes) before converting it to variable-length lookbehind:
^(?!.*(?*(.+)(.*$))(?!^1$|(?*(.)+.*2$)((((?!3).)*(3|2$)){2})*$))
^
(?!
.*(?*(.+)(.*$)) # cycle through all non-empty substrings;
# 1 = the current substring;
# 2 = the part to exclude from the end
(?! # Assert that no character in the substring appears a
# total even number of times.
^1$ # Assert that our substring is not the whole string
# (i.e. it's a strict substring)
|
(?*(.)+.*2$) # 3 = Cycle through all characters that appear in this
# substring.
# Assert (within this context) that this character appears an odd number
# of times within our substring.
(
(
((?!3).)*
(3|2$)
){2}
)*$
)
)
And to aid in making my regex itself well-linked, I've been using a variation of the above regex:
(?*(.+)(.*$))(?!^1$|(?*(.)+.*2$)((((?!3).)*(3|2$)){2})*$)1
When used with regex -xml,rs -o
, this identifies a strict substring of the input that contains an even number of every character (if one exists). Sure, I could have written a non-regex program to do this for me, but where would be the fun in that?
$endgroup$
8
$begingroup$
wtf it's still being golfed
$endgroup$
– ASCII-only
Feb 4 at 8:51
$begingroup$
@ASCII-only and still being golfed...
$endgroup$
– Quintec
Feb 5 at 14:27
add a comment |
$begingroup$
Regex (ECMAScript 2018 or .NET), 140 126 118 100 98 82 bytes
^(?!(^.*)(.+)(.*$)(?<!^2|^1(?=(|(<?(|(?!8).)*(8|3$){1}){2})*$).*(.)+3$)!?=*)
This is much slower than the 98 byte version, because the ^1
is left of the lookahead and is thus evaluated after it. See below for a simple switcheroo that regains the speed. But due to this, the two TIOs below are limited to completing a smaller test case set than before, and the .NET one is too slow to check its own regex.
Try it online! (ECMAScript 2018)
Try it online! (.NET)
To drop 18 bytes (118 → 100), I shamelessly stole a really nice optimization from Neil's regex that avoids the need to put a lookahead inside the negative lookbehind (yielding an 80 byte unrestricted regex). Thank you, Neil!
That became obsolete when it dropped an incredible 16 more bytes (98 → 82) thanks to jaytea's ideas which led to a 69 byte unrestricted regex! It's much slower, but that's golf!
Note that the (|(
no-ops for making the regex well-linked have the result of making the it evaluate very slowly under .NET. They do not have this effect in ECMAScript because zero-width matches in a loop are treated as non-matches.
ECMAScript prohibits quantifiers on assertions, so this makes golfing the restricted-source requirements harder. However, at this point it's so well-golfed that I don't think lifting that particular restriction would open up any further golfing possibilities.
Without the extra characters needed to make it pass the restrictions (101 69 bytes):
^(?!(.*)(.+)(.*$)(?<!^2|^1(?=((((?!8).)*(8|3$)){2})*$).*(.)+3))
It's slow, but this simple switcheroo (for 74 bytes) regains all the lost speed and more:
^(?!(.*)(.+)(.*$)(?<!^2|(?=((((?!8).)*(8|3$)){2})*$)(?<=^1).*(.)+3))
^
(?!
(.*) # cycle through all starting points of substrings;
# 1 = part to exclude from the start
(.+) # cycle through all ending points of non-empty substrings;
# 2 = the substring
(.*$) # 3 = part to exclude from the end
(?<! # Assert that every character in the substring appears a total
# even number of times.
^2 # Assert that our substring is not the whole string. We don't
# need a $ anchor because we were already at the end before
# entering this lookbehind.
| # Note that the following steps are evaluated right to left,
# so please read them from bottom to top.
^1 # Do not look further left than the start of our substring.
(?=
# Assert that the number of times the character 8 appears in our
# substring is odd.
(
(
((?!8).)*
(8|3$) # This is the best part. Until the very last iteration
# of the loop outside the {2} loop, this alternation
# can only match 8, and once it reaches the end of the
# substring, it can match 3$ only once. This guarantees
# that it will match 8 an odd number of times, in matched
# pairs until finding one more at the end of the substring,
# which is paired with the 3$ instead of another 8.
){2}
)*$
)
.*(.)+ # 8 = cycle through all characters in this substring
# Assert (within this context) that at least one character appears an odd
# number of times within our substring. (Outside this negative lookbehind,
# that is equivalent to asserting that no character appears an odd number
# of times in our substring.)
3 # Skip to our substring (do not look further right than its end)
)
)
I wrote it using molecular lookahead (103 69 bytes) before converting it to variable-length lookbehind:
^(?!.*(?*(.+)(.*$))(?!^1$|(?*(.)+.*2$)((((?!3).)*(3|2$)){2})*$))
^
(?!
.*(?*(.+)(.*$)) # cycle through all non-empty substrings;
# 1 = the current substring;
# 2 = the part to exclude from the end
(?! # Assert that no character in the substring appears a
# total even number of times.
^1$ # Assert that our substring is not the whole string
# (i.e. it's a strict substring)
|
(?*(.)+.*2$) # 3 = Cycle through all characters that appear in this
# substring.
# Assert (within this context) that this character appears an odd number
# of times within our substring.
(
(
((?!3).)*
(3|2$)
){2}
)*$
)
)
And to aid in making my regex itself well-linked, I've been using a variation of the above regex:
(?*(.+)(.*$))(?!^1$|(?*(.)+.*2$)((((?!3).)*(3|2$)){2})*$)1
When used with regex -xml,rs -o
, this identifies a strict substring of the input that contains an even number of every character (if one exists). Sure, I could have written a non-regex program to do this for me, but where would be the fun in that?
$endgroup$
8
$begingroup$
wtf it's still being golfed
$endgroup$
– ASCII-only
Feb 4 at 8:51
$begingroup$
@ASCII-only and still being golfed...
$endgroup$
– Quintec
Feb 5 at 14:27
add a comment |
$begingroup$
Regex (ECMAScript 2018 or .NET), 140 126 118 100 98 82 bytes
^(?!(^.*)(.+)(.*$)(?<!^2|^1(?=(|(<?(|(?!8).)*(8|3$){1}){2})*$).*(.)+3$)!?=*)
This is much slower than the 98 byte version, because the ^1
is left of the lookahead and is thus evaluated after it. See below for a simple switcheroo that regains the speed. But due to this, the two TIOs below are limited to completing a smaller test case set than before, and the .NET one is too slow to check its own regex.
Try it online! (ECMAScript 2018)
Try it online! (.NET)
To drop 18 bytes (118 → 100), I shamelessly stole a really nice optimization from Neil's regex that avoids the need to put a lookahead inside the negative lookbehind (yielding an 80 byte unrestricted regex). Thank you, Neil!
That became obsolete when it dropped an incredible 16 more bytes (98 → 82) thanks to jaytea's ideas which led to a 69 byte unrestricted regex! It's much slower, but that's golf!
Note that the (|(
no-ops for making the regex well-linked have the result of making the it evaluate very slowly under .NET. They do not have this effect in ECMAScript because zero-width matches in a loop are treated as non-matches.
ECMAScript prohibits quantifiers on assertions, so this makes golfing the restricted-source requirements harder. However, at this point it's so well-golfed that I don't think lifting that particular restriction would open up any further golfing possibilities.
Without the extra characters needed to make it pass the restrictions (101 69 bytes):
^(?!(.*)(.+)(.*$)(?<!^2|^1(?=((((?!8).)*(8|3$)){2})*$).*(.)+3))
It's slow, but this simple switcheroo (for 74 bytes) regains all the lost speed and more:
^(?!(.*)(.+)(.*$)(?<!^2|(?=((((?!8).)*(8|3$)){2})*$)(?<=^1).*(.)+3))
^
(?!
(.*) # cycle through all starting points of substrings;
# 1 = part to exclude from the start
(.+) # cycle through all ending points of non-empty substrings;
# 2 = the substring
(.*$) # 3 = part to exclude from the end
(?<! # Assert that every character in the substring appears a total
# even number of times.
^2 # Assert that our substring is not the whole string. We don't
# need a $ anchor because we were already at the end before
# entering this lookbehind.
| # Note that the following steps are evaluated right to left,
# so please read them from bottom to top.
^1 # Do not look further left than the start of our substring.
(?=
# Assert that the number of times the character 8 appears in our
# substring is odd.
(
(
((?!8).)*
(8|3$) # This is the best part. Until the very last iteration
# of the loop outside the {2} loop, this alternation
# can only match 8, and once it reaches the end of the
# substring, it can match 3$ only once. This guarantees
# that it will match 8 an odd number of times, in matched
# pairs until finding one more at the end of the substring,
# which is paired with the 3$ instead of another 8.
){2}
)*$
)
.*(.)+ # 8 = cycle through all characters in this substring
# Assert (within this context) that at least one character appears an odd
# number of times within our substring. (Outside this negative lookbehind,
# that is equivalent to asserting that no character appears an odd number
# of times in our substring.)
3 # Skip to our substring (do not look further right than its end)
)
)
I wrote it using molecular lookahead (103 69 bytes) before converting it to variable-length lookbehind:
^(?!.*(?*(.+)(.*$))(?!^1$|(?*(.)+.*2$)((((?!3).)*(3|2$)){2})*$))
^
(?!
.*(?*(.+)(.*$)) # cycle through all non-empty substrings;
# 1 = the current substring;
# 2 = the part to exclude from the end
(?! # Assert that no character in the substring appears a
# total even number of times.
^1$ # Assert that our substring is not the whole string
# (i.e. it's a strict substring)
|
(?*(.)+.*2$) # 3 = Cycle through all characters that appear in this
# substring.
# Assert (within this context) that this character appears an odd number
# of times within our substring.
(
(
((?!3).)*
(3|2$)
){2}
)*$
)
)
And to aid in making my regex itself well-linked, I've been using a variation of the above regex:
(?*(.+)(.*$))(?!^1$|(?*(.)+.*2$)((((?!3).)*(3|2$)){2})*$)1
When used with regex -xml,rs -o
, this identifies a strict substring of the input that contains an even number of every character (if one exists). Sure, I could have written a non-regex program to do this for me, but where would be the fun in that?
$endgroup$
Regex (ECMAScript 2018 or .NET), 140 126 118 100 98 82 bytes
^(?!(^.*)(.+)(.*$)(?<!^2|^1(?=(|(<?(|(?!8).)*(8|3$){1}){2})*$).*(.)+3$)!?=*)
This is much slower than the 98 byte version, because the ^1
is left of the lookahead and is thus evaluated after it. See below for a simple switcheroo that regains the speed. But due to this, the two TIOs below are limited to completing a smaller test case set than before, and the .NET one is too slow to check its own regex.
Try it online! (ECMAScript 2018)
Try it online! (.NET)
To drop 18 bytes (118 → 100), I shamelessly stole a really nice optimization from Neil's regex that avoids the need to put a lookahead inside the negative lookbehind (yielding an 80 byte unrestricted regex). Thank you, Neil!
That became obsolete when it dropped an incredible 16 more bytes (98 → 82) thanks to jaytea's ideas which led to a 69 byte unrestricted regex! It's much slower, but that's golf!
Note that the (|(
no-ops for making the regex well-linked have the result of making the it evaluate very slowly under .NET. They do not have this effect in ECMAScript because zero-width matches in a loop are treated as non-matches.
ECMAScript prohibits quantifiers on assertions, so this makes golfing the restricted-source requirements harder. However, at this point it's so well-golfed that I don't think lifting that particular restriction would open up any further golfing possibilities.
Without the extra characters needed to make it pass the restrictions (101 69 bytes):
^(?!(.*)(.+)(.*$)(?<!^2|^1(?=((((?!8).)*(8|3$)){2})*$).*(.)+3))
It's slow, but this simple switcheroo (for 74 bytes) regains all the lost speed and more:
^(?!(.*)(.+)(.*$)(?<!^2|(?=((((?!8).)*(8|3$)){2})*$)(?<=^1).*(.)+3))
^
(?!
(.*) # cycle through all starting points of substrings;
# 1 = part to exclude from the start
(.+) # cycle through all ending points of non-empty substrings;
# 2 = the substring
(.*$) # 3 = part to exclude from the end
(?<! # Assert that every character in the substring appears a total
# even number of times.
^2 # Assert that our substring is not the whole string. We don't
# need a $ anchor because we were already at the end before
# entering this lookbehind.
| # Note that the following steps are evaluated right to left,
# so please read them from bottom to top.
^1 # Do not look further left than the start of our substring.
(?=
# Assert that the number of times the character 8 appears in our
# substring is odd.
(
(
((?!8).)*
(8|3$) # This is the best part. Until the very last iteration
# of the loop outside the {2} loop, this alternation
# can only match 8, and once it reaches the end of the
# substring, it can match 3$ only once. This guarantees
# that it will match 8 an odd number of times, in matched
# pairs until finding one more at the end of the substring,
# which is paired with the 3$ instead of another 8.
){2}
)*$
)
.*(.)+ # 8 = cycle through all characters in this substring
# Assert (within this context) that at least one character appears an odd
# number of times within our substring. (Outside this negative lookbehind,
# that is equivalent to asserting that no character appears an odd number
# of times in our substring.)
3 # Skip to our substring (do not look further right than its end)
)
)
I wrote it using molecular lookahead (103 69 bytes) before converting it to variable-length lookbehind:
^(?!.*(?*(.+)(.*$))(?!^1$|(?*(.)+.*2$)((((?!3).)*(3|2$)){2})*$))
^
(?!
.*(?*(.+)(.*$)) # cycle through all non-empty substrings;
# 1 = the current substring;
# 2 = the part to exclude from the end
(?! # Assert that no character in the substring appears a
# total even number of times.
^1$ # Assert that our substring is not the whole string
# (i.e. it's a strict substring)
|
(?*(.)+.*2$) # 3 = Cycle through all characters that appear in this
# substring.
# Assert (within this context) that this character appears an odd number
# of times within our substring.
(
(
((?!3).)*
(3|2$)
){2}
)*$
)
)
And to aid in making my regex itself well-linked, I've been using a variation of the above regex:
(?*(.+)(.*$))(?!^1$|(?*(.)+.*2$)((((?!3).)*(3|2$)){2})*$)1
When used with regex -xml,rs -o
, this identifies a strict substring of the input that contains an even number of every character (if one exists). Sure, I could have written a non-regex program to do this for me, but where would be the fun in that?
edited Feb 7 at 21:25
answered Feb 4 at 4:36
DeadcodeDeadcode
1,4641017
1,4641017
8
$begingroup$
wtf it's still being golfed
$endgroup$
– ASCII-only
Feb 4 at 8:51
$begingroup$
@ASCII-only and still being golfed...
$endgroup$
– Quintec
Feb 5 at 14:27
add a comment |
8
$begingroup$
wtf it's still being golfed
$endgroup$
– ASCII-only
Feb 4 at 8:51
$begingroup$
@ASCII-only and still being golfed...
$endgroup$
– Quintec
Feb 5 at 14:27
8
8
$begingroup$
wtf it's still being golfed
$endgroup$
– ASCII-only
Feb 4 at 8:51
$begingroup$
wtf it's still being golfed
$endgroup$
– ASCII-only
Feb 4 at 8:51
$begingroup$
@ASCII-only and still being golfed...
$endgroup$
– Quintec
Feb 5 at 14:27
$begingroup$
@ASCII-only and still being golfed...
$endgroup$
– Quintec
Feb 5 at 14:27
add a comment |
$begingroup$
Jelly, 20 bytes
ĠẈḂẸẆṖÇ€Ạ
ĠẈḂẸ
ẆṖÇ€Ạ
Try it online!
The first line is ignored. It's only there to satisfy the condition that every character appear an even number of times.
The next line first Ġ
roups indices by their value. If we then take the length of each sublist in the resulting list (Ẉ
), we get the number of times each character appears. To check whether any of these are non-even, we get the last Ḃ
it of each count and ask whether there Ẹ
xists a truthy (nonzero) value.
Therefore, this helper link returns whether a substring cannot be circled.
In the main link, we take all substrings of the input (Ẇ
), Ṗ
op off the last one (so that we don't check whether the entire string can be circled), and run the helper link (Ç
) on €
ach substring. The result is then whether Ạ
ll substrings cannot be circled.
$endgroup$
$begingroup$
So, yeah, this would be my solution too, but, unfortunately, it's boring... :(
$endgroup$
– Erik the Outgolfer
Feb 3 at 9:58
add a comment |
$begingroup$
Jelly, 20 bytes
ĠẈḂẸẆṖÇ€Ạ
ĠẈḂẸ
ẆṖÇ€Ạ
Try it online!
The first line is ignored. It's only there to satisfy the condition that every character appear an even number of times.
The next line first Ġ
roups indices by their value. If we then take the length of each sublist in the resulting list (Ẉ
), we get the number of times each character appears. To check whether any of these are non-even, we get the last Ḃ
it of each count and ask whether there Ẹ
xists a truthy (nonzero) value.
Therefore, this helper link returns whether a substring cannot be circled.
In the main link, we take all substrings of the input (Ẇ
), Ṗ
op off the last one (so that we don't check whether the entire string can be circled), and run the helper link (Ç
) on €
ach substring. The result is then whether Ạ
ll substrings cannot be circled.
$endgroup$
$begingroup$
So, yeah, this would be my solution too, but, unfortunately, it's boring... :(
$endgroup$
– Erik the Outgolfer
Feb 3 at 9:58
add a comment |
$begingroup$
Jelly, 20 bytes
ĠẈḂẸẆṖÇ€Ạ
ĠẈḂẸ
ẆṖÇ€Ạ
Try it online!
The first line is ignored. It's only there to satisfy the condition that every character appear an even number of times.
The next line first Ġ
roups indices by their value. If we then take the length of each sublist in the resulting list (Ẉ
), we get the number of times each character appears. To check whether any of these are non-even, we get the last Ḃ
it of each count and ask whether there Ẹ
xists a truthy (nonzero) value.
Therefore, this helper link returns whether a substring cannot be circled.
In the main link, we take all substrings of the input (Ẇ
), Ṗ
op off the last one (so that we don't check whether the entire string can be circled), and run the helper link (Ç
) on €
ach substring. The result is then whether Ạ
ll substrings cannot be circled.
$endgroup$
Jelly, 20 bytes
ĠẈḂẸẆṖÇ€Ạ
ĠẈḂẸ
ẆṖÇ€Ạ
Try it online!
The first line is ignored. It's only there to satisfy the condition that every character appear an even number of times.
The next line first Ġ
roups indices by their value. If we then take the length of each sublist in the resulting list (Ẉ
), we get the number of times each character appears. To check whether any of these are non-even, we get the last Ḃ
it of each count and ask whether there Ẹ
xists a truthy (nonzero) value.
Therefore, this helper link returns whether a substring cannot be circled.
In the main link, we take all substrings of the input (Ẇ
), Ṗ
op off the last one (so that we don't check whether the entire string can be circled), and run the helper link (Ç
) on €
ach substring. The result is then whether Ạ
ll substrings cannot be circled.
edited Feb 3 at 1:24
answered Feb 3 at 1:10
Doorknob♦Doorknob
54.6k17114348
54.6k17114348
$begingroup$
So, yeah, this would be my solution too, but, unfortunately, it's boring... :(
$endgroup$
– Erik the Outgolfer
Feb 3 at 9:58
add a comment |
$begingroup$
So, yeah, this would be my solution too, but, unfortunately, it's boring... :(
$endgroup$
– Erik the Outgolfer
Feb 3 at 9:58
$begingroup$
So, yeah, this would be my solution too, but, unfortunately, it's boring... :(
$endgroup$
– Erik the Outgolfer
Feb 3 at 9:58
$begingroup$
So, yeah, this would be my solution too, but, unfortunately, it's boring... :(
$endgroup$
– Erik the Outgolfer
Feb 3 at 9:58
add a comment |
$begingroup$
J, 42 bytes
(*#'.,012&|@~#')=1#.[:,([:*/0=2&|@#/.~).
Try it online!
explanation
(*#'.,012&|@~#') = 1 #. [: , ([: */ 0 = 2&|@#/.~).
(*#'.,012&|@~#') NB. this evaluates to 1
NB. while supplying extra
NB. chars we need. hence...
= NB. does 1 equal...
1 #. NB. the sum of...
[: , NB. the flatten of...
( ). NB. the verb in parens applied
NB. to every suffix of every
NB. prefix, ie, all contiguous
NB. substrings
([: */ 0 = 2&|@#/.~) NB. def of verb in paren:
/.~ NB. when we group by each
NB. distinct char...
[: */ NB. is it the case that
NB. every group...
@# NB. has a length...
0 = 2&| NB. divisible by 2...
$endgroup$
1
$begingroup$
@Deadcode Since handling that amounts to doing the opposite test for the whole string as for every other substring, it seems a safe bet that most solutions would leave that out. Testing withabc
, only the Perl entry doesn't "fail" on it. (It has other problems though.)
$endgroup$
– Ørjan Johansen
Feb 4 at 3:12
1
$begingroup$
@ØrjanJohansen You misunderstood. I said strings with an odd total number of any character (which only disqualifies programs' source code, not well-linked strings) can be well-linked, and this program returns falsey for some of those well-linked strings. The question explicitly allows this undefined behavior, so the program is valid. Jonah, I think it's really interesting that your program does this and I admire that you figured out a method that works this way. I would love an explanation. This kind of programming is completely alien to me so I don't understand the comments and code.
$endgroup$
– Deadcode
Feb 4 at 9:12
add a comment |
$begingroup$
J, 42 bytes
(*#'.,012&|@~#')=1#.[:,([:*/0=2&|@#/.~).
Try it online!
explanation
(*#'.,012&|@~#') = 1 #. [: , ([: */ 0 = 2&|@#/.~).
(*#'.,012&|@~#') NB. this evaluates to 1
NB. while supplying extra
NB. chars we need. hence...
= NB. does 1 equal...
1 #. NB. the sum of...
[: , NB. the flatten of...
( ). NB. the verb in parens applied
NB. to every suffix of every
NB. prefix, ie, all contiguous
NB. substrings
([: */ 0 = 2&|@#/.~) NB. def of verb in paren:
/.~ NB. when we group by each
NB. distinct char...
[: */ NB. is it the case that
NB. every group...
@# NB. has a length...
0 = 2&| NB. divisible by 2...
$endgroup$
1
$begingroup$
@Deadcode Since handling that amounts to doing the opposite test for the whole string as for every other substring, it seems a safe bet that most solutions would leave that out. Testing withabc
, only the Perl entry doesn't "fail" on it. (It has other problems though.)
$endgroup$
– Ørjan Johansen
Feb 4 at 3:12
1
$begingroup$
@ØrjanJohansen You misunderstood. I said strings with an odd total number of any character (which only disqualifies programs' source code, not well-linked strings) can be well-linked, and this program returns falsey for some of those well-linked strings. The question explicitly allows this undefined behavior, so the program is valid. Jonah, I think it's really interesting that your program does this and I admire that you figured out a method that works this way. I would love an explanation. This kind of programming is completely alien to me so I don't understand the comments and code.
$endgroup$
– Deadcode
Feb 4 at 9:12
add a comment |
$begingroup$
J, 42 bytes
(*#'.,012&|@~#')=1#.[:,([:*/0=2&|@#/.~).
Try it online!
explanation
(*#'.,012&|@~#') = 1 #. [: , ([: */ 0 = 2&|@#/.~).
(*#'.,012&|@~#') NB. this evaluates to 1
NB. while supplying extra
NB. chars we need. hence...
= NB. does 1 equal...
1 #. NB. the sum of...
[: , NB. the flatten of...
( ). NB. the verb in parens applied
NB. to every suffix of every
NB. prefix, ie, all contiguous
NB. substrings
([: */ 0 = 2&|@#/.~) NB. def of verb in paren:
/.~ NB. when we group by each
NB. distinct char...
[: */ NB. is it the case that
NB. every group...
@# NB. has a length...
0 = 2&| NB. divisible by 2...
$endgroup$
J, 42 bytes
(*#'.,012&|@~#')=1#.[:,([:*/0=2&|@#/.~).
Try it online!
explanation
(*#'.,012&|@~#') = 1 #. [: , ([: */ 0 = 2&|@#/.~).
(*#'.,012&|@~#') NB. this evaluates to 1
NB. while supplying extra
NB. chars we need. hence...
= NB. does 1 equal...
1 #. NB. the sum of...
[: , NB. the flatten of...
( ). NB. the verb in parens applied
NB. to every suffix of every
NB. prefix, ie, all contiguous
NB. substrings
([: */ 0 = 2&|@#/.~) NB. def of verb in paren:
/.~ NB. when we group by each
NB. distinct char...
[: */ NB. is it the case that
NB. every group...
@# NB. has a length...
0 = 2&| NB. divisible by 2...
edited Feb 4 at 1:52
answered Feb 4 at 0:57
JonahJonah
2,121916
2,121916
1
$begingroup$
@Deadcode Since handling that amounts to doing the opposite test for the whole string as for every other substring, it seems a safe bet that most solutions would leave that out. Testing withabc
, only the Perl entry doesn't "fail" on it. (It has other problems though.)
$endgroup$
– Ørjan Johansen
Feb 4 at 3:12
1
$begingroup$
@ØrjanJohansen You misunderstood. I said strings with an odd total number of any character (which only disqualifies programs' source code, not well-linked strings) can be well-linked, and this program returns falsey for some of those well-linked strings. The question explicitly allows this undefined behavior, so the program is valid. Jonah, I think it's really interesting that your program does this and I admire that you figured out a method that works this way. I would love an explanation. This kind of programming is completely alien to me so I don't understand the comments and code.
$endgroup$
– Deadcode
Feb 4 at 9:12
add a comment |
1
$begingroup$
@Deadcode Since handling that amounts to doing the opposite test for the whole string as for every other substring, it seems a safe bet that most solutions would leave that out. Testing withabc
, only the Perl entry doesn't "fail" on it. (It has other problems though.)
$endgroup$
– Ørjan Johansen
Feb 4 at 3:12
1
$begingroup$
@ØrjanJohansen You misunderstood. I said strings with an odd total number of any character (which only disqualifies programs' source code, not well-linked strings) can be well-linked, and this program returns falsey for some of those well-linked strings. The question explicitly allows this undefined behavior, so the program is valid. Jonah, I think it's really interesting that your program does this and I admire that you figured out a method that works this way. I would love an explanation. This kind of programming is completely alien to me so I don't understand the comments and code.
$endgroup$
– Deadcode
Feb 4 at 9:12
1
1
$begingroup$
@Deadcode Since handling that amounts to doing the opposite test for the whole string as for every other substring, it seems a safe bet that most solutions would leave that out. Testing with
abc
, only the Perl entry doesn't "fail" on it. (It has other problems though.)$endgroup$
– Ørjan Johansen
Feb 4 at 3:12
$begingroup$
@Deadcode Since handling that amounts to doing the opposite test for the whole string as for every other substring, it seems a safe bet that most solutions would leave that out. Testing with
abc
, only the Perl entry doesn't "fail" on it. (It has other problems though.)$endgroup$
– Ørjan Johansen
Feb 4 at 3:12
1
1
$begingroup$
@ØrjanJohansen You misunderstood. I said strings with an odd total number of any character (which only disqualifies programs' source code, not well-linked strings) can be well-linked, and this program returns falsey for some of those well-linked strings. The question explicitly allows this undefined behavior, so the program is valid. Jonah, I think it's really interesting that your program does this and I admire that you figured out a method that works this way. I would love an explanation. This kind of programming is completely alien to me so I don't understand the comments and code.
$endgroup$
– Deadcode
Feb 4 at 9:12
$begingroup$
@ØrjanJohansen You misunderstood. I said strings with an odd total number of any character (which only disqualifies programs' source code, not well-linked strings) can be well-linked, and this program returns falsey for some of those well-linked strings. The question explicitly allows this undefined behavior, so the program is valid. Jonah, I think it's really interesting that your program does this and I admire that you figured out a method that works this way. I would love an explanation. This kind of programming is completely alien to me so I don't understand the comments and code.
$endgroup$
– Deadcode
Feb 4 at 9:12
add a comment |
$begingroup$
Python 2, 74 bytes
bmn0=f=lambda s,P={0},d=:s<" "if(P in d)else+f(s[f<s:],P^{s[0^0]},[P]+d)
Try it online!
Iterates through the string, keeping track in P
of the set of characters seen an odd number of times so far. The list d
stores all past values of P
, and if see the current P
already in d
, this means that in the characters seen since that time, each character has appeared an even number of times. If so, check if we've gone through the entire input: if we have, accept because the whole string is paired as expected, and otherwise reject.
Now about the source restriction. Characters needing pairing are stuffed into various harmless places, underlined below:
bmn0=f=lambda s,P={0},d=:s<" "if(P in d)else+f(s[f<s:],P^{s[0^0]},[P]+d)
_____ _ _ _ _ ___ ___
The f<s
evaluates to 0
while pairing off an f
, taking advantage of the function name also being f
so that it's defined (by the time the function is called.) The 0^0
absorbs an ^
symbol.
The 0
in P={0}
is unfortunate: in Python {}
evaluates to an empty dict rather than an empty set as we want, and here we can put in any non-character element and it will be harmless. I don't see anything spare to put in though, and have put in a 0
and duplicated it in bmn0
, costing 2 bytes. Note that initial arguments are evaluated when the function is defined, so variables we define ourselves can't be put in here.
$endgroup$
add a comment |
$begingroup$
Python 2, 74 bytes
bmn0=f=lambda s,P={0},d=:s<" "if(P in d)else+f(s[f<s:],P^{s[0^0]},[P]+d)
Try it online!
Iterates through the string, keeping track in P
of the set of characters seen an odd number of times so far. The list d
stores all past values of P
, and if see the current P
already in d
, this means that in the characters seen since that time, each character has appeared an even number of times. If so, check if we've gone through the entire input: if we have, accept because the whole string is paired as expected, and otherwise reject.
Now about the source restriction. Characters needing pairing are stuffed into various harmless places, underlined below:
bmn0=f=lambda s,P={0},d=:s<" "if(P in d)else+f(s[f<s:],P^{s[0^0]},[P]+d)
_____ _ _ _ _ ___ ___
The f<s
evaluates to 0
while pairing off an f
, taking advantage of the function name also being f
so that it's defined (by the time the function is called.) The 0^0
absorbs an ^
symbol.
The 0
in P={0}
is unfortunate: in Python {}
evaluates to an empty dict rather than an empty set as we want, and here we can put in any non-character element and it will be harmless. I don't see anything spare to put in though, and have put in a 0
and duplicated it in bmn0
, costing 2 bytes. Note that initial arguments are evaluated when the function is defined, so variables we define ourselves can't be put in here.
$endgroup$
add a comment |
$begingroup$
Python 2, 74 bytes
bmn0=f=lambda s,P={0},d=:s<" "if(P in d)else+f(s[f<s:],P^{s[0^0]},[P]+d)
Try it online!
Iterates through the string, keeping track in P
of the set of characters seen an odd number of times so far. The list d
stores all past values of P
, and if see the current P
already in d
, this means that in the characters seen since that time, each character has appeared an even number of times. If so, check if we've gone through the entire input: if we have, accept because the whole string is paired as expected, and otherwise reject.
Now about the source restriction. Characters needing pairing are stuffed into various harmless places, underlined below:
bmn0=f=lambda s,P={0},d=:s<" "if(P in d)else+f(s[f<s:],P^{s[0^0]},[P]+d)
_____ _ _ _ _ ___ ___
The f<s
evaluates to 0
while pairing off an f
, taking advantage of the function name also being f
so that it's defined (by the time the function is called.) The 0^0
absorbs an ^
symbol.
The 0
in P={0}
is unfortunate: in Python {}
evaluates to an empty dict rather than an empty set as we want, and here we can put in any non-character element and it will be harmless. I don't see anything spare to put in though, and have put in a 0
and duplicated it in bmn0
, costing 2 bytes. Note that initial arguments are evaluated when the function is defined, so variables we define ourselves can't be put in here.
$endgroup$
Python 2, 74 bytes
bmn0=f=lambda s,P={0},d=:s<" "if(P in d)else+f(s[f<s:],P^{s[0^0]},[P]+d)
Try it online!
Iterates through the string, keeping track in P
of the set of characters seen an odd number of times so far. The list d
stores all past values of P
, and if see the current P
already in d
, this means that in the characters seen since that time, each character has appeared an even number of times. If so, check if we've gone through the entire input: if we have, accept because the whole string is paired as expected, and otherwise reject.
Now about the source restriction. Characters needing pairing are stuffed into various harmless places, underlined below:
bmn0=f=lambda s,P={0},d=:s<" "if(P in d)else+f(s[f<s:],P^{s[0^0]},[P]+d)
_____ _ _ _ _ ___ ___
The f<s
evaluates to 0
while pairing off an f
, taking advantage of the function name also being f
so that it's defined (by the time the function is called.) The 0^0
absorbs an ^
symbol.
The 0
in P={0}
is unfortunate: in Python {}
evaluates to an empty dict rather than an empty set as we want, and here we can put in any non-character element and it will be harmless. I don't see anything spare to put in though, and have put in a 0
and duplicated it in bmn0
, costing 2 bytes. Note that initial arguments are evaluated when the function is defined, so variables we define ourselves can't be put in here.
edited Feb 4 at 8:21
answered Feb 4 at 7:04
xnorxnor
90.4k18185439
90.4k18185439
add a comment |
add a comment |
$begingroup$
Perl 6, 76 bytes
*.comb[^*X+(^*).map(^*)].grep({$_&[&]($_)}).none.Bag{*}.none%2#*^+XBob2rec%#
Try it online!
A Whatever lambda that returns a None Junction of None Junctions that can be boolified to a truthy/falsey value. I would recommend not removing the ?
that boolifies the return result though, otherwise the output gets rather large.
This solution is a little more complex than needed, due to several involved functions being unlinked, e.g. ..
, all
, >>
, %%
etc. Without the source restriction, this could be 43 bytes:
*.comb[^*X.. ^*].grep(?*).one.Bag{*}.all%%2
Try it online!
Explanation:
*.comb # Split the string to a list of characters
[^*X+(^*).map(^*)] # Get all substrings, alongside some garbage
.grep({$_&[&]($_)}) # Filter out the garbage (empty lists, lists with Nil values)
.none # Are none of
.Bag{*} # The count of characters in each substring
.none%2 # All not divisible by 2
#*^+XBob2rec%# And garbage to even out character counts
$endgroup$
add a comment |
$begingroup$
Perl 6, 76 bytes
*.comb[^*X+(^*).map(^*)].grep({$_&[&]($_)}).none.Bag{*}.none%2#*^+XBob2rec%#
Try it online!
A Whatever lambda that returns a None Junction of None Junctions that can be boolified to a truthy/falsey value. I would recommend not removing the ?
that boolifies the return result though, otherwise the output gets rather large.
This solution is a little more complex than needed, due to several involved functions being unlinked, e.g. ..
, all
, >>
, %%
etc. Without the source restriction, this could be 43 bytes:
*.comb[^*X.. ^*].grep(?*).one.Bag{*}.all%%2
Try it online!
Explanation:
*.comb # Split the string to a list of characters
[^*X+(^*).map(^*)] # Get all substrings, alongside some garbage
.grep({$_&[&]($_)}) # Filter out the garbage (empty lists, lists with Nil values)
.none # Are none of
.Bag{*} # The count of characters in each substring
.none%2 # All not divisible by 2
#*^+XBob2rec%# And garbage to even out character counts
$endgroup$
add a comment |
$begingroup$
Perl 6, 76 bytes
*.comb[^*X+(^*).map(^*)].grep({$_&[&]($_)}).none.Bag{*}.none%2#*^+XBob2rec%#
Try it online!
A Whatever lambda that returns a None Junction of None Junctions that can be boolified to a truthy/falsey value. I would recommend not removing the ?
that boolifies the return result though, otherwise the output gets rather large.
This solution is a little more complex than needed, due to several involved functions being unlinked, e.g. ..
, all
, >>
, %%
etc. Without the source restriction, this could be 43 bytes:
*.comb[^*X.. ^*].grep(?*).one.Bag{*}.all%%2
Try it online!
Explanation:
*.comb # Split the string to a list of characters
[^*X+(^*).map(^*)] # Get all substrings, alongside some garbage
.grep({$_&[&]($_)}) # Filter out the garbage (empty lists, lists with Nil values)
.none # Are none of
.Bag{*} # The count of characters in each substring
.none%2 # All not divisible by 2
#*^+XBob2rec%# And garbage to even out character counts
$endgroup$
Perl 6, 76 bytes
*.comb[^*X+(^*).map(^*)].grep({$_&[&]($_)}).none.Bag{*}.none%2#*^+XBob2rec%#
Try it online!
A Whatever lambda that returns a None Junction of None Junctions that can be boolified to a truthy/falsey value. I would recommend not removing the ?
that boolifies the return result though, otherwise the output gets rather large.
This solution is a little more complex than needed, due to several involved functions being unlinked, e.g. ..
, all
, >>
, %%
etc. Without the source restriction, this could be 43 bytes:
*.comb[^*X.. ^*].grep(?*).one.Bag{*}.all%%2
Try it online!
Explanation:
*.comb # Split the string to a list of characters
[^*X+(^*).map(^*)] # Get all substrings, alongside some garbage
.grep({$_&[&]($_)}) # Filter out the garbage (empty lists, lists with Nil values)
.none # Are none of
.Bag{*} # The count of characters in each substring
.none%2 # All not divisible by 2
#*^+XBob2rec%# And garbage to even out character counts
edited Feb 4 at 10:24
answered Feb 4 at 10:14
Jo KingJo King
22.3k251115
22.3k251115
add a comment |
add a comment |
$begingroup$
Perl 5 -p
, 94, 86, 78 bytes
m-.+(?{$Q|=@q&grp,$|=$&eq$_^!grep+/^/&(@m=$g=~/Q$_/g),($g=$&)=~/./g})(?!)-}{
ouput 0 if well-linked 1 otherwise.
78 bytes
86 bytes
94 bytes
How it works
-p
with}{
ending trick to output$
at the end
m-.+(?{
..})(?!)-
, to execute code over all non-empty substring (.+
matches the whole string first, and after executing code between(?{
..})
backtracks because of failed forced(?!)
$Q|=@q&grp,
garbage because of source restriction
$|=
integer bitwise or assignment, if there's almost one 1,$
will be 1 (true), by default it is empty (false)
$&eq$_
the case where the sbustring is the whole string is bitwise xored^
with "no odd character occurence"
($g=$&)=~/./g
to copy the matched substring into$g
(because will be overwirtten after next regex match) and return the array of character of substring.
/^/
garbage which evaluates to 1
grep
1&(@m=$g=~/Q$_/g),
for each character in substring get the array of character in$g
matching itself, array in scalar evaluates to its size andgrep
to filter the chracters with odd occurence1&x
is equivalent tox%2==1
$endgroup$
$begingroup$
I don't think this satisfies the source restriction: I count an odd number of open parentheses, for example
$endgroup$
– msh210
Feb 3 at 21:14
$begingroup$
@msh210 Isn't that the point? If there's an even number, it's not well linked
$endgroup$
– Quintec
Feb 3 at 23:58
$begingroup$
@Quintec One of the requirements for being well linked is that there are an even number of each character.
$endgroup$
– Ørjan Johansen
Feb 4 at 0:31
$begingroup$
my first answer had the requirement but after trying to golf, lost it. updated, but can be golfed.
$endgroup$
– Nahuel Fouilleul
Feb 4 at 5:32
1
$begingroup$
all the sources here satisfy source restriction, also the code returns 0 if well-linked and even number of each character
$endgroup$
– Nahuel Fouilleul
Feb 4 at 9:48
|
show 3 more comments
$begingroup$
Perl 5 -p
, 94, 86, 78 bytes
m-.+(?{$Q|=@q&grp,$|=$&eq$_^!grep+/^/&(@m=$g=~/Q$_/g),($g=$&)=~/./g})(?!)-}{
ouput 0 if well-linked 1 otherwise.
78 bytes
86 bytes
94 bytes
How it works
-p
with}{
ending trick to output$
at the end
m-.+(?{
..})(?!)-
, to execute code over all non-empty substring (.+
matches the whole string first, and after executing code between(?{
..})
backtracks because of failed forced(?!)
$Q|=@q&grp,
garbage because of source restriction
$|=
integer bitwise or assignment, if there's almost one 1,$
will be 1 (true), by default it is empty (false)
$&eq$_
the case where the sbustring is the whole string is bitwise xored^
with "no odd character occurence"
($g=$&)=~/./g
to copy the matched substring into$g
(because will be overwirtten after next regex match) and return the array of character of substring.
/^/
garbage which evaluates to 1
grep
1&(@m=$g=~/Q$_/g),
for each character in substring get the array of character in$g
matching itself, array in scalar evaluates to its size andgrep
to filter the chracters with odd occurence1&x
is equivalent tox%2==1
$endgroup$
$begingroup$
I don't think this satisfies the source restriction: I count an odd number of open parentheses, for example
$endgroup$
– msh210
Feb 3 at 21:14
$begingroup$
@msh210 Isn't that the point? If there's an even number, it's not well linked
$endgroup$
– Quintec
Feb 3 at 23:58
$begingroup$
@Quintec One of the requirements for being well linked is that there are an even number of each character.
$endgroup$
– Ørjan Johansen
Feb 4 at 0:31
$begingroup$
my first answer had the requirement but after trying to golf, lost it. updated, but can be golfed.
$endgroup$
– Nahuel Fouilleul
Feb 4 at 5:32
1
$begingroup$
all the sources here satisfy source restriction, also the code returns 0 if well-linked and even number of each character
$endgroup$
– Nahuel Fouilleul
Feb 4 at 9:48
|
show 3 more comments
$begingroup$
Perl 5 -p
, 94, 86, 78 bytes
m-.+(?{$Q|=@q&grp,$|=$&eq$_^!grep+/^/&(@m=$g=~/Q$_/g),($g=$&)=~/./g})(?!)-}{
ouput 0 if well-linked 1 otherwise.
78 bytes
86 bytes
94 bytes
How it works
-p
with}{
ending trick to output$
at the end
m-.+(?{
..})(?!)-
, to execute code over all non-empty substring (.+
matches the whole string first, and after executing code between(?{
..})
backtracks because of failed forced(?!)
$Q|=@q&grp,
garbage because of source restriction
$|=
integer bitwise or assignment, if there's almost one 1,$
will be 1 (true), by default it is empty (false)
$&eq$_
the case where the sbustring is the whole string is bitwise xored^
with "no odd character occurence"
($g=$&)=~/./g
to copy the matched substring into$g
(because will be overwirtten after next regex match) and return the array of character of substring.
/^/
garbage which evaluates to 1
grep
1&(@m=$g=~/Q$_/g),
for each character in substring get the array of character in$g
matching itself, array in scalar evaluates to its size andgrep
to filter the chracters with odd occurence1&x
is equivalent tox%2==1
$endgroup$
Perl 5 -p
, 94, 86, 78 bytes
m-.+(?{$Q|=@q&grp,$|=$&eq$_^!grep+/^/&(@m=$g=~/Q$_/g),($g=$&)=~/./g})(?!)-}{
ouput 0 if well-linked 1 otherwise.
78 bytes
86 bytes
94 bytes
How it works
-p
with}{
ending trick to output$
at the end
m-.+(?{
..})(?!)-
, to execute code over all non-empty substring (.+
matches the whole string first, and after executing code between(?{
..})
backtracks because of failed forced(?!)
$Q|=@q&grp,
garbage because of source restriction
$|=
integer bitwise or assignment, if there's almost one 1,$
will be 1 (true), by default it is empty (false)
$&eq$_
the case where the sbustring is the whole string is bitwise xored^
with "no odd character occurence"
($g=$&)=~/./g
to copy the matched substring into$g
(because will be overwirtten after next regex match) and return the array of character of substring.
/^/
garbage which evaluates to 1
grep
1&(@m=$g=~/Q$_/g),
for each character in substring get the array of character in$g
matching itself, array in scalar evaluates to its size andgrep
to filter the chracters with odd occurence1&x
is equivalent tox%2==1
edited Feb 4 at 17:25
answered Feb 3 at 17:45
Nahuel FouilleulNahuel Fouilleul
2,27029
2,27029
$begingroup$
I don't think this satisfies the source restriction: I count an odd number of open parentheses, for example
$endgroup$
– msh210
Feb 3 at 21:14
$begingroup$
@msh210 Isn't that the point? If there's an even number, it's not well linked
$endgroup$
– Quintec
Feb 3 at 23:58
$begingroup$
@Quintec One of the requirements for being well linked is that there are an even number of each character.
$endgroup$
– Ørjan Johansen
Feb 4 at 0:31
$begingroup$
my first answer had the requirement but after trying to golf, lost it. updated, but can be golfed.
$endgroup$
– Nahuel Fouilleul
Feb 4 at 5:32
1
$begingroup$
all the sources here satisfy source restriction, also the code returns 0 if well-linked and even number of each character
$endgroup$
– Nahuel Fouilleul
Feb 4 at 9:48
|
show 3 more comments
$begingroup$
I don't think this satisfies the source restriction: I count an odd number of open parentheses, for example
$endgroup$
– msh210
Feb 3 at 21:14
$begingroup$
@msh210 Isn't that the point? If there's an even number, it's not well linked
$endgroup$
– Quintec
Feb 3 at 23:58
$begingroup$
@Quintec One of the requirements for being well linked is that there are an even number of each character.
$endgroup$
– Ørjan Johansen
Feb 4 at 0:31
$begingroup$
my first answer had the requirement but after trying to golf, lost it. updated, but can be golfed.
$endgroup$
– Nahuel Fouilleul
Feb 4 at 5:32
1
$begingroup$
all the sources here satisfy source restriction, also the code returns 0 if well-linked and even number of each character
$endgroup$
– Nahuel Fouilleul
Feb 4 at 9:48
$begingroup$
I don't think this satisfies the source restriction: I count an odd number of open parentheses, for example
$endgroup$
– msh210
Feb 3 at 21:14
$begingroup$
I don't think this satisfies the source restriction: I count an odd number of open parentheses, for example
$endgroup$
– msh210
Feb 3 at 21:14
$begingroup$
@msh210 Isn't that the point? If there's an even number, it's not well linked
$endgroup$
– Quintec
Feb 3 at 23:58
$begingroup$
@msh210 Isn't that the point? If there's an even number, it's not well linked
$endgroup$
– Quintec
Feb 3 at 23:58
$begingroup$
@Quintec One of the requirements for being well linked is that there are an even number of each character.
$endgroup$
– Ørjan Johansen
Feb 4 at 0:31
$begingroup$
@Quintec One of the requirements for being well linked is that there are an even number of each character.
$endgroup$
– Ørjan Johansen
Feb 4 at 0:31
$begingroup$
my first answer had the requirement but after trying to golf, lost it. updated, but can be golfed.
$endgroup$
– Nahuel Fouilleul
Feb 4 at 5:32
$begingroup$
my first answer had the requirement but after trying to golf, lost it. updated, but can be golfed.
$endgroup$
– Nahuel Fouilleul
Feb 4 at 5:32
1
1
$begingroup$
all the sources here satisfy source restriction, also the code returns 0 if well-linked and even number of each character
$endgroup$
– Nahuel Fouilleul
Feb 4 at 9:48
$begingroup$
all the sources here satisfy source restriction, also the code returns 0 if well-linked and even number of each character
$endgroup$
– Nahuel Fouilleul
Feb 4 at 9:48
|
show 3 more comments
$begingroup$
Retina, 150 96 bytes
^(?!(.*)(.+)(.*)$(?<!^2|^1(?:(^?(?:(?!6).)*6){2})*(|(?!6).)*(?!.+6.*3$)(.){1,}3)(?!,<)?)
Try it online! Link includes test cases, including itself. Edit: Golfed down the original regex a fair bit with help from @Deadcode, then padded back up slightly less extravagently to maintain the source layout. Explanation:
^(?!(.*)(.+)(.*)$
Assert that no substring 3
exists that matches the following constraints.
(?<!^2|
Assert that the substring is not the whole original string.
^1(?:(^?(?:(?!6).)*6){2})*(|(?!6).)*(?!.+6.*3$)(.){1,}3)(?!,<)?)
Assert that there is no character 6
such that:
- it does not appear between the character itself (exclusive) and the end of the substring
- it appears an even number of times between the start of the substring and itself (exclusive)
In order to pass the source layout constraint, I replaced ((((
with (?:(^?(?:(
and ((
with (|(
. I still had one source constraint ))
left and the characters !()1<{}
left over, so I changed a +
into {1,}
and inserted the useless (?!,<)?
to consume the rest.
$endgroup$
2
$begingroup$
This seems not to satisfy the restricted source requirements.
$endgroup$
– Ørjan Johansen
Feb 3 at 2:00
$begingroup$
@ØrjanJohansen Finally, I've come up with a valid solution. Lots of junk in there though, so there might be something shorter available...
$endgroup$
– Neil
Feb 3 at 23:55
add a comment |
$begingroup$
Retina, 150 96 bytes
^(?!(.*)(.+)(.*)$(?<!^2|^1(?:(^?(?:(?!6).)*6){2})*(|(?!6).)*(?!.+6.*3$)(.){1,}3)(?!,<)?)
Try it online! Link includes test cases, including itself. Edit: Golfed down the original regex a fair bit with help from @Deadcode, then padded back up slightly less extravagently to maintain the source layout. Explanation:
^(?!(.*)(.+)(.*)$
Assert that no substring 3
exists that matches the following constraints.
(?<!^2|
Assert that the substring is not the whole original string.
^1(?:(^?(?:(?!6).)*6){2})*(|(?!6).)*(?!.+6.*3$)(.){1,}3)(?!,<)?)
Assert that there is no character 6
such that:
- it does not appear between the character itself (exclusive) and the end of the substring
- it appears an even number of times between the start of the substring and itself (exclusive)
In order to pass the source layout constraint, I replaced ((((
with (?:(^?(?:(
and ((
with (|(
. I still had one source constraint ))
left and the characters !()1<{}
left over, so I changed a +
into {1,}
and inserted the useless (?!,<)?
to consume the rest.
$endgroup$
2
$begingroup$
This seems not to satisfy the restricted source requirements.
$endgroup$
– Ørjan Johansen
Feb 3 at 2:00
$begingroup$
@ØrjanJohansen Finally, I've come up with a valid solution. Lots of junk in there though, so there might be something shorter available...
$endgroup$
– Neil
Feb 3 at 23:55
add a comment |
$begingroup$
Retina, 150 96 bytes
^(?!(.*)(.+)(.*)$(?<!^2|^1(?:(^?(?:(?!6).)*6){2})*(|(?!6).)*(?!.+6.*3$)(.){1,}3)(?!,<)?)
Try it online! Link includes test cases, including itself. Edit: Golfed down the original regex a fair bit with help from @Deadcode, then padded back up slightly less extravagently to maintain the source layout. Explanation:
^(?!(.*)(.+)(.*)$
Assert that no substring 3
exists that matches the following constraints.
(?<!^2|
Assert that the substring is not the whole original string.
^1(?:(^?(?:(?!6).)*6){2})*(|(?!6).)*(?!.+6.*3$)(.){1,}3)(?!,<)?)
Assert that there is no character 6
such that:
- it does not appear between the character itself (exclusive) and the end of the substring
- it appears an even number of times between the start of the substring and itself (exclusive)
In order to pass the source layout constraint, I replaced ((((
with (?:(^?(?:(
and ((
with (|(
. I still had one source constraint ))
left and the characters !()1<{}
left over, so I changed a +
into {1,}
and inserted the useless (?!,<)?
to consume the rest.
$endgroup$
Retina, 150 96 bytes
^(?!(.*)(.+)(.*)$(?<!^2|^1(?:(^?(?:(?!6).)*6){2})*(|(?!6).)*(?!.+6.*3$)(.){1,}3)(?!,<)?)
Try it online! Link includes test cases, including itself. Edit: Golfed down the original regex a fair bit with help from @Deadcode, then padded back up slightly less extravagently to maintain the source layout. Explanation:
^(?!(.*)(.+)(.*)$
Assert that no substring 3
exists that matches the following constraints.
(?<!^2|
Assert that the substring is not the whole original string.
^1(?:(^?(?:(?!6).)*6){2})*(|(?!6).)*(?!.+6.*3$)(.){1,}3)(?!,<)?)
Assert that there is no character 6
such that:
- it does not appear between the character itself (exclusive) and the end of the substring
- it appears an even number of times between the start of the substring and itself (exclusive)
In order to pass the source layout constraint, I replaced ((((
with (?:(^?(?:(
and ((
with (|(
. I still had one source constraint ))
left and the characters !()1<{}
left over, so I changed a +
into {1,}
and inserted the useless (?!,<)?
to consume the rest.
edited Feb 4 at 23:42
answered Feb 3 at 1:45
NeilNeil
80.6k744178
80.6k744178
2
$begingroup$
This seems not to satisfy the restricted source requirements.
$endgroup$
– Ørjan Johansen
Feb 3 at 2:00
$begingroup$
@ØrjanJohansen Finally, I've come up with a valid solution. Lots of junk in there though, so there might be something shorter available...
$endgroup$
– Neil
Feb 3 at 23:55
add a comment |
2
$begingroup$
This seems not to satisfy the restricted source requirements.
$endgroup$
– Ørjan Johansen
Feb 3 at 2:00
$begingroup$
@ØrjanJohansen Finally, I've come up with a valid solution. Lots of junk in there though, so there might be something shorter available...
$endgroup$
– Neil
Feb 3 at 23:55
2
2
$begingroup$
This seems not to satisfy the restricted source requirements.
$endgroup$
– Ørjan Johansen
Feb 3 at 2:00
$begingroup$
This seems not to satisfy the restricted source requirements.
$endgroup$
– Ørjan Johansen
Feb 3 at 2:00
$begingroup$
@ØrjanJohansen Finally, I've come up with a valid solution. Lots of junk in there though, so there might be something shorter available...
$endgroup$
– Neil
Feb 3 at 23:55
$begingroup$
@ØrjanJohansen Finally, I've come up with a valid solution. Lots of junk in there though, so there might be something shorter available...
$endgroup$
– Neil
Feb 3 at 23:55
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 208 206 200 198 bytes
x=>!x.GroupBy(c=>c).Any(g=>g.Count()%2>0)&!Enumerable.Repeat(x.Count,x.Count*x.Count).Where(
(l,i)=>i%l>0&!x.Skip(i/l).Take(i%l).GroupBy(c=>c).Any(g=>g.Count()%2>0)
).Any()/*>!oyAnC0EmeablR*WhiS/T*/
Try it online!
-2 bytes thanks to @KevinCruijssen!
Finally got it below 200, so I might be done golfing for now :) I ended up creating a second TIO to test things out based on a previous answer.
Try it online!
Things that made this task tricky:
- Equality operator
==
was not allowed - Increment/assign operator
++
was not allowed - Linq
All()
function was not allowed
Commented code below:
// anonymous function that takes an IList as input
x=>
// the first condition makes sure the string even
// counts of each character
!x.GroupBy(c=>c).Any(g=>g.Count()%2>0)&
// the second condition generates all proper substrings of x
// and tests for any that contain even character counts
// the string length `l` is repeated `l*l` times
!Enumerable.Repeat(x.Count,x.Count*x.Count)
.Where((l,i)=>
// check that the substring length is greater than 0
i%l>0&
// use skip/take to generate a substring
// and check for a character count thats odd
// negate the result meaning we have found
// a substring that invalidates the input
!x.Skip(i/l).Take(i%l)
.GroupBy(c=>c).Any(g=>g.Count()%2>0)
)
// if any invalid substrings are found
// then the result in invalid
// the comment string at the end is needed
// to make the program well-linked
.Any()/*>!oyAnC0EmeablR*WhiS/T*/
$endgroup$
$begingroup$
You can remove the two spaces in your trailing comment.
$endgroup$
– Kevin Cruijssen
Feb 4 at 10:56
$begingroup$
@KevinCruijssen - good one :) I had forgotten that I added a space already. I had to throw another one into the source.
$endgroup$
– dana
Feb 4 at 11:04
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 208 206 200 198 bytes
x=>!x.GroupBy(c=>c).Any(g=>g.Count()%2>0)&!Enumerable.Repeat(x.Count,x.Count*x.Count).Where(
(l,i)=>i%l>0&!x.Skip(i/l).Take(i%l).GroupBy(c=>c).Any(g=>g.Count()%2>0)
).Any()/*>!oyAnC0EmeablR*WhiS/T*/
Try it online!
-2 bytes thanks to @KevinCruijssen!
Finally got it below 200, so I might be done golfing for now :) I ended up creating a second TIO to test things out based on a previous answer.
Try it online!
Things that made this task tricky:
- Equality operator
==
was not allowed - Increment/assign operator
++
was not allowed - Linq
All()
function was not allowed
Commented code below:
// anonymous function that takes an IList as input
x=>
// the first condition makes sure the string even
// counts of each character
!x.GroupBy(c=>c).Any(g=>g.Count()%2>0)&
// the second condition generates all proper substrings of x
// and tests for any that contain even character counts
// the string length `l` is repeated `l*l` times
!Enumerable.Repeat(x.Count,x.Count*x.Count)
.Where((l,i)=>
// check that the substring length is greater than 0
i%l>0&
// use skip/take to generate a substring
// and check for a character count thats odd
// negate the result meaning we have found
// a substring that invalidates the input
!x.Skip(i/l).Take(i%l)
.GroupBy(c=>c).Any(g=>g.Count()%2>0)
)
// if any invalid substrings are found
// then the result in invalid
// the comment string at the end is needed
// to make the program well-linked
.Any()/*>!oyAnC0EmeablR*WhiS/T*/
$endgroup$
$begingroup$
You can remove the two spaces in your trailing comment.
$endgroup$
– Kevin Cruijssen
Feb 4 at 10:56
$begingroup$
@KevinCruijssen - good one :) I had forgotten that I added a space already. I had to throw another one into the source.
$endgroup$
– dana
Feb 4 at 11:04
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 208 206 200 198 bytes
x=>!x.GroupBy(c=>c).Any(g=>g.Count()%2>0)&!Enumerable.Repeat(x.Count,x.Count*x.Count).Where(
(l,i)=>i%l>0&!x.Skip(i/l).Take(i%l).GroupBy(c=>c).Any(g=>g.Count()%2>0)
).Any()/*>!oyAnC0EmeablR*WhiS/T*/
Try it online!
-2 bytes thanks to @KevinCruijssen!
Finally got it below 200, so I might be done golfing for now :) I ended up creating a second TIO to test things out based on a previous answer.
Try it online!
Things that made this task tricky:
- Equality operator
==
was not allowed - Increment/assign operator
++
was not allowed - Linq
All()
function was not allowed
Commented code below:
// anonymous function that takes an IList as input
x=>
// the first condition makes sure the string even
// counts of each character
!x.GroupBy(c=>c).Any(g=>g.Count()%2>0)&
// the second condition generates all proper substrings of x
// and tests for any that contain even character counts
// the string length `l` is repeated `l*l` times
!Enumerable.Repeat(x.Count,x.Count*x.Count)
.Where((l,i)=>
// check that the substring length is greater than 0
i%l>0&
// use skip/take to generate a substring
// and check for a character count thats odd
// negate the result meaning we have found
// a substring that invalidates the input
!x.Skip(i/l).Take(i%l)
.GroupBy(c=>c).Any(g=>g.Count()%2>0)
)
// if any invalid substrings are found
// then the result in invalid
// the comment string at the end is needed
// to make the program well-linked
.Any()/*>!oyAnC0EmeablR*WhiS/T*/
$endgroup$
C# (Visual C# Interactive Compiler), 208 206 200 198 bytes
x=>!x.GroupBy(c=>c).Any(g=>g.Count()%2>0)&!Enumerable.Repeat(x.Count,x.Count*x.Count).Where(
(l,i)=>i%l>0&!x.Skip(i/l).Take(i%l).GroupBy(c=>c).Any(g=>g.Count()%2>0)
).Any()/*>!oyAnC0EmeablR*WhiS/T*/
Try it online!
-2 bytes thanks to @KevinCruijssen!
Finally got it below 200, so I might be done golfing for now :) I ended up creating a second TIO to test things out based on a previous answer.
Try it online!
Things that made this task tricky:
- Equality operator
==
was not allowed - Increment/assign operator
++
was not allowed - Linq
All()
function was not allowed
Commented code below:
// anonymous function that takes an IList as input
x=>
// the first condition makes sure the string even
// counts of each character
!x.GroupBy(c=>c).Any(g=>g.Count()%2>0)&
// the second condition generates all proper substrings of x
// and tests for any that contain even character counts
// the string length `l` is repeated `l*l` times
!Enumerable.Repeat(x.Count,x.Count*x.Count)
.Where((l,i)=>
// check that the substring length is greater than 0
i%l>0&
// use skip/take to generate a substring
// and check for a character count thats odd
// negate the result meaning we have found
// a substring that invalidates the input
!x.Skip(i/l).Take(i%l)
.GroupBy(c=>c).Any(g=>g.Count()%2>0)
)
// if any invalid substrings are found
// then the result in invalid
// the comment string at the end is needed
// to make the program well-linked
.Any()/*>!oyAnC0EmeablR*WhiS/T*/
edited Feb 5 at 3:38
answered Feb 4 at 1:32
danadana
1,06165
1,06165
$begingroup$
You can remove the two spaces in your trailing comment.
$endgroup$
– Kevin Cruijssen
Feb 4 at 10:56
$begingroup$
@KevinCruijssen - good one :) I had forgotten that I added a space already. I had to throw another one into the source.
$endgroup$
– dana
Feb 4 at 11:04
add a comment |
$begingroup$
You can remove the two spaces in your trailing comment.
$endgroup$
– Kevin Cruijssen
Feb 4 at 10:56
$begingroup$
@KevinCruijssen - good one :) I had forgotten that I added a space already. I had to throw another one into the source.
$endgroup$
– dana
Feb 4 at 11:04
$begingroup$
You can remove the two spaces in your trailing comment.
$endgroup$
– Kevin Cruijssen
Feb 4 at 10:56
$begingroup$
You can remove the two spaces in your trailing comment.
$endgroup$
– Kevin Cruijssen
Feb 4 at 10:56
$begingroup$
@KevinCruijssen - good one :) I had forgotten that I added a space already. I had to throw another one into the source.
$endgroup$
– dana
Feb 4 at 11:04
$begingroup$
@KevinCruijssen - good one :) I had forgotten that I added a space already. I had to throw another one into the source.
$endgroup$
– dana
Feb 4 at 11:04
add a comment |
$begingroup$
Python 2, 108 bytes
lambda d,e=enumerate:min(max(d[l:l+b].count(k)%2for(k)in d)for b,c in e(d[2:],2)for l,f in e(d) )#b =:x+.%2#
Try it online!
-2 thanks to Ørjan Johansen.
$endgroup$
$begingroup$
@ØrjanJohansen Huh, nice. EDIT: Looks like it saves only two bytes overall.
$endgroup$
– Erik the Outgolfer
Feb 3 at 12:47
add a comment |
$begingroup$
Python 2, 108 bytes
lambda d,e=enumerate:min(max(d[l:l+b].count(k)%2for(k)in d)for b,c in e(d[2:],2)for l,f in e(d) )#b =:x+.%2#
Try it online!
-2 thanks to Ørjan Johansen.
$endgroup$
$begingroup$
@ØrjanJohansen Huh, nice. EDIT: Looks like it saves only two bytes overall.
$endgroup$
– Erik the Outgolfer
Feb 3 at 12:47
add a comment |
$begingroup$
Python 2, 108 bytes
lambda d,e=enumerate:min(max(d[l:l+b].count(k)%2for(k)in d)for b,c in e(d[2:],2)for l,f in e(d) )#b =:x+.%2#
Try it online!
-2 thanks to Ørjan Johansen.
$endgroup$
Python 2, 108 bytes
lambda d,e=enumerate:min(max(d[l:l+b].count(k)%2for(k)in d)for b,c in e(d[2:],2)for l,f in e(d) )#b =:x+.%2#
Try it online!
-2 thanks to Ørjan Johansen.
edited Feb 3 at 13:02
answered Feb 3 at 11:16
Erik the OutgolferErik the Outgolfer
31.8k429103
31.8k429103
$begingroup$
@ØrjanJohansen Huh, nice. EDIT: Looks like it saves only two bytes overall.
$endgroup$
– Erik the Outgolfer
Feb 3 at 12:47
add a comment |
$begingroup$
@ØrjanJohansen Huh, nice. EDIT: Looks like it saves only two bytes overall.
$endgroup$
– Erik the Outgolfer
Feb 3 at 12:47
$begingroup$
@ØrjanJohansen Huh, nice. EDIT: Looks like it saves only two bytes overall.
$endgroup$
– Erik the Outgolfer
Feb 3 at 12:47
$begingroup$
@ØrjanJohansen Huh, nice. EDIT: Looks like it saves only two bytes overall.
$endgroup$
– Erik the Outgolfer
Feb 3 at 12:47
add a comment |
$begingroup$
Python 3.8.0a1, 66 bytes
lambda l,b={id}:len({str(b:=b^{c})for(c)in l})<len(l)#,<^fmnost{}#
The Era of Assignment Expressions is upon us. With PEP 572 included in Python 3.8, golfing will never be the same. You can install the early developer preview 3.8.0a1 here.
Assignment expressions let you use :=
to assign to a variable inline while evaluating to that value. For example, (a:=2, a+1)
gives (2, 3)
. This can of course be used to store variables for reuse, but here we go a step further and use it as an accumulator in a comprehension.
For example, this code computes the cumulative sums [1, 3, 6]
t=0
l=[1,2,3]
print([t:=t+x for x in l])
Note how with each pass through the list comprehension, the cumulative sum t
is increased by x
and the new value is stored in the list produced by the comprehension.
Similarly, b:=b^{c}
updates the set of characters b
to toggle whether it includes character c
, and evaluates to the new value of b
. So, the code [b:=b^{c}for c in l]
iterates over characters c
in l
and accumulates the set of characters seen an odd number of times in each non-empty prefix.
This list is checked for duplicates by making it a set comprehension instead and seeing if its length is smaller than that of s
, which means that some repeats were collapsed. If so, the repeat means that in the portion of s
seen in between those times every character encountered an even number of numbers, making the string non-well-linked. Python doesn't allow sets of sets for being unhashable, so the inner sets are converted to strings instead.
The set b
is initialized as an optional arguments, and successfully gets modified in the function scope. I was worried this would make the function non-reusable, but it seems to reset between runs.
For the source restriction, unpaired characters are stuffed in a comment at the end. Writing for(c)in l
rather than for c in l
cancels the extra parens for free. We put id
into the initial set b
, which is harmless since it can start as any set, but the empty set can't be written as {}
because Python will make an empty dictionary. Since the letters i
and d
are among those needing pairing, we can put the function id
there.
Note that the code outputs negated booleans, so it will correctly give False
on itself.
$endgroup$
add a comment |
$begingroup$
Python 3.8.0a1, 66 bytes
lambda l,b={id}:len({str(b:=b^{c})for(c)in l})<len(l)#,<^fmnost{}#
The Era of Assignment Expressions is upon us. With PEP 572 included in Python 3.8, golfing will never be the same. You can install the early developer preview 3.8.0a1 here.
Assignment expressions let you use :=
to assign to a variable inline while evaluating to that value. For example, (a:=2, a+1)
gives (2, 3)
. This can of course be used to store variables for reuse, but here we go a step further and use it as an accumulator in a comprehension.
For example, this code computes the cumulative sums [1, 3, 6]
t=0
l=[1,2,3]
print([t:=t+x for x in l])
Note how with each pass through the list comprehension, the cumulative sum t
is increased by x
and the new value is stored in the list produced by the comprehension.
Similarly, b:=b^{c}
updates the set of characters b
to toggle whether it includes character c
, and evaluates to the new value of b
. So, the code [b:=b^{c}for c in l]
iterates over characters c
in l
and accumulates the set of characters seen an odd number of times in each non-empty prefix.
This list is checked for duplicates by making it a set comprehension instead and seeing if its length is smaller than that of s
, which means that some repeats were collapsed. If so, the repeat means that in the portion of s
seen in between those times every character encountered an even number of numbers, making the string non-well-linked. Python doesn't allow sets of sets for being unhashable, so the inner sets are converted to strings instead.
The set b
is initialized as an optional arguments, and successfully gets modified in the function scope. I was worried this would make the function non-reusable, but it seems to reset between runs.
For the source restriction, unpaired characters are stuffed in a comment at the end. Writing for(c)in l
rather than for c in l
cancels the extra parens for free. We put id
into the initial set b
, which is harmless since it can start as any set, but the empty set can't be written as {}
because Python will make an empty dictionary. Since the letters i
and d
are among those needing pairing, we can put the function id
there.
Note that the code outputs negated booleans, so it will correctly give False
on itself.
$endgroup$
add a comment |
$begingroup$
Python 3.8.0a1, 66 bytes
lambda l,b={id}:len({str(b:=b^{c})for(c)in l})<len(l)#,<^fmnost{}#
The Era of Assignment Expressions is upon us. With PEP 572 included in Python 3.8, golfing will never be the same. You can install the early developer preview 3.8.0a1 here.
Assignment expressions let you use :=
to assign to a variable inline while evaluating to that value. For example, (a:=2, a+1)
gives (2, 3)
. This can of course be used to store variables for reuse, but here we go a step further and use it as an accumulator in a comprehension.
For example, this code computes the cumulative sums [1, 3, 6]
t=0
l=[1,2,3]
print([t:=t+x for x in l])
Note how with each pass through the list comprehension, the cumulative sum t
is increased by x
and the new value is stored in the list produced by the comprehension.
Similarly, b:=b^{c}
updates the set of characters b
to toggle whether it includes character c
, and evaluates to the new value of b
. So, the code [b:=b^{c}for c in l]
iterates over characters c
in l
and accumulates the set of characters seen an odd number of times in each non-empty prefix.
This list is checked for duplicates by making it a set comprehension instead and seeing if its length is smaller than that of s
, which means that some repeats were collapsed. If so, the repeat means that in the portion of s
seen in between those times every character encountered an even number of numbers, making the string non-well-linked. Python doesn't allow sets of sets for being unhashable, so the inner sets are converted to strings instead.
The set b
is initialized as an optional arguments, and successfully gets modified in the function scope. I was worried this would make the function non-reusable, but it seems to reset between runs.
For the source restriction, unpaired characters are stuffed in a comment at the end. Writing for(c)in l
rather than for c in l
cancels the extra parens for free. We put id
into the initial set b
, which is harmless since it can start as any set, but the empty set can't be written as {}
because Python will make an empty dictionary. Since the letters i
and d
are among those needing pairing, we can put the function id
there.
Note that the code outputs negated booleans, so it will correctly give False
on itself.
$endgroup$
Python 3.8.0a1, 66 bytes
lambda l,b={id}:len({str(b:=b^{c})for(c)in l})<len(l)#,<^fmnost{}#
The Era of Assignment Expressions is upon us. With PEP 572 included in Python 3.8, golfing will never be the same. You can install the early developer preview 3.8.0a1 here.
Assignment expressions let you use :=
to assign to a variable inline while evaluating to that value. For example, (a:=2, a+1)
gives (2, 3)
. This can of course be used to store variables for reuse, but here we go a step further and use it as an accumulator in a comprehension.
For example, this code computes the cumulative sums [1, 3, 6]
t=0
l=[1,2,3]
print([t:=t+x for x in l])
Note how with each pass through the list comprehension, the cumulative sum t
is increased by x
and the new value is stored in the list produced by the comprehension.
Similarly, b:=b^{c}
updates the set of characters b
to toggle whether it includes character c
, and evaluates to the new value of b
. So, the code [b:=b^{c}for c in l]
iterates over characters c
in l
and accumulates the set of characters seen an odd number of times in each non-empty prefix.
This list is checked for duplicates by making it a set comprehension instead and seeing if its length is smaller than that of s
, which means that some repeats were collapsed. If so, the repeat means that in the portion of s
seen in between those times every character encountered an even number of numbers, making the string non-well-linked. Python doesn't allow sets of sets for being unhashable, so the inner sets are converted to strings instead.
The set b
is initialized as an optional arguments, and successfully gets modified in the function scope. I was worried this would make the function non-reusable, but it seems to reset between runs.
For the source restriction, unpaired characters are stuffed in a comment at the end. Writing for(c)in l
rather than for c in l
cancels the extra parens for free. We put id
into the initial set b
, which is harmless since it can start as any set, but the empty set can't be written as {}
because Python will make an empty dictionary. Since the letters i
and d
are among those needing pairing, we can put the function id
there.
Note that the code outputs negated booleans, so it will correctly give False
on itself.
answered Feb 5 at 5:58
xnorxnor
90.4k18185439
90.4k18185439
add a comment |
add a comment |
$begingroup$
05AB1E, 22 20 bytes
Œε¢Pà}KŒIKεSIS¢ÈP}àÈ
Outputs 1
if the string is well-linked, and 0
if the string is not well-linked.
Try it online or verify all test cases.
Explanation:
The base program is ŒsKεsS¢ÈP}à
(11 bytes), which outputs 0
if well-linked and 1
if not well-linked. The trailing È
(is_even) is a semi no-op that inverts the output, so 1
for well-linked strings and 0
for not well-linked strings. The other parts are no-ops to comply to the challenge rules.
Œε¢Pà}K # No-ops: substrings, map, count, product, maximum, close map, remove
# Due to the last remove, we're back at the (implicit) input again
Œ # Take the substrings of the input
IK # Remove the input itself from that list of substrings
ε # Map each substring to:
S # No-op: transform the substring into a list of characters
IS # Get the input-string as a list of characters
¢ # Count the occurrence of each character in the current substring
È # Check which counts are even (1 if truthy; 0 if falsey)
P # Take the product of that
}à # After the map: check if any are truthy by taking the maximum
È # Semi no-op: check if this maximum is even (0 becomes 1; 1 becomes 0)
# (and output the result implicitly)
$endgroup$
add a comment |
$begingroup$
05AB1E, 22 20 bytes
Œε¢Pà}KŒIKεSIS¢ÈP}àÈ
Outputs 1
if the string is well-linked, and 0
if the string is not well-linked.
Try it online or verify all test cases.
Explanation:
The base program is ŒsKεsS¢ÈP}à
(11 bytes), which outputs 0
if well-linked and 1
if not well-linked. The trailing È
(is_even) is a semi no-op that inverts the output, so 1
for well-linked strings and 0
for not well-linked strings. The other parts are no-ops to comply to the challenge rules.
Œε¢Pà}K # No-ops: substrings, map, count, product, maximum, close map, remove
# Due to the last remove, we're back at the (implicit) input again
Œ # Take the substrings of the input
IK # Remove the input itself from that list of substrings
ε # Map each substring to:
S # No-op: transform the substring into a list of characters
IS # Get the input-string as a list of characters
¢ # Count the occurrence of each character in the current substring
È # Check which counts are even (1 if truthy; 0 if falsey)
P # Take the product of that
}à # After the map: check if any are truthy by taking the maximum
È # Semi no-op: check if this maximum is even (0 becomes 1; 1 becomes 0)
# (and output the result implicitly)
$endgroup$
add a comment |
$begingroup$
05AB1E, 22 20 bytes
Œε¢Pà}KŒIKεSIS¢ÈP}àÈ
Outputs 1
if the string is well-linked, and 0
if the string is not well-linked.
Try it online or verify all test cases.
Explanation:
The base program is ŒsKεsS¢ÈP}à
(11 bytes), which outputs 0
if well-linked and 1
if not well-linked. The trailing È
(is_even) is a semi no-op that inverts the output, so 1
for well-linked strings and 0
for not well-linked strings. The other parts are no-ops to comply to the challenge rules.
Œε¢Pà}K # No-ops: substrings, map, count, product, maximum, close map, remove
# Due to the last remove, we're back at the (implicit) input again
Œ # Take the substrings of the input
IK # Remove the input itself from that list of substrings
ε # Map each substring to:
S # No-op: transform the substring into a list of characters
IS # Get the input-string as a list of characters
¢ # Count the occurrence of each character in the current substring
È # Check which counts are even (1 if truthy; 0 if falsey)
P # Take the product of that
}à # After the map: check if any are truthy by taking the maximum
È # Semi no-op: check if this maximum is even (0 becomes 1; 1 becomes 0)
# (and output the result implicitly)
$endgroup$
05AB1E, 22 20 bytes
Œε¢Pà}KŒIKεSIS¢ÈP}àÈ
Outputs 1
if the string is well-linked, and 0
if the string is not well-linked.
Try it online or verify all test cases.
Explanation:
The base program is ŒsKεsS¢ÈP}à
(11 bytes), which outputs 0
if well-linked and 1
if not well-linked. The trailing È
(is_even) is a semi no-op that inverts the output, so 1
for well-linked strings and 0
for not well-linked strings. The other parts are no-ops to comply to the challenge rules.
Œε¢Pà}K # No-ops: substrings, map, count, product, maximum, close map, remove
# Due to the last remove, we're back at the (implicit) input again
Œ # Take the substrings of the input
IK # Remove the input itself from that list of substrings
ε # Map each substring to:
S # No-op: transform the substring into a list of characters
IS # Get the input-string as a list of characters
¢ # Count the occurrence of each character in the current substring
È # Check which counts are even (1 if truthy; 0 if falsey)
P # Take the product of that
}à # After the map: check if any are truthy by taking the maximum
È # Semi no-op: check if this maximum is even (0 becomes 1; 1 becomes 0)
# (and output the result implicitly)
edited Feb 4 at 8:55
answered Feb 4 at 8:47
Kevin CruijssenKevin Cruijssen
37.8k557195
37.8k557195
add a comment |
add a comment |
If this is an answer to a challenge…
…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.
…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.
More generally…
…Please make sure to answer the question and provide sufficient detail.
…Avoid asking for help, clarification or responding to other answers (use comments instead).
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f179422%2fa-well-linked-challenge%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
$begingroup$
Test case:
abcbca -> False
.$endgroup$
– Ørjan Johansen
Feb 3 at 0:56
$begingroup$
I think your hint contains a superfluous
there
.$endgroup$
– Jonathan Frech
Feb 3 at 1:05
2
$begingroup$
So to be clear: Whether a string has a total even number of every character is irrelevant to whether it's a well-linked string. That requirement applies only to programs' source code. This is of course only a matter of semantics, because programs are allowed to have undefined behavior for inputted strings having an odd total number of any character (and at least one submitted program takes advantage of this).
$endgroup$
– Deadcode
Feb 4 at 6:11
$begingroup$
What kinds of characters can be in the input?
$endgroup$
– xnor
Feb 4 at 6:43
$begingroup$
@xnor I added it to the challenge. Hopefully that clears that up.
$endgroup$
– TRITICIMAGVS
Feb 4 at 14:34