/now
projects
ramblings
smol projects

censored strings

16.11.2023 2 min read

Someone has attempted to censor my strings by replacing every vowel with a *, l*k* th*s. Luckily, I’ve been able to find the vowels that were removed.

Given a censored string and a string of the censored vowels, return the original uncensored string.

uncensor("Wh*r* d*d my v*w*ls g*?", "eeioeo") ➞ "Where did my vowels go?"
uncensor("abcd", "") ➞ "abcd"
uncensor("*PP*RC*S*", "UEAE") ➞ "UPPERCASE"

Twofers all the way down:

def uncensor(s : String, v : String)
  v_arr = v.split("").reverse
  s.split("").map { |ch| ch == "*" ? v_arr.pop : ch }.join
end

def uncensor_func(s : String, v : String)
  return s if v.empty?
  idx = s.chars.index! { |e| e == '*' }
  new_s = s[0...idx] + v[0] + s[idx + 1..]
  uncensor_func(new_s, v[1..])
end
func uncensor(s: string, v: string): string =
    var 
        sArr = s.toSeq
        vArr = v.toSeq.reversed
    sArr.map(proc(c: char): char =
        if c == '*': return vArr.pop 
        return c
    ).join

func uncensorFunc(s: string, v: string): string =
    if v.len == 0: return s
    else:
        let idx = s.find('*')
        let newS = s[0..<idx] & v[0] & s[idx + 1..^1]
        uncensorFunc(newS, v[1..^1])
sub uncensor(Str $s, Str $v) {
    my @v = $v.comb.reverse;
    my @s = $s.comb;
    @s.map({$_ eq "*" ?? @v.pop() !! $_}).join
}

sub uncensor-func(Str $s, Str $v) {
    return $s if $v.comb.elems == 0;
    my $i = $s.comb.first("*", :k);
    my $n = $s.substr(0, $i) ~ $v.comb.head ~ $s.substr($i + 1);
    uncensor-func($n, $v.substr(1));
}
function uncensor(str: string, vowels: string) {
  const strArr = str.split("");
  const vowelsArr = vowels.split("").reverse();
  return strArr
    .map((ch) => {
      if (ch == "*") ch = vowelsArr.pop() as string;
      return ch;
    })
    .join("");
}

function uncensorFunc(str: string, vowels: string) {
  if (vowels.length == 0) return str;
  const idx = str.indexOf("*");
  const newStr = str.slice(0, idx) + vowels[0] + str.slice(idx + 1);
  return uncensorFunc(newStr, vowels.slice(1));
}
Built with Astro and Tailwind 🚀