How to resolve the algorithm Chinese zodiac step by step in the J programming language
How to resolve the algorithm Chinese zodiac step by step in the J programming language
Table of Contents
Problem Statement
Traditionally, the Chinese have counted years using two lists of labels, one of length 10 (the "celestial stems") and one of length 12 (the "terrestrial branches"). The labels do not really have any meaning outside their positions in the two lists; they're simply a traditional enumeration device, used much as Westerners use letters and numbers. They were historically used for months and days as well as years, and the stems are still sometimes used for school grades. Years cycle through both lists concurrently, so that both stem and branch advance each year; if we used Roman letters for the stems and numbers for the branches, consecutive years would be labeled A1, B2, C3, etc. Since the two lists are different lengths, they cycle back to their beginning at different points: after J10 we get A11, and then after B12 we get C1. The result is a repeating 60-year pattern within which each pair of names occurs only once. Mapping the branches to twelve traditional animal deities results in the well-known "Chinese zodiac", assigning each year to a given animal. For example, Sunday, January 22, 2023 CE (in the common Gregorian calendar) began the lunisolar Year of the Rabbit. The celestial stems do not have a one-to-one mapping like that of the branches to animals; however, the five pairs of consecutive stems are each associated with one of the five traditional Chinese elements (Wood, Fire, Earth, Metal, and Water). Further, one of the two years within each element is assigned to yin, the other to yang. Thus, the Chinese year beginning in 2023 CE is also the yin year of Water. Since 12 is an even number, the association between animals and yin/yang doesn't change; consecutive Years of the Rabbit will cycle through the five elements, but will always be yin. You may optionally provide more information in the form of the year's numerical position within the 60-year cycle and/or its actual Chinese stem-branch name (in Han characters or Pinyin transliteration). Thus, year 1 of a cycle is the year of the Wood Rat (yang), year 2 the Wood Ox (yin), and year 3 the Fire Tiger (yang). The year 2023 - which, as already noted, is the year of the Water Rabbit (yin) - is the 40th year of the current cycle. Therefore 1984 was 甲子 (jiă-zĭ, or jia3-zi3). 2023 is 癸卯 (gŭi-măo or gui3-mao3).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Chinese zodiac step by step in the J programming language
Source code in the j programming language
ELEMENTS=: _4 |. 2 # ;:'Wood Fire Earth Metal Water'
YEARS=: 1935 1938 1968 1972 1976 2017
ANIMALS=: _4 |. ;:'Rat Ox Tiger Rabbit Dragon Snake Horse Goat Monkey Rooster Dog Pig'
YINYANG=: ;:'yang yin'
cz=: (|~ #)~ { [
ANIMALS cz YEARS
┌───┬─────┬──────┬───┬──────┬───────┐
│Pig│Tiger│Monkey│Rat│Dragon│Rooster│
└───┴─────┴──────┴───┴──────┴───────┘
YINYANG cz YEARS
┌───┬────┬────┬────┬────┬───┐
│yin│yang│yang│yang│yang│yin│
└───┴────┴────┴────┴────┴───┘
chinese_zodiac =: 3 : ';:inv(<":y),(ELEMENTS cz y),(ANIMALS cz y),(<''(''),(YINYANG cz y),(<'')'')'
chinese_zodiac&>YEARS
1935 Wood Pig ( yin )
1938 Earth Tiger ( yang )
1968 Earth Monkey ( yang )
1972 Water Rat ( yang )
1976 Fire Dragon ( yang )
2017 Fire Rooster ( yin )
'CELESTIAL TERRESTRIAL'=:7&u:&.>{&a.&.> 16be7 16b94 16bb2 16be4 16bb9 16b99 16be4 16bb8 16b99 16be4 16bb8 16b81 16be6 16b88 16b8a 16be5 16bb7 16bb1 16be5 16bba 16b9a 16be8 16bbe 16b9b 16be5 16ba3 16bac 16be7 16b99 16bb8; 16be5 16bad 16b90 16be4 16bb8 16b91 16be5 16baf 16b85 16be5 16b8d 16baf 16be8 16bbe 16bb0 16be5 16bb7 16bb3 16be5 16b8d 16b88 16be6 16b9c 16baa 16be7 16b94 16bb3 16be9 16b85 16b89 16be6 16b88 16b8c 16be4 16bba 16ba5
ANIMALS=: ;/ _4 |. TERRESTRIAL
ELEMENTS=: ;/ _4 |. CELESTIAL
chinese_zodiac&>YEARS
1935 乙 亥 ( yin )
1938 戊 寅 ( yang )
1968 戊 申 ( yang )
1972 壬 子 ( yang )
1976 丙 辰 ( yang )
2017 丁 酉 ( yin )
You may also check:How to resolve the algorithm Reverse a string step by step in the REBOL programming language
You may also check:How to resolve the algorithm Count in octal step by step in the PARI/GP programming language
You may also check:How to resolve the algorithm Read a configuration file step by step in the PowerShell programming language
You may also check:How to resolve the algorithm Operator precedence step by step in the Scala programming language
You may also check:How to resolve the algorithm Trigonometric functions step by step in the Pop11 programming language