February 12, 2016 9:00 AM
We have three simple exercises on strings today:
Your task is to write the three exercises described above. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
Posted by programmingpraxis
Categories: Exercises
Tags:
Mobile Site | Full Site
Get a free blog at WordPress.com Theme: WordPress Mobile Edition by Alex King.
If people want an extra challenge, assume UTF-8 encoding and try to handle combining characters etc (see http://www.unicode.org/reports/tr29/)
To quote http://unicode.org/faq/char_combmark.html:
“Q: How are characters counted when measuring the length or position of a character in a string?
A: Computing the length or position of a “character” in a Unicode string can be a little complicated, as there are four different approaches to doing so, plus the potential confusion caused by combining characters. The correct choice of which counting method to use depends on what is being counted and what the count or position is used for.
Each of the four approaches is illustrated below with an example string [U+0061, U+0928, U+093F, U+4E9C, U+10083]. The example string consists of the Latin small letter a, followed by the Devanagari syllable “ni” (which is represented by the syllable “na” and the combining vowel character “i”), followed by a common Han ideograph, and finally a Linear B ideogram for an “equid” (horse):
aनि亜𐂃
…”
By matthew on February 12, 2016 at 12:31 PM
In Python’s itertools.compress, more or less. That was fun.
from itertools import chain, compress, count, repeat from operator import mul, eq, ge, lt def length(s): return next(compress(count(), chain(map(mul, map(len, s), repeat(0)), repeat(1)))) def index(s, c, k = 0): return next(compress(count(), map(mul, map(eq, s, repeat(c)), map(ge, count(), repeat(k)))), None) def substring(s, b, e): return ''.join(compress(s, map(mul, map(ge, count(), repeat(b)), map(lt, count(), repeat(e))))) print(length(""), length("x"), length("oo"), length("foobar")) print(index("kaksi", "k"), index("kaksi", "k", 1), index("kaksi", "k", 2), index("kaksi", "k", 3)) print(substring("notting", -3, 3), substring("notting", 3, 3) or "|", substring("notting", 3, 10)) print(index("", "w") or ":", substring("", 0, 1) or "-)", sep = "") # 0 1 2 6 # 0 2 2 None # not | ting # :-)By Jussi Piitulainen on February 12, 2016 at 6:24 PM