Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Licence CC BY-NC-ND Thierry Parmentelat & Arnaud Legout

random dates

étape 1: générer une date

Help on function generate_random_date in module randomdate:

generate_random_date(start='01/01/2024', end='15/06/2024')
    generate a random date with a uniform distribution
    between two given dates, inclusive
    all dates use format dd/mm/yyyy

    Parameters:
        start: str - start date
        end: str - end date
    Returns: str - generated date
    Examples:
        generate_random_date()
            -> "30/04/2024"

'12/06/2024'
'06/12/2020'

indices

ne même pas essayer

bien sûr, si vous essayez de générer les trois morceaux indépendamment

le module random

1858

le module datetime

pour représenter :

voici quelques-uns des traits qu’on va utiliser

datetime.datetime(2017, 2, 15, 0, 0)

ici le deuxième paramètre représente le format utilisé pour afficher les dates, voir la liste complète ici https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior

datetime.datetime(2017, 2, 17, 0, 0)
'17 02 2017'
'2017/02/17'

solution


étape 2: écrire n échantillons dans un fichier

mêmes modalités, récrivez la fonction suivante

Help on function write_random_data in module randomdate:

write_random_data(output: TextIO, nb_lines=1000)
    Writes into the *output* object <nb_lines> lines

    Each line will contain, separated by spaces:
    * a line number
    * a random date (same format)
    * a random string : containing lowercase letters,
      with a length itself a random number between 3 and 9.

    Parameters:
        output: TextIO
            the opened file - typically the result of
            open(..., 'w'); it is NOT a filename !
        nb_lines: int
            the number of lines to be generated

1 07/02/2024 ejf
2 15/01/2024 dzbbdyds
3 08/04/2024 mfyuih
4 18/03/2024 jkimxsv
5 31/05/2024 rxppva

indices

solution

étape 3: relire le fichier et le trier

mêmes modalités, récrivez la fonction suivante

Help on function sort_data in module randomdate:

sort_data(input_filename, output_filename)
    Reads the input file, sorts them by date,
    and stores the result in the output file

    NOTE that as opposed to write_random_data, the
    parameters this time are FILENAMES - i.e. strings

    Parameters:
        input_filename: str
          the name of the input file
        output_filename: str
          the name of the output file

2 15/01/2024 dzbbdyds
1 07/02/2024 ejf
4 18/03/2024 jkimxsv
3 08/04/2024 mfyuih
5 31/05/2024 rxppva

indices

solution

variantes

indépendantes les unes des autres: