package space; import static java.util.stream.Collectors.joining; public class SpaceMaker{ public static String spaceMakerV1(String str){ return str.chars() .mapToObj(c -> String.valueOf((char)c)) .collect(joining(" ")); } public static String spaceMakerV2(String str){ char[] chars = str.toCharArray(); StringBuilder spaced = new StringBuilder(); for(char c:chars){ spaced.append(c).append(' '); } return spaced.toString().trim(); } }
function spaceMaker(str){return str.split("").join(" ");- package space;
- import static java.util.stream.Collectors.joining;
- public class SpaceMaker{
- public static String spaceMakerV1(String str){
- return str.chars()
- .mapToObj(c -> String.valueOf((char)c))
- .collect(joining(" "));
- }
- public static String spaceMakerV2(String str){
- char[] chars = str.toCharArray();
- StringBuilder spaced = new StringBuilder();
- for(char c:chars){
- spaced.append(c).append(' ');
- }
- return spaced.toString().trim();
- }
- }
import static org.junit.Assert.*; import static space.SpaceMaker.spaceMakerV1; import static space.SpaceMaker.spaceMakerV2; import org.junit.Test; public class SpaceMakerTest{ @Test public void testV1(){ assertEquals("o c t o b e r" , spaceMakerV1("october") ); assertEquals("s p a c e" , spaceMakerV1("space") ); assertEquals("r a n d o m" , spaceMakerV1("random") ); assertEquals("j a v a s c r i p t", spaceMakerV1("javascript")); assertEquals("c o d e w a r s" , spaceMakerV1("codewars") ); assertEquals("s t r i n g" , spaceMakerV1("string") ); } @Test public void testV2(){ assertEquals("o c t o b e r" , spaceMakerV2("october") ); assertEquals("s p a c e" , spaceMakerV2("space") ); assertEquals("r a n d o m" , spaceMakerV2("random") ); assertEquals("j a v a s c r i p t", spaceMakerV2("javascript")); assertEquals("c o d e w a r s" , spaceMakerV2("codewars") ); assertEquals("s t r i n g" , spaceMakerV2("string") ); } }
// TODO: Replace examples and use TDD development by writing your own tests- import static org.junit.Assert.*;
- import static space.SpaceMaker.spaceMakerV1;
- import static space.SpaceMaker.spaceMakerV2;
- import org.junit.Test;
// These are some CW specific test methods available:// Test.expect(boolean, [optional] message)// Test.assertEquals(actual, expected, [optional] message)// Test.assertSimilar(actual, expected, [optional] message)// Test.assertNotEquals(actual, expected, [optional] message)// NodeJS assert is also automatically required for you.// assert(true)// assert.strictEqual({a: 1}, {a: 1})// assert.deepEqual({a: [{b: 1}]}, {a: [{b: 1}]})// You can also use Chai (http://chaijs.com/) by requiring it yourself// var expect = require("chai").expect;// var assert = require("chai").assert;// require("chai").should();Test.expect(spaceMaker("october"), "o c t o b e r" );Test.expect(spaceMaker("space"), "s p a c e" );Test.expect(spaceMaker("random"), "r a n d o m" );Test.expect(spaceMaker("javascript"), "j a v a s c r i p t" );Test.expect(spaceMaker("codewars"), "c o d e w a r s" );Test.expect(spaceMaker("string"), "s t r i n g" );- public class SpaceMakerTest{
- @Test
- public void testV1(){
- assertEquals("o c t o b e r" , spaceMakerV1("october") );
- assertEquals("s p a c e" , spaceMakerV1("space") );
- assertEquals("r a n d o m" , spaceMakerV1("random") );
- assertEquals("j a v a s c r i p t", spaceMakerV1("javascript"));
- assertEquals("c o d e w a r s" , spaceMakerV1("codewars") );
- assertEquals("s t r i n g" , spaceMakerV1("string") );
- }
- @Test
- public void testV2(){
- assertEquals("o c t o b e r" , spaceMakerV2("october") );
- assertEquals("s p a c e" , spaceMakerV2("space") );
- assertEquals("r a n d o m" , spaceMakerV2("random") );
- assertEquals("j a v a s c r i p t", spaceMakerV2("javascript"));
- assertEquals("c o d e w a r s" , spaceMakerV2("codewars") );
- assertEquals("s t r i n g" , spaceMakerV2("string") );
- }
- }
import java.util.stream.*; class Solution { public static String largestNumber(final Integer[] nums) { return Stream.of(nums) .sorted((a,b)->(""+b+a).compareTo(""+a+b)) .collect(Collectors.reducing("",(x)->""+x,String::concat)); } }
- import java.util.stream.*;
- class Solution {
- public static String largestNumber(final Integer[] nums) {
- return Stream.of(nums)
- .sorted((a,b)->(""+b+a).compareTo(""+a+b))
.map(n->""+n).collect(Collectors.joining());- .collect(Collectors.reducing("",(x)->""+x,String::concat));
- }
- }
import java.util.*; class Solution { public static String largestNumber(final Integer[] nums) { Arrays.sort(nums, (a,b) -> (""+b+a).compareTo(""+a+b) ); String result = ""; for (final Integer num : nums) { result += num; } return result; } }
- import java.util.*;
- class Solution {
- public static String largestNumber(final Integer[] nums) {
Arrays.sort(nums, new Comparator<Integer>() {@Overridepublic int compare(final Integer a, final Integer b) {return (""+b+a).compareTo(""+a+b);}});- Arrays.sort(nums, (a,b) -> (""+b+a).compareTo(""+a+b) );
- String result = "";
- for (final Integer num : nums) {
- result += num;
- }
- return result;
- }
- }