title: IE's Extra Robust Color Parsing®

text:
# Question
Title: IE's Extra Robust Color Parsing®
Recently, I've found a rant from 2004 about Internet Explorer's color parsing. Turns out, MS made sure that just about *anything* will be parsed as a color somehow and I think it makes for a neat challenge.

## Well, how does IE parse colors?

Let's give it an input with a length of...

* ...less than 4 chars:
    1. Replace chars not matching `[0-9a-fA-F]` with "0"
    2. Append "0" until the length is 3 chars
    3. Split into 3 parts, 1 char each
    4. Prepend each part with "0"
* more or equal than 4 chars:
    1. Replace chars not matching `[0-9a-fA-F]` with "0"
    2. Append "0" until the length is divisible by 3
    3. Split into 3 parts of equal size
    4. Remove chars from the front of each part until they are 8 chars each
    5. Remove chars from the front of each part until one of them starts with something that isn't "0" or until all parts are empty
    6. Look at the length of the parts: Are shorter than 2? If so, prepend each part with a "0" until each part is 2 chars long. If not, remove characters from the back of each part until they are 2 chars each

## ... w h a t

It's true! Here are some examples (irrelevant steps omitted):

```
F
--> F00        // append "0" until length is 3
--> F, 0, 0    // split into 3 parts
--> 0F, 00, 00 // prepend 0
--> #0F0000    // done

FLUFF
--> F00FF      // replace invalid chars with 0
--> F00FF0     // pad to length that is divisible by 3
--> F0, 0F, F0 // split into 3 parts
--> #F00FF0    // done

rADioACtivE
--> 0AD00AC000E       // replace invalid chars with 0
--> 0AD00AC000E0      // pad to length that is divisible by 3
--> 0AD0, 0AC0, 00E0  // split into 3 parts
--> AD0, AC0, 0E0     // remove "0"s from the front of each part
                      // until one part doesn't start with a "0"
--> AD, AC, 0E        // remove chars from the back of each part until
                      // each part is of length 2
--> #ADAC0E           // done

1234567890ABCDE1234567890ABCDE
--> 1234567890, ABCDE12345, 67890ABCDE // split into 3 parts
--> 34567890, CDE12345, 890ABCDE       // remove chars from the front until
                                       // each part is of length 8
--> 34, CD, 89                         // remove chars from the back of
                                       // each part until each part is of length 2
--> #34CD89                            // done

rules
--> 000E0      // replace invalid chars with "0"
--> 000E00     // pad to length divisible by 3
--> 00, 0E, 00 // split into 3 parts
--> 0, E, 0    // remove "0"s from the front of each part until one part
               // doesn't start with a "0"
--> 00 0E 00   // parts are too short, prepend a "0"
--> #000E00    // done

GHIJKL
--> 000000      // replace invalid chars with "0"
--> 00, 00, 00  // split into 3 parts
--> , , ,       // remove "0" from the front of each part until empty
--> 00, 00, 00  // parts are too short, prepend "0" until they are length 2
--> #000000     // done

```

## Rules

* This is , shortest answer wins
* No standard loopholes
* Expect any input matching `[a-zA-Z0-9]*`. Yes, empty input as well (results in #000000).
* Output any representation of the resulting RGB color (including but not limited to the actual color, tuples, hex codes, with or without `#`, any case, ...)
* For the sake of this challenge, special color names are not respected but instead processed according to the rules. Hence, "red" will result in #000E0D and not #FF0000

## Test cases

Note that the `#` is completely optional

```
red                              -> #000E0D
rules                            -> #000E00
1234567890ABCDE1234567890ABCDE   -> #34CD89
rADioACtivE                      -> #ADAC0E
FLUFF                            -> #F00FF0
F                                -> #0F0000
                                 -> #000000
zqbttv                           -> #00B000
6db6ec49efd278cd0bc92d1e5e072d68 -> #6ECDE0
102300450067                     -> #100000
GHIJKL                           -> #000000
000000                           -> #000000

```

## EDIT

These are the longest Jelly and Vyxal answers I've seen on this site up to this point :o

## EDIT 2

Lecdi pointed out that the specification had a flaw that caused undefined behaviour if the input turned into `000000` at some point, such as `GHIJKL`. As of now, all answers seem to handle this with the exception of lyxal's 64 byte Vyxal answer.

The flaw in the spec should now be fixed. Sorry!

# Answer
# Vyxal, ~~51~~ ~~50~~ ~~47~~ 44 bytes

```
⇧ƛk^$c∧;ṅ3/\0ÞḞṅ3/8Nvȯ∩\03*:£øl:L2∵Ẏ¥p¥p2Nȯ∩

```

Try it Online!

A huge mess, but at least I outgolfed lyxal.
> 7 votes

# Answer
# JavaScript + HTML, 50 bytes

```
document.body.setAttribute('bgcolor','#'+prompt())

```

Works in any browser, as the legacy behavior was kept with `bgcolor` (even though it won't work with CSS). Displays as graphical output.

```
<script>document.body.setAttribute("bgcolor",'#'+prompt());</script>
```

If graphical output is not allowed, then:

# JavaScript + HTML, 97 bytes

```
c=document.body;c.setAttribute('bgcolor','#'+prompt());alert(getComputedStyle(c).backgroundColor)

```

```
<script>c=document.body;c.setAttribute('bgcolor','#'+prompt());alert(getComputedStyle(c).backgroundColor)</script>
```

Uses the computed styles generated. Will output like `rgb(X, Y, Z)`.

To output a hex code, this would be 156 bytes:

```
c=document.body;c.setAttribute('bgcolor','#'+prompt());alert(getComputedStyle(c).backgroundColor.match(/\d+/g).map(x=>(x|256).toString(16).slice(1)).join``)

```

```
<script>c=document.body;c.setAttribute('bgcolor','#'+prompt());alert(getComputedStyle(c).backgroundColor.match(/\d+/g).map(x=>(x|256).toString(16).slice(1)).join``)</script>
```
> 7 votes

# Answer
# JavaScript (Browser), 138 bytes, non-competitive

```
c=>(document.body.innerHTML=`<font id=g color=${c}>`,getComputedStyle(g)).color.match(/\d+/g).map(n=>(n|256).toString(16).slice(1)).join``

```

The same color parsing algorithm also works on browsers other than IE (I had tested on my Firefox 103 and it works). But the code will output `#ff0000` for input `red` so marked non-competitive.

```
f=

c=>(document.body.innerHTML=`<font id=g color=${c}>`,getComputedStyle(g)).color.match(/\d+/g).map(n=>(n|256).toString(16).slice(1)).join``

console.log(f('red                             '));
console.log(f('rules                           '));
console.log(f('1234567890ABCDE1234567890ABCDE  '));
console.log(f('rADioACtivE                     '));
console.log(f('FLUFF                           '));
console.log(f('F                               '));
console.log(f('                                '));
console.log(f('zqbttv                          '));
console.log(f('6db6ec49efd278cd0bc92d1e5e072d68'));
console.log(f('102300450067                    '));
```

Save ~30 bytes by Kaiido
> 5 votes

# Answer
# Jelly,  ~~44~~  39 bytes

-5 thanks to UnrelatedString (clever use of `;Ṫ` for the empty string special case).

```
ØhḊiⱮŒla;Ṫ$œs3z0ZŻ€ṫ€-7ZḊẸ€Ḣ¬Ɗ¡ƬḊƇṪḣ2ZF

```

A full program that accepts a string and prints the resulting colour code (keeping character casing from the input\*).

**Try it online!** Or see the test-suite.

### How?

```
ØhḊiⱮŒla;Ṫ$œs3z0ZŻ€ṫ€-7ZḊẸ€Ḣ¬Ɗ¡ƬḊƇṪḣ2ZF - ...f(X)
Øh                                     - hex characters = "0123456789abcdef"
  Ḋ                                    - dequeue -> "123456789abcdef"
     Œl                                - lower-case X
    Ɱ                                  - map with:
   i                                   -   first 1-indexed index or 0 if not found
          $                            - last two links as a monad - f(X):
         Ṫ                             -   tail (yields zero when X is empty)
        ;                              -   X (without its tail) concatenated with that (its tail or a zero)
       a                               - logical AND the hex-char-indicator list with X or [0] (vectorises)
           œs3                         - split into three equal chunks
                 Ż€                    - prefix each with a zero
                   ṫ€-7                - tail each from index -7 -> last (up to) eight values
                       Z               - transpose
                              Ƭ        - collect up while distinct, applying:
                             ¡         -   repeat...
                            Ɗ          -   ...number of times: last three links as a monad:
                        Ẹ€             -     any? for each
                          Ḣ            -     head
                           ¬           -     logical NOT
                       Ḋ               -   ...action: dequeue
                                Ƈ      - keep those which are truthy under:
                               Ḋ       -   dequeue
                                 Ṫ     - tail
                                  ḣ2   - head to index two
                                    Z  - transpose
                                     F - flatten
                                       - implicit, smashing print

```
> 4 votes

# Answer
## JavaScript, ~~238~~ 234 bytes

Way longer than the others, but actually competing!

```
c=>(M=Math,s='slice',t=M.ceil(c.length/3),c=c.padEnd(M.max(t*3,3)).replace(/[g-zG-Z ]/g,'0'),c=[c[s](0,t),c[s](t,t*2),c[s](t*2)]).map(p=>p[s](i=M.min(...c.map(p=>(r=p.search(/[^0].{0,7}$/),r<0?3e333:r))),i+2).padStart(2,'0')).join('')

```

Less golfed:

```
c => (
  M = Math, s = 'slice', // these are shorter
  t = M.ceil(c.length / 3), // prepare length for the parts
  c = c
    .padEnd(M.max(t*3, 3)) // pad with spaces until length divisible by 3, with a minimum of 3
    .replace(/[g-zG-Z ]/g, '0'), // replace all non-hex characters with '0'
  c = [c[s](0, t), c[s](t, t*2), c[s](t*2)] // split into 3 equal parts
).map(p => (
  i = M.min(...c.map(p=>(r=p.search(/[^0].{0,7}$/),r<0?3e333:r))), // search for a character that's not '0' in the last 8 characters, returning its index, or infinity when negative (no match found), and take the lowest of the parts
  p[s](i, i+2).padStart(2, '0') // slice max two characters from the lowest non-'0', and pad to two characters
)).join('')

```
> 4 votes

# Answer
# Vyxal, ~~67~~ 64 bytes

```
k6:⇧∪ṅ\^pøB?0øṙ₅:ǒ3εǒ+↲›3/?L4<[2↳›|8Nvȯ{:vh0J≈|ƛh0=ßḢ}ƛ₃[0p|2Ẏ}ṅ

```

Try it Online!

A big mess of unicode and lesser-used overloads of things

## Explained

There are 10 parts here:

```
Generating the hex-digit regex: k6:⇧∪ṅ\^pøB
Replacing non-hex with 0: ?0øṙ
Padding to nearest multiple of 3: ₅:ǒ3εǒ+↲›
Splitting into 3 equal parts: 3/
The length 4 conditional branch: ?L4<[
  Making each bit 2 digits with 0s if needed: 2↳›
|
  Getting the last 8 characters: 8Nvȯ
  Removing 0s until something doesn't start with a 0: {:vh0J≈|ƛh0=ßḢ}
  Either prepending a 0 or getting the last 2 chars: ƛ₃[0p|2Ẏ}
Outputting the result: ṅ

```

### Generating the hex-digit regex

```
k6:⇧∪ṅ\^pøB
k6           # The string "0123456789abcdef"
  :⇧         # Uppercased ("0123456789ABCDEF")
    ∪ṅ       # Unioned with the lowercase string ("0123456789abcdefABCDEF")
       \^p   # With a caret prepended ("^0123456789abcdefABCDEF")
          øB # Surrounded in "[]" ("[^0123456789abcdefABCDEF]")

```

This regex (`[^0123456789abcdefABCDEF]`, called `re` from here) will be used in the next step to determine which characters to replace with 0s.

### Replacing non-hex with 0

```
?0øṙ # Before this part, the stack is [re]
?    # Push the input : [re, input]
 0   # Push a 0 to the stack : [re, input, 0]
  øṙ # Replace matches of re in input with 0

```

This completes the step of replacing invalid characters with 0s, which is common to strings less than 4 chars and strings with at least 4 chars. This string will be called `0-str` from here.

### Padding to nearest multiple of 3

```
₅:ǒ3εǒ+↲› # Before this part, the stack is [0-str]
₅:        # Push two copies of the length of 0-str without popping it
  ǒ3ε     # Modulo the first copy by 3 and take the absolute difference of that and 3 to get how far away the length is from the next multiple of 3. Note that this may result in the result being 3 because a string of length that is divisible by 3 will return 3, as 3 - (length % 3 => 0) = 3
     ǒ    # Modulo 3 again to make a 3 a 0
      +↲› # Add that to the remaining copy of the length to get how many characters should be in the padded string, left pad 0-str with spaces to that many characters and replace all spaces with 0s.

```

The result of this step is common to both string length possibilities and will be referred to as `pad-0-str`.

### Splitting into 3 equal parts

```
3/ # Before this part, the stack is [pad-0-str]
3/ # Divide the string into 3 equal parts

```

Yes, there really *is* a built-in for this. The result will be referred to as `3-part-pad`.

### The length 4 conditional branch

```
?L4<[...|...} # Before this part, the stack is [3-pad-str]
?L            # Is the length of the input
  4<          # Less than 4?
    [...      # If so, move onto the steps for a string with less than 4 characters
        |...} # Otherwise, move onto the steps for a string with at least 4 characters. A `}` is used instead of a `]` because it saves a byte over `;]` later on.

```

The stack hasn't changed after this step.

#### Less than 4 - Making each bit 2 digits with 0s if needed

```
2↳› # Before this part, the stack is [3-pad-str]
2↳  # Right pad each part of 3-pad-str to be two characters, using spaces. This is needed because components of 3-pad-str will be empty if the input is the empty string. In the case of the empty string, this returns "  ". Otherwise, it returns " " + char
  › # Replace the spaces with 0s. This turns "  " into "00" and everything else into "0" + char.

```

The result of this will be called `res`.

#### At least 4 - Getting the last 8 characters

```
8Nvȯ # Before this part, the stack is [3-pad-str]
8N   # Push -8 to the stack : [3-pad-str, -8]
  vȯ # Push [part[-8:] for part in 3-pad-str]

```

The result of this will be referred to as `last-8`

#### At least 4 - Removing 0s until something doesn't start with a 0

```
{:vh0J≈|ƛh0=ßḢ}   # Before this part, the stack is [last-8]
{                  # While ...
 :vh               # the first character of each part of last-8 (leaving last-8 on the stack)...
    0J≈            # are all equal to 0:
       |ƛh0=       # For each item, check if the first character is 0
            ßḢ}    # And if so, remove the head of that part. The `}` here closes both the map lambda and the while loop.

```

This is probably the messiest section. The result of this will be referred to as `second-last`.

#### At least 4 - Either prepending a 0 or getting the last 2 chars

```
ƛ₃[0p|2Ẏ} # Before this part, the stack is [second-last]
ƛ         # For each item in second-last, which has leading 0s removed:
 ₃[       # If the length is 1:
   0p     #   Prepend a 0
     |2Ẏ} # Else: take the last two characters.
          # The `}` here closes both this inner if-statement, the map lambda, and the outer if-statement

```

The result of this part will be referred to as `res`. This is analogous to the step taken in the less than 4 branch.

### Outputting the result

```
ṅ # Join res on empty string - concatenate into a single string.

```

I could have used the `s` flag here, but at this point, we're already play the long golf game, so I thought I might as well go flagless.
> 3 votes

# Answer
# Retina 0.8.2, 117 bytes

```
T`op`a-fA-FRd
$
00
^((.)*?)((?<_-2>.)*)((?<-_>.)*)0?0?$
00$1¶00$3¶00$4¶
.+(.{8})
$1
+`^0(..+¶)0(.+¶)0
$1$2
(..).*¶
$1

```

Try it online! Link includes test cases. Explanation:

```
T`op`a-fA-FRd

```

Replace non-hex digits with zeros.

```
$
00

```

Append two zeros in case the length is not a multiple of 3.

```
^((.)*?)((?<_-2>.)*)((?<-_>.)*)0?0?$
00$1¶00$3¶00$4¶

```

Divide the string into three equal parts, discarding the trailing zeros just added if necessary. Prefix `00` to each part in case it is short.

```
.+(.{8})
$1

```

Trim each part to the last 8 digits.

```
+`^0(..+¶)0(.+¶)0
$1$2

```

Trim leading zeros while all parts have one, but don't trim to fewer than two digits.

```
(..).*¶
$1

```

Keep only the first two digits of each part and join everything together.
> 3 votes

# Answer
# 05AB1E, ~~48~~ 47 bytes

```
lA6.$S0:₄¦«3ô¨S3ä8δ.£'e¶:øJ0Û¶'e:2£₄¦D‚ì2.£€Sø˜

```

Output as a lowercase list of characters without `#`.

Try it online or verify all test cases.

**Explanation:**

Step 1: Convert the input to lowercase, and replace all invalid letters with a `0`:

```
l              # Lowercase the (implicit) input-string
 A             # Push the lowercase alphabet
  6.$          # Remove its first 6 characters: "ghijklmnopqrstuvwxyz"
     S         # Convert it to a list of characters
      0:       # And replace all those characters with a "0" in the uppercase input

```

Try just step 1 online.

Step 2: Pad with trailing 0s, and split it into three equal-sized parts (with each part as a list of characters):

```
₄              # Push 1000
 ¦             # Remove it's first character: "000"
  «            # Append it
   3ô          # Split it into parts of size 3
     ¨         # Remove the last (potentially shorter) part
      S        # Convert it to a flattened list of characters
       3ä      # Split it into 3 equal-sized parts

```

Try just the first two steps online.

Step 3: Only keep the last 8 characters of each inner list:

```
 δ             # Map over each inner list
8 .£           # Only keep (up to) the last 8 characters

```

Try just the first three steps online.

Step 4: Remove leading 0s from each list, until a column doesn't start with a `0` anymore:

```
'e¶:          '# Replace all "e" with a newline
               # (workaround, because "0e0" would be interpret as 0 in 05AB1E)
    ø          # Zip/transpose; swapping rows/columns
     J         # Join each inner list together
      0Û       # Trim all leading 0s
        ¶'e:  '# Undo the workaround, by replacing all newlines back to "e"s

```

Try just the first four steps online.

Step 5: Only keep the first two characters of each row, potentially prepending the result with `0`s, and output the final result:

```
2£             # Only keep the first two triplets of the list
  ₄¦           # Push "000" again
    D‚         # Pair it with itself: ["000","000"]
      ì        # Prepend this in front of the list
       2.£     # Now only keep the last two triplets
          €S   # Convert each inner string back to a list of characters
            ø  # Zip/transpose back; swapping rows/columns
             ˜ # Flatten the pair of triplets of characters
               # (after which the sixtet of characters is output implicitly as result)

```
> 3 votes

# Answer
# Rust, ~~372~~ ~~367~~ 340 bytes

```
|mut s:Vec<u8>|{for c in &mut s{if!c.is_ascii_hexdigit(){*c=48}}s.extend(b"000");let d:Vec<_>=s.chunks((s.len()-1).max(3)/3).take(3).map(|c|&c[c.len().saturating_sub(8)..]).collect();d.iter().flat_map(|c|match&c[d.iter().map(|c|c.iter().take_while(|c|**c==48).count()).min().unwrap()..]{[]=>*b"00",&[c]=>[48,c],&[x,y,..]=>[x,y]}).collect()}

```

Ungolfed:

```
fn f(mut s: Vec<u8>) -> Vec<u8> {
    for c in &mut s {
        if !c.is_ascii_hexdigit() {
            *c = b'0'
        }
    }
    s.extend(b"000");

    let chunks: Vec<_> = s
        .chunks((s.len() - 1).max(3) / 3)
        .take(3)
        .map(|c| &c[c.len().saturating_sub(8)..])
        .collect();

    let to_remove = chunks
        .iter()
        .map(|c| c.iter().take_while(|c| **c == b'0').count())
        .min()
        .unwrap();

    chunks
        .iter()
        .flat_map(|c| match &c[to_remove..] {
            [] => *b"00",
            &[c] => [b'0', c],
            &[c1, c2, ..] => [c1, c2],
        })
        .collect()
}

```

Playground

The chunking is a little tricky, but otherwise a relatively boring solution.
> 3 votes

# Answer
# Vyxal, 29 bytes

```
S⇩3ẇ3↲∑k6Ǐ~vḟİ∑3/⟑8NȯHH2∆Z2Ẏ₴

```

Try it Online!

```
S⇩                            # Stringify ("" -> 0 edgecase) and lowercase
  3ẇ                          # Cut into chunks of length 3
     3↲                       # Pad each to length 3 with spaces (will be zeroed)
      ∑                       # Concatenate
       k6                     # Lowercase hex
          Ǐ                   # Append the leading zero
          ~vḟ                 # Without popping, for each char in input, find it in the hex
             İ                # Index into the hex + 0 - ones not found, -1s, get indexed into the end
              ∑               # Concat into a string
               3/             # Divide into 3 parts
                 ⟑            # Over each...
                  8Nȯ         # Get last 8 chars
                     HH       # Convert to/from hex to remove leading zeroes
                       2∆Z    # zfill each to length 2
                          2Ẏ  # Get the first two chars of each
                            ₴ # Print

```
> 3 votes

# Answer
# C#, ~~235~~ ~~229~~ 227 bytes

-6 bytes thanks to Kevin Cruijssen

```
s=>{int l;for(s=Regex.Replace(s,"[^0-9a-fA-F]|^$","0");(l=s.Length)%3>0;s+="0");for(s=l<4?"0"+s[0]+0+s[1]+0+s[2]:s;(l=s.Length/3)>8|s[0]+s[l]+s[2*l]<145&l>2;s=string.Concat(s.Where((_,i)=>i%l>0)));return s.Where((_,i)=>i%l<2);}

```

Try it online!

---

Ungolfed:

```
input => {
    //the three while loops are golfed into for loops in the submission, with the last two then being combined into one

    // replace non hex chars with 0
    input = Regex.Replace(input, "[^0-9a-fA-F]|^$", "0");

    // pad input to next multiple of 3
    while (input.Length % 3 > 0) {
        input += "0";
    }
    // add 0 at the start of each section if the input is now 3 chars long
    // the final two zeroes here get coerced to strings
    if (input.Length == 3) input = "0" + input[0] + 0 + input[1] + 0 + input[2];

    // used to keep track of the current section length (changes as 0s are removed)
    int partLength = input.Length / 3;

    // if part length is >8 continuously remove the first char from each part (notice the first char of each part is always 0 mod partLength)
    while (partLength > 8) {
        // because LINQ, the .Where has to be cast to something to actually evaluate within a while, string.Concat is the shortest way (including other changes to have other variables be the correct type)
        input = string.Concat(input.Where((_, i) => i % partLength > 0));
    }

    //if every part starts with 0, remove the first char from each
    //this is golfed into a sum using char codes in the submission ((int)'0' = 48)
    while (input[0] == '0' && input[partLength] == '0' && input[2 * partLength] == '0' && partLength > 2) {
        input = string.Concat(input.Where((_, i) => i % partLength > 0));
    }
    //return the first two chars from each part
    return input.Where((_, i) => i % partLength < 2);
}

```
> 3 votes

# Answer
# C (clang), 273 bytes

```
int p(char*c){unsigned int l=(strlen(c)+2)/3,i,j,r,g,b,v;for(j=0;j<3;j++){v=0;for(i=0;i<l;i++)v=v<<4|(*c?*c>47&&*c<58?*c++-48:*c>64&&*c<71?*c++-55:*c>96&&*c<103?*c++-87:*c++&0:0);!j?r=v:j==1?g=v:(b=v);}while(r>255||g>255||b>255){r/=16;g/=16;b/=16;}return (r<<16)|(g<<8)|b;}

```

Try it online!

### Ungolfed

```
int parse(char *c) {
    unsigned int length;
    unsigned int i, j;
    unsigned int r, g, b;
    unsigned int value;

    /* Round up to multiple of 3, then divide by 3 to get the component length */
    length = (strlen(c) + 2) / 3;

    /* For each component (r,g,b) */
    for (j = 0; j < 3; j++) {
        value = 0; /* Start at 0 */

        for (i = 0; i < l; i++) {
            int char_value;

            if (*c != '\0') {
                /* Parse a hex digit */
                if (*c > '0' && *c < '9') {
                    char_value = *c - '0';
                } else if (*c > 'A' && *c <= 'F') {
                    char_value = *c - 'A' + 10;
                } else if (*c >= 'a' && *c <= 'f') {
                    char_value = *c - 'a' + 10;
                } else {
                    /* Invalid digits are 0 */
                    char_value = 0;
                }

                /* Next character */
                *c++;
            } else {
                /* At the end of the string. Pretend string is right padded with '0' */
                char_value = 0;
            }

            /* Now append. If there are more than 8 digits, the leftmost ones will be lost to overflow */
            value = value << 4 | char_value;
        }

        /* Set the correct component value */
        if (j == 0) {
            r = value;
        } else if (j == 1) {
            g = value;
        } else {
            b = value;
        }
    }

    /* Remove the last few digits until all are within the range 0-255 */
    while (r > 255 || g > 255 || b > 255) {
        r /= 16;
        g /= 16;
        b /= 16;
    }

    /* Return the colour as an integer in the form 0x00RRGGBB */
    return (r << 16) | (g << 8) | b;
}

```

### Explanation

This algorithm is different from the one given, so why does it work? Well when I was reading the rant I noticed the part where if the component length was greater than 8 digits, it would remove the leftmost digits until there were 8. The number 8 stuck out to me as 8 hex digits fit neatly inside a 32-bit integer. And the whole removing leading '0' thing also fits with the whole integer idea, as you can't tell a leading '0' from no leading '0' anyway in an integer. Also the steps for lengths \<= 3 seemed to be superfluous as they could be explained using just the general case.

Going through each of the steps given:

1. "Replace chars not matching \[0-9a-fA-F\] with '0'". This is handled by invalid hex digits being treated as "0". Essentially, just as 'a' and 'A' both equal 10, '0' and 'G' both equal 0.
2. "Append "0" until the length is divisible by 3". This is handled in two ways. First the length of the string is rounded up to a multiple of 3 (using `(length + 2) / 3 * 3`), then if the end of the string is encountered, treat it as if it were 0 *without incrementing the pointer* (Here be UB).
3. "Split into 3 parts of equal size". This is simply dividing the rounded up length by 3 to get the component length (which simplifies into `(length + 2) / 3` since the rounded up length is not used anywhere else) and counting this many characters for each component.
4. "Remove chars from the front of each part until they are 8 chars each". This is handled with integer overflow; the leading digits get shifted out once 8 characters are parsed.
5. "Remove chars from the front of each part until one of them starts with something that isn't "0" or until all parts are empty". This is done for free with integers.
6. "Look at the length of the parts: Are shorter than 2? If so, prepend each part with a "0" until each part is 2 chars long. If not, remove characters from the back of each part until they are 2 chars each". The prepending part is also done for free. The while loop handles the case where the components are longer than 2 characters.

As for the case with 3 or fewer characters: those steps actually turn out to line up with the steps for 4 or more anyway:

1. "Replace chars not matching \[0-9a-fA-F\] with "0"". Literally identical.
2. "Append "0" until the length is 3 chars". For lengths 1-3, this is equivalent to step 2 of the general case. For length of 0 the general step would result in "" rather than "000" at this stage.
3. "Split into 3 parts, 1 char each". This is also equivalent to step 3 of the general case, although "" would be split into the parts "", "", "", whereas "000" would become "0", "0", "0".
4. "Prepend each part with "0"". This is essentially equivalent to step 6 of the general case. As the maximum length of a component is 1, step 4 of the general case does not apply. Step 5 would turn "0", "0", "0" into "", "", "" making the two equivalent again. Then, since they are shorter than 2, they would be prepended with a 0. "", "", "" would be prepended with 2 zeros giving us "00", "00", "00", which is what it should be.

### Notes

* This could be how it was implemented in IE. The implementation seems sensible for a hex code parser. That it accepts "long" colours could be future proofing for if 36-bit or 48-bit colour becomes popular (In that case I think the fact that #ABC becomes #0A0B0C rather than something like #AABBCC may have been a bug). That it "pads" may have been some sort of fix. That it accepts invalid characters may just be the nature of their hex parsing implementation.
* For some reason (TIO) clang does not like it when I do `!j?r=v:j==1?g=v:b=v`, forcing me to add parentheses to make `!j?r=v:j==1?g=v:(b=v)`. In contrast MSVC didn't care. I am not digging through the spec to find out who is wrong, so I took the 2 byte penalty instead.
> 1 votes

# Answer
# Python 3, ~~339~~ 337 bytes

```
def f(s):
 s=re.sub('[g-zG-Z ]','0',s)
 if len(s)<4:s=s.ljust(3,'0');return['0'+h for h in s]
 while len(s)%3:s+='0'
 L=len(s);l=[s[i:i+L//3]for i in range(0,L,L//3)];l=[j[len(j)-8:]for j in l]
 while l[0]and all(k[0]=='0'for k in l):l=[m[1:]for m in l]
 if len(l[0])<2:return[n.rjust(2,'0')for n in l]
 return[o[:2]for o in l]
import re

```

Try it online!

#### Commented

```
def f(s):                      # Create a function, f, which takes in a string, s
 s=re.sub('[g-zG-Z ]','0',s)   # Apply the regex so all non-hex characters are replaced with '0'
 if len(s)<4:                  # If the length is less than 4:
  s=s.ljust(3,'0')             #  Append '0' until the length is 3
  return['0'+h for h in s]     #  Return the elements, prepended by a '0'
 while len(s)%3:               # Until the length is divisible by 3:
  s+='0'                       #  Append '0'
 L=len(s)                      # Assign L to the length of s
 l=[s[i:i+L//3]for i in        # Split s into
    range(0,L,L//3)]           # three equal chunks
 l=[j[len(j)-8:]for j in l]    # Remove characters from the front if the length is more than 8
 while l[0]and all(            # While the strings are not empty
       k[0]=='0'for k in l):   # And all the strings start with '0':
  l=[m[1:]for m in l]          #  Remove the first character from each string
 if len(l[0])<2:               # If the length of the first string is less than 2:
  return[n.rjust(2,'0')        # Return each string,
         for n in l]           # with a '0' prepended until the length is 2
 return[o[:2]for o in l]       # Return the first two characters of each string
import re                      # Import the re module for regex handling

```
> 0 votes

# Answer
# Go, 359 bytes

```
import."regexp"
func f(s string)(k string){s=MustCompile(`[^0-9a-fA-F]`).ReplaceAllString(s,"0")
for;len(s)%3>0;s+="0"{}
L:=len(s)/3
S:=[]string{s[:L],s[L:2*L],s[2*L:]}
if len(s)<4{return "0"+S[0]+"0"+S[1]+"0"+S[2]}
for i:=range S{a:=S[i]
for;len(a)>8;a=a[1:]{}
for;a!=""&&a[0]!='0';a=a[1:]{}
for;len(a)<2;a="0"+a{}
for;len(a)>2;a=a[:len(a)-1]{}
k+=a}
return}

```

Attempt This Online!
> 0 votes

# Answer
# Japt v2.0a0, 52 bytes

```
u r/[^1-9A-F]/S
Êc3 ª3
úSV òVz3)®t8n)x2Ãù ®¯2 ù2 rS0

```

Try it

Outputs as an array of hex values.

Explanation:

```
u r/[^1-9A-F]/S
u               # Convert input to uppercase
  r/[^1-9A-F]/S # Replace everything other than 1-9 and A-F with spaces
                # Store as U

Êc3 ª3
Ê      # The length of U
 c3    # Rounded up to the nearest multiple of 3
    ª3 # If the result is 0, use 3 instead
       # Store as V

úSV òVz3)®t8n)x2Ãù ®¯2 ù2 rS0
úSV                           # Right-pad U with spaces until the length is V
    ò   )                     # Split into substrings with length:
     Vz3                      #  V divided by 3
         ®      Ã             # For each substring:
          t8n)                #  Get the last 8 characters
              x2              #  Trim any leading spaces
                 ù            # Left-pad each substring to the same length
                   ®          # For each padded substring:
                    ¯2        #  Get the first 2 characters
                       ù2     #  Left-pad with spaces to length 2
                          rS0 #  Replace all spaces with "0"

```
> 0 votes

---
Tags: code-golf, color
---


title: Is this position a pure mirror mate?

text:
# Question
Title: Is this position a pure mirror mate?
## Task

A *pure mirror checkmate* is a checkmate position, where the mated king is surrounded by **8** empty squares which are attacked or guarded by the winning side ***exactly* once**. You will be given a valid chess position where Black is in mate. Your task is to find if that mate is a *pure mirror checkmate*.

#### Examples (click for larger images)

1. This is a pure mirror mate. Squares f5, g5, are protected by the king only, h5, h6, h7 by the rook only and f6, f7, g7 by the two knights only.

---

2. This position does not match the criteria. It is a mirror mate, but *it's not pure*, as d7 is guarded by the bishop **and** attacked by the rook.

---

3. This position is not a pure mirror mate as well, since the king is not surrounded by 8 empty squares.

---

4. This is a pure mate, but not a mirror mate (not all 8 squares surrounding the king are empty), therefore it doesn't meet the criteria:

## Input

As mentioned above, your input will be a **valid** chess position, where black will be in mate. You may take the input as an FEN or as a grid/matrix/2D array. From the examples above:

1. first position:

```
4N2N/4q3/6k1/8/5K2/8/8/7R

. . . . N . . N
. . . . q . . .
. . . . . . k .
. . . . . . . .
. . . . . K . .
. . . . . . . .
. . . . . . . .
. . . . . . . R

```
2. second position

```
8/R7/4k3/2P3P1/3K4/7B/8/8

. . . . . . . .
R . . . . . . .
. . . . k . . .
. . P . . . P .
. . . K . . . .
. . . . . . . B
. . . . . . . .
. . . . . . . .

```
3. third position

```
7b/8/8/5N2/8/7k/5N2/7K

. . . . . . . b
. . . . . . . .
. . . . . . . .
. . . . . N . .
. . . . . . . .
. . . . . . . k
. . . . . N . .
. . . . . . . K

```
4. fourth position:

```
2NN4/4k3/1b2NNb1/8/6N1/8/6K1/q6n

. . N N . . . .
. . . . k . . .
. b . . N N b .
. . . . . . . .
. . . . . . N .
. . . . . . . .
. . . . . . K .
q . . . . . . n

```

## Rules

* The chess position will *always* be valid and black will *always* be in mate.
* There will be no double checks (the king won't be attacked twice).
* You can receive input through any of the standard IO methods.
* This is , so shortest code in bytes wins!

# Answer
> 6 votes
# Python \+ chess, ~~175~~ ~~166~~ 161 bytes

```
def f(e):b=chess.Board(e);s=b.attacks(k:=b.king(0));b.remove_piece_at(k);return(any(len(b.attackers(1,q))>1or b.piece_at(q)for q in s)or len(s)<8)^1
import chess

```

Attempt This Online!

-9 bytes thanks to rule clarification by @l4m2
-4 bytes because I realised I could just use `chess.Board` instead of `chess.BaseBoard`

Takes the fen as input and outputs `1` for a pure mirror mate and `0` otherwise. Using the chess library certainly made this easier. Explanation:

```
def f(e):
    b=chess.Board(e)                # Generates the board with the input fen
    k=b.king(0)                     # Finds the square of the black king
    s=b.attacks(k)                  # Get a generator of all squares surrounding the king by finding the squares he attacks
    b.remove_piece_at(k)            # Remove the king from the square to allow us to find guarded squares
    return(
        any(                        # If any of the following return True
            [len(b.attackers(1,q))>1# There is more than one attacker
                or b.piece_at(q)    # Or there is a piece present
                for q in s]         # In the squares surrounding the king
            )or                     # or
        len(s)<8                    # There are less than 8 squares surrounding the king
        )^1                         # Flip boolean and return

```

# Answer
> 4 votes
# JavaScript (Node.js), ~~381 334~~  330 bytes

```
B=>(Q=q=>B.some((b,i)=>b.some((R,j)=>q?+q?'*kk'[(K-i)**2+(L-j)**2]==R&&!(Q[++k,[K,L]]^=1):!eval(q,x=I-i,y=J-j)&R=='.'&&Q(1,K=i,L=j):(I=i,J=j,h=(m,n,r=4,p=i+m,q=j+n,x=B[K=p])=>x&&(x[L=q]in{k,'.':0})?Q(1)|h(m,n,r,p+m,q+n):r&&h(n,-m,r-1),z={N:`x*x-5`,P:`-x`,K:`x*x>2`}[R])?Q('y*y+'+z):R in{R,Q}&&h(1,0)||R in{B,Q}&&h(1,1))))(k=0)|k<8

```

Try it online!

-2 bytes from Kevin Cruijssen

-4 bytes from emanresu A

```
foo = a=>g1(k=0)|k<8;
g1 = q=>a.some((b,i)=>b.some((c,j)=>(
    I=i,J=j,
    c=='N'?g2((x,y)=>x*x+y*y-5): // Knight
    c=='P'?g2((x,y)=>y*y-x):
    c=='K'?g2((x,y)=>x*x+y*y>2):
    c=='R'?h(1,0):
    c=='B'?h(1,1):
    c=='Q'?h(1,1)|h(1,0):
    0
)));
g2 = q=>a.some((b,i)=>b.some((c,j)=>
    !q(I-i,J-j)&  // Correct destination
    c=='.'&&      // Empty
    g3(1,K=i,L=j) // then go
));
g3 = q=>a.some((b,i)=>b.some((c,j)=>
    [,1,1][(K-i)**2+(L-j)**2]&c=='k'? // Near king
    !(g[M=[K,L]]=!g[++k,M]) // count as an attack
    :0                      // and memory the pos
));
h = (m,n,r=4,p=i+m,q=j+n,x=a[K=p])=>
    x&&(x[L=q]=='.'|x[q]=='k')? // Not blocked
    g3(1)|h(m,n,r,p+m,q+n):      // Next step
    r&&h(n,-m,r-1) // rotate 90 degree

```

---
Tags: code-golf, decision-problem, grid, chess
---


# Question
Title: Make every consonant after a vowel uppercase
I have a string like this:

```
s = "helloworldandhellocodegolf"

```

I want to make every consonant after a vowel uppercase.

Desired output:

```
heLloWoRldaNdheLloCoDeGoLf

```

As you can see, `l` gets uppercased because it's right after the vowel `e` etc..

If there are repeated vowels, like:

```
s = "aabeekad"

```

Desired output:

```
s = "aaBeeKaD"

```

This is , so shortest code in bytes wins.

The consonants are all alphabet characters except for `aeiou`.

# Answer
# JavaScript (Node.js), 47 bytes

Expects and returns a string.

```
s=>Buffer(s).map(c=>c^s&~(s=68174912>>c)&32)+''

```

Try it online!

### How?

The constant \\$68174912\\$ (which I've already used here) is a bitmask describing the positions of the vowels:

```
00000100000100000100010001000000
     v     v     v   v   v
zyxwvutsrqponmlkjihgfedcba`_^]\[

```

### Commented

```
s =>           // s = input string
Buffer(s)      // turn it into a buffer
.map(c =>      // for each ASCII code c:
  c ^          //   toggle the case if
  s &          //   the previous letter was a vowel
  ~(           //   and the current letter is a consonant
    s =        //   save in s the new state obtained by
      68174912 //   right-shifting the bit-mask
      >> c     //   by c positions (mod 32)
  ) & 32       //   isolate the case bit
) + ''         // end of map(), coerce back to a string

```

---

# JavaScript (ES6), 38 bytes

Expects and returns an array of ASCII codes.

```
a=>a.map(p=c=>c^p&~(p=68174912>>c)&32)

```

Try it online!
> 10 votes

# Answer
# C (clang), 61 bytes

A full program that takes input from STDIN, and outputs to STDOUT.

```
c;main(d){for(;read(0,&c,1);)putchar(c^d-(d='Xz'%c>0)&32);}

```

Try it online!

I use the same formula to check for vowels as in this other task. Here, `(d='Xz'%c>0)` sets `d` equal to \\$ 0 \\$ if the current letter `c` is a vowel, and \\$ 1 \\$ otherwise. `c` should be uppercased if and only if the current `d` is \\$ 1 \\$, and the previous `d` is \\$ 0 \\$. To apply the uppercasing, `c` is XOR'ed with \\$ 32 \\$, but only when the conditions are met. Below is a table to visualize what is happening:

```
 | prev_d | d | prev_d - d | & 32 | ^ c  |
-+--------+---+------------+------+------+-
 |    0   | 0 |      0     |   0  |   c  |
 |    0   | 1 |     -1     |  32  | 32^c |
 |    1   | 0 |      1     |   0  |   c  |
 |    1   | 1 |      0     |   0  |   c  |

```
> 9 votes

# Answer
# JavaScript (Node.js), 56 bytes

```
x=>x.replace(/(?<=[aeiou])[^aeiou]/g,x=>x.toUpperCase())

```

Try it online!

# Perl 5 `-p`, ~~30~~ ~~27~~ 26 bytes

```
s/[aeiou]\K[^aeiou]/\U$&/g

```

Try it online!

-3 bytes thanks to Sisyphus

-1 byte thanks to Jo King
> 8 votes

# Answer
# Vyxal, 11 bytes

```
\zp⁽AḊ⁽ǐẆṅḢ

```

Try it Online!

This is a terrible way to do it but it works.

```
\zp⁽AḊ⁽ǐẆṅḢ
\zp         # Prepend "z" so that there is a leading consonant
   ⁽AḊ      # Adjacent-group by is-vowel
      ⁽ǐẆ   # Title-case every second item, starting on the first item
         ṅḢ # Join by nothing and remove the leading "z"

```
> 7 votes

# Answer
# Vyxal, 11 bytes

```
`° ß`$‡ǐNøṙ

```

Try it Online!

An alternate 11 byter that uses regex

See this golfing tip of mine that explains why the string `"° ß"` is `"[aeiou] [bcdfghjklmnpqrstvwxyz]"`

## Explained

```
`° ß`$‡ǐNøṙ
`° ß`       # The string `[aeiou] [bcdfghjklmnpqrstvwxyz]`
     $      # Make the stack input, ^
      ‡ǐN   # Push a function that title cases a string and then swapscase.
            # This is because regex matches will be vowel-consonant, and title case makes the vowel uppercase, consonant lowercase, swapcase inverts that for the desired result
         øṙ # Apply that function to regex matches in the input

```
> 7 votes

# Answer
# , 48 bytes

```
\(s)gsub("(?<=[aeiou])([^aeiou])","\\U\\1",s,,T)

```

Attempt This Online!
> 6 votes

# Answer
# Python 3.8 (pre-release), ~~78~~ ~~66~~ 65 bytes

```
d=0
for c in input():print(end=[c,c.upper()][d>(d:=c in"aeiou")])

```

Complete program. Thanks to @Neil and @Steffan for making this more concise. Try it online!

## Python 3, 73 bytes (@Steffan)

```
lambda x:re.sub("(?<=[aeiou])[^aeiou]",lambda a:a[0].upper(),x)
import re

```

Function solution. Try it online!
> 5 votes

# Answer
# Pip, ~~21~~ 16 bytes

```
aRXV.XC;@_.UC_@1

```

Try It Online!

-5 bytes thanks to @DLosc
Regex solution, ported from @Steffan's regex, with some pip builtin regex patterns used to save bytes

# Pip, ~~21~~ 17 bytes

```
{Iy>YaNVWUC:aa}Ma

```

Try It Online!

-4 bytes thanks to @DLosc
Non-regex alternative
> 5 votes

# Answer
# Go, ~~170~~ 164 bytes

```
import(."regexp";."strings")
func f(s string)string{return MustCompile(`[aeiou][^aeiou]`).ReplaceAllStringFunc(s,func(x string)string{return x[:1]+ToUpper(x[1:])})}

```

Attempt This Online!

No `(?<=)`, non-capturing groups only work with `Find(All)String(Submatch)` which leads to a longer solution, so this mess is what I've had to come with.

* -6 bytes for simplifying the regex.
> 5 votes

# Answer
# Ly, 30 bytes

```
"aeiou"0>ir[sp<l~!' **lf-ol~>]

```

Try it online!

This isn't even close to the smallest, but the way it works is probably very different from other languages, so I thought it was worth posting.

It uses two stacks, one with a list of vowels, and one with the STDIN string. Each character on the STDIN stack is copied to the vowel stack and the code search to see if there's a match. That "am I a vowel" result is combined with a "was the last char as vowel" result that lives on the stack to decide whether or not to capitalize the current character before printing it.

```
"aeiou"                        - push "is a vowel" comparison data on stack
       0                       - push "0" as "was last char a vowel" starter val
        >i                     - switch to new stack, read STDIN onto it
          r                    - reverse stack
           [                 ] - for each input char/codepoint
            sp<l               - stash the char, delete, switch stacks and restore
                ~!             - search for the char in "aeiou", negate result
                  '            - push " " (32), the "capitalize" codepoint offset
                    *          - multiple to clear if not this IS a vowel
                     *         - multiple against "was previous char vowel?" result
                      l        - reload the current char/codepoint
                       f-      - flip stack and subtract to capitalize if needed
                         o     - output current char
                          l~   - reload char, search against vowels
                            >  - switch back to the STDIN stack

```
> 4 votes

# Answer
# Dart (v2.18.4), ~~149~~ 80 bytes

```
f(s,[d=0])=>String.fromCharCodes([for(s in s.runes)s^d&~(d=68174912>>s%32)&32]);

```

Extra test-cases:

| Input | Output |
| --- | --- |
| loremipsumdolorsitamet | loReMiPsuMdoLoRsiTaMeT |
| namauctoranteelementumarculaciniaaceuismodestiaculis | naMauCtoRaNteeLeMeNtuMaRcuLaCiNiaaCeuiSmoDeStiaCuLiS |

---

*Thanks to @steffan who cut down to 80 bytes (!) by porting @arnauld answer.*
> 4 votes

# Answer
# Japt, 13 bytes

```
r"%v%c"ÈÎ+XÌu

```

Try it

```
r"%v%c"ÈÎ+XÌu     :Implicit input of string
r                 :Replace
 "%v%c"           :RegEx /[aeiou][b-df-hj-np-tv-z]/gi
       È          :Pass each match through the following function as X
        Î         :  First character
         +        :  Append
          XÌ      :  Last character
            u     :  Uppercased

```
> 4 votes

# Answer
# Vim, 23 keystrokes

```
:s/[aeiou]\+\zs./\u&/g<cr>

:s/                        # Search and replace...
   [aeiou]\+               #    A string of one or more vowels
            \zs            #    (start the match here)
               .           #    Any character
                           #    (because of greedy matching, this will always be the first consonant)
                /          # Replace with
                 \u        #    Make uppercase
                   &       #    The matched string (after \zs)
                    /g<cr> # Replace all occurrences

```
> 4 votes

# Answer
# Retina 0.8.2, 25 bytes

```
T`aeioul`aeiouL`[aeiou]+.

```

Try it online! Explanation: Transliterates vowels to themselves and other lowercase letters to uppercase letters, but only the letter after a run of vowels. (This is so that a double vowel doesn't confuse the transliteration.)

Alternative, also 25 bytes:

```
rT`aeioul`aeiouL`[aeiou].

```

Try it online! Explanation: The `r` makes it match the pattern from right to left solving the double vowel problem.

Both programs work in Retina 1 but 8 bytes can be saved because the `aeiou` can be replaced by `v` in the transliteration patterns.
> 4 votes

# Answer
# 05AB1E, 17 bytes

```
'zì.γžMså}2Å€™}J¦

```

Try it online!

* -4 thanks to Kevin Cruijssen

Port of Steffan's Vyxal answer

```
'zì.γžMså}2Å€™}J¦         # Implicit input                           TOP OF THE STACK:
'zì                       # Prepend a 'z'                            "zhelloworldandhellocodegolf"
   .γ    }                # Group by
     žM                   #  The string of vowels                    'aeiou'
       så                 #  Contains the letter                     ["zh", "e", "ll", "o", "w", "o", "rld", "a", "ndh", "e", "ll", "o", "c", "o", "d", "e", "g", "o", "lf"]
          2Å€ }           # Every second item:
             ™            #  Convert to title case                   ["Zh", "e", "Ll", "o", "W", "o", "Rld", "a", "Ndh", "e", "Ll", "o", "C", "o", "D", "e", "G", "o", "Lf"]
               J          # Join everything                          'ZheLloWoRldaNdheLloCoDeGoLf'
                ¦         # And remove the leading 'z'               'heLloWoRldaNdheLloCoDeGoLf'

```
> 3 votes

# Answer
# C (clang), 48 bytes

```
d;f(*s){for(d=1;*s;)*s++^=d-(d='Xz'%*s>0)&32;}

```

Try it online!

dingledooper's answer as a function.
> 3 votes

# Answer
# Charcoal, 22 bytes

```
⭆θ⎇№⌕Ａ⭆θ№aeiouλ10⊖κ↥ιι

```

Try it online! Link is to verbose version of code. Explanation: Maps each letter to its vowel-ness as a binary flag, then searches for the substring `10` in the result, the second character of which is where the consonants that needs to be uppercased are.

```
 θ                      Input string
⭆                       Map over letters and join
   №                    Count of
                  κ     Current index
                 ⊖      Decremented
       θ                In input string
      ⭆                 Map over letters and join
        №               Count of
              λ         Inner letter in
         aeiou          Literal string `aeiou`
    ⌕Ａ                  Find all matches of
               10       Literal string `10`
  ⎇                     If found then
                    ι   Current consonant
                   ↥    Uppercased
                     ι  Else current letter
                        Implicitly print

```
> 3 votes

# Answer
# sed, 25 bytes

```
s/[aeiou][^aeiou]/\U\l&/g

```

Attempt This Online!

It makes the whole match uppercase and then it turns first letter to lowercase. This is 5 bytes shorter than `s/([aeiou])([^aeiou])/\1\u\2/g` solution and it also doesn't require `-E` flag.
> 3 votes

# Answer
# Retina, 23 bytes

```
(?<=[aeiou])[^aeiou]
$u

```

Try it online!

`$u` is replace with uppercase.
> 2 votes

# Answer
# Jelly, 12 bytes

```
Œue€ØḄ<ƝŻTƲ¦

```

A monadic Link that accepts a list of lowercase characters and yields a list of characters.

**Try it online!**

### How?

```
Œue€ØḄ<ƝŻTƲ¦ - Link: list of lowercase characters, S
           ¦ - sparse application (to S)...
          Ʋ  - ...to indices: last four links as a monad - f(S):
    ØḄ       -     lowercase consonants
   €         -       for each (character in S):
  e          -         exists in? -> 1 if a consonant else 0
       Ɲ     -     for neighbours:
      <      -       less than? -> 1 if [0,1] else 0
        Ż    -     prepend a zero
         T   -     truthy indices -> indices of consonants directly after a vowel
Œu           - ...action: convert to uppercase

```
> 2 votes

# Answer
# Zsh `--extendedglob`, 52 bytes

```
<<<${1//(#m)[aeiou][^aeiou]/$MATCH[1]${(U)MATCH[2]}}

```

Try it Online! ~~69 bytes~~

Hat-tips: one two
> 2 votes

# Answer
# Factor, 51 bytes

```
[ R/ [aeiou][^aeiou]/ [ "\0 "v- ] re-replace-with ]

```

Try it online!
> 2 votes

# Answer
# Python 3, 90 bytes

```
lambda x:"".join(b.upper()if a in"aeiou"and b not in"aeiou"else b for a,b in zip(" "+x,x))

```

Would be 71 bytes if didn't have to deal with repetitive vowels, still got 90 bytes though. Try it online!
> 1 votes

# Answer
# Raku, 26 bytes

```
{S:g[<[aeiou]>+<(.]=$/.uc}

```

Try it online!
> 1 votes

# Answer
# Ruby `-p`, 36 bytes

```
gsub /(?<=[aeiou])[^aeiou]/,&:upcase

```

Attempt This Online!
> 1 votes

# Answer
# Pyth, 19 bytes

```
sm?*Z!=Z}d"aeiou"r1

```

Try it online!

### Explanation

```
sm?*Z!=Z}d"aeiou"r1ddQ    # implicitly add ddQ to the end
                          # implicitly assign Q = eval(input())
s                         # sum of
 m                   Q    # map lambda d over Q
  ?                       # ternary if (
   *Z!                    # Z and not
      =Z}d"aeiou"         # assign Z to d being a vowel):
                 r1d      #   d capitalized
                    d     # else: d

```
> 0 votes

# Answer
# Ruby 2.7, 110 bytes

```
->(s){v="aeiou";s.chars.map.with_index{v.include?(s[_2-1])&&!v.include?(_1)?(_1.upcase):_1}.join}

```
> 0 votes

---
Tags: code-golf, string
---