CodeWars自虐之 8kyu

大概是上周开始玩CodeWars的,反正就是觉得蛮有意思的,编程刷荣誉。一开始等级都是8kyu,经过一个星期的摸爬滚打,本鸟升到了7kyu哈哈,记录下这个过程吧。(注:Java版本1.8.0_91 (Java 8))
1、Square Every Digit
Description:
Welcome. In this kata, you are asked to square every digit of a number.
For example, if we run 9119 through the function, 811181 will come out.
Note: The function accepts an integer and returns an integer

我的答案:

public class SquareDigit {
  public int squareDigits(int n) {
  String str = String.valueOf(n);
    int length = str.length();
    String s=new String();
    for (int i = 0; i < length; i++) {
      char b = str.charAt(i);
      int num = Integer.valueOf(b + "");
      num = num * num;
      s=s+num;
    }
    int numder=Integer.valueOf(s);
    return numder;
  }
}

获赞最多的答案:

public class SquareDigit {
  public int squareDigits(int n) {
    String result = ""; 
    while (n != 0) {
      int digit = n % 10 ;
      result = digit*digit + result ;
      n /= 10 ;
    }
    return Integer.parseInt(result) ;
  }
}

哪个答案更优雅这显而易见,我这就不再自我吐槽了。

2、Compare Strings by Sum of Chars
Description:
Compare two strings by comparing the sum of their values (ASCII character code).
For comparing treat all letters as UpperCase.
Null-Strings should be treated as if they are empty strings.
If the string contains other characters than letters, treat the whole string as it would be empty.
Examples:
"AD","BC" -> equal
"AD","DD" -> not equal
"gf","FG" -> equal
"zz1","" -> equal
"ZzZz", "ffPFF" -> equal
"kl", "lz" -> not equal
null, "" -> equal
Your method should return true, if the strings are equal and false if they are not equal.

我的答案:

public class Kata
{
  public static Boolean compare(String s1, String s2)
  {    
  int strLength01 = 0;
    int strLength02 = 0;
    if (s1!=null) {
      strLength01=s1.length();
    }
    if (s2!=null) {
      strLength02=s2.length();
    }
    int sum01 = 0;
    int sum02 = 0;
    boolean hasOther01 = false;
    boolean hasOther02 = false;
    for (int i = 0; i < strLength01; i++) {
      int asii = s1.charAt(i);
      if ((asii >= 'a' && asii <= 'z') || (asii >= 'A' && asii <= 'Z')) {
        if (asii >= 'a') {
          asii -= 32;
        }
        sum01 += asii;
      } else// 有其他字符
      {
        hasOther01=true;
        System.out.println("s1 has other char");
        if (s2==null||s2.equals("")) {
          System.out.println("s2 是null或者empet");
          return true;
        }
      }
    }
    System.out.println();

    for (int i = 0; i < strLength02; i++) {
      int asii = s2.charAt(i);
      if ((asii >= 'a' && asii <= 'z') || (asii >= 'A' && asii <= 'Z')) {
        if (asii >= 'a') {
          asii -= 32;
        }
        sum02 += asii;
      } 
      else// 有其他字符
      {
        hasOther02=true;
        if (s1==null||s1.equals("")) {
          return true;
        }
      }
    }
    System.out.println();
    if(hasOther01&&hasOther02)
    {
    return true;
    }
    if ((sum01 == sum02)&&!hasOther01&&!hasOther02) {
      return true;
    }
    return false;
  }
}

推荐答案:

public class Kata {

  public static boolean compare(String s1, String s2) {
  
    if (s1 == null || !s1.matches("[a-zA-Z]+")) s1 = "";
    if (s2 == null || !s2.matches("[a-zA-Z]+")) s2 = "";
    
    return s1.toUpperCase().chars().sum() == s2.toUpperCase().chars().sum();
  }
}

我咋就没想到用正则呢???不说了,让本鸟哭会儿......

3、Mumbling
Description:
This time no story, no theory. The examples below show you how to write function accum:
Examples:
Accumul.accum("abcd"); // "A-Bb-Ccc-Dddd"
Accumul.accum("RqaEzty"); // "R-Qq-Aaa-Eeee-Zzzzz-Tttttt-Yyyyyyy"
Accumul.accum("cwAt"); // "C-Ww-Aaa-Tttt"
The parameter of accum is a string which includes only letters from a..z and A..Z.

我的答案:

public class Accumul {
    
    public static String accum(String s) {
     // your code
    int strLength = s.length();
    String s1 = new String();
    StringBuffer s3=new StringBuffer();
    for (int i = 0; i < strLength; i++) {
      StringBuffer s2 = new StringBuffer();
      char ch = s.charAt(i);
      for (int j = 0; j <= i; j++) {
        if (j == 0) {
          ch = Character.toUpperCase(ch);
          s2.append(ch);
        }
        else
        {
          ch = Character.toLowerCase(ch);
          s2.append(ch);
        }    
      }
      s1=s2.toString();
      if(i==strLength-1)
      {
        s3.append(s1);
      }
      else
      s3.append(s1+"-");
    }    
    return s3.toString();
}
}

推荐答案:

public class Accumul {
  public static String accum(String s) {
    StringBuilder bldr = new StringBuilder();
    int i = 0;
    for(char c : s.toCharArray()) {
      if(i > 0) bldr.append('-');
      bldr.append(Character.toUpperCase(c));
      for(int j = 0; j < i; j++) bldr.append(Character.toLowerCase(c));
      i++;
    }
    return bldr.toString();
  }
}

4、Descending Order

Description:
Your task is to make a function that can take any non-negative integer as a argument and return it with it's digits in descending order. Essentially, rearrange the digits to create the highest possible number.
Examples:
Input: 21445 Output: 54421
Input: 145263 Output: 654321
Input: 1254859723 Output: 9875543221
我的答案:

public class DescendingOrder {
  public static int sortDesc(final int num) {
         //Your code
        String s=num+"";
        int length=s.toString().length();
        int[] array=new int[length];
        int nu=1;
        int rest=0;
        for (int i = 1; i <= array.length; i++) {
        for (int j = array.length-i; j >=1; j--) {
          nu=nu*10;
        }
        if(i==1)
        {
          System.out.println(nu);
          array[i-1]=num/nu;
          rest=num%nu;
          System.out.println("rest "+rest);
        }       
        else
        {
          System.out.println(nu);
          array[i-1]=rest/nu;
          rest=rest%nu;
          System.out.println("rest "+rest);
        }       
        nu=1;        
      }
        int key;
        int sa=0;
        //开始排序
        for (int i = 1; i < array.length; i++) {
          key=array[i];
          sa=i-1;
          while (sa>=0&&array[sa]<key) {
          array[sa+1]=array[sa];
          sa--;
        }
           array[sa + 1] = key;
      }
        String number="";
        for (int i = 0; i < array.length; i++) {          
          number=number+array[i];         
      }
        int numb=Integer.valueOf(number);
        System.out.println(numb+"");
      return numb;            
  }
}

推荐答案:

import java.util.Comparator;
import java.util.stream.Collectors;

public class DescendingOrder {
    public static int sortDesc(final int num) {
        return Integer.parseInt(String.valueOf(num)
                                      .chars()
                                      .mapToObj(i -> String.valueOf(Character.getNumericValue(i)))
                                      .sorted(Comparator.reverseOrder())
                                      .collect(Collectors.joining()));
    }
}

5、Sum of odd numbers
Description:
Given the triangle of consecutive odd numbers:
1
3 5
7 9 11
13 15 17 19
21 23 25 27 29
...
Calculate the row sums of this triangle from the row index (starting at index 1) e.g.:
rowSumOddNumbers(1); // 1
rowSumOddNumbers(2); // 3 + 5 = 8

推荐答案:

class RowSumOddNumbers {
    public static int rowSumOddNumbers(int n) {
        return n * n * n;
    }
}

6、Multiples of 3 and 5
Description:
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Finish the solution so that it returns the sum of all the multiples of 3 or 5 below the number passed in.
Note: If the number is a multiple of both 3 and 5, only count it once.
Courtesy of ProjectEuler.net

推荐答案:

public class Solution {

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

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 8,578评论 0 23
  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,660评论 0 33
  • 9/21 1.笑来老师《财富自由之路》,「⑧如何启动第三种个人商业模式?」: 所谓“个人商业模式”,就是一个人出售...
    医路洁行阅读 317评论 0 1
  • 嘿,你还好吗? 我想象的未来里其实有你。 我们一起在厨房忙碌的喜悦, 为你庆祝生日的惊喜,亲手为你戴上项链的激动,...
    巨宝的城阅读 248评论 0 0
  • 时间磨平了什么,每个人心中都有答案。 (一) 闲聊中听妈妈讲她过去的事,提到了我从未见过的姥爷。在我出生之前,姥爷...
    冷凝忆阅读 445评论 0 1