Code Golf Asked on October 27, 2021
Given a positive integer return the geologic era and period it was that many million years ago.
The input is a string or integer; the output is a string. Eras and periods are separated by slashes (/
). Input will always be between 0
and 542
inclusive
The possible outputs for eras and periods and their corresponding time periods (in millions of years) are:
Submissions will be scored by byte count. Lowest byte count wins.
314 -> Paleozoic/Carboniferous
0 -> Cenozoic/Quaternary
542 -> Paleozoic/Cambrian
100 -> Mesozoic/Cretaceous
67‹i’¦ì€¸zo¾ã/’™?¹4‹i’QuâìÒÇry’?ë¹₂Í‹i’ÖÌž¹’™?ë’æ§ož¹’™?}}ë¹253‹i’€á€Êzo¾ã/’™?¹145‹i’±ƒˆÆÁàÔºs’™?ë¹т·Ì‹i’JҢ턾ã’?ë’Æ×턾㒙?}}ë’槢äo¾ã/’™?¹3т*‹i’„œéên’™?ë¹₆T*‹i’®´€¬²¡Ôºs’™?ë¹420‹i’Ùç·‹’™?ë¹445‹i’¯ë„¹´€¤’™?ë¹486‹i’€—€·É²·‹’™?뒧袰’™?
i # if
# implicit input
67‹ # is less than 67
? # then output without trailing newline
’¦ì€¸zo¾ã/’™ # "Cenozoic/"
i # if
¹ # first item from input history
4‹ # is less than 4
? # then output without trailing newline
’QuâìÒÇry’ # "Quaternary"
ë i # elif
¹ # first item from input history
‹ # is less than
₂ # 26
Í # - 2 = 24
? # then output without trailing newline
’ÖÌž¹’™ # "Neogene"
ë # else
? # output without trailing newline
’æ§ož¹’™ # "Paleogene"
}} # end if statement (2x)
ë i # elif
¹ # first item from input history
253‹ # is less than 253
? # then output without trailing newline
’€á€Êzo¾ã/’™ # "Mesozoic/"
i # if
¹ # first item from input history
145‹ # is less than 145
? # then output without trailing newline
’±ƒˆÆÁàÔºs’™ # "Cretaceous"
ë i # elif
¹ # first item from input history
‹ # is less than
т # 100
· # * 2 = 200
Ì # + 2 = 202
? # then output without trailing newline
’JҢ턾㒠# "Jurassic"
ë # else
? # output without trailing newline
’Æ×턾㒙 # "Triassic"
}} # end if statement (2x)
ë # else
? # output without trailing newline
’槢äo¾ã/’™ # "Paleozoic"
i # if
¹ # first item from input history
‹ # is less than
3т* # 3 * 100 = 300
? # then output without trailing newline
’„œéên’™ # "Permian"
ë i # elif
¹ # first item from input history
‹ # is less than
₆ # 36
T* # * 10 = 360
?# then output without trailing newline
’®´€¬²¡Ôºs’™ # "Carboniferous"
ë i # elif
¹ # first item from input history
420‹ # is less than 420
? # then output without trailing newline
’Ùç·‹’™ # "Devonian"
ë i # elif
¹ # first item from input history
445‹ # is less than 445
? # then output without trailing newline
’¯ë„¹´€¤’™ # "Silurian"
ë i # elif
¹ # first item from input history
486‹ # is less than 486
? # then output without trailing newline
’€—€·É²·‹’™ # "Ordovician"
ë # else
?# output without trailing newline
’§è¢°’™ # "Cambrian"
# implicit end if statement (5x)
Answered by Makonede on October 27, 2021
->a{a<31?"Cenozoic/"+(a<4?"Quaternary":(a<24?"Neogene":(a<67?"Paleogene":""))):a<253?"Mesozoic/"+(a<146?"Cretaceous":(a<202?"Jurassic":(a<253?"Triassic":""))):a<543?"Paleozoic/"+(a<300===a ?"Permian":(a<360 ?"Carboniferous":(a<420?"Devonian":(a<445===a ?"Silurian":(a<486 ?"Ordovician": (a<543?"Cambrian":"")))))):""}
Answered by Razetime on October 27, 2021
Try it online!
For some reason it doesn't work quite right on TIO, try it using the IDE I wrote it in, Whitelips
Since the only conditionals in whitespace are jump-if-negative
and jump-if-zero
, I figure out if the inputed number minus the number for the era plus one is negative, then jump to the respective label and push the era onto the stack. I then jump into a loop that prints the entire stack. (loops until it reaches a -1
, which I push at the very start of the program.) I save repetive bytes by pushing "ogene" and then "Ne" or "Pale", "ozoic/" and then "Mes" or "Cen", etc.
Answered by nope on October 27, 2021
Lots of ternary operators is the shortest solution I've found. Then golfed for some redundant suffixes. Only twice the amount of bytes needed to spell out each era uncompressed.
$a=$argv[1];echo($a<67?Cen:($a<253?Mes:Pale)).'ozoic/'.($a<4?Quarternary:($a<24?Neogene:($a<67?Paleogene:($a<146?Cretaceous:($a<202?Jurassic:($a<253?Triassic:($a<300?Permian:($a<360?Carboniferous:($a<420?Devon:($a<445?Silur:($a<486?Ordovic:Cambr))).ian))))))));
Answered by YetiCGN on October 27, 2021
Port of Noodle9's Python answer.
method(y,("Pale "repeated(67).."Mes "repeated(186).."Pale "repeated(290))split at(y).."ozoic/"..("Quaternary "repeated(4).."Neogene "repeated(20).."Paleogene "repeated(43).."Cretaceous "repeated(79).."Jurassic "repeated(56).."Triassic "repeated(51).."Permian "repeated(47).."Carboniferous "repeated(60).."Devonian "repeated(60).."Silurian "repeated(25).."Ordovician "repeated(41).."Cambrian "repeated(57))split at(y))
Answered by user92069 on October 27, 2021
u/x
m/x{486}x*/"Cambr#/x{445}x*/"Ordov%#/x{420}x*/"Silur#/x{360}x*/"Devon#/x{300}x*/"Carbonifer&/x{253}x*/"Perm#/x{202}x*/'Tri,/x{146}x*/'Jur,/x{67}x*/'Cretace&/x{24}x*/- 0/x{4}x*/-N0/x*/-Quaternary/0/eogene/-/Cen!/,/ass%/'/Mes!/&/ous/#/ian/"/ e!/!/ozo%//%/ic/ /Pal/g
Basically just compression.
Answered by PkmnQ on October 27, 2021
“¥Þ+O83/<<ı)‘Ä>⁸Sµ:3»1;ị"“¥nq9r&t“¥Ḅr¶4ḍẏżæœġẓÆ$Ụ>¬ḞḂṂĊƁÄ`zṚƥṀȯ4N»Ḳ€¤j”/
A monadic Link accepting number in $[0,542]$ which yields a list of characters (or a full program which prints the result).
“...‘Ä>⁸Sµ:3»1;ị"“...“...»Ḳ€¤j”/ - Link: integer, N e.g. 100
“...‘ - list of code page indices [4,20,43,79,56,51,47,60,60,25,41]
Ä - cumulative sums [4,24,67,146,202,253,300,360,420,445,486]
>⁸ - greater than N? [0,0,0,1,1,1,1,1,1,1,1]
S - sum 8
µ - start a new monadic chain, call that X
:3 - integer divide (X) by 3 2
»1 - maximum of that and 1 2
; - concatenate with X [2,8]
¤ - nilad followed by link(s) as a nilad:
“...“...» - list of compressed strings ["Paleozoic Mesozoic Cenozoic","Ordovician Silurian Devonian Carboniferous Permian Triassic Jurassic Cretaceous Paleogene Neogene Quaternary Cambrian"]
Ḳ€ - split each at spaces [["Paleozoic","Mesozoic","Cenozoic"],["Ordovician","Silurian","Devonian","Carboniferous","Permian","Triassic","Jurassic","Cretaceous","Paleogene","Neogene","Quaternary","Cambrian"]
" - zip with:
ị - index into (modular) ["Mesozoic","Cretaceous"]
j”/ - join with a '/' "Mesozoic/Cretaceous"
Answered by Jonathan Allan on October 27, 2021
n->(n<67?"Cen":n<253?"Mes":"Pale")+"ozoic/"+"QuaternaryNeogenePaleogeneCretaceousJurassicTriassicPermianCarboniferousDevonianSilurianOrdovicianCambrian".split("(?=[A-Z])")[(n=java.util.Arrays.binarySearch("BÉüīŧƣƼǥȞ".toCharArray(),(char)n))<0?~n:n]
"u0003u0017u0042u0091u00c9u00fcu012bu0167u01a3u01bcu01e5u021e"
.Answered by Olivier Grégoire on October 27, 2021
&:"C"`#v_"neC">,,,"/ciozo",,,,,,:4`#v_"auQ",,,"yranret">,,,,,,,@
*54+65:< > ^"Mes"_v#!`*+3 >#># ^#
"leogene"<^"ale","P"< v_v#`*64:<> "enegoeN"^
,,,"Cre"< ^ ,,"Pa"_v#!`"C":< > ^^"taceous"
`*2"e":_^#`*+*5545:<v_v#
,"T"_v#!`*+*543+65:< >"J","cissaru" ^"riassic"
*"d"3:<v_v#!`
"rmian"< >:"x"3*`!#v_:"F"6*`#v_"D",v ^"Pe"
"ous",,,,,,"Carbon"< : ^"ifer"
"enovian" < ^
"brian","C"_v#`*"Q"6_v#`*"Y"5< ^"am"
"an",,,"Ord"< >"S","nairuli" ^"ovici"
This is basically a giant if/elsif/if
chain (or rather, two of them), one for the era, and one for the period.
Answered by Abigail on October 27, 2021
.•TćFh/>ytTOb•#櫕͇,®ǝ₁êË2•80в.¥I›©₆12ªèOè.•Hα^B·´‘›∞ÕΣHı[šyZÙΣpK5|´Ø·ëu"dw$мïjéåpºöÒ@’»©I›¼ä^и9Σ߃ôƶI<Ægª6XyrмÏ•#®Oè'/ý™
Try it online or verify all test cases.
Explanation:
.•TćFh/>ytTOb• # Push compressed string "ozoic cen pale mes"
# # Split it on spaces: ["ozoic","cen","pale","mes"]
ć # Extract head; pop and push remainder-list and first item separated
« # Append this head to each item in the list:
# ["cenozoic","paleozoic","mesozoic"]
•Í‡,®ǝ₁êË2• # Push compressed integer 3656627291841362083337
80в # Convert it to base-80 as list: [4,20,43,79,56,51,47,60,60,25,41,57]
.¥ # Undelta this list: [0,4,24,67,146,202,253,300,360,420,445,486,543]
I› # Check for each whether it's larger than the input-integer
# (1 if truthy; 0 if falsey)
© # Store this list of checks in variable `®` (without popping)
₆ # Push builtin 36
12ª # Convert it to a list of digits, and append 12: [3,6,12]
è # Index those into the checks (to get those for [67,253,543])
O # Sum those checks to get the amount of truthy values
è # Index it into the string-list (modulair 0-based,
# so 3 indexes into "cenozoic")
.•Hα^B·´‘›∞ÕΣHı[šyZÙΣpK5|´Ø·ëu"dw$мïjéåpºöÒ@’»©I›¼ä^и9Σ߃ôƶI<Ægª6XyrмÏ•
"# Push compressed string "quaternary cambrian ordevician silurian devonian carboniferous permian triassic jurassic cretaceous paleogene neogene"
# # Split it on spaces: ["quaternary","cambrian","ordevician","silurian","devonian","carboniferous","permian","triassic","jurassic","cretaceous","paleogene","neogene"]
® # Push the list of checks from variable `®`
Oè # Do the same as we did earlier
'/ý '# Join the two words on the stack with "/" delimiter
™ # And titlecase both words in this string
# (after which the result is output implicitly)
See this 05AB1E tip of mine (sections How to compress strings not part of the dictionary?, How to compress large integers?, and How to compress integer lists?) to understand how the compression works.
Answered by Kevin Cruijssen on October 27, 2021
Nθ§⪪Cen Mes Pale¦ ⁺‹θ⁶⁷‹θ²⁵³ozoic§⪪”↶0s⟧↶'⪪Q⟧⪪⪫(cI‹qκyQυ⌕↖↙″⁼R¤´⁵σι↙¡Z⟧+]IΦc№M⁰$✂%¹νν⬤~[Z⊕←⊘↖τF¤H>¹Sk◨ⅈγδ;§⟦⬤▶%↨Φ;{θi∕Nβ” LΦI⪪”)¶5″⁶vρ⁶S⊘l²Kκ~y”³‹ιθ
Try it online! Link is to verbose version of code. Explanation:
Nθ
Input the year.
§⪪Cen Mes Pale¦ ⁺‹θ⁶⁷‹θ²⁵³
Print one of the three era prefixes depending on the year.
ozoic
Print the era suffix.
§⪪”...” LΦI⪪”...”³‹ιθ
Extract the years that the periods end from a compressed list of padded years and count how many periods have ended before the input year and index into a compressed list of names of periods to print the current period. (Padding the years to three digits saves 4 bytes overall due to not needing any separators.)
Answered by Neil on October 27, 2021
lambda y:(['Cen']*67+['Mes']*186+['Pale']*290)[y]+'ozoic/'+(['Quaternary']*4+['Neogene']*20+['Paleogene']*43+['Cretaceous']*79+['Jurassic']*56+['Triassic']*51+['Permian']*47+['Carboniferous']*60+['Devonian']*60+['Silurian']*25+['Ordovician']*41+['Cambrian']*57)[y]
Answered by Noodle9 on October 27, 2021
Assumes that input is 0-542, otherwise ~+7 bytes.
(Cen`Mes`Pale{~66 252&I.);@,'ozoic/';Quaternary`Neogene`Paleogene`Cretaceous`Jurassic`Triassic`Permian`Carboniferous`Devonian`Silurian`Ordovician`Cambrian{~(+/3 20 43 79 56 51 47 60 60 25 41)I.]
Answered by xash on October 27, 2021
n=>(Buffer('!%5LpYTP]]:JZ').some(c=>(n-=c-34)<j++,j=1),a='CenMesPaleQuaternaryNeogenePaleogeneCretaceousJurassicTriassicPermianCarboniferousDevonianSilurianOrdovicianCambrian'.match(/.[a-z]+/g))[j>8?2:j/6|0]+'ozoic/'+a[j]
Answered by Arnauld on October 27, 2021
f=(a)=>{g=(b,q,w)=>{for(i=0;;i++){if(a<q[i])return w[i]}}
x=[67,253,543]
y=['Cenozoic/','Mesozoic/','Paleozoic/']
n=[4,24,67,146,202,253,300,360,420,445,486,543]
m=['Quaternary','Neogene','Paleogene','Cretaceous','Jurassic','Triassic','Permian','Carboniferous','Devonian','Silurian','Ordovician','Cambrian']
console.log(g(a,x,y)+g(a,n,m))}
simplest solution to start golfing
Answered by Godric on October 27, 2021
@a=((Cenozoic)x67,(Mesozoic)x186,(Paleozoic)x290);@b=((Quaternary)x4,(Neogene)x20,(Paleogene)x43,(Cretaceous)x79,(Jurassic)x56,(Triassic)x51,(Permian)x47,(Carboniferous)x60,(Devonian)x60,(Silurian)x25,(Ordovician)x41,(Cambrian)x57);$_=$a[$_].$".$b[$_]
This creates two arrays with 543 elements each. Finding the era and period is then just a matter of indexing in the arrays.
Edit: The challenge was changed; we can now assume the input is between 0 and 542 inclusive. So we don't need to test the input anymore. And we don't require a slash between era and period either.
Answered by Abigail on October 27, 2021
(0 4 24 67 146 202 253 300 360 420 445 486 543⍸⎕)⊃⊃,/1(⌷,¨↓)¨(9⍴4↑1)⊂(∊∘⎕A⊂⊢)'Cenozoic/QuaternaryNeogenePaleogeneMesozoic/CretaceousJurassicTriassicPaleozoic/PermianCarboniferousDevonianSilurianOrdovicianCambrian'
Try it online! (uses c
as a polyfill for ⊂
since TIO has not upgraded to 18.0 yet)
Answered by Adám on October 27, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP