Data Structures
Containers

 Besides lists and tables, Mint supports a few more data structures. Sets are lists that can only hold one of each element. You cannot put the number 3 in the same set twice. ```import "data.mint" numberSet = set() numberSet.add(2.5) numberSet.add(2) numberSet.add(15) numberSet.add(15) numberSet.add(15) numberSet.add(-32) print numberSet.toString()``` The code above will print: {2.5, 2, 15, -32} Sorted lists are lists that always have an ordering, from smallest to greatest. ```import "data.mint" srted = sortedList() srted.append(10) srted.append(5) srted.append(2.5) srted.append(0) srted.append(1) srted.append(1.75) print srted.toString()``` Trees are data structures that have a root and more child trees. To make the tree:``` 50 / \ 2 25``` Use:```import "data.mint" fiftyTree = tree(50) a = tree(2) b = tree(25) fiftyTree.addChild(a) fiftyTree.addChild(b) ``` Finally, there are subprograms that can sort lists, such as bubble sort and quick sort. ```import "data.mint" print quickSort([9, 8, 7, 100, 200, -9]) ``` Let us talk a bit more about strings and lists. Because strings and lists are both native data structures, you don't need to import anything to use them. One interesting thing about lists... If a subprogram takes in exactly one list as an argument, you can omit the parentheses that call the subprogram: ```import "data.mint" quickSort[900, -4, 0, 1, 20, 28, 9, -450, 1000, 200, 256, 1024]``` To remove items from a list, use subtraction. ```femaleNames = ["Alice", "Jenny", "Jane", "Beth", "Sophia"] femaleNames = femaleNames - ["Jane", "Alice"] print femaleNames // Prints ["Jenny", "Beth", "Sophia"]``` To reverse a string, just make it a negative string by putting a negative sign in front of it. ```someText = "indefinite integral" backwards = -someText print backwards``` The above code will print the reversed string "largetni etinifedni". ```deepblue = "My name is DeepBlue!" watson = "My name is Watson." print deepblue and watson print watson and deepblue print deepblue xor watson print watson xor deepblue``` For strings, the keywords `and` and `xor` will both return a list of two Mint values. The `and` operator for strings will take the two strings, and find all the characters that match and all the characters that do not. The characters that match go into the first element of the list, and the characters that don't match go into the second element of the list. The `xor` operator for strings will take two strings, and find all the characters that exactly match at the same position, which go into the first element of the returned list, and all the characters that don't match at the same position, which go into the second element. The `or` operator on strings can be used to alphabetize strings. The expression `ALPHA or BETA` will return ALPHA if ALPHA contains a string that is alphabetically before BETA, and will return BETA otherwise. ```/* If we alphabetize these words, * you get "what", "whence", "where", and "whoa" * The following statement prints "what", which is * the first word that comes alphabetically in this list * of 4 words. */ print "whence" or "where" or "what" or "whoa"``` Strings can be compared by length using the <, >, <=, and >= operators. The quickSort subprogram is written in Mint, and it uses the < and > operators, so it also works on lists of strings. ```if thisString > thatString // This will only be executed if and only // if thisString has a greater length // than thatString. end import "data.mint" // The following line will sort strings by length, // and therefore it will display: // ["tiny", "mouse", "tigress", "elephant"] // onto the computer screen. print quickSort["elephant", "tiny", "mouse", "tigress"]``` Navigation Previous Lesson: Tables Next Lesson: Subprograms Table of Contents