1

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
    else:
        if (alg == 'R'):
            return "L'"
        if (alg[0] == 'G'):
            return alg
        if (alg[-1] == "'"):
            return alg[:-1]
        elif (alg[-1] == '2'):
            return alg
        else:
            return (alg + "'")

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

3
  • 1
    FWIW, that's very unpythonic python. No need for any of those parentheses Commented Nov 11, 2012 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... Commented Nov 11, 2012 at 15:15
  • @MarkAmery I did not write this code :) Commented Nov 11, 2012 at 16:26

1 Answer 1

4

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]
'r'
>>> "rohit"[-1]
't'
>>> "rohit"[0:2]
'ro'

Its similar to the way you access indices in lists.

Sign up to request clarification or add additional context in comments.

5 Comments

Probably worth mentioning you can do it in C# too (however it's not that flexible) Console.WriteLine("Hi!"[1]);
@Ken.. Actually, I don't have any idea about C#. But thanks for mentioning that. :)
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#
@AivanMonceller Do you mean to get a range of characters with the '[] operator'?
@AivanMonceller You can use String.Substring.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.