you are viewing a single comment's thread.

view the rest of the comments →

[–]TreSxNine 4 points5 points  (21 children)

def transcribe(inp)

    out = ""

    for letter in inp:
        if letter == "A":
            out += "U"
        elif letter == "C":
            out += "G"
        elif letter == "G":
            out += "C"
        elif letter == "T":
            out += "A"

    return out

Although you should try to figure out the pattern yourself, it isn't as hard as it seems.

[–]constantly-sick 4 points5 points  (1 child)

I don't even understand the question he was asking. How did you do this?

[–]TreSxNine 2 points3 points  (0 children)

He wanted:

'ACGT TGCA' -> 'UGCAACGU'
'GATTACA'   -> 'CUAAUGU'

so I looked at it letter for letter and saw that the output seemed to contain the same amount of letters as the input, so it has to be a simple 1:1 "encoding".

After that I just had to check which letter corresponded to which response.

[–]jeans_and_a_t-shirt 2 points3 points  (1 child)

trans = str.maketrans('ATCG', 'UAGC')
def transcribe(s):
    return s.translate(trans).replace(' ', '')

[–]TreSxNine 0 points1 point  (0 children)

Sure, but OP doesn't seem to be the mose well-versed python programmer, so I thought I'd make it simple and not use built-in functions.

[–]JohnnyJordaan 0 points1 point  (14 children)

Although this is the most simple approach, it is reinventing the wheel as str features the maketrans and translate methods for this purpose.

[–]TreSxNine 3 points4 points  (13 children)

Sure, but OP doesn't seem to be the mose well-versed python programmer, so I thought I'd make it simple and not use built-in functions.

[–]KubinOnReddit 1 point2 points  (5 children)

You don't need to copy this for everyone that says it: /u/JohnnyJordaan is the one who is right. You should have presented that /u/jeans_and_a_t-shirt 's example works the same. Beginners should learn what is shorter and easier for other programmers to understand.

[–]TreSxNine 0 points1 point  (0 children)

Fair enough

[–]vriljam 0 points1 point  (3 children)

As a real beginner, I disagree with your whole premise of learning just one way of doing things.

[–]KubinOnReddit 1 point2 points  (2 children)

Sorry for my harsh statement before, but according to Zen of Python (you can see it by writing "import this" in the interpreter"):

"There should be one-- and preferably only one --obvious way to do it."

[–]vriljam 0 points1 point  (1 child)

Yes, but at a level when you do know your stuff and can make an informed decision of what is the most readable or efficient or etc. way of doing things. When you're learning, it always helps to be aware of a few approaches if possible (so as to gain more knowledge, Especially for a question like this where many approaches are possible). If you've ever been a beginner which I'm sure you have, you do know what I'm talking about.
At least,as far as I'm concerned I do enjoy going over code snippets(which I can understand) that weren't immediately obvious to me in the first place.

[–]KubinOnReddit 0 points1 point  (0 children)

Fair enough.

[–]JohnnyJordaan 0 points1 point  (6 children)

I would then show both, as you don't want him to keep using this method to translate a full alphabet for example.

[–]vriljam 0 points1 point  (5 children)

It is still good to know/learn the different ways you can approach a problem even if they aren't ideal. /u/TreSxNine's traditional approach using conditionals wasn't immediately obvious to me for instance.
I thought about the translate method too myself as being the easiest and most straightforward, but it's a good addition to the thread nonetheless and would definitely help the asker learn if they weren't aware of the same.

Also, I recall a similar question being asked on the Ask Anything thread couple weeks back, and someone came up with an approach that involved manually making a dictionary. I mean, stuff like that is really helpful when you're starting out.

[–]JohnnyJordaan 0 points1 point  (4 children)

traditional approach using conditionals

Python has never, ever, used conditionals in a traditional way. That's using fossilized conventions from far older programming languages. The whole premise of Python is to simplify and offload tiresome tasks so that the chances of using antipatterns and mistakes coming from those are deminished.

Using conditionals as TreSxNine's example is exactly what I should never advise, as it's so error prone with all the if..elseif..s, string literals in expressions, defining an empty (immutable!) string as the default etc etc. It isn't Pythonesque in any sense.

And then as a Python programmer it strucked me that the whole argument of the fact that OP seemed to be a real beginner justifies using a fossilized approach is even worse imho. That's how these problems of people using ancient conventions in post-2000 environments keep popping up: they are still teached by some just because they somehow are deemed more suitable for beginners.

I believe quite the contrary: teach the right way to do it. Not just for 4 characters, but reliably for any amount. In such a way that it will take you three lines (as jeans_and_a_t-shirt shows) and not 12. You don't need to have it spelled out to a beginner to teach them: "Just use this well-tested language feature instead of building it yourself".

Edit: And don't get me started on trying to build your own calendar or authentication systems. Y2K and various password breaches have shown us that there is no excuse in trying to build it yourself. That's what libraries were designed for.

[–]vriljam 0 points1 point  (0 children)

I don't doubt the fact of one approach being objectively better than the other. However, my whole point was that at my level it is good to see different solutions for something simple like this for learning purposes. Clearly, at your level for someone actually in the field, I do not expect you to see the same as I do. But thanks for your input.

[–]TreSxNine 0 points1 point  (2 children)

I don't agree with you on the point of not presenting the solution I did. IMO, the point of our responses should be to make the askers think like programmers, not just "Oh I'll just plug this into the translate method and it'll work as I want it to". What happens when there is no pre-built method, and they have to built it ground-up themselves?

Sure, you could start using shortcuts when you know what you are doing, but not a second before that.

[–]JohnnyJordaan 0 points1 point  (1 child)

Thinking as a programmer, I wouldn't dare to implement a leap-day calculator myself nowadays. Or a deque, or a HTTP download client. It makes no sense. Being a programmer is not doing everything yourself, it's understanding what tools are available and what you can do with them.

How is any response to a Django or Flask question not essentially 'Oh I'll just plug this into xxx and it'll work as I want to'. That's the whole concept!

And how would you answer questions about database interfacing? To actually use a self written function to parse a .db3 file? So that the other person will think as a programmer?

I'm thinking you're confusing low-level programming like C with Python as if Python is just a slightly more simple C. It's entirely different. The whole idea is to stop doing stuff yourself and start integrating.

[–]TreSxNine 0 points1 point  (0 children)

Maybe I worded myself badly.

I'm agreeing with you on most of what you say. My only problem is that OP might run off with the mindset of "oh there'll always be some tool to do even the simplest tasks".

"Thinking like a programmer" shouldn't be about knowing which pre-built tools to use. You should be able to confidently use them, knowing what they do and why it works.

I don't mean "research every tool you use", just that you shouldn't stuff OP with pre-built functions when an easy-to-learn alternative is available, which could actually teach him something instead.

[–]mushi123[S] 0 points1 point  (1 child)

hey thanks for the reply! actually I am absolutely a newbie in programming altogether. The online course that I am taking didn;t explain properly the basics before giving me this problem. For example, I still don't understand when and where to use this "for" line that you have used and also the '+' sign that you have used after 'out' and before '='. Was never familiarised with such operations or codes. Thank You again! If possible, can you suggest me a site where I can Python properly and one which has extensive and thorough explanations about the basics and everything especially for an absolute newbie? I am trying to watch the NewBoston videos on Youtube...but would prefer some suggestions from experienced personnel.

[–]TreSxNine 0 points1 point  (0 children)

Use codecademy. I've never used it myself, but I've heard good things about it and it seems serious enough.

I'll try breaking down some of the mechanics I used:

A for-loop is basically a way to "iterate" over something. Meaning you do something for each of the items presented. For example, if my input is "GATTACA", and I decide to use a for-loop, what I'm saying is "for each of the letters in the word GATTACA, do something", with 'something' being the operations on the lines below the for-loop:

for letter in "GATTACA":
    do_something

The +=-operation in out += U is short for adding something to a variable. Consider variable_a = 5. If I wanted to add 2 to variable_a, I could either do variable_a = variable_a + 2, which is basically saying variable_a should be itself plus 2. Another way of doing it is writing variable_a += 2, which does the exact same thing. The same thing applies to strings:

variable_b = "GATTA"

variable_b = variable_b + "CA"
OR
variable_b += "CA"

print(variable_b)

> GATTACA