Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Build a replacement ball in regex.

+2
−0

In this challenge you will take a number $n$ and a string $X$ of length $\geq n$, and produce a regular expression which matches all strings that are withing $n$ character substitutions of $X$.

Specifically you will take $X$ and $n$ and ouptut all the ways to replace exactly $n$ characters with the regex wildcard ., separated by the regex or symbol |. So for bar and 2 as an input a valid output would be:

..r|.a.|b..

Order doesn't matter, but each replacement should appear exactly once, and no additional symbols should appear. Other regular expressions may perform the same task, but your output should meet the specifications above.

You can assume the input will consist only of the alphanumeric characters a-zA-Z0-9.

This is code-golf, the goal is to minimize the size of your source code as measured in bytes.

Test cases

o, 1 -> .
at, 1 -> .t|a.
moon, 1 -> .oon|m.on|mo.n|moo.
at, 2 -> ..
bar, 2 -> ..r|.a.|b..
test, 2 -> ..st|.e.t|.es.|t..t|t.s.|te..
case, 2 -> ..se|.a.e|.as.|c..e|c.s.|ca..
test, 3 -> ...t|..s.|.e..|t...
History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

3 answers

+2
−0

Ruby, 82 bytes

->w,n{[*0...w.size].permutation(n).map{e=w*1;_1.map{|x|e[x]='.'};e}.uniq.join '|'}

Attempt This Online!

uses permutation to do most of the work. Might be shorter with something recursive, maybe.

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+1
−0

Haskell + hgl, 30 bytes

ic"|"<<tv(:".")><fl<ce '.'><eq

Attempt This Online!

Explanation

This performs a cartesian product to get all possible ways to replace characters with .. It then filters for those that have exactly $n$ .s, and concatenates them with |s.

Alternative version

(ic"|"<ic"."<<mm tl)<<mp"?"><pST<eL<P1

This version is much longer, but in my opinion it has better potential to be shorter than the above version if hgl were improved.

Explanation

This adds a dummy character ? to the front of the list then gets all ways to partition that list, and filters the list down to those that have $n+1$ parts. Then it takes removes the first character of each partition, and concatenates them back together with . as the separator. Finally it concatenates all those together with | as the separator.

Reflection

Reflections on the 30 byte version

  • ce and eq could be combined into a single function which checks if an element occurs exactly $n$ times. This could actually be made to short circuit faster than ce and eq do by default.
  • There should be a function that takes two elements and creates a list containing just those elements. This could be done in terms of free monoids. It would not save bytes here since (:".") is so short, but it is close to being useful here.

Reflections on the alternative version

  • There should be a function to partition a list into chunks of a certain size.
  • I could really use m4 both as a prefix or an infix here.
  • Here I do a two dimensional intercalation using two ics. There could be a builtin that does this.
History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+1
−0

Vyxal, 62 bitsv2, ~8 bytes

ẏḋ\.vȦ\|j

Try it Online!

Really 7.75 bytes, but leaderboard regex is a thing.

Explained

ẏḋ\.vȦ\|j
ẏ         # Range [0, len(X))
 ḋ        # n-length combinations of X without replacement
  \.vȦ    # for each combination c:
          #  replace the item in X at each index in c with "." - basically double vectorised assignment
      \|j # join that on "|"s
History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »