Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
5.3 kB
4
Indexable
Never
    public static String solution(String input){
        
        long sec = Long.parseLong(input);
        
        if(sec == 0) return "0 second";
        long numberOfYear = 0;
        long numberOfMothsInYear = 0;
        long numberOfDaysInYear = 0;
        long numberOfHoursInYear = 0;
        long numberOfMinInYear = 0;
        List<Long> list = new ArrayList<>();
        
        if(sec >= (365*24*60*60)){
            
            numberOfYear = (long)Math.floor(sec/(365*24*60*60));
            sec = (sec -  numberOfYear*3600*24*365);
        }
        
        if(sec >= (30*24*60*60)){
            
            numberOfMothsInYear = (long)Math.floor(sec / (30*24*60*60));
            sec = (sec -  numberOfMothsInYear*3600*24*30);
        }
        
        if(sec >= (24*60*60)){
            numberOfDaysInYear = (long)Math.floor(sec / (24*60*60));
            sec = (sec -  numberOfDaysInYear*60*60*24);
        }
        
        if(sec >= (60*60)){
            numberOfHoursInYear = (long)Math.floor(sec / (60*60));
            sec = (sec -  numberOfHoursInYear*60*60);            
        }
 

        if(sec >= 60){
            numberOfMinInYear = (long)Math.floor(sec / (60));
            sec = (sec -  numberOfMinInYear*60);
        }
        
        list.add(numberOfMothsInYear);
        list.add(numberOfDaysInYear); 
        list.add(numberOfHoursInYear);
        list.add(numberOfMinInYear);
        list.add(sec);
        
        String res = "";
        boolean flag = false;
        
        if(numberOfYear > 0){
            
            if(numberOfYear == 1){
                res = res + "1 year";
            }else{
                res = res + numberOfYear + " years";
            }
            
            if(numberOfMothsInYear == 0 && numberOfDaysInYear == 0 && numberOfHoursInYear == 0 && numberOfMinInYear == 0 && sec == 0){
                return res;
            }else{
                
                if(countZero(list) >= 2){
                    res = res + ", ";
                }else{
                    flag = true;
                    res = res + " and ";
                }
            }
            
            
        }
        
        list.remove(0);
        
        if(numberOfMothsInYear > 0){
            
            if(numberOfMothsInYear == 1){
                res = res + "1 month";
            }else{
                res = res + numberOfMothsInYear + " months";
            }
            
            if(numberOfDaysInYear == 0 && numberOfHoursInYear == 0 && numberOfMinInYear == 0 && sec == 0){
                return res;
            }else{
                
                if(countZero(list) >= 2){
                    res = res + ", ";
                }else if(!flag){
                    flag = true;
                    res = res + " and ";
                }
            }
        }
        
        list.remove(0);
        
        if(numberOfDaysInYear > 0){
            
            if(numberOfDaysInYear == 1){
                res = res + "1 day";
            }else{
                res = res + numberOfDaysInYear + " days";
            }
            
            if(numberOfHoursInYear == 0 && numberOfMinInYear == 0 && sec == 0){
                return res;
            }else{
                
                if(countZero(list) >= 2){
                    res = res + ", ";
                }else if(!flag){
                    flag = true;
                    res = res + " and ";
                }
            }
            
        }
        
        list.remove(0);
        
        if(numberOfHoursInYear > 0){
            
            if(numberOfHoursInYear == 1){
                res = res + "1 hour";
            }else{
                res = res + numberOfHoursInYear + " hours";
            }
            
            if(numberOfMinInYear == 0 && sec == 0){
                return res;
            }else{
                
                if(countZero(list) >= 2){
                    res = res + ", ";
                }else if(!flag){
                    flag = true;
                    res = res + " and ";
                }
            }
        }
        
        list.remove(0);
        
        if(numberOfMinInYear > 0){
            
            if(numberOfMinInYear == 1){
                res = res + "1 minute";
            }else{
                res = res + numberOfMinInYear + " minutes";
            }
            
            if(sec == 0){
                return res;
            }
        }
            
        list.remove(0);
        
        if(sec > 0){
            
            if(res.equals("")){
                
                if(sec == 1){
                    return "1 second";
                }else{
                    return sec + " seconds";
                }
            }else{
                
                if(sec == 1){
                    return res + " and 1 second";
                }else{
                    return res + " and " + sec + " seconds";
                }
            }
        }
        
        return res;
    }
    
    private static int countZero(List<Long> list){
        
        int countZero = 0;
        for(Long num: list){
            if(num > 0){
                countZero += 1;
            }
        }
        
        return countZero;
    }