Codewars 練習

stream’s blog
2 min readMay 5, 2020

--

第一題:

Deoxyribonucleic acid, DNA is the primary information storage molecule in biological systems. It is composed of four nucleic acid bases Guanine (‘G’), Cytosine (‘C’), Adenine (‘A’), and Thymine (‘T’).

Ribonucleic acid, RNA, is the primary messenger molecule in cells. RNA differs slightly from DNA its chemical structure and contains no Thymine. In RNA Thymine is replaced by another nucleic acid Uracil (‘U’).

Create a function which translates a given DNA string into RNA.

For example:

new Bio().dnaToRna("GCAT") // returns "GCAU"

The input string can be of arbitrary length — in particular, it may be empty. All input is guaranteed to be valid, i.e. each input string will only ever consist of 'G', 'C', 'A' and/or 'T'.

題目要求遇到字串T,轉成U
用String內建的replace function即可

public class Bio {
public String dnaToRna(String dna) {

dna=dna.replace("T","U");
return dna; // Do your magic!
}
}

第二題

It’s pretty straightforward. Your goal is to create a function that removes the first and last characters of a string. You’re given one parameter, the original string. You don’t have to worry with strings with less than two characters.

運用String的function substring
注意後面兩個參數,第一個包含,第二個不包含即可

public class RemoveChars {
public static String remove(String str) {
str=str.substring(1,str.length()-1);
return str;
}
}

第三題

If you can’t sleep, just count sheep!!

Task:

Given a non-negative integer, 3 for example, return a string with a murmur: "1 sheep...2 sheep...3 sheep...". Input will always be valid, i.e. no negative integers.

把指定條件的重複字串塞進去迴圈,重複塞進去就好了

class Kata {
public static String countingSheep(int num) {
//Add your code here
String sheep =" sheep...";
String sleep ="";
for(int i=1;i<=num;i++)
{String countingSheep=i+sheep;
sleep =sleep + countingSheep;
}
return sleep;
}
}

寫完的感想就是自己其實也沒這麼爛,跟當初剛學,什麼都不會,
寫基本題目沒什麼太大問題,不過有些地方還是會卡住,要好好練習。

--

--