Three String Exercises

February 12, 2016

We have three simple exercises on strings today:

  1. Write a function that determines the length of a string.
  2. Write a function that finds the index of the first occurrence of a character in a string, optionally starting at a given index.
  3. Write a function that creates a new string as a substring of an input string, from a given starting index to a given ending index.

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.

Pages: 1 2

2 Responses to “Three String Exercises”

  1. matthew said

    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नि亜𐂃

    …”

  2. Jussi Piitulainen said

    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
    # :-)
    

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: