47
\$\begingroup\$

The Challenge

Output a list of years that starts with the current year and ends 120 years ago. The birth year of every living human would be included in this list.

Details

The list should be in descending order.

Every built-in function to manipulate arrays and/or lists is allowed.

Shortest code in bytes wins.

When run this year 2016, the output would be

2016, 2015, ..., 1897, 1896

When run next year 2017, the output would be

2017, 2016, ..., 1898, 1897

Etc.

Update

  • Some have asked about the format of the list. As most have guessed, it doesn't matter. Insert any separator between the numbers. Intuitively most inserted a comma or space or both, newline or output an array.
  • Quasi superhumans like Jeanne Calment are an exception to the rule stated in my question.
\$\endgroup\$
12
  • 8
    \$\begingroup\$ I recommend the sandbox for getting feedback before posting a challenge (not just for your first challenge - most of us use it for every challenge). \$\endgroup\$ Aug 11, 2016 at 11:31
  • 4
    \$\begingroup\$ I don't see anything wrong with this challenge. One thing that might be worth specifying is whether the output should always start with 2016, or with the year in which it is run (will it start with 2017 if run next year?). This will affect whether it is a fixed output challenge, or needs to access the current date. \$\endgroup\$ Aug 11, 2016 at 11:33
  • 20
    \$\begingroup\$ Jeanne Louise Calment lived 122 years. \$\endgroup\$
    – Zenadix
    Aug 11, 2016 at 15:32
  • 11
    \$\begingroup\$ Lad, that was way too early of an accept. \$\endgroup\$ Aug 11, 2016 at 17:30
  • 7
    \$\begingroup\$ Is the challenge: "Print all the numbers from y - 120 to y" or "print all the birth years of living people"? Because if someone born in 1896 is alive today, that doesn't mean that there are also still people from 1898 around. \$\endgroup\$
    – CompuChip
    Aug 14, 2016 at 9:58

66 Answers 66

2
\$\begingroup\$

jq, 46 characters

(45 characters code + 1 character command line option)

now|strftime("%Y")|tonumber|range(.;.-121;-1)

Sample run:

bash-4.3$ jq -n 'now|strftime("%Y")|tonumber|range(.;.-121;-1)' | head
2016
2015
2014
2013
2012
2011
2010
2009
2008
2007

On-line test

\$\endgroup\$
2
\$\begingroup\$

Vim, 32 29 keystrokes

Thanks to @daniero for some help on saving some keystrokes and making the output a little neater.

"=strftime('%Y')<Enter>pqqYp<Ctrl-x>q118@q
  • <Enter> is Enter
  • <Ctrl-x> is Ctrl + X

Explanation:

"                                          # Put into the register {
 =strftime('%Y')                           #   This year in YYYY form
                <Enter>                    # }
                       p                   # Paste the register
                        qq                 # Record macro q {
                          Y                #   Yank (copy) the current line
                           p               #   Paste
                            <Ctrl-x>       #   Decrment number at cursor
                                    q      # }
                                     118@q # Do macro q 118 times

Output format:

Each number is on a separate line like below.

2016
2015
.
.
.
1898
1897
\$\endgroup\$
3
  • \$\begingroup\$ OP clearly asks for a future-proof version, so only the second one answers the question. Don't you need to punch the Enter key (commonly refered to as <CR> in Vim context) after :pu=strftime('%Y')? Also, you can skip some keystrokes by using Y with copies the whole line. In total: :pu=strftime('%Y')<CR>qqYp<Ctrl-x>q118@q - 30 keystrokes \$\endgroup\$
    – daniero
    Aug 12, 2016 at 18:35
  • \$\begingroup\$ Also, you can yank from the = register: "=strftime('%Y')<CR>pqqYp<Ctrl-x>q118@q - 29 keystrokes, and it gets rid of the first empty line \$\endgroup\$
    – daniero
    Aug 12, 2016 at 18:42
  • \$\begingroup\$ If you're allowed to use shell as well then you can save five more keystrokes with the date utility: :.!date +\%Y<CR>qqYp<C-x>q118@q \$\endgroup\$ Aug 15, 2016 at 10:04
2
\$\begingroup\$

PHP, 40 35 33 bytes

while($i<121)echo date(Y)-$i++._;

I'm just going to pretend that error reporting is always disabled for code golfing... :)

[Edit 1: Saved 5 bytes via manatwork]

[Edit 2: Saved 2 bytes via Titus]

\$\endgroup\$
8
  • \$\begingroup\$ Forget good coding habits here. ;) Call that date() 121 times: while($i<121)echo date(Y)-$i++." ";. \$\endgroup\$
    – manatwork
    Aug 13, 2016 at 11:33
  • \$\begingroup\$ @manatwork: save another two bytes by using the underscore (or any letter or 0) as separator: while($i<121)echo date(Y)-$i++,_; (33 bytes) \$\endgroup\$
    – Titus
    Aug 13, 2016 at 12:35
  • \$\begingroup\$ @manatwork Oh cripes how did I miss that? :) \$\endgroup\$ Aug 13, 2016 at 14:10
  • \$\begingroup\$ @Titus Nice. I used the "interpret an unquoted constant as a string" thing already once here, and still totally missed this one. \$\endgroup\$ Aug 13, 2016 at 14:18
  • \$\begingroup\$ It is always assumed that config is at default settings, so in PHP: E_NOTICE, E_DEPRECATED and E_STRICT are off -> You´re safe. (for different settings you have to either add the command or the string to add to config to your byte count) \$\endgroup\$
    – Titus
    Aug 13, 2016 at 15:59
2
\$\begingroup\$

Japt, 12 6 bytes

Saved 6 bytes thanks to @ETHproductions

#yonKi

Try it online!

Explanation:

#yonKi
#y          // # gets the char-code of y, which is 121
  o         // Create a range from [0...121]
   nKi      // At each item, perform .n(K.i()), which subtracts each item from Ki (Current year)


     
\$\endgroup\$
2
\$\begingroup\$

Fourier, 17 bytes

121(5d-io10ai^~i)

Since no output format is specified, each year is separated by a newline:

Try it online

\$\endgroup\$
2
\$\begingroup\$

Minim, 49 Bytes

Program is run as follows:

C:\Users\chris> minim -f golf.min

Golfed:

[]="xyear".\<1.\>[1].#<[1]--i.$<32._^!--[0].C-=4.

With whitespace and comments:

[] = "xyear".  ; Insert the string 'xyear' into memory from index 0 \
                 The 'x' character corresponds to the value '120'
\< 1.          ; Insert 1 as a system call argument \
                 This corresponds to the index in memory of the \
                 function name to call, 'year'
\> [1].        ; Call the function and insert the result at memory index 1
    #< [1]--i. ; Print memory index 1 as an integer, post-decremented
    $< 32.     ; Print a space
    _^ !--[0]. ; Skip the next statement if memory index 0, pre-decremented, is 0 
C -= 4.        ; Subtract 4 from the program counter

GitHub Repository

\$\endgroup\$
1
\$\begingroup\$

VBA, 54 bytes

In the immediate pane:

For i=0 To 120:?Year(Date)-i &IIf(i=120,"",", ");:Next

In an actual Sub procedure (the VBE adds whitespace and changes ? to Print, but the code is per language specs without the whitespace and using the ? shorthand nonetheless):

Sub A()
For i = 0 To 120: Print Year(Date) - i & IIf(i = 120, "", ", ");: Next
End Sub

That's 88 characters per Notepad++, with i being an undeclared, implicit Variant local variable.

Both produce the output exactly as specified in the question, comma-separated and with a space between each year:

2016, 2015, 2014, 2013, 2012, 2011, 2010, 2009, 2008, 2007, 2006, 2005, 2004, 2003, 2002, 2001, 2000, 1999, 1998, 1997, 1996, 1995, 1994, 1993, 1992, 1991, 1990, 1989, 1988, 1987, 1986, 1985, 1984, 1983, 1982, 1981, 1980, 1979, 1978, 1977, 1976, 1975, 1974, 1973, 1972, 1971, 1970, 1969, 1968, 1967, 1966, 1965, 1964, 1963, 1962, 1961, 1960, 1959, 1958, 1957, 1956, 1955, 1954, 1953, 1952, 1951, 1950, 1949, 1948, 1947, 1946, 1945, 1944, 1943, 1942, 1941, 1940, 1939, 1938, 1937, 1936, 1935, 1934, 1933, 1932, 1931, 1930, 1929, 1928, 1927, 1926, 1925, 1924, 1923, 1922, 1921, 1920, 1919, 1918, 1917, 1916, 1915, 1914, 1913, 1912, 1911, 1910, 1909, 1908, 1907, 1906, 1905, 1904, 1903, 1902, 1901, 1900, 1899, 1898, 1897, 1896

If the commas aren't a requirement (as some other answers seem to presume), then the IIf part can be dropped, cutting the immediate pane code down to 33 bytes:

For i=0 To 120:?Year(Date)-i:Next
\$\endgroup\$
1
\$\begingroup\$

Python 2, 67 bytes

from datetime import*;for i in range(121):print date.today().year-i

Thanks to Sp3000 for removing one byte

\$\endgroup\$
1
  • \$\begingroup\$ For future reference: In most situations, __import__ isn't really needed, e.g. here you can do from datetime import* and print date.today().year-i instead for -1 byte. \$\endgroup\$
    – Sp3000
    Aug 11, 2016 at 15:01
1
\$\begingroup\$

ListSharp, 71 bytes

NUMB a=<c#DateTime.Now.Yearc#>
[FOREACH NUMB IN a TO a-120 AS y]
SHOW=y

Uses embedded c# code, new feature!!

\$\endgroup\$
1
\$\begingroup\$

Nim, 60 bytes

import times
for a in 0..120:echo getTime().getGmTime.year-a

Outputs each year on a new line. We use the getTime to get the current UNIX time, then convert it to UTC with getGmTime, get the year minus the counter variable, and echo it.

\$\endgroup\$
1
\$\begingroup\$

Batch, 67 bytes

@set/ay=%date:~-4%,z=y-120
@for /l %%i in (%y%,-1,%z%)do @echo %%i
\$\endgroup\$
1
\$\begingroup\$

Haskell 125 bytes

The imports take up a large part of the byte count

import Data.Time.Clock
import Data.Time.Calendar
main=fmap((\(x,_,_)->[x-120..x]).toGregorian.utctDay)getCurrentTime>>=print
\$\endgroup\$
1
  • 1
    \$\begingroup\$ I'm afraid the list is in the wrong order, the spec says current year first. Howevery we can save a few bytes: a) import Data.Time should be enough for all needed functions. b) use the infix version of fmap, i.e. <$>. c) do x<-toGregorain ... ;print[x-120..x] is shorter than the lambda. d) You can extract the year from getZonedTime via read.take 4.show<$>getZonedTime. All in all, including correct order: import Data.Time;do y<-read.take 4.show<$>getZonedTime;print[y,y-1..y-120]. \$\endgroup\$
    – nimi
    Aug 12, 2016 at 18:28
1
\$\begingroup\$

C# - DotNet core - 133 bytes

Golfed

class Program{static void Main(){int x=0,y=System.DateTime.Now.Year;while(x<121){System.Console.Write($"{y-x++}"+(x<121?", ":""));}}}

Ungolfed

class Program
{
    static void Main()
    {
        int x=0, y=System.DateTime.Now.Year;

        while(x<121)
        {
            System.Console.Write($"{y-x++}" + (x < 121 ? ", " : ""));
        }
    }
}

Output:

enter image description here

I'm sure this can be improved. I don't particularly like the if statement to display or hide the trailing comma.

\$\endgroup\$
1
\$\begingroup\$

Javascript: 68 82 59 bytes

i=121;while(i--){x[i]=Date().substr(11,4)-i;}console.log(x)

\$\endgroup\$
2
  • \$\begingroup\$ Will this give a proper output when run in 2017? \$\endgroup\$
    – Leibrug
    Aug 12, 2016 at 13:21
  • \$\begingroup\$ yes, now it will, oops :-) \$\endgroup\$ Aug 12, 2016 at 14:01
1
\$\begingroup\$

Coffeescript, 29 bytes

->a=Date()[11..14];[a..a-120]

\$\endgroup\$
2
  • \$\begingroup\$ Welcome to Programming Puzzles & Code Golf! \$\endgroup\$
    – Dennis
    Aug 12, 2016 at 22:42
  • \$\begingroup\$ Thanks! I've been lurking for a while, finally had time to start \$\endgroup\$ Aug 12, 2016 at 23:07
1
\$\begingroup\$

Ruby, 47 40 39 bytes

p [*0..Time.new.year].last(121).reverse

Thanks to @Value Ink for 7 bytes!

Ideone link: https://ideone.com/yRovUl

\$\endgroup\$
2
  • 2
    \$\begingroup\$ Some people are submitting programs where the separator is newline, so you can take out the joining operation and just puts it. Or use p *<the rest> because they're all integers \$\endgroup\$
    – Value Ink
    Aug 12, 2016 at 19:31
  • \$\begingroup\$ Thank you @Value Ink. Since outputting an array is allowed, I used plain p instead p *<the rest> \$\endgroup\$
    – Leibrug
    Aug 16, 2016 at 5:28
1
\$\begingroup\$

SQLite, 82 80 bytes

with b(y)as(select strftime('%Y')union select y-1 from b limit 121)select*from b

SQLFiddle

(For ANSI SQL, replace the strftime() with extract(year from current_date).)

(2 bytes saved thanks to @MickyT)

\$\endgroup\$
1
  • \$\begingroup\$ You can save yourself a couple with select*from b rather than select y from b \$\endgroup\$
    – MickyT
    Aug 21, 2016 at 20:57
1
\$\begingroup\$

Python 3, 54

import time
print(*range(time.gmtime()[0],0,-1)[:121])
\$\endgroup\$
1
\$\begingroup\$

C#, 83 76 bytes

n=>{for(n=0;n<121;)System.Console.Write(System.DateTime.Now.Year-n+++" ");};
\$\endgroup\$
1
\$\begingroup\$

JavaScript (ES6), 66 54 51 bytes

My first foray into code golf so I'm open to suggestions for improvements.

The following will output an array of the required years.

f=

_=>[...Array(121)].map((x,y)=>Date().split` `[3]-y)

console.log(f());


History

54 bytes

_=>Array(121).fill(Date().split` `[3]).map((x,y)=>x-y)

66 bytes

(y=[Date().split` `[3]],x=121)=>{while(x--)y[x]=(y[0]-x);return y}
\$\endgroup\$
1
  • \$\begingroup\$ Well done this was my solution before i read any comments _=>[...Array(121)].map((v,i)=>Date().substr(11,4)-i) \$\endgroup\$ Nov 26, 2017 at 20:32
1
\$\begingroup\$

JavaScript (ES6), 65 bytes

[...Array(1+- -Date().substr(11,4)).keys()].slice(-121).reverse()

You're welcome to improve and shorten it...

Thanks to @Yay295 for the fix. I was 1 year off.

\$\endgroup\$
4
  • \$\begingroup\$ [...Array(+Date().substr(11,4)).keys()].slice(-120).reverse() is less! \$\endgroup\$
    – eithed
    Aug 11, 2016 at 14:21
  • \$\begingroup\$ 56 Bytes in ES5: s='';for(i=120;i--;)s+=new Date().getFullYear()-120+', ' \$\endgroup\$
    – innovati
    Aug 11, 2016 at 14:40
  • \$\begingroup\$ @innovati: That prints the same year over and over. If you replace the last '120' with 'i' as I expect you meant, it's still in the wrong order. \$\endgroup\$
    – Yay295
    Aug 11, 2016 at 14:46
  • 2
    \$\begingroup\$ @ChristiaanWesterbeek,@eithedog: You're both off by 1. Try [...Array(1+- -Date().substr(11,4)).keys()].slice(-121).reverse() instead. \$\endgroup\$
    – Yay295
    Aug 11, 2016 at 15:01
1
\$\begingroup\$

Vyxal, 11 9 bytes

kðt:122-r

Try it Online!

-2 thx to @lyxal

\$\endgroup\$
1
\$\begingroup\$

K (ngn/k), 12 bytes (Non-competitive)

{x,x-1+!120}

Try it online!

K lacks any date-related functions, so this one is non-competitive.

Explanations:

{x,x-1+!120}  Main function. Takes x as input
       !120   Generate a range from 0 to 120 (exclusive)
     1+       +1 to each number (1..120)
   x-         x subtract to each number (x-1..x-120)
 x,           Concat x itself as the first number of the array (x,x-1..x-120)
\$\endgroup\$
1
\$\begingroup\$

Julia 1.0, 46 45 bytes

using Dates
y=year(now())
show([y:-1:y-120;])

Try it online!

  • -1 byte thanks to @amelies: replace print with show
\$\endgroup\$
1
  • 1
    \$\begingroup\$ show instead of print to save 1 byte \$\endgroup\$
    – amelies
    Nov 17 at 15:44
0
\$\begingroup\$

Java 75 74 bytes

public void possible(){
  for(int i=0;i<120;){
    System.out.println(new Date().getYear()-i++);
  }
}

Golfed :

void p(){for(int i=0;i<120;)System.out.println(new Date().getYear()-i++);}

1 byte off. Thanks to @LeakyNun

\$\endgroup\$
3
  • \$\begingroup\$ void p(){for(int i=0;i<120;)System.out.println(new Date().getYear()-i++);} \$\endgroup\$
    – Leaky Nun
    Aug 11, 2016 at 14:08
  • 1
    \$\begingroup\$ What version of Java is this? As far as I'm aware, getYear() gives you the year since 1900. That means that the current output would be [116, -3]. You'd have to add +1900 (5 bytes) in order to get the correct output. Also, Date requires an import (java.util.Date), which would add to the byte count too. \$\endgroup\$
    – MH.
    Aug 11, 2016 at 19:41
  • \$\begingroup\$ @MH. is right, this code will not compile without the imports so you need to either new java.util.Date() or add the import. Also, why don't you shorten it further by replacing the method with a lambda expression? \$\endgroup\$
    – Shaun Wild
    Aug 15, 2016 at 9:26
0
\$\begingroup\$

Python 2, 113 bytes

import datetime
theYear = datetime.datetime.now()
for i in range(theYear.year, theYear.year-121, -1):
   print i,','
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Welcome to Code Golf! You need to list the language you're using, and because this is a code-golf challenge, you also need the bytecount. \$\endgroup\$
    – Value Ink
    Aug 12, 2016 at 9:24
  • \$\begingroup\$ This is definitely Python 2. \$\endgroup\$
    – user45941
    Aug 12, 2016 at 9:58
  • \$\begingroup\$ Thanks. How can I calculate the bytes? \$\endgroup\$ Aug 12, 2016 at 11:02
0
\$\begingroup\$

QBIC, 33 bytes

A=right$(_D,4)┘[!A!,!A!-120,-1|?a
\$\endgroup\$
0
\$\begingroup\$

VBA 49 bytes

a=year(now):for i=a to a-120 step-1:msgbox i:next
\$\endgroup\$
0
\$\begingroup\$

Groovy, 57 bytes

d=new Date().getYear()+1899;(d​..d-120).each{println it​}

Explanation

d=new Date().getYear()                               //returns how many years have passed since 1900
                      +1899;                         //adding 1900 will give us the current year, but we want the program to start one year before, so we do +1900-1=1899
                            (d..d-120)               //a range from the current year -1 to 120 years before that
                                      .each{print it}// for each element in the range, print it.

Output

2016
2015
2014
...
1898
1897
1896

Because of the 1899 trick, it starts with 2016 instead of 2017

Tested on the Groovy Web Console

\$\endgroup\$
0
\$\begingroup\$

TI-Basic, 22 19 bytes

max(getDate:seq(I,I,Ans,Ans-120,~1
\$\endgroup\$
1
  • \$\begingroup\$ getDate:Ans(1 can be max(getDate. \$\endgroup\$
    – lirtosiast
    Jul 11, 2017 at 1:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.