Question

Kotlin Language : How many Sundays fell on the first of the month during the twentieth...

Kotlin Language : How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?

Homework Answers

Answer #1
Here is your solution:
fun sundaysInTwentCentury(): Int {
    fun daysInMon(month: Int, year: Int) = when(month) {
        1,3,5,7,8,10,12 -> 31
        4,6,9,11 -> 30
        2 -> when {
            year % 400 == 0 -> 29
            year % 100 == 0 -> 28
            year % 4 == 0 -> 29
            else -> 28
        }
        else -> throw Exception("Invalid Month ${month}")
    }
    fun daysInYears(year: Int): Int {
        var days = 0
        for(month in 1..12) {
            days += daysInMon(month, year)
        }
        return days
    }
    fun dayOfWeekAfter(currentDow: Int, numberOfDays: Int) = ((currentDow-1) + numberOfDays) % 7 + 1
    var weekDayOnFirstOfMonth = dayOfWeekAfter(2, daysInYears(1900))
    var totalSundays = 0
    for (year in 1901..2000) {
        for(month in 1..12) {
            if (weekDayOnFirstOfMonth == 1) totalSundays++
            weekDayOnFirstOfMonth = dayOfWeekAfter(weekDayOnFirstOfMonth, daysInMon(month,year))
        }
    }
    return totalSundays
}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
During the second half of the twentieth century, many poor countries invested heavily in education and...
During the second half of the twentieth century, many poor countries invested heavily in education and dramatically increasing their enrollment rates. Despite this effort, most of them remain poor. How is this possible?
How could have Enlightenment ideals influenced the events that transpired in Europe during the twentieth century?
How could have Enlightenment ideals influenced the events that transpired in Europe during the twentieth century?
Exercise 3-8 Marigold Corp. opened a dental practice on January 1, 2019. During the first month...
Exercise 3-8 Marigold Corp. opened a dental practice on January 1, 2019. During the first month of operations, the following transactions occurred. 1. Performed services for patients who had dental plan insurance. On January 31, $840 of such services were performed but not yet recorded. 2. Utility expenses incurred but not paid before January 31 totaled $600. 3. Purchased dental equipment on January 1 for $87,000, paying $30,000 in cash and signing a $57,000, 3-year note payable. The equipment depreciates...
How did railroads impact the development and growth of many cities and geographic regions during the...
How did railroads impact the development and growth of many cities and geographic regions during the 19th century? (including other countries, other than the U.S.) How did the motor carrier industry impact the development and growth of many cities and geographic regions during the 20th century and currently in the 21st century? (including other countries, other than the U.S.) What advantages do railroads have today vs. the motor carrier industry? What advantages does the motor carrier industry have today vs....
1]How many elements are in the list quizScores? 1)quizScores.length() 2)quizScores.length 3)len(quizScores) 4)quizScores.num_elements ------------------------------------------- 2)A list has...
1]How many elements are in the list quizScores? 1)quizScores.length() 2)quizScores.length 3)len(quizScores) 4)quizScores.num_elements ------------------------------------------- 2)A list has the number of days in the month. days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] How would I display the number of days in March? Group of answer choices a)days[3] b)days[2] c)days[4] d)days['March'] ---------------------------------- 3)A dictionary is created with the number of days in each month. days = {'Jan' : 31, 'Feb' : 28, 'Mar' : 31, 'Apr'...
The events listed below all took place during the month of Dec 2019. You are required...
The events listed below all took place during the month of Dec 2019. You are required to determine if journal entries are needed to record these events in the books of Beauty Hairdressers. Provide the complete journal entries or state if no journal entries are necessary. Write yours answers in the space provided. Note: Complete journals include the correct date, DR/CR and narration regarding the transaction. 1. On 1 Dec 2019 Beauty Hairdressers obtained a start-up loan from a bank...
Write a program (C language) that will read the number of a month and will print...
Write a program (C language) that will read the number of a month and will print the number of days in the month. Ignore leap years. Use 28 days for February. Have the program runs in a continuous loop, allowing the user to enter a month number, see that number of days, and repeat. Use month number = 0 to exit the loop and the program. Program must meet the following criteria: 1.Your name and the name of the program...
The transactions completed by Revere Courier Company during December, the first month of the fiscal year,...
The transactions completed by Revere Courier Company during December, the first month of the fiscal year, were as follows: Dec. 1. Issued Check No. 610 for December rent, $4,200. Dec. 2. Issued Invoice No. 940 to Clifford Co., $1,740 Dec. 3. Received check for $4,800 from Ryan Co. in payment of account. Dec. 5. Purchased a vehicle on account from Platinum Motors, $37,300. Dec. 6. Purchased office equipment on account from Austin Computer Co., $4,500. Dec. 6. Issued Invoice No....
The three accounts shown below appear in the general ledger of Cullumber Corp. during 2021. Equipment...
The three accounts shown below appear in the general ledger of Cullumber Corp. during 2021. Equipment Date Debit Credit Balance Jan. 1 Balance 163,200 July 31 Purchase of equipment 71,400 234,600 Nov. 10 Cost of equipment sold 50,000 184,600 Accumulated Depreciation—Equipment Date Debit Credit Balance Jan. 1 Balance 72,400 Nov. 10 Accumulated depreciation on equipment sold 30,600 41,800 Dec. 31 Depreciation for year 28,600 70,400 Retained Earnings Date Debit Credit Balance Jan. 1 Balance 107,100 Aug. 23 Dividends (cash) 14,300...
Absorption and Variable Costing Income Statements During the first month of operations ended July 31, YoSan...
Absorption and Variable Costing Income Statements During the first month of operations ended July 31, YoSan Inc. manufactured 9,400 flat panel televisions, of which 8,800 were sold. Operating data for the month are summarized as follows: Sales $1,144,000 Manufacturing costs:     Direct materials $573,400     Direct labor 169,200     Variable manufacturing cost 150,400     Fixed manufacturing cost 75,200 968,200 Selling and administrative expenses:     Variable $88,000     Fixed 40,500 128,500 1. Prepare an income statement based on the variable costing...