Java 算法 CodeWar——Day 2

Code War

  • code war honor 27
  • uestc position 58

quesetion 1

Mr. Scrooge has a sum of money 'P' that wants to invest, and he wants to know how many years 'Y' this sum has to be kept in the bank in order for this sum of money to amount to 'D'.
The sum is kept for 'Y' years in the bank where interest 'I' is paid yearly, and the new sum is re-invested yearly after paying tax 'T'
Note that the principal is not taxed but only the year's accrued interest

  Example:
  Let P be the Principal = 1000.00      
    Let I be the Interest Rate = 0.05      
    Let T be the Tax Rate = 0.18      
    Let D be the Desired Sum = 1100.00
  
  
  After 1st Year -->
    P = 1041.00
  After 2nd Year -->
    P = 1083.86
  After 3rd Year -->
    P = 1128.30

Thus Mr. Scrooge has to wait for 3 years for the initial pricipal to ammount to the desired sum.
Your task is to complete the method provided and return the number of years 'Y' as a whole in order for Mr. Scrooge to get the desired sum.
Assumptions : Assume that Desired Principal 'D' is always greater than the initial principal, however it is best to take into consideration that if the Desired Principal 'D' is equal to Principal 'P' this should return 0 Years.

translation 1

Scrooge將本金(principal)存入銀行芯急,每年的利息和交稅計算公式如下:result = princial * ( 1 + interest)- princialinteresttax
給定目標金額穿铆、本金、利率拾枣、稅率付燥,求出需要幾年可以達到這個金額奢入。如果目標金額和本金相等唧取,返回0;

public class Money {

    public static int calculateYears(double principal, double interest,  double tax, double desired) {
        int years = 0;

        while( principal < desired)
        {
            years++;
            principal = principal * ( 1 + interest ) - principal*interest*tax;
        }
        return years;
    }
}

question 2

Return the number (count) of vowels in the given string.
We will consider a, e, i, o, and u as vowels for this Kata.
The input string will only consist of lower case letters and/or spaces.

translation 2

返回字符串中的元音字母奥邮,元音字母為a万牺、e、i洽腺、o脚粟、u,輸入?yún)?shù)只包含小寫字母 空格 和“/”(斜杠)

public class Vowels {

    public static int getCount(String str) {
        int j = 0;
        char[] key = str.toCharArray();
        for(int i = 0; i < key.length; i++)
        {
            if(key[i] == 'a' | key[i] =='e'| key[i] == 'i' | key[i] == 'o' | key[i] == 'u')
            {
                j++;
            }
        }
        return j;
    }
}

question 3

Part of Series 1/3: This kata is part of a series on the Morse code. After you solve this kata, you may move to the next one.
In this kata you have to write a simple Morse code decoder. While the Morse code is now mostly superceded by voice and digital data communication channels, it still has its use in some applications around the world.
The Morse code encodes every character as a sequence of "dots" and "dashes". For example, the letter A is coded as ·?, letter Q is coded as ??·?, and digit 1 is coded as ·???. The Morse code is case-insensitive, traditionally capital letters are used. When the message is written in Morse code, a single space is used to separate the character codes and 3 spaces are used to separate words. For example, the message HEY JUDE in Morse code is ···· · ?·?? ·??? ··? ?·· ·.
NOTE: Extra spaces before or after the code have no meaning and should be ignored.
In addition to letters, digits and some punctuation, there are some special service codes, the most notorious of those is the international distress signal SOS (that was first issued by Titanic), that is coded as ···???···. These special codes are treated as single special characters, and usually are transmitted as separate words.
Your task is to implement a function decodeMorse(morseCode), that would take the morse code as input and return a decoded human-readable string.
For example:

MorseCodeDecoder.decode(".... . -.--   .--- ..- -.. .")
//should return "HEY JUDE"

The Morse code table is preloaded for you as a dictionary, feel free to use it. In CoffeeScript, C++, JavaScript, PHP, Python, Ruby and TypeScript, the table can be accessed like this: MORSE_CODE['.--'], in Java it is MorseCode.get('.--'), in C# it is MorseCode.Get('.--'), in Haskell the codes are in a Map String String and can be accessed like this: morseCodes ! ".--", in Elixir it is morse_codes variable.
All the test strings would contain valid Morse code, so you may skip checking for errors and exceptions. In C#, tests will fail if the solution code throws an exception, please keep that in mind. This is mostly because otherwise the engine would simply ignore the tests, resulting in a "valid" solution.
Good luck!
After you complete this kata, you may try yourself at Decode the Morse code, advanced.

translation 3

制作一個摩斯碼的解碼器
摩斯碼用'.'和'-'來表示信息已脓,不區(qū)分大小寫珊楼,一個空格分割字符,三個空格分隔單詞度液,你的任務
是實現(xiàn)一個函數(shù)decodeMorse(morseCode) 傳入摩斯碼厕宗,返回英文字符串。摩斯碼表我們已經(jīng)預先提供給你了堕担,Java使用方法: MorseCode.get('.--')

/*
    pass the BaseTest but not the ComplexTest 
*/
public class MorseCodeDecoder {
    public static String decode(String morseCode) {
      String s = morseCode.replaceAll("   ", "#").replaceAll(" ", "|");
        char[] key = s.toCharArray();
        String result = "";
        String temp = "";
        String str = "";
        for(int i = 0; i < key.length; i++)
        {
            if(key[i] == '|')
            {
              str += MorseCode.get(temp);
               temp = "";
            }
            else if(key[i] == '#')
            {
               str += MorseCode.get(temp);
                result += str + " ";
                str = "";
                temp = "";
            }
            else{
                temp += key[i];
            }

        }
        str += MorseCode.get(temp);
        result += str;
        return result;
    }
}
  1. public String replaceAll(@NotNull String regex, @NotNull String replacement)將字符串按照正則表達式用參數(shù)replacement進行替換

question 4

Implement a method that accepts 3 integer values a, b, c. The method should return true if a triangle can be built with the sides of given length and false in any other case.
(In this case, all triangles must have surface greater than 0 to be accepted).

translation 4

實現(xiàn)一個方法已慢,輸入三角形的三個邊長度,如果能構成三角形霹购,則返回true佑惠,否則返回false,注:輸入值均大于0齐疙;

public class TriangleTester {

    public static boolean isTriangle(int a, int b, int c){
        if( a+b > c & a+c > b & b+c > a )
        {
            return true;
        }
        return false;
    }
}

對應代碼鏈接膜楷,歡迎fork和star

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市贞奋,隨后出現(xiàn)的幾起案子赌厅,更是在濱河造成了極大的恐慌,老刑警劉巖轿塔,帶你破解...
    沈念sama閱讀 222,729評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件特愿,死亡現(xiàn)場離奇詭異仲墨,居然都是意外死亡,警方通過查閱死者的電腦和手機揍障,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,226評論 3 399
  • 文/潘曉璐 我一進店門目养,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人毒嫡,你說我怎么就攤上這事癌蚁。” “怎么了审胚?”我有些...
    開封第一講書人閱讀 169,461評論 0 362
  • 文/不壞的土叔 我叫張陵匈勋,是天一觀的道長礼旅。 經(jīng)常有香客問我膳叨,道長,這世上最難降的妖魔是什么痘系? 我笑而不...
    開封第一講書人閱讀 60,135評論 1 300
  • 正文 為了忘掉前任菲嘴,我火速辦了婚禮,結果婚禮上汰翠,老公的妹妹穿的比我還像新娘龄坪。我一直安慰自己,他們只是感情好复唤,可當我...
    茶點故事閱讀 69,130評論 6 398
  • 文/花漫 我一把揭開白布健田。 她就那樣靜靜地躺著,像睡著了一般佛纫。 火紅的嫁衣襯著肌膚如雪妓局。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,736評論 1 312
  • 那天呈宇,我揣著相機與錄音好爬,去河邊找鬼。 笑死甥啄,一個胖子當著我的面吹牛存炮,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播蜈漓,決...
    沈念sama閱讀 41,179評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼穆桂,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了融虽?” 一聲冷哼從身側響起享完,我...
    開封第一講書人閱讀 40,124評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎衣形,沒想到半個月后驼侠,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體姿鸿,經(jīng)...
    沈念sama閱讀 46,657評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,723評論 3 342
  • 正文 我和宋清朗相戀三年倒源,在試婚紗的時候發(fā)現(xiàn)自己被綠了苛预。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,872評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡笋熬,死狀恐怖热某,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情胳螟,我是刑警寧澤昔馋,帶...
    沈念sama閱讀 36,533評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站糖耸,受9級特大地震影響秘遏,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜嘉竟,卻給世界環(huán)境...
    茶點故事閱讀 42,213評論 3 336
  • 文/蒙蒙 一邦危、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧舍扰,春花似錦倦蚪、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,700評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至个束,卻和暖如春慕购,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背播急。 一陣腳步聲響...
    開封第一講書人閱讀 33,819評論 1 274
  • 我被黑心中介騙來泰國打工脓钾, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人桩警。 一個月前我還...
    沈念sama閱讀 49,304評論 3 379
  • 正文 我出身青樓可训,卻偏偏與公主長得像,于是被迫代替她去往敵國和親捶枢。 傳聞我的和親對象是個殘疾皇子握截,可洞房花燭夜當晚...
    茶點故事閱讀 45,876評論 2 361

推薦閱讀更多精彩內(nèi)容