Programing Challenge: Replace String Pairs
A coding challenge.
write a function “replace_string_pairs”. The function will take a ‹input string› and ‹pairs›, and returns a string.
the ‹pairs› is a list of pairs:
[ [‹search string 1›, ‹replace string 1›] [‹search string 2›, ‹replace string 2›] [‹search string 3›, ‹replace string 3›] … ]
but here's the tricky part:
Once a subsring in the input string is replaced, that part is not changed again. (the replacement start with first pair, then second, …) For example:
# -*- coding: utf-8 -*- # python 3 replace_string_pairs("abcd", [["a", "c"], ["c", "d"]])
then, result should be
- All strings are case sensitive. They are not regex.
- All strings are expected to be UTF-8 encoded.
- All strings may contain any Unicode character. (no worry about composition characters or BOM mark or direction reversal etc. Assume they are all just simple printable Unicode chars.)
- The size of input string may be up to 500k characters.
- The expected number of replacement pairs is expected to be up to 50.
- Length of each search or replacement string is anywhere from 1 char to about few thousand chars.
- the algorithm used and or code speed should be reasonably efficient/fast. This function is expected to be called on 5 thousand files, each file content is considered a input string.
I'll run your program in linux, on the command line, with the linux
I'll post a solution in emacs lisp in 3 days. If no solution is given in one of the language, i'll supply one, after a week.
the langs i'll be able to run are, on linux command line:
emacs --script ‹fr.el›(24.2.1)
- In-place Algorithm for Reversing a List in Perl, Python, Lisp, Mathematica
- Programing Problem: Construct a Tree Given Its Edges
- Programing: Decimalize Latitude Longitude
- Lisp, Python, Perl, Ruby Code to Validate Matching Brackets
- One Language to Rule Them All? Or, What Language to Use for Find Replace?
Patreon me $5. Ask me question on patreon