Inspired by this.
Given a string as input consisting of only upper and lowercase alphabet characters, wicka-wub it.
How do I wicka-wub a string?
The example text used is "DJMcMayhem".
Split the string before each capital letter, so you get ["D", "J", "Mc", "Mayhem"]
.
Next, take the two halves of the list as sublists. This gives us [["D", "J"],["Mc", "Mayhem"]]
. If the list is an odd length (i.e. 3), the first sublist will contain the middle substring (i.e. [[a,b], [c]]
).
Create a list of wicka
s and wub
s. The number of wicka
s should be as many as the length of the first part of the input list (i.e. ["D", "J"] -> ["wicka", "wicka"]
), and the number of wubs
should be as many as the length of the second part of the input list. In our case, this gives ["wicka", "wicka", "wub", "wub"]
.
Now join the sublists of the input list into single strings and flatten.
We currently have ["DJ", "McMayhem"]
and ["wicka", "wicka", "wub", "wub"]
.
Join the wicka
/wub
list with -
s: wicka-wicka-wub-wub
. Prepend a -
. If there is more than one capital letter in the input, append another -
.
Now we have ["DJ", "McMayhem"]
and "-wicka-wicka-wub-wub-"
.
Append the wicka-wub
string to the end of the first item in the input list, to get ["DJ-wicka-wicka-wub-wub-","McMayhem"]
.
Lastly, repeat the characters in the second part of the string by their 0-indexed value in the original input string. In our example, that means the first M
would be repeated twice, then the c
three times, and the next M
four times. Join the list, so the second part (the part you just repeated letters in) is appended to the first part ("DJ-wicka-wicka-wub-wub-"
).
Final result of input:
"DJ-wicka-wicka-wub-wub-MMcccMMMMaaaaayyyyyyhhhhhhheeeeeeeemmmmmmmmm"
Total process:
["D", "J", "Mc", "Mayhem"] =>
[["D", "J"], ["Mc", "Mayhem"]] =>
["DJ", "McMayhem"] and ["wicka", "wicka", "wub", "wub"] =>
["DJ", "McMayhem"] and "-wicka-wicka-wub-wub-" =>
["DJ-wicka-wicka-wub-wub-", "McMayhem"] =>
"DJ-wicka-wicka-wub-wub-MMcccMMMMaaaaayyyyyyhhhhhhheeeeeeeemmmmmmmmm"
Your task
Your task is, given a string that consists of only upper and lowercase alphabet characters, output the wicka-wubbed version of that string.
A few rules
- The input may consist entirely of lowercase letters, or entirely of uppercase letters, or any number of each, but no other characters.
- If the input consists of entirely lowercase letters the correct output should simply be the final stage (the string with the characters repeated according to their 0-indexed position). There should be no
wicka
orwub
in that case. - Standard rules apply, full programs or functions, up to you.
- This is a code-golf so shortest code wins.
GoodLuck-wicka-wicka-wub-GGGGGGGGooooooooollllllllllfffffffffffeeeeeeeeeeerrrrrrrrrrrrrssssssssssssss
Test cases
input => output
DJMcMayhem => DJ-wicka-wicka-wub-wub-MMcccMMMMaaaaayyyyyyhhhhhhheeeeeeeemmmmmmmmm
PPCG => PP-wicka-wicka-wub-wub-CCGGG
foobarbaz => fooooobbbbaaaaarrrrrrbbbbbbbaaaaaaaazzzzzzzzz
FooBarBaz => FooBar-wicka-wicka-wub-BBBBBBaaaaaaazzzzzzzz
HelloWorld => Hello-wicka-wub-WWWWWoooooorrrrrrrllllllllddddddddd
Test => Test-wicka
UPPER => UPP-wicka-wicka-wicka-wub-wub-EEERRRR
fooBarBaz => fooBar-wicka-wicka-wub-BBBBBBaaaaaaazzzzzzzz
5 Answers 5
Java 8 (782 bytes)
import com.google.common.collect.*;import java.util.*;import java.util.function.*;public class p{Function<String,String>b=s->{List<String>p=Arrays.asList(s.split("(?=[A-Z])"));if(!s.matches("[^A-Z]*[A-Z].+")){String w="";int i=0;for(char c:String.join("",s).toCharArray()){w+=String.join("",Collections.nCopies(i,Character.toString(c)));i++;}return w;}String h="-";List<List<String>>k;if(p.size()!=1){k=Lists.partition(p,2);String w=String.join("",k.get(0))+h+String.join(h,Collections.nCopies(k.get(0).size(),"wicka"))+h+String.join(h,Collections.nCopies(k.get(1).size(),"wub"))+h;int i=String.join("", k.get(0)).length();for(char c:String.join("",k.get(1)).toCharArray()){w+=String.join("",Collections.nCopies(i,Character.toString(c)));i++;}return w;}return p.get(0)+h+"wicka";};}
Ungolfed:
import com.google.common.collect.*;
import java.util.*;
import java.util.function.*;
public class p {
Function<String, String> b = s -> {
List<String> p = Arrays.asList(s.split("(?=[A-Z])"));
if (!s.matches("[^A-Z]*[A-Z].+")) {
String w = "";
int i = 0;
for (char c : String.join("", s).toCharArray()) {
w += String.join("", Collections.nCopies(i, Character.toString(c)));
i++;
}
return w;
}
String h = "-";
List<List<String>> k;
if (p.size() != 1) {
k = Lists.partition(p, 2);
String w = String.join("", k.get(0)) + h + String.join(h, Collections.nCopies(k.get(0).size(), "wicka")) + h + String.join(h, Collections.nCopies(k.get(1).size(), "wub")) + h;
int i = String.join("", k.get(0)).length();
for (char c : String.join("", k.get(1)).toCharArray()) {
w += String.join("", Collections.nCopies(i, Character.toString(c)));
i++;
}
return w;
}
return p.get(0) + h + "wicka";
};
}
Perl 5, 142 bytes
130 bytes code + 12 for -F(?=[A-Z])
.
This now correctly matches all the test cases provided.
map$a+=y///c,@E=($z=/[A-Z]/)?splice@F,0,@F/2+.5:();print@E,("-wicka"x@E,"-wub"x@F,"-"x!!@F)x$z,map$_ x$a++,$z?map/./g,@F:('',/./g)
Note: TIO includes -l
to run all tests at once.
Kotlin 1.1 - (削除) 494 (削除ここまで) 492 bytes
Submission
typealias S=String
fun Iterable<*>.j(s:S="")=this.joinToString(s)
fun s(s:S):S{var c=0
val m=mutableListOf<S>()
var t=""
s.map{if(it.isUpperCase()){c+=1
if(t!=""){m.add(t)
t=""}}
t+=it}
if(c==0)return e(s,1)
m.add(t)
val p=Math.ceil(m.size/2.0).toInt()
val f=m.subList(0,p)
val z=m.subList(p,m.size)
val w=List(f.size,{"wicka"})
val u=List(z.size,{"wub"})
val x=f.j()
var v="-"+(w+u).j("-")
if(c>1)v+="-"
return x+v+e(z.j(),x.length)}
fun e(s:S,o:Int)=s.mapIndexed{c,i->List(c+o,{i}).j()}.j()
Test
typealias S=String
fun Iterable<*>.j(s:S="")=this.joinToString(s)
fun s(s:S):S{var c = 0
val m=mutableListOf<S>()
var t=""
s.map{if(it.isUpperCase()){c+=1
if(t!=""){m.add(t)
t=""}}
t+=it}
if(c==0)return e(s,1)
m.add(t)
val p=Math.ceil(m.size/2.0).toInt()
val f=m.subList(0,p)
val z=m.subList(p,m.size)
val w=List(f.size,{"wicka"})
val u=List(z.size,{"wub"})
val x=f.j()
var v="-"+(w+u).j("-")
if(c>1)v+="-"
return x+v+e(z.j(),x.length)}
fun e(s:S,o:Int)=s.mapIndexed{c,i->List(c+o,{i}).j()}.j()
data class TestData(val input: String, val output: String)
fun main(args: Array<String>) {
val tests = listOf(
TestData("DJMcMayhem", "DJ-wicka-wicka-wub-wub-MMcccMMMMaaaaayyyyyyhhhhhhheeeeeeeemmmmmmmmm"),
TestData("PPCG", "PP-wicka-wicka-wub-wub-CCGGG"),
TestData("foobarbaz", "fooooobbbbaaaaarrrrrrbbbbbbbaaaaaaaazzzzzzzzz"),
TestData("FooBarBaz", "FooBar-wicka-wicka-wub-BBBBBBaaaaaaazzzzzzzz"),
TestData("HelloWorld", "Hello-wicka-wub-WWWWWoooooorrrrrrrllllllllddddddddd"),
TestData("Test", "Test-wicka"),
TestData("UPPER", "UPP-wicka-wicka-wicka-wub-wub-EEERRRR"),
TestData("fooBarBaz", "fooBar-wicka-wicka-wub-BBBBBBaaaaaaazzzzzzzz")
)
for (test in tests) {
var out = s(test.input)
if (out != test.output) {
System.err.println("TEST FAILED")
System.err.println("IN " + test.input)
System.err.println("EXP " + test.output)
System.err.println("OUT " + out)
return
}
}
println("Test Passed")
}
Running
Works on KotlinLang, but not on TryItOnline as 1.1 is not supported
Ran through my compressor, saved 2 bytes
Vyxal Ṫ
, 64 bytes
ꜝ[`(?=[A-Z])`ṡḢ:51⁄2⌈:‟ẎDƛ`≤ȧka-`;∑$∑‹p^ȯ:ƛ‛≤¶;‹∑∇∑f:ẏ∇∇∑L+*÷|ėvΠ÷
Since the stack manipulation is quite complex, I've added a representation of the stack after each step.
ꜝ[ # If any letters are uppercase (example input: DJMcMayhem
`(?=[A-Z])`ṡ # Split before uppercase letters
# [['','D','J','Mc','Mayhem']]
Ḣ # Lop off the first value
# [['D','J','Mc','Mayhem']]
:51⁄2⌈ # Duplicate and push ceil(length/2)
# [['D','J','Mc','Mayhem'],['D','J','Mc','Mayhem'],2]
:‟Ẏ # Push a copy on the bottom and slice from the start
# [2,['D','J','Mc','Mayhem'],['D','J']]
Dƛ`≤ȧka-`; # Triplicate and fill one copy with 'wicka-'
# [2,['D','J','Mc','Mayhem'],['D','J'],['D','J'],['wicka-','wicka-']]
∑$∑ # Sum these and sum the pair under that
# [2,['D','J','Mc','Mayhem'],['D','J'],'wicka-wicka-','DJ']
‹p # Add a hyphen and prepend
# [2,['D','J','Mc','Mayhem'],['D','J'],'DJ-wicka-wicka-']
^ȯ # Reverse the stack and slice
# ['DJ-wicka-wicka-',['D','J'],['Mc','Mayhem']]
:ƛ‛≤¶;‹∑ # Duplicate, fill a copy with 'wub-' and sum that
# ['DJ-wicka-wicka-',['D','J'],['Mc','Mayhem'],'wub-wub-']
∇∑f # Shift and turn the full second half into a list of characters
# ['DJ-wicka-wicka-','wub-wub-',['D','J'],['M','c','M','a','y','h','e','m']]
:ẏ # Duplicate and make a range of 0...length
# ['DJ-wicka-wicka-','wub-wub-',['D','J'],['M','c','M','a','y','h','e','m'],[0,1,2,3,4,5,6,7]]
∇∇∑L+ # Get the full length of the first half and add it to each item of this range
# ['DJ-wicka-wicka-','wub-wub-',['M','c','M','a','y','h','e','m'],[2,3,4,5,6,7,8,9]]
*÷ # Multiply (repeat) (vectorising) and iterate out over the stack
# ['DJ-wicka-wicka-','wub-wub-','MM','ccc','MMMM','aaaaa','yyyyyy','hhhhhhh','eeeeeeee','mmmmmmmmm']
# Then the Ṫ flag smash-prints this.
| # Else...
ė # Enumerate (zip with range to length)
vΠ # Reduce each by multiplication
÷ # Iterate (push each to the stack)
# Then the Ṫ flag smash-prints this.
Python 3, (削除) 234 (削除ここまで) (削除) 281 (削除ここまで) (削除) 270 (削除ここまで) (削除) 248 (削除ここまで) 246 bytes
s,j=input(),"".join;f=lambda l,n:j(c*i for i,c in enumerate(j(l),n));import re;u=re.split("([A-Z])",s);l=len(u)
m=[j(u[i:i+2])for i in range(1,l,2)];i=-~l//4;d=j(m[:i]);print((j([d+"-wicka"*i+"-wub"*(l//2-i)+"-",f(m[i:],len(d))]),f(u[0],1))[l<2])
Added 47 bytes, thanks to Mr. Xcoder ;)
Saved 11 bytes thanks to Jonathan Frech
Saved 22 bytes thanks to Halvard Hummel
Saved 2 more bytes thanks to Mr. Xcoder
-
1\$\begingroup\$ Fails for
foobar
. \$\endgroup\$Mr. Xcoder– Mr. Xcoder2017年08月31日 21:45:01 +00:00Commented Aug 31, 2017 at 21:45 -
\$\begingroup\$ @Mr.Xcoder Fixed! \$\endgroup\$jferard– jferard2017年09月01日 15:08:22 +00:00Commented Sep 1, 2017 at 15:08
-
2\$\begingroup\$ Added 47 bytes, thanks to Mr. Xcoder ;) >_> \$\endgroup\$Mr. Xcoder– Mr. Xcoder2017年09月01日 15:08:45 +00:00Commented Sep 1, 2017 at 15:08
-
\$\begingroup\$ Possible 270 bytes. \$\endgroup\$Jonathan Frech– Jonathan Frech2017年09月01日 18:44:31 +00:00Commented Sep 1, 2017 at 18:44
-
\$\begingroup\$ 248 bytes \$\endgroup\$anon– anon2017年09月02日 21:20:23 +00:00Commented Sep 2, 2017 at 21:20
5
uppercase letters? \$\endgroup\$foobarbaz
simply returnfoobarbaz
? Is there a second part at all? \$\endgroup\$