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,输入参数只包含小写字母 空格 和“/”(斜杠)

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

制作一个摩斯码的解码器
摩斯码用'.'和'-'来表示信息,不区分大小写,一个空格分割字符,三个空格分隔单词,你的任务
是实现一个函数decodeMorse(morseCode) 传入摩斯码,返回英文字符串。摩斯码表我们已经预先提供给你了,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)将字符串按照正则表达式用参数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

实现一个方法,输入三角形的三个边长度,如果能构成三角形,则返回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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,378评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,356评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,702评论 0 342
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,259评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,263评论 5 371
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,036评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,349评论 3 400
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,979评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,469评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,938评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,059评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,703评论 4 323
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,257评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,262评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,485评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,501评论 2 354
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,792评论 2 345

推荐阅读更多精彩内容