Take the 2-minute tour ×

I am trying to convert a Python program into C#. I do not understand what is being done here.

def mrF(alg, times = 1):
    if ((times % 2) == 0):
        return alg
        if (alg == 'R'):
            return "L'"
        if (alg[0] == 'G'):
            return alg
        if (alg[-1] == "'"):
            return alg[:-1]
        elif (alg[-1] == '2'):
            return alg
            return (alg + "'")

Is alg a string or a string array or a list?

share|improve this question
FWIW, that's very unpythonic python. No need for any of those parentheses –  Eric Nov 11 '12 at 15:05
@Eric Quite true. Also, replacing, for instance, alg[0] == 'G' with alg.startswith('G') would be more readable. And then there's the cryptic names alg and mrF, and the fact that this function's purpose is opaque and yet it is uncommented... –  Mark Amery Nov 11 '12 at 15:15
@MarkAmery I did not write this code :) –  Aivan Monceller Nov 11 '12 at 16:26
add comment

2 Answers

up vote 4 down vote accepted

Its a string. You can use a subscript operator on strings in Python, which I assume is the only part in your code, that made you to post it as question.

>>> "rohit"[0]
>>> "rohit"[-1]
>>> "rohit"[0:2]

Its similar to the way you access indices in lists.

share|improve this answer
Probably worth mentioning you can do it in C# too (however it's not that flexible) Console.WriteLine("Hi!"[1]); –  MasterMastic Nov 11 '12 at 14:58
@Ken.. Actually, I don't have any idea about C#. But thanks for mentioning that. :) –  Rohit Jain Nov 11 '12 at 14:59
That was a quick one, I have read list stuff on Python. didn't know this could be applied on strings. Thanks! @Ken , I am not aware I could do that. got to check How I could do [:::] in C# –  Aivan Monceller Nov 11 '12 at 15:04
@AivanMonceller Do you mean to get a range of characters with the '[] operator'? –  MasterMastic Nov 11 '12 at 15:07
@AivanMonceller You can use String.Substring. –  poke Nov 11 '12 at 15:12
show 1 more comment

It looks like alg is expected to be of type str or at least some kind of stringlike object here. There are three things being done with alg in this code that provide clues to its type:

Three clues:

1) Indexing it, e.g. referring to alg[0]. Strings, lists and tuples are all indexable in Python, though, so that doesn't narrow it down much.

2) Comparing it to a string, i.e. alg == 'R'. This only makes sense if alg is expected on at least some occasions when this function is called to be a string.

Note that points 1 and 2 still leave open the possibility that the function is sometimes expected to be called with a string, and sometimes with a list of characters. However, there's a third clue:

3) Concatenating it with strings. We have return (alg + "'") in the else block at the end. Note that there is no builtin type in Python that can be concatenated with a string that is not a string. Also note that in one of the branches of the if statement there is a string literal being explicitly returned, while in others alg is returned. This is all very strong evidence that the author of the function expected alg to always be a string.

Note that since Python is dynamically typed, theoretically there's no absolutely certain guarantee that there isn't some perverse case in which this function is used with something other than a string; passing in the list ['G'], for example, would not result in an exception. However, unless the code you're converting was written by sadistic lunatics deliberately trying to make your life difficult, for all practical purposes you can be certain here that alg is always going to be a string.

share|improve this answer
add comment

Your Answer


