History homework helper
Jelly, 17 16 bytes
DUµn/TṪṁ@Ṫ,j”-FṚ
A full program taking a list of years from, to
and printing the result.
Try it online! or see the test suite.
How?
DUµn/TṪṁ@Ṫ,j”-FṚ - Main link: list of years [from, to] e.g [1833,1871]
D - convert to decimals [[1,8,3,3],[1,8,7,1]]
U - upend (to cater for differing lengths) [[3,3,8,1],[1,7,8,1]]
µ - monadic chain separation, call that V
/ - reduce V with:
n - not equal? [1,1,0,0]
T - truthy indices [1, 2]
Ṫ - tail 2
Ṫ - tail V (pop from & modify V) [1,7,8,1]
ṁ@ - mould (swap @rguments) V like that length [1,7]
, - pair that with (the modified) V [[1,7],[[3,3,8,1]]
”- - literal '-' character
j - join [1,7,'-',[3,3,8,1]]
F - flatten [1,7,'-',3,3,8,1]
Ṛ - reverse [1,8,3,3,'-',7,1]
- implicit print 1833-71
Javascript ES6, 59 57 chars
(x,y)=>(x+'-'+y).replace(x*10>y?/^((.*).*-)\2/:/()/,"$1")
Test:
f=(x,y)=>(x+'-'+y).replace(x*10>y?/^((.*).*-)\2/:/()/,"$1")
console.log(`1505, 1516 -> 1505-16
1989, 1991 -> 1989-91
1914, 1918 -> 1914-8
1833, 1871 -> 1833-71
1000, 2000 -> 1000-2000
1776, 2017 -> 1776-2017
2016, 2016 -> 2016-
1234567890, 1234567891 -> 1234567890-1
600, 1600 -> 600-1600
1235, 1424 -> 1235-424`.split`
`.map(t => t.match(/(\d+), (\d+) -> (.*)/)).every(([m,x,y,key]) => f(x,y)===key || console.log(x,y,key,f(x,y))))
console.log(f(600,6000))