1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | //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. package Suss; public class CodrWar_5 { // best! // 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(); // } public static Boolean compare(String s1, String s2){ if( s1 == null ){ s1= ""; } if( s2 == null ){ s1= ""; } s1 = s1.toUpperCase(); s2 = s2.toUpperCase(); int sum_s1 =0; int sum_s2 =0; for(int i = 0 ; i<s1.length() ; i++){ int a =(int)s1.charAt(i); System.out.println(a); if( a < 65 || a > 90){ sum_s1 = 0; break; } sum_s1+=(int)s1.charAt(i); } System.out.println(s1+" "+sum_s1); for(int i = 0 ; i<s2.length() ; i++){ int a =(int)s2.charAt(i); System.out.println(a); if(a < 65 || a > 90){ sum_s1 = 0; break; } sum_s2+=(int)s2.charAt(i); } System.out.println(s2+" "+sum_s2); if(sum_s1 == sum_s2){ return true; } return false; } public static void main(String[] args) { //"ZzZz", "ffPFF" -> equal String s1 = "ZzZz"; String s2 = "ffPFF"; compare(s1, s2); } } |
오락기/codeWar
20170801
2017. 8. 1. 10:39
codewar
2017. 7. 24. 11:17
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | /*Given the triangle of consecutive odd numbers: 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 ... 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 */ public class CodeWar_4 { public static int rowSumOddNumbers(int n) { int end =1; int t=2; int count=0; int sum=0; if(n==end){ return n; } for(int i = 1 ; i<=n-1; i++){ count=t*2; t++; end =end+count; } System.out.println(end); for(int i=n ; i> 0; i--){ System.out.println(end); sum=sum+end; end =end-2; } System.out.println(sum); return sum; } public static int best_rowSumOddNumbers(int n) { /* sum of consequent M numbers is (M+1)M/2, so * we may know how many numbers were below * our ROW : numbersBelow = ((n-1)*(n))/2. * Now we may calculate first number in row: * firstNumberInRow = 2*numbersBelow+1. * So, firstNumberInRow = n*n-n+1 and * last number in ROW is n*n-n+1 + 2(n-1). * Let assume that last number before row is * x1 and last number in row is x2. It's known * that 1+3+5+...+(2k-1) = k*k. * Sum in row must be x2*x2 - x1*x1. // OUR x1 = (n*n-n)/2 and x2 = (n*n+n)/2. * After some simplification: SUM = n*n*n. */ return n*n*n; } public static void main(String[] args) { CodeWar_4 c = new CodeWar_4(); c.rowSumOddNumbers(42); } } |
code3
2017. 7. 21. 16:12
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | package Suss; //Check to see if a string has the same amount of 'x's and 'o's. //The method must return a boolean and be case insensitive. //The string can contains any char. // //Examples input/output: // //XO("ooxx") => true //XO("xooxx") => false //XO("ooxXm") => true //XO("zpzpzpp") => true // when no 'x' and 'o' is present should return true ////XO("zzoo") => false public class CodeWar_3 { public static boolean getXO(String str) { str = str.toLowerCase(); int o_c = 0; int x_c = 0; boolean out = false; for(int i =0 ;i<str.length() ; i++){ if(String.valueOf(str.charAt(i)).equals("o")) o_c++; if(String.valueOf(str.charAt(i)).equals("x")) x_c++; } if( (o_c ==x_c) || (o_c+x_c == 0)) out=true; System.out.println(out); return out; } public static boolean best_getXO (String str) { str = str.toLowerCase(); return str.replace("o","").length() == str.replace("x","").length(); } public static void main(String[] args) { CodeWar_3 c = new CodeWar_3(); c.getXO("ooxx"); c.getXO("xooxx"); c.getXO("ooxXm"); c.getXO("zpzpzpp"); c.getXO("zzoo"); } } |
code2
2017. 7. 21. 16:11
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | package Suss; //Given two integers, which can be positive and negative, //find the sum of all the numbers between including them too and return it. //If both numbers are equal return a or b. // //Note! a and b are not ordered! // //Example: //GetSum(1, 0) == 1 // 1 + 0 = 1 //GetSum(1, 2) == 3 // 1 + 2 = 3 //GetSum(0, 1) == 1 // 0 + 1 = 1 //GetSum(1, 1) == 1 // 1 Since both are same //GetSum(-1, 0) == -1 // -1 + 0 = -1 //GetSum(-1, 2) == 2 // -1 + 0 + 1 + 2 = 2 //Waiting for the Feedback! Thanks! public class CodeWar_2 { public static void main(String[] args) { int a = -50; int b = 0; int c = 0; int min=0; int max=0; if(a > b){ max = a; min = b; } else if(b>a){ max = b; min = a; } else{ if(a==0 & b==0) { System.out.println(0); // return a; } System.out.println(1); // return a; } while(true){ System.out.println(min); c=c+min; if(min==max) break; min++; } System.out.println("결과 : "+c); } } |
codewar1
2017. 7. 21. 16:07
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | package Suss; //Return the number (count) of vowels in the given string. // //We will consider a, e, i, o, and u as vowels for this Kata. public class CodeWar_1 { public static void main(String[] args) { String str= "abcde"; int count =0; for(int i =0 ; i<str.length(); i++){ String substr = str.substring(i, i+1); System.out.println(substr); if(substr.equals("a")) count++; if(substr.equals("e")) count++; if(substr.equals("i")) count++; if(substr.equals("o")) count++; if(substr.equals("u")) count++; } System.out.println(count); } } |