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]

推荐答案:

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;
  }
}

你可能感兴趣的:(CodeWars自虐之 8kyu)