Rebol3 Code Examplex


Five weekends

Count months that contain five weekends.

Rebol [
    title: "Rosetta code: Five weekends"
    file:  %Five_weekends.r3
    url:   https://rosettacode.org/wiki/Five_weekends
    note:  "Based on Hinjo's Red version"
]

five-weekends: function/with [
    "Show all months that have five full weekends."
    start [integer!] "Start year"
    end   [integer!] "End year"
][
    boring-years: total-fw-months: awesome-years: 0
    for year start end 1 [
        fw-months: clear []
        ;; A month has five weekends when its first day is a Saturday (weekday 7)
        foreach month long-months [
            if 1 = get-day year month 1 6 [
                ++ total-fw-months
                append fw-months month-names/:month
            ]
        ]
        prin  ajoin [as-yellow year ": "]
        print switch count: length? fw-months [
            0 [
                ++ boring-years
                as-red "Boring year!"
            ]
            1 [
                ajoin ["1 month:  " fw-months]
            ]
            2 [
                ++ awesome-years
                ajoin [as-green count " months: " ajoin/with fw-months ", "]
            ]
        ]  
    ]
    print-hline
    print ["Total five-weekend months:               " total-fw-months]
    print ["Total years with no five-weekend months: " boring-years ]
    print ["Total years with 2+ five-weekend months: " awesome-years]
][
    ;; Return the day-of-month for the Nth weekday in a given month,
    ;; or NONE if that occurrence falls outside the month.
    get-day: function [
        year     [integer!]
        month    [integer!]
        week     [integer!]
        weekday  [integer!]
    ][
        date: to-date reduce [1 month year] ; first-of-month
        ;; Rebol weekday is Mon=1 .. Sun=7; shift to Sun=1 .. Sat=7
        weekday-1st: (date/weekday + 1) % 7
        ;; Days from the 1st to the target weekday in the requested week
        offset: (week - 1) * 7 + (weekday - weekday-1st)
        target-date: date + offset
        ;; Return the day number only if it still falls within the same month or none
        if target-date/month = month [target-date/day]
    ]
    month-names: system/locale/months
    ;; Months with 31 days are the only ones that can hold five full weekends
    long-months: [1 3 5 7 8 10 12]
]

five-weekends 1900 2100

Output:

1900: Boring year!
1901: 1 month:          MARCH        
1902: 1 month:  August
1903: 1 month:           MAY         
1904: 2 months:        JANUARY       , July
1905: 1 month:  December
1906: Boring year!
1907: 1 month:          MARCH        
1908: 1 month:           MAY         
1909: 2 months:        JANUARY       , October
1910: 1 month:  July
1911: 1 month:  December
1912: 1 month:          MARCH        
1913: 1 month:  August
1914: 1 month:           MAY         
1915: 2 months:        JANUARY       , October
1916: 1 month:  December
1917: Boring year!
1918: 1 month:          MARCH        
1919: 1 month:  August
1920: 1 month:  October
1921: 1 month:  July
1922: 1 month:  December
1923: Boring year!
1924: 1 month:  August
1925: 1 month:           MAY         
1926: 2 months:        JANUARY       , October
1927: 1 month:  July
1928: Boring year!
1929: 1 month:          MARCH        
1930: 1 month:  August
1931: 1 month:           MAY         
1932: 2 months:        JANUARY       , July
1933: 1 month:  December
1934: Boring year!
1935: 1 month:          MARCH        
1936: 1 month:           MAY         
1937: 2 months:        JANUARY       , October
1938: 1 month:  July
1939: 1 month:  December
1940: 1 month:          MARCH        
1941: 1 month:  August
1942: 1 month:           MAY         
1943: 2 months:        JANUARY       , October
1944: 1 month:  December
1945: Boring year!
1946: 1 month:          MARCH        
1947: 1 month:  August
1948: 1 month:  October
1949: 1 month:  July
1950: 1 month:  December
1951: Boring year!
1952: 1 month:  August
1953: 1 month:           MAY         
1954: 2 months:        JANUARY       , October
1955: 1 month:  July
1956: Boring year!
1957: 1 month:          MARCH        
1958: 1 month:  August
1959: 1 month:           MAY         
1960: 2 months:        JANUARY       , July
1961: 1 month:  December
1962: Boring year!
1963: 1 month:          MARCH        
1964: 1 month:           MAY         
1965: 2 months:        JANUARY       , October
1966: 1 month:  July
1967: 1 month:  December
1968: 1 month:          MARCH        
1969: 1 month:  August
1970: 1 month:           MAY         
1971: 2 months:        JANUARY       , October
1972: 1 month:  December
1973: Boring year!
1974: 1 month:          MARCH        
1975: 1 month:  August
1976: 1 month:  October
1977: 1 month:  July
1978: 1 month:  December
1979: Boring year!
1980: 1 month:  August
1981: 1 month:           MAY         
1982: 2 months:        JANUARY       , October
1983: 1 month:  July
1984: Boring year!
1985: 1 month:          MARCH        
1986: 1 month:  August
1987: 1 month:           MAY         
1988: 2 months:        JANUARY       , July
1989: 1 month:  December
1990: Boring year!
1991: 1 month:          MARCH        
1992: 1 month:           MAY         
1993: 2 months:        JANUARY       , October
1994: 1 month:  July
1995: 1 month:  December
1996: 1 month:          MARCH        
1997: 1 month:  August
1998: 1 month:           MAY         
1999: 2 months:        JANUARY       , October
2000: 1 month:  December
2001: Boring year!
2002: 1 month:          MARCH        
2003: 1 month:  August
2004: 1 month:  October
2005: 1 month:  July
2006: 1 month:  December
2007: Boring year!
2008: 1 month:  August
2009: 1 month:           MAY         
2010: 2 months:        JANUARY       , October
2011: 1 month:  July
2012: Boring year!
2013: 1 month:          MARCH        
2014: 1 month:  August
2015: 1 month:           MAY         
2016: 2 months:        JANUARY       , July
2017: 1 month:  December
2018: Boring year!
2019: 1 month:          MARCH        
2020: 1 month:           MAY         
2021: 2 months:        JANUARY       , October
2022: 1 month:  July
2023: 1 month:  December
2024: 1 month:          MARCH        
2025: 1 month:  August
2026: 1 month:           MAY         
2027: 2 months:        JANUARY       , October
2028: 1 month:  December
2029: Boring year!
2030: 1 month:          MARCH        
2031: 1 month:  August
2032: 1 month:  October
2033: 1 month:  July
2034: 1 month:  December
2035: Boring year!
2036: 1 month:  August
2037: 1 month:           MAY         
2038: 2 months:        JANUARY       , October
2039: 1 month:  July
2040: Boring year!
2041: 1 month:          MARCH        
2042: 1 month:  August
2043: 1 month:           MAY         
2044: 2 months:        JANUARY       , July
2045: 1 month:  December
2046: Boring year!
2047: 1 month:          MARCH        
2048: 1 month:           MAY         
2049: 2 months:        JANUARY       , October
2050: 1 month:  July
2051: 1 month:  December
2052: 1 month:          MARCH        
2053: 1 month:  August
2054: 1 month:           MAY         
2055: 2 months:        JANUARY       , October
2056: 1 month:  December
2057: Boring year!
2058: 1 month:          MARCH        
2059: 1 month:  August
2060: 1 month:  October
2061: 1 month:  July
2062: 1 month:  December
2063: Boring year!
2064: 1 month:  August
2065: 1 month:           MAY         
2066: 2 months:        JANUARY       , October
2067: 1 month:  July
2068: Boring year!
2069: 1 month:          MARCH        
2070: 1 month:  August
2071: 1 month:           MAY         
2072: 2 months:        JANUARY       , July
2073: 1 month:  December
2074: Boring year!
2075: 1 month:          MARCH        
2076: 1 month:           MAY         
2077: 2 months:        JANUARY       , October
2078: 1 month:  July
2079: 1 month:  December
2080: 1 month:          MARCH        
2081: 1 month:  August
2082: 1 month:           MAY         
2083: 2 months:        JANUARY       , October
2084: 1 month:  December
2085: Boring year!
2086: 1 month:          MARCH        
2087: 1 month:  August
2088: 1 month:  October
2089: 1 month:  July
2090: 1 month:  December
2091: Boring year!
2092: 1 month:  August
2093: 1 month:           MAY         
2094: 2 months:        JANUARY       , October
2095: 1 month:  July
2096: Boring year!
2097: 1 month:          MARCH        
2098: 1 month:  August
2099: 1 month:           MAY         
2100: 2 months:        JANUARY       , October
------------------------------------------------------------------------------------------------------------
Total five-weekend months:                201
Total years with no five-weekend months:  29
Total years with 2+ five-weekend months:  29