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\$
  • \$\begingroup\$ Can we take array of strings input, i.e. ["regex1","regex2","split1/0"]? \$\endgroup\$ – Leaky Nun Aug 10 '16 at 15:13
  • \$\begingroup\$ I'll modify the question a bit to allow it =) \$\endgroup\$ – Jylo Aug 10 '16 at 15:13
  • 1
    \$\begingroup\$ You may want to put the last line elsewhere, at least not in the scoring section \$\endgroup\$ – Leaky Nun Aug 10 '16 at 15:16
  • \$\begingroup\$ It's still in the scoring section... \$\endgroup\$ – Leaky Nun Aug 10 '16 at 15:21
  • \$\begingroup\$ Is extra trailing newlines okay? \$\endgroup\$ – Leaky Nun Aug 10 '16 at 15:24
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\$
  • \$\begingroup\$ Wow, Upvoted because 120 seconds to answer is so impressive ! \$\endgroup\$ – Jylo Aug 10 '16 at 15:12
  • 2
    \$\begingroup\$ @Pierre.Sassoulas This site has seen faster answers. \$\endgroup\$ – Leaky Nun Aug 10 '16 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\$
2
\$\begingroup\$

Javascript, 92 78 bytes

a=a=>[...new Set(a.replace(/\d/g,"X").replace(/\/./g,"/Y").split` `)].join` `;
\$\endgroup\$
  • \$\begingroup\$ Oh whoops... I'll go fix that \$\endgroup\$ – Bald Bantha Aug 10 '16 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\$ – Neil Aug 11 '16 at 2:33
  • \$\begingroup\$ I tried the first suggestion, JSfiddle didn't like it. I'll try it on babel. \$\endgroup\$ – Bald Bantha Aug 11 '16 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, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.