"Go Language Programming" study (VI)

Source: Internet
Author: User

One, shrink sort

Package Mainimport ("FMT"    "Sort"    "Strings")varoriginal = []string{    "nonmentals",    "hydrogen",    "Carbon",    "nitrogenl",    "Oxygen",    "Inner transitionals",    "lanthanides",    "europium",    "cerium",    "actinides",    "Uranium",    "Plutonium",    "Curium",    "Alkali Metals",    "Lithium",    "Sodium",    "Potassium",}func Main () {FMT. Println ("|        Original | Sorted |") fmt. Println ("|----------------------|-------------------|") Sorted:=sortedindentedstrings (original) forI: =Range Original {fmt. Printf ("|%-19s|%-19s|\n", Original[i], sorted[i])}} Func sortedindentedstrings (Slice []string) []string{entries:=populatedentries (Slice)returnsortedentries (entries)}func populatedentries (Slice []string) Entries {indent, indentsize:=computeindent (Slice) fmt. Printf ("[%s]%d =%d\n", indent, Len (indent), indentsize) entries:= Make (Entries,0)     for_, Item: =Range Slice {i, Level:=0,0         forstrings. Hasprefix (item[i:], indent) {i+=indentsize Level++} key:=strings. ToLower (Strings. Trimspace (item)) AddEntry (level, key, item,&entries)} returnentries}func computeindent (Slice []string) (string,int) {     for_, Item: =Range Slice {ifLen (item) >0&& (item[0] ==' '|| item[0] =='\ t') {whitspace:= Rune (item[0])             forIChar: = Range item[1:] {                if Char!=Whitspace {i++returnStrings. Repeat (string(Whitspace), i), i} }}}return "",0}func addentry ( levelint, key, valuestring, Entries *Entries) {    ifLevel = =0 {        *entries = Append (*entries, Entry{key, value, make (entries,0)})    } Else{addentry ( level-1, key, value,& ((*entries) [entries. Len ()-1].children))}}func sortedentries (entries entries) []string {    varIndentedslice []stringsort. Sort (entries) for_, Entry: =Range Entries {populatedindentedstrings (entry,&Indentedslice)} returnIndentedslice}func populatedindentedstrings (Entry entry, Indentedslice*[]string) {    *indentedslice = Append (*Indentedslice, Entry.value) sort. Sort (Entry.children) for_, Child: =range Entry.children {populatedindentedstrings (Child, Indentedslice)}}type entrystruct{Keystringvaluestringchildren Entries}type Entries []entryfunc (Entries Entries) Len ()int{returnlen (entries)}func (entries entries) less (i, Jint)BOOL {    returnEntries[i].key <Entries[j].key}func (entries entries) Swap (i, Jint) {entries[i], Entries[j]=Entries[j], Entries[i]}

This code is still very simple, but not perfect, because there is a hypothetical premise-all lines of text use the same tightening.

Code logic:

The first step is to iterate through all the rows of data until a row is found, the text that begins with a space tab, and how many of the characters in the line are used as the basis for judging the tightening level.

It then facilitates all row data again, computes the string "start" repeatedly with String.hasprefix, and calculates the result as a shrink level.

To construct a data structure using the Shrink level entry, tighten to 0, then add the root entrie, tighten to 1, then on the child of the root entry, build entry, 2, on the child of entry to build on the child.

Finally, a comparison of strings using the less defined method is used to sort each layer of each root entry.

Using the data structure, if you assume that there is a entry node called root, you can think of a tree.

"Go Language Programming" study (VI)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.