Prime Dates

Sort by

recency

|

81 Discussions

|

  • + 0 comments

    There's no code for JavaScript

  • + 0 comments

    Java:

    here is the sol:

    import java.util.; import java.lang.; import java.io.; import java.math.;

    public class Main {

    public static int month[];
    
    public static void main (String[] args) throws java.lang.Exception {
        Scanner in  = new Scanner(System.in);
    
        month = new int[15];
    
        String s = in.nextLine();
    
        StringTokenizer str = new StringTokenizer(s, "- ");
    
        int d1 = Integer.parseInt(str.nextToken());
        int m1 = Integer.parseInt(str.nextToken());
        int y1 = Integer.parseInt(str.nextToken());
        int d2 = Integer.parseInt(str.nextToken());
        int m2 = Integer.parseInt(str.nextToken());
        int y2 = Integer.parseInt(str.nextToken());
    
        int result = findPrimeDates(d1, m1, y1, d2, m2, y2);
        System.out.println(result);
    

    }

    public static void updateLeapYear(int year) {
        if(year % 400 == 0) {
            month[2] = 29;     //correction 1
        } else if(year % 100 == 0) {
            month[2] = 28;    //correction 2
        } else if(year % 4 == 0) {
            month[2] = 29;
        } else {
            month[2] = 28;
        }
    }
    
    public static void storeMonth() {
        month[1] = 31;
        month[2] = 28;
        month[3] = 31;
        month[4] = 30;
        month[5] = 31;
        month[6] = 30;
        month[7] = 31;
        month[8] = 31;
        month[9] = 30;
        month[10] = 31;
        month[11] = 30;
        month[12] = 31;
    }
    

    public static int findPrimeDates(int d1, int m1, int y1, int d2, int m2, int y2) { storeMonth();

        int result = 0;
    
        while(true) {
            int x = d1;
            x = x * 100 + m1;
            x = x * 10000 + y1;    //correction 3
            if(x % 4 == 0 || x % 7 == 0) {    //correction 4
                result = result + 1;
            }
            if(d1 == d2 && m1 == m2 && y1 == y2) {
                break;
            }
            updateLeapYear(y1);
            d1 = d1 + 1;
            if(d1 > month[m1]) {
                m1 = m1 + 1;
                d1 = 1;
                if(m1 > 12) {
                    y1 =  y1 + 1;
                    m1 = 1;         //correction 5
                }
            }
        }
        return result;
    }
    

    }

  • + 0 comments

    This is the solution for c#, i used a c++ to c# converter to convert the code from the one provided for c++ 11 and get the original code(with the mistakes) of the problem because there was no code provided to debug

    using System; using System.Linq;

    class Program { static int[] month = new int[15];

    static void UpdateLeapYear(int year)
    {
        if (year % 400 == 0) 
        {
            month[2] = 29; //three
        }
        else if (year % 100 == 0) 
        {
            month[2] = 28; //two
        }
        else if (year % 4 == 0 && year % 100 != 0) //four
        {
            month[2] = 29;
        }
        else
        {
            month[2] = 28;
        }
    }
    
    static void StoreMonth()
    {
        month[1] = 31;
        month[2] = 28;
        month[3] = 31;
        month[4] = 30;
        month[5] = 31;
        month[6] = 30;
        month[7] = 31;
        month[8] = 31;
        month[9] = 30;
        month[10] = 31;
        month[11] = 30;
        month[12] = 31;
    }
    
    static int FindLuckyDates(int d1, int m1, int y1, int d2, int m2, int y2)
    {
        StoreMonth();
    
        int result = 0;
    
        while (true)
        {
            int x = d1;
            x = x * 100 + m1;
            x = x * 10000 + y1; //five
            if (x % 4 == 0 || x % 7 == 0) //one
            {
                result++;
            }
            if (d1 == d2 && m1 == m2 && y1 == y2)
            {
                break;
            }
            UpdateLeapYear(y1);
            d1++;
            if (d1 > month[m1])
            {
                m1++;
                d1 = 1;
                if (m1 > 12)
                {
                    y1++;
                    m1 = 1;
                }
            }
        }
        return result;
    }
    
    static void Main()
    {
        string str = Console.ReadLine();
        int d1, m1, y1, d2, m2, y2;
        str = str.Replace('-', ' ');
        var parts = str.Split(' ').Select(int.Parse).ToArray();
        d1 = parts[0];
        m1 = parts[1];
        y1 = parts[2];
        d2 = parts[3];
        m2 = parts[4];
        y2 = parts[5];
    
        int result = FindLuckyDates(d1, m1, y1, d2, m2, y2);
        Console.WriteLine(result);
    }
    

    }

  • + 0 comments

    Here is HackerRank prime dates problem solution in Python, Java, C++, C and javascript

  • + 0 comments

    C# has an empty Main method, so impossible to complete without modifying more than 5 lines of code.