Language/Dart

[Exercism] Rna Transcription

paran21 2022. 9. 18. 23:39

https://exercism.org/tracks/dart/exercises/rna-transcription

 

Rna Transcription in Dart on Exercism

Can you solve Rna Transcription in Dart? Improve your Dart skills with support from our world-class team of mentors.

exercism.org

주어진 String에서 특정 단어를 변경하면 되는 문제이다.

함수형 프로그래밍에 아직 익숙하지 않아서, 가급적 함수형으로 해보려고 노력중이다.

 

split으로 iterable을 만들고 map을 돌려서 조건에 해당하는 경우에는 글자를 바꿔주었고, reduce로 다시 새로운 String으로 만들어서 return 하였다.

 

class RnaTranscription {

  String toRna(String dna) {
    if (dna == '') return '';
    return dna.split('').map(_convert).reduce((prev, next) => prev += next);
  }
  
  String _convert(String strand) {
    if (strand == 'G') return 'C';
    if (strand == 'C') return 'G';
    if (strand == 'T') return 'A';
    if (strand == 'A') return 'U';
    return '';
  }
}

'Language > Dart' 카테고리의 다른 글

[Exercism] Anagram #sort!!!!!  (0) 2022.09.21
[Exercism] Raindrops #String #Stringbuffer #immutual  (0) 2022.09.11
[Exercism] Gigasecond #Duration #Named parameter  (0) 2022.09.08
[Exercism] Hamming  (0) 2022.09.08
[Excercism] Space Age #enum  (0) 2022.09.08