How to resolve the algorithm Chinese zodiac step by step in the Julia programming language

Published on 22 June 2024 08:30 PM

How to resolve the algorithm Chinese zodiac step by step in the Julia 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 Julia programming language

The provided Julia code is a function chinese that takes a year as input and returns a string describing the Chinese zodiac sign for that year.

The function uses a dictionary pinyin to map Chinese characters to their pinyin equivalents, and arrays elements, animals, celestial, terrestrial, and aspects to store the elements, animals, celestial stems, terrestrial branches, and yin/yang aspects associated with each year in the Chinese zodiac cycle.

The function first calculates the cyclical year by subtracting a base year (4) from the input year. It then calculates the stem number and corresponding celestial stem and pinyin representation, the element associated with the stem, the branch number and corresponding terrestrial branch and pinyin representation, the animal associated with the branch, and the yin/yang aspect associated with the year.

Finally, the function returns a string that includes the input year, the celestial stem and terrestrial branch characters, their pinyin equivalents, the element and animal associated with the year, the yin/yang aspect, and the index of the year in the 60-year cycle.

The code also includes a call to the chinese function for a list of years, including the current year, and prints the results.

Here is an example output for the provided years:

1935: 乙亥 (yĭ-hài, Wood Pig; yin - year 51 of the cycle)
1938: 戊寅 (wù-yín, Earth Tiger; yang - year 54 of the cycle)
1968: 戊申 (wù-shēn, Earth Monkey; yang - year 24 of the cycle)
1972: 壬子 (rén-zĭ, Water Rat; yang - year 28 of the cycle)
1976: 丙辰 (bĭng-chén, Fire Dragon; yang - year 32 of the cycle)
2023: 癸卯 (gŭi-măo, Water Rabbit; yin - year 39 of the cycle)

Source code in the julia programming language

function chinese(year::Int)
    pinyin = Dict(
        "甲" => "jiă",
        "乙" => "yĭ",
        "丙" => "bĭng",
        "丁" => "dīng",
        "戊" => "wù",
        "己" => "jĭ",
        "庚" => "gēng",
        "辛" => "xīn",
        "壬" => "rén",
        "癸" => "gŭi",
        "子" => "zĭ",
        "丑" => "chŏu",
        "寅" => "yín",
        "卯" => "măo",
        "辰" => "chén",
        "巳" => "sì",
        "午" => "wŭ",
        "未" => "wèi",
        "申" => "shēn",
        "酉" => "yŏu",
        "戌" => "xū",
        "亥" => "hài",
    )
    elements    = ["Wood", "Fire", "Earth", "Metal", "Water"]
    animals     = ["Rat", "Ox", "Tiger", "Rabbit", "Dragon", "Snake",
                   "Horse", "Goat", "Monkey", "Rooster", "Dog", "Pig"]
    celestial   = ["甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"]
    terrestrial = ["子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"]
    aspects     = ["yang", "yin"]
    base = 4

    cycleyear = year - base

    stemnumber = cycleyear % 10 + 1
    stemhan    = celestial[stemnumber]
    stempinyin = pinyin[stemhan]

    elementnumber = div(stemnumber, 2) + 1
    element       = elements[elementnumber]

    branchnumber = cycleyear % 12 + 1
    branchhan    = terrestrial[branchnumber]
    branchpinyin = pinyin[branchhan]
    animal       = animals[branchnumber]

    aspectnumber = cycleyear % 2 + 1
    aspect       = aspects[aspectnumber]

    index = cycleyear % 60 + 1

    return "$year: $stemhan$branchhan ($stempinyin-$branchpinyin, $element $animal; $aspect - year $index of the cycle)"
end

curryr = Dates.year(now())
yrs = [1935, 1938, 1968, 1972, 1976, curryr]
foreach(println, map(chinese, yrs))


  

You may also check:How to resolve the algorithm Sorting algorithms/Stooge sort step by step in the Go programming language
You may also check:How to resolve the algorithm Stack step by step in the RPL programming language
You may also check:How to resolve the algorithm Delete a file step by step in the Plain English programming language
You may also check:How to resolve the algorithm Compiler/virtual machine interpreter step by step in the Raku programming language
You may also check:How to resolve the algorithm Hello world/Standard error step by step in the Yabasic programming language