There are a few differences but most of the time they aren’t anything to worry about. If you care, this is a good overview: levelup.gitconnected.com/7-differe...
function DNA_strand (str) {
let result = '';
for(i=0; i<str.length; i++){
switch (str[i]) {
case 'A':
result = result + 'T';
break;
case 'T':
result = result + 'A';
break;
case 'C':
result = result + 'G';
break;
case 'G':
result = result + 'C';
break;
default:
return 'Your DNA is not from this planet';
}
}
return result
};
```
functiondnaStrand(string$dnaString){$symbols=str_split($dnaString);$complementaryString='';foreach($symbolsas$symbol){switch($symbol){case'A':$complementaryString.='T';break;case'T':$complementaryString.='A';break;case'C':$complementaryString.='G';break;case'G':$complementaryString.='C';break;default:return"Is '".$symbol."' really in your DNA?";}}return$complementaryString;}
There is always a better solution, suggest one please - I'm here to learn more .)
defDNA_strand(strand):'''
Returns the complementary strand of DNA for a given strand.
'''complementary_strand=""forsymbolinstrand:ifsymbol=='A':complementary_strand+='T'continueifsymbol=='T':complementary_strand+='A'continueifsymbol=='C':complementary_strand+='G'continueifsymbol=='G':complementary_strand+='C'continuereturncomplementary_strandprint(DNA_strand("ATTGC"))print(DNA_strand("GTTAAC"))
This sounds like a one-liner in JS. Let's see...
As a bonus, any non-DNA letters will be ignored.
i am fresher can you tell me what does this ( => )operator do.
Your help is appreciated.
It’s an arrow function (or anonymous function).
function(a, b) {return a + b;}Is (almost) the same as:
(a, b) => a + bThere are a few differences but most of the time they aren’t anything to worry about. If you care, this is a good overview: levelup.gitconnected.com/7-differe...
JavaScript:
JS:
Another javascript solution
Ruby keeps on pleasantly surprising me:
Seems like Lua also has a neat solution:
Swift:
I'm trying to complete these everyday for 30 days (hopefully more) - feedback more that welcome here please, thank you! Github PR
C#
---- -|--- this bit always changes---| ---|- this bits only changes when---- --|-is set to 0Haskell:
Returns a Left value if there is a character other than AGTC
PHP:
There is always a better solution, suggest one please - I'm here to learn more .)
OK, I found another one for PHP (7.2 and higher):
Javascript using Map:
In Python:
Swift solution :
Scala