SECTION
Published 2019. 10. 26. 22:47
[CodeWar] Highest and Lowest PS/Codewar

https://www.codewars.com/kata/highest-and-lowest/train/java

 

Codewars: Train your coding skills

Codewars is where developers achieve code mastery through challenge. Train on kata in the dojo and reach your highest potential.

www.codewars.com

Instructions

 

In this little assignment you are given a string of space separated numbers, and have to return the highest and lowest number.

 

Example:

highAndLow("1 2 3 4 5")  // return "5 1"
highAndLow("1 2 -3 4 5") // return "5 -3"
highAndLow("1 9 3 4 -5") // return "9 -5"

 

 

 

Solution

public class Kata {
  public static String highAndLow(String numbers) {
    
    String[] array = numbers.split(" ");
	String big = array[0];
	String small = array[0];
		
	for(int i=0; i<array.length; i++) 
	{
	  if(Integer.parseInt(big) <= Integer.parseInt(array[i])) 
	  {
		big = array[i];
	  }
	  else if(Integer.parseInt(small) >= Integer.parseInt(array[i])) 
	  {
  		small = array[i];
          }
	}
	return (big +" "+ small);
  }
}

 

profile

SECTION

@SectionR0

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!

검색 태그