Published 4 months ago
by
Eric

package intro import spock.lang.* @Narrative(""" We will be using the functions to take actions on lists. See if you can find the methods needed to make these tests pass. Documentation is at: http://docs.groovy-lang.org/latest/html/groovy-jdk/java/util/List.html """) class FunctionSpec extends Specification { def "Sort a list"() { given: "An unsorted list" def numList = [3,5,2,1,4] when: "What method would sort this list?" numList then: "The list is sorted" numList == [1,2,3,4,5] } def "add to a list"() { given: "A list with 1 element" def numList = [1] when: "How can we add or push another element onto the list?" numList then: numList == [1,2] } def "take 1 from a list"() { given: "Given a list of 2 numbers" def numList = [1,2] when: "How can we remove the last one from the list?" numList then: numList == [1] } def "get first item"() { given: def numList = [3,6,9] when: def first = numList.first() then: first == 3 } def "get last item"() { given: "A list of numbers" def numList = [3,6,9] def lastNumber when: "How can we get the last number in the list?" //?? then: lastNumber == 9 } def "add numbers in list"() { given: "A list of any numbers" def numList = [3,6,9] def sum when: "How can we add each number in the list to get a sum?" //?? then: sum == 18 } def "multiply numbers in list"() { given: "A list of numbers" def numList = [3,6,9] def product = 1 when: "How can we multiply each number to get the product" // Hint, we'll have to do this ourselves then: "The product of the list is" product == 162 } def "multiply numbers in list - closure"() { given: def numList = [3,6,9] def product = 1 and: "We add our product " def productClosure = { // We can assign our `product` logic to a variable. } when: "We call each with the logic above" numList.each productClosure then: "The product of the list is" product == 162 } def "BONUS - Shuffle a list"() { given:"An ordered list of numbers" def numList = [1,2,3,4,5] when:"We use the shuffle method on the Collections class" Collections.shuffle(numList) then: "But what should numList NOT equal?" numList != null // ?? } }