Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint

4.5. Exercises

There are five exercises, each one requiring the creation of a small function, and drawing on the coverage of slices and maps presented in this chapter. We have put all five functions in the same .go file (chap4_ans/chap4_ans.go), and added a main() function that makes use of them all to make testing easier. (Proper unit testing is covered later, in Chapter 9, §9.1.1.3, 414.)

  1. Create a function that accepts an []int and returns an []int which is a copy of the given []int but with duplicates removed. For example, given an argument of []int{9, 1, 9, 5, 4, 4, 2, 1, 5, 4, 8, 8, 4, 3, 6, 9, 5, 7, 5}, the function should return []int{9, 1, 5, 4, 2, 8, 3, 6, 7}. In the chap4_ans.go solution file the function is called UniqueInts(). The function uses composite literal syntax rather than the built-in make() function and is 11 lines long. It should be quite easy to do.

  2. Create a function that accepts an [][]int (i.e., a two-dimensional slice of ints), and returns a single []int that contains all the ints from the two-dimensional slice’s first slice, then from its second slice, and so on. For example, if the function is called Flatten():

    irregularMatrix := [][]int{{1, 2, 3, 4},
        {5, 6, 7, 8},
        {9, 10, 11},
        {12, 13, 14, 15},
        {16, 17, 18, 19, 20}}
    slice := Flatten(irregularMatrix)
    fmt.Printf("1x%d: %v\n", len(slice), slice)
    1x20: [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20]


    The Flatten() function in chap4_ans.go is a mere nine lines. The function is slightly subtle to ensure that it works correctly even when the lengths of the inner slices vary (as they do in the irregularMatrix), but is quite straightforward.

  3. Create a function that accepts an []int and a column count (as an int), and that returns an [][]int where each inner slice’s length is equal to the given number of columns. For example, if the argument is []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}, here are some sample results, each preceded by the number of columns that was passed:

    3 [[1 2 3] [4 5 6] [7 8 9] [10 11 12] [13 14 15] [16 17 18] [19 20 0]]
    4 [[1 2 3 4] [5 6 7 8] [9 10 11 12] [13 14 15 16] [17 18 19 20]]
    5 [[1 2 3 4 5] [6 7 8 9 10] [11 12 13 14 15] [16 17 18 19 20]]
    6 [[1 2 3 4 5 6] [7 8 9 10 11 12] [13 14 15 16 17 18] [19 20 0 0 0 0]]


    Notice that since there are 20 ints, neither 3 nor 6 columns are exact multiples, so we have padded the last inner slice with zeros when necessary to keep all column (i.e., inner slice) lengths the same.

    The Make2D() function in chap4_ans.go is 12 lines long and makes use of a helper function that’s 7 lines long. The Make2D() function and its helper need a little bit of thought to get right, but aren’t difficult.

  4. Create a function that accepts a []string containing the lines of an .ini-style file and that returns a map[string]map[string]string whose keys are group names and whose values are keyvalue maps of each group’s keys and values. Blank lines and lines beginning with ; should be ignored. Each group is indicated by a name in square brackets on its own line, and each group’s keys and values are indicated by one or more lines of the form key=value. Here is an example []string that the function could process.

    iniData := []string{
        "; Cut down copy of Mozilla application.ini file",
        "",
        "[App]",
        "Vendor=Mozilla",
        "Name=Iceweasel",
        "Profile=mozilla/firefox",
        "Version=3.5.16",
        "[Gecko]",
        "MinVersion=1.9.1",
        "MaxVersion=1.9.1.*",
        "[XRE]",
        "EnableProfileMigrator=0",
        "EnableExtensionManager=1",
    }


    Given this data, the function should return the following map which we have “pretty-printed” to make it easier to see its structure.

    map[Gecko: map[MinVersion: 1.9.1
                   MaxVersion: 1.9.1.*]
        XRE: map[EnableProfileMigrator: 0
                 EnableExtensionManager: 1]
        App: map[Vendor: Mozilla
                 Profile: mozilla/firefox
                 Name: Iceweasel
                 Version: 3.5.16]]


    The ParseIni() solution function assumes a group of “General” for any keyvalues that are not within the scope of a group. It is 24 lines long and might take a bit of care to get right.

  5. Create a function that accepts a map[string]map[string]string that represents an .ini file’s data. The function should print out the data as an .ini file with groups in alphabetical order and keys within groups in alphabetical order, and with a blank line between each group. For example, given the data from the previous exercise the output should be:

    [App]
    Name=Iceweasel
    Profile=mozilla/firefox
    Vendor=Mozilla
    Version=3.5.16
    
    [Gecko]
    MaxVersion=1.9.1.*
    MinVersion=1.9.1
    
    [XRE]
    EnableExtensionManager=1
    EnableProfileMigrator=0


    The PrintIni() solution function is 21 lines long and should be easier to do than the previous exercise’s ParseIni() function.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint