7
\$\begingroup\$

The goal of this challenge is to reduce a list of string to a shorter more general list of string.

Input

The Input is provided with space between the string (a b c) or in list form (["a","b", "c"]). It's a list list of string that can be of the following form :

  • aaa
  • aaaX
  • aaaX/Y

With aaa any set of non capital alphabetic letter, and X or Y any digit between 0 and 9.

Ouput

A list of generalized string that can be separated by anything. String are of the form :

  • aaa
  • aaaX
  • aaaX/Y

With aaa any set of non capital alphabetic letter that were in the input list. X and Y stay the same and symbolise the digit. The goal of the challenge is to reduce the input list of string into their generic representation.

Examples

Two strings can be reduced :

Input : regex1 regex2 split1/0
Output : regexX splitX/Y

Full example :

Input : toto toto titi titi1 titi2 titi5 tutu tutu0 tutu1 tutu1/2 tutu0/1
Output : toto titi titiX tutu tutuX tutuX/Y

Another example :

Input: ["foo0","bar0","baz0/0","eth0/0","eth0/1","eth1/0","eth1/1","vlan8","modem0"]

Output: ["fooX","barX","bazX/Y","ethX/Y","vlanX","modemX"]

Scoring

This is code-golf; shortest code in bytes wins.

\$\endgroup\$
9
  • \$\begingroup\$ Can we take array of strings input, i.e. ["regex1","regex2","split1/0"]? \$\endgroup\$ Commented Aug 10, 2016 at 15:13
  • \$\begingroup\$ I'll modify the question a bit to allow it =) \$\endgroup\$ Commented Aug 10, 2016 at 15:13
  • 1
    \$\begingroup\$ You may want to put the last line elsewhere, at least not in the scoring section \$\endgroup\$ Commented Aug 10, 2016 at 15:16
  • \$\begingroup\$ It's still in the scoring section... \$\endgroup\$ Commented Aug 10, 2016 at 15:21
  • \$\begingroup\$ Is extra trailing newlines okay? \$\endgroup\$ Commented Aug 10, 2016 at 15:24

7 Answers 7

9
\$\begingroup\$

Retina, 25 18 13 bytes

6 byte thanks to FryAmTheEggman, and for inspiration of extra 1 byte.

\d
X
/X
/Y
D`

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ Wow, Upvoted because 120 seconds to answer is so impressive ! \$\endgroup\$ Commented Aug 10, 2016 at 15:12
  • 2
    \$\begingroup\$ @Pierre.Sassoulas This site has seen faster answers. \$\endgroup\$ Commented Aug 10, 2016 at 15:13
5
\$\begingroup\$

Perl, 29 27 bytes

Includes +1 for -p

Give input list as seperate lines on STDIN:

perl reduce.pl
regex1
regex2
split1/0
^D

reduce.pl:

#!/usr/bin/perl -p
s/\d/X/&&s//Y/;$_ x=!$$_++
\$\endgroup\$
3
\$\begingroup\$

Dyalog APL, 21 bytes

∪'/\d' '\d'⎕R'/Y' 'X'

⎕R regex replaces the left-side strings with the right-side ones

returns the unique elements

TryAPL online!

\$\endgroup\$
3
\$\begingroup\$

Brachylog, 61 59 44 42 bytes

2 bytes thanks to Fatalize.

:{:ef:{~s"0123456789","X".;?.}ac.}a:{rbh"/",?rbr:"Y"c.;?.}ad.
:{:ef:{~s"0123456789","X".;?.}ac.L(rbh"/",Lrbr:"Y"c.;.)}ad.
:{s"/",?rbbbr:"X/Y"c.;.?t~s@A;?rbr:"X"c.}ad.
:{s"/",?rbbbr:"X/Y"c.|.t~s@A|rbr:"X"c.}ad.

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Python, 69 bytes

import re
lambda s:set(re.sub("\d","X",s).replace("/X","/Y").split())
\$\endgroup\$
0
2
\$\begingroup\$

Javascript, 92 78 bytes

a=a=>[...new Set(a.replace(/\d/g,"X").replace(/\/./g,"/Y").split` `)].join` `;
\$\endgroup\$
3
  • \$\begingroup\$ Oh whoops... I'll go fix that \$\endgroup\$ Commented Aug 10, 2016 at 17:45
  • 1
    \$\begingroup\$ You don't need a bunch of that boilerplate, just write this as a=>[...new Set(a.replace(/[0-9]/g,"X").replace(/\/./g,"/Y").split` `)].join` `. Also why not use \d instead of [0-9]? \$\endgroup\$ Commented Aug 11, 2016 at 2:33
  • \$\begingroup\$ I tried the first suggestion, JSfiddle didn't like it. I'll try it on babel. \$\endgroup\$ Commented Aug 11, 2016 at 13:29
1
\$\begingroup\$

PHP, 104 bytes

<?=join(' ',array_unique(explode(' ',preg_replace('%\d%','X',preg_replace('%\d/\d%', 'X/Y',$argv[1])))))

expects input as single argument

I could replace the join(' ',array_unique(explode(' ',...))) with preg_replace('%(\b\w+\b)(?=.*\b\1\b)%','',...), but it´s 6 bytes shorter via the array.

127 bytes for list of arguments:

<?foreach($argv as$i=>$s)if($i)$r[]=preg_replace('%\d%','X',preg_replace('%\d/\d%', 'X/Y',$s));echo join(' ',array_unique($r));

120 bytes for a function on an array:

function r($a){foreach($a as$s)$r[]=preg_replace('%\d%','X',preg_replace('%\d/\d%', 'X/Y',$s));return array_unique($r);}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.