8
\$\begingroup\$

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 wickas and wubs. The number of wickas 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 or wub in that case.
  • Standard rules apply, full programs or functions, up to you.
  • This is a 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
\$\endgroup\$
16
  • \$\begingroup\$ Sandbox \$\endgroup\$ Aug 31, 2017 at 18:05
  • \$\begingroup\$ Can you add a test with 5 uppercase letters? \$\endgroup\$
    – Rod
    Aug 31, 2017 at 18:14
  • \$\begingroup\$ This is an ungolfed reference implementation in Python \$\endgroup\$ Aug 31, 2017 at 19:01
  • 1
    \$\begingroup\$ Can I use "Wubba Lubba Dub Dub" instead? Doesn't save me bytes though :P. \$\endgroup\$ Aug 31, 2017 at 19:18
  • 2
    \$\begingroup\$ Should foobarbaz simply return foobarbaz? Is there a second part at all? \$\endgroup\$ Sep 1, 2017 at 7:43

5 Answers 5

1
\$\begingroup\$

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";
    };
}
\$\endgroup\$
1
\$\begingroup\$

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)

Try it online!

Note: TIO includes -l to run all tests at once.

\$\endgroup\$
1
\$\begingroup\$

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

\$\endgroup\$
1
\$\begingroup\$

Vyxal , 64 bytes

ꜝ[`(?=[A-Z])`ṡḢ:₅½⌈:‟ẎDƛ`≤ȧka-`;∑$∑‹p^ȯ:ƛ‛≤¶;‹∑∇∑f:ẏ∇∇∑L+*÷|ėvΠ÷

Try it Online!

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']]
 :₅½⌈              # 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.
\$\endgroup\$
0
\$\begingroup\$

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])

Try it online!

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

\$\endgroup\$
6
  • 1
    \$\begingroup\$ Fails for foobar. \$\endgroup\$
    – Mr. Xcoder
    Aug 31, 2017 at 21:45
  • \$\begingroup\$ @Mr.Xcoder Fixed! \$\endgroup\$
    – jferard
    Sep 1, 2017 at 15:08
  • 2
    \$\begingroup\$ Added 47 bytes, thanks to Mr. Xcoder ;) >_> \$\endgroup\$
    – Mr. Xcoder
    Sep 1, 2017 at 15:08
  • \$\begingroup\$ Possible 270 bytes. \$\endgroup\$ Sep 1, 2017 at 18:44
  • \$\begingroup\$ 248 bytes \$\endgroup\$ Sep 2, 2017 at 21:20

Your Answer

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

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