Home > database >  Given array of string contain X number of words return the same word for a given day
Given array of string contain X number of words return the same word for a given day

Time:08-26

Background

My Objective is I am basically trying to create an end point where a user calls the get_word() function and they get back a word. The requirement is every single user that calls the get_word() function should get back the same word on a given day. No words should repeat until we have cycled through all the words.

So for example let's assume we have a array of words.

words = ["lebron","kobe","jordan","durant","pipen"]

day 1 and users call the function get_word() they get back lebron

day 2 and users call the function get_word() they get back kobe

day 3 and users call the function get_word() they get back jordan

day 4 and users call the function get_word() they get back durant

day 5 and users call the function get_word() they get back pipen

day 6 and users call the function get_word() they get back lebron

day 7 and users call the function get_word() they get back kobe

The goal is to have list of 500 unique words and on a given day every user should get the same word back. I would also want the ability to add more words in the future.

Solution

One very naive solution i am able to think of is i can generate a dictionary with everyday of the year as a key and words as the value. The issue with this is we only have 365 days in a year so i can only store 365 words but not 500.

I appreciate any thoughts or feedback on how i can come up with a better solution here.

This problem is language agnostic hence the java and python tag.

CodePudding user response:

You could make use of the modulus here. Assuming the get_word() function accepts an integer day index, we can try:

words = ["lebron", "kobe", "jordan", "durant", "pipen"]

def get_words(day):
    return words[(day - 1) % len(words)]  # assuming day starts at 1

CodePudding user response:

You can use a combination of modulo and a static date to calculate a growing interval as an index that iterates through the list and loops back to start after it's exhausted.

startingDate = "2022-08-25"

words = ["lebron","kobe","jordan","durant","pipen"]

// get the number of days that have passed since the starting date
currentWordIndex = getDaysSince(startingDate) % count(words)

echo words[currentWordIndex] // "lebron" on day 0, "kobe" on day 1, etc.

In this example, the words should loop every 5 days.

As you rightly pointed out, using the day of the year limits you to 365 words. Calculating the number of days since the static date helps track an ever-growing interval.

This should work in perpetuity and even allows for the word list to grow, so long as you only ever add words to the end. If a word is added somewhere in the middle, "before" the current day's word, it will cause it to change. If you add words to the end, the current word will not change, but the loop back to the start will obviously take longer.

CodePudding user response:

You don't anything fancy apart from a List of String (or array if you wish, but it is advisable to favor Collections over arrays).

To retrieve a word for a particular day, you need to store a date, which would denote a starting point.

And then you need only to calculate the index of the word based on the number of dates between the current date and start date. For that you can make use of ChronoUnit.DAYS.between().

And to avoid exceeding the size of the list, we need to use modulo operator %.

List<String> words = List.of(
    "day1", "day2", "day3", "etc"
);
        
LocalDate start = LocalDate.of(2022, 1,1);
System.out.println(start);
        
int index = (int) ChronoUnit.DAYS.between(start, LocalDate.now());
String wordOfDay = words.get(index % words.size());

System.out.println(wordOfDay);

CodePudding user response:

You can start with an initial date and set that day as day 1. So if day 1 equals 2022.01.05 then 2022.01.07 would be day 3. You can use the modulo operator to get back to day 1 at day 500.

  • Related