- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find number of times every day occurs in a Year in Python
When it is required to find the number of times every day of the week occurs in a year, a list is defined, and it is iterated over, and is count is incremented respectively.
Below is a demonstration of the same −
Example
import math def num_of_occurrence( n, firstday): my_days = [ "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday","Sunday" ] my_count= [4 for i in range(0,7)] my_position = -1 for i in range(0,7): if (first_day == my_days[i]): my_position = i break inc = n - 28 for i in range( my_position, my_position + inc): if (i > 6): my_count[i % 7] = 5 else: my_count[i] = 5 for i in range(0,7): print (my_days[i] , " " , my_count[i]) num = 31 first_day = "Thursday" num_of_occurrence(num, first_day)
Output
Monday 4 Tuesday 4 Wednesday 4 Thursday 5 Friday 5 Saturday 5 Sunday 4
Explanation
The required packages are imported.
A method named ‘num_of_occurence’ is defined that takes a number and a day of week as parameter.
A list with number of days of week is defined.
Another list with numbers in the range of 0 and 7 is defined.
The range is iterated over, and if the day passed as parameter matches a day from the list, its position is set.
Another iteration is used and the count of every day of the week is incremented depending on the day passed as first day to the method.
The method is called by passing the respective parameters.
The output is displayed on the console.
- Related Articles
- Day of the Year in Python
- Python Program to Search the Number of Times a Particular Number Occurs in a List
- Find the number of times a value of an object property occurs in an array with JavaScript?
- Convert day of year to day of month in Java
- Python - Check if k occurs atleast n times in a list
- Which country publishes the most number of books every year?
- Finding day of week from date (day, month, year) in JavaScript
- How to find the day of the year from dates in R?
- Python Pandas - Create a PeriodIndex and get the day of the year
- Select last day of current year in MySQL?
- Write a program in Python to print the day of the year in a given date series
- How to count the number of times a value occurs in a column of an R data frame?
- Write a function that counts the number of times a given int occurs in a Linked List in C++
- Find a number which give minimum sum when XOR with every number of array of integer in Python
- What does saline water contain?On which day World Water Day is celebrated every year?Name one cold ocean current?
