40
\$\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 the output would be

2016, 2015, ..., 1897, 1896

When run next year 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\$
  • 7
    \$\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\$ – trichoplax Aug 11 '16 at 11:31
  • 3
    \$\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\$ – trichoplax Aug 11 '16 at 11:33
  • 19
    \$\begingroup\$ Jeanne Louise Calment lived 122 years. \$\endgroup\$ – Zenadix Aug 11 '16 at 15:32
  • 10
    \$\begingroup\$ Lad, that was way too early of an accept. \$\endgroup\$ – Addison Crump Aug 11 '16 at 17:30
  • 6
    \$\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 '16 at 9:58

59 Answers 59

19
\$\begingroup\$

Pyke, 6 bytes

wC7m-

Try it here!

w     - 121
   m- - for i in range(^):
 C7   -  current_year-i

(After w, the codepoint for 153 is present but it isn't printable)

\$\endgroup\$
  • 1
    \$\begingroup\$ Wow, that was a quick checkmark. Maybe wait for a Jelly answer? \$\endgroup\$ – Adám Aug 11 '16 at 15:10
  • \$\begingroup\$ Whilst I agree it was a quick checkmark, I doubt even Jelly can do it in 5 \$\endgroup\$ – Blue Aug 11 '16 at 15:28
  • \$\begingroup\$ Why do you have a variable for 121? Just because it is a square number? But well done! \$\endgroup\$ – Denker Aug 11 '16 at 16:00
  • 3
    \$\begingroup\$ The w actually has an unprintable byte afterwards that makes it 121 \$\endgroup\$ – Blue Aug 11 '16 at 16:02
  • 1
    \$\begingroup\$ @LeakyNun it's configurable, whatever the default for the server it's running on. On pyke.catbus, I set it to Windows-1252 \$\endgroup\$ – Blue Aug 11 '16 at 16:10
0
\$\begingroup\$

VBA, 31 Bytes

Anonymous VBE immediate window function that takes no input and outputs to the VBE immediate window

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

Tcl, 76 bytes

set y [clock format [clock seconds] -format %Y]
time {puts $y;incr y -1} 121

Try it online!

\$\endgroup\$
3
\$\begingroup\$

JavaScript, 60 53 48 Bytes

f=x=>x>Date().split` `[3]-121&&(alert(x),f(x-1))

I used a recursive solution.

\$\endgroup\$
  • 1
    \$\begingroup\$ You can replace console.log with alert, and remove the last ;. \$\endgroup\$ – NoOneIsHere Aug 12 '16 at 22:09
  • 1
    \$\begingroup\$ Save 5 bytes with f=x=>x>Date().split" "[3]-121&&(alert(x),f(x-1)), replacing the quotation marks with backticks. \$\endgroup\$ – Shaggy Apr 20 '17 at 13:24
0
\$\begingroup\$

JavaScript, 75 71 bytes

Saved 4 bytes thanks to Zachary.

e="";for(y=(new Date).getFullYear(),i=y;i>=y-120;i--)e+=i+" ";alert(e)

Prints all years into a single alert.

I'm sure it could be improved.

History

75 bytes

var e="";for(y=(new Date).getFullYear(),i=y;i>=y-120;i--)e+=i+" ";alert(e);

(Every year was in a separate alert)

for(y=(new Date).getFullYear(),i=y;i>=y-120;i--)alert(i);
\$\endgroup\$
  • \$\begingroup\$ Welcome to PPCG! I'm not sure whether there exists a consensus on this, but I do not think that opening an alert for each element counts as outputting the list. \$\endgroup\$ – Laikoni Jul 11 '17 at 14:29
  • \$\begingroup\$ Hello. It is now in a single alert. (but with more bytes... :)) \$\endgroup\$ – maracuja-juice Jul 11 '17 at 15:31
  • \$\begingroup\$ Two things, I don't think you need the var before e, and I also don't think you need a semicolon after alert(e). \$\endgroup\$ – Zacharý Jul 11 '17 at 19:08
0
\$\begingroup\$

TI-Basic, 22 19 bytes

max(getDate:seq(I,I,Ans,Ans-120,~1
\$\endgroup\$
  • \$\begingroup\$ getDate:Ans(1 can be max(getDate. \$\endgroup\$ – lirtosiast Jul 11 '17 at 1:43
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\$
  • \$\begingroup\$ [...Array(+Date().substr(11,4)).keys()].slice(-120).reverse() is less! \$\endgroup\$ – eithed Aug 11 '16 at 14:21
  • \$\begingroup\$ 56 Bytes in ES5: s='';for(i=120;i--;)s+=new Date().getFullYear()-120+', ' \$\endgroup\$ – innovati Aug 11 '16 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 '16 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 '16 at 15:01
2
\$\begingroup\$

Fourier, 17 bytes

Non competing

I added the date feature (d) earlier today, so this answer is invalid. Despite this, I decided to add this for reference.

121(5d-io10ai^~i)

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

Try it online

\$\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\$

VBA 49 bytes

a=year(now):for i=a to a-120 step-1:msgbox i:next
\$\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\$
  • \$\begingroup\$ Well done this was my solution before i read any comments _=>[...Array(121)].map((v,i)=>Date().substr(11,4)-i) \$\endgroup\$ – James Harrington Nov 26 '17 at 20:32
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\$
0
\$\begingroup\$

QBIC, 33 bytes

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

05AB1E, 9 bytes

121FžgN-=

Try online

\$\endgroup\$
  • 2
    \$\begingroup\$ Welcome to the site! :) \$\endgroup\$ – DJMcMayhem Apr 3 '17 at 18:57
1
\$\begingroup\$

C#, 83 76 bytes

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

Python 3, 54

import time
print(*range(time.gmtime()[0],0,-1)[:121])
\$\endgroup\$
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\$
  • \$\begingroup\$ You can save yourself a couple with select*from b rather than select y from b \$\endgroup\$ – MickyT Aug 21 '16 at 20:57
2
\$\begingroup\$

LaTeX, 129 bytes

Or, if I'm allowed to skip the document class definition & setup, and just count the package import and for loop code: 79 bytes.

\documentclass{book}\usepackage{tikz}\begin{document}\foreach \n in {0,...,120}{\pgfmathint{\year\n}\pgfmathresult}\end{document}

Ungolfed:

\documentclass{book}
\usepackage{tikz}
\begin{document}
\foreach \n in {0,...,120}
{
\pgfmathint{\year-\n}\pgfmathresult}

\end{document} 

Output (w/ free page number :) ):

enter image description here

\$\endgroup\$
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
    \$\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 '16 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 '16 at 5:28
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\$
  • \$\begingroup\$ Forget good coding habits here. ;) Call that date() 121 times: while($i<121)echo date(Y)-$i++." ";. \$\endgroup\$ – manatwork Aug 13 '16 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 '16 at 12:35
  • \$\begingroup\$ @manatwork Oh cripes how did I miss that? :) \$\endgroup\$ – Alex Howansky Aug 13 '16 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\$ – Alex Howansky Aug 13 '16 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 '16 at 15:59
1
\$\begingroup\$

Coffeescript, 29 bytes

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

\$\endgroup\$
  • \$\begingroup\$ Welcome to Programming Puzzles & Code Golf! \$\endgroup\$ – Dennis Aug 12 '16 at 22:42
  • \$\begingroup\$ Thanks! I've been lurking for a while, finally had time to start \$\endgroup\$ – user3080953 Aug 12 '16 at 23:07
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\$
  • \$\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 '16 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 '16 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\$ – John Gowers Aug 15 '16 at 10:04
1
\$\begingroup\$

Javascript: 68 82 59 bytes

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

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

C, 87 85 76 74 65 bytes

main(int i,char**v){for(i=0;i<121;printf("%d,",atoi(v[1])-i++));}

Ungolfed:

main(int i,char**v){
    for(i = 0; i < 121; printf("%d,", atoi(v[1])-i++));
}

My first code golf - Any pointers would be well received. Would be nice if I could cut out the argc/argv junk, but I'm not that skilled a C programmer. Improvements welcome.

EDIT: The current year is obtained from the commandline - specifically by a group of automated, well trained drinking birds.

EDIT 2: 85 bytes Thanks to Easterly Irk (removed spaces around arg function parameters)

EDIT 3: 76 bytes Thanks to anatolyg for pointing out the obvious (removed verbose argc/argv param names)

EDIT 4: 74 bytes Thanks to Yay295 (char**v, changed year delimiter)

EDIT 5: 65 bytes thanks to Yay295 and matt (re-used the variable i, removed variable x, changed while to for loop, updated printf to include atoi() read and i++)

\$\endgroup\$
  • 1
    \$\begingroup\$ Can't you a space? int argc, char -> int argc,char? \$\endgroup\$ – Rɪᴋᴇʀ Aug 11 '16 at 16:01
  • 2
    \$\begingroup\$ Replace argc by c (or better replace int argc by just int) and argv by v. Also replace while by for. The latter change requires some experience in C (but may be fun regardless). \$\endgroup\$ – anatolyg Aug 11 '16 at 17:11
  • 1
    \$\begingroup\$ char*v[] -> char**v saves 1 byte. You can save another by delimiting your years with a space or a comma instead of a newline. \$\endgroup\$ – Yay295 Aug 11 '16 at 17:57
  • 1
    \$\begingroup\$ You can make it shorter. 71 bytes. main(int c,char**v){int i=0;while(i<120)printf("%d,",atoi(v[1])-i++);} \$\endgroup\$ – Matt Aug 11 '16 at 20:57
  • 2
    \$\begingroup\$ Even shorter. 65 bytes main(int i,char**v){for(i=0;i<121;printf("%d,",atoi(v[1])-i++));} \$\endgroup\$ – Matt Aug 12 '16 at 2:07
3
\$\begingroup\$

JavaScript, 55 52 50 49 bytes

for(a=d=s=Date().substr(11,4);d-->s-120;)a+=" "+d

+9 bytes to alert the data (not necessary if you run this in a console).

for(a=d=s=Date().substr(11,4);d-->s-120;)a+=" "+d;alert(a)

-4 bytes if the delimiter isn't necessary.

for(a=d=s=Date().substr(11,4);d-->s-120;)a+=d

for (a = d = s = Date().substr(11, 4); d --> s - 120; )
    a += " " + d;
console.log(a);

\$\endgroup\$
  • 1
    \$\begingroup\$ The []s appear to be unnecessary. \$\endgroup\$ – Neil Aug 11 '16 at 22:48
  • \$\begingroup\$ @Neil: Thanks. My original solution used an array, but I hadn't thought to remove it. \$\endgroup\$ – Yay295 Aug 12 '16 at 0:26
  • 1
    \$\begingroup\$ You can save one byte using a for instead of while on this: for(a=d=s=Date().substr(11,4);d-->s-120;)a+=" "+d! \$\endgroup\$ – Dom Hastings Aug 12 '16 at 7:58
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\$
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\$
  • 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 '16 at 9:24
  • \$\begingroup\$ This is definitely Python 2. \$\endgroup\$ – Mego Aug 12 '16 at 9:58
  • \$\begingroup\$ Thanks. How can I calculate the bytes? \$\endgroup\$ – Yousef. Python Aug 12 '16 at 11:02
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\$

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
    \$\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 '16 at 18:28
5
\$\begingroup\$

MATL, 10 bytes

1&Z'0:120-

Try it online!

1&Z'      % Push first component of "clock" vector, which is year
0:120     % Literal vector [0, 1, 2, ..., 120]
-         % Subtract element-wise. Implicitly display
\$\endgroup\$

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.