From 296f8fce54e4c1542acc8bacfdfade4797c82d4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20B=20Nagy?= <20251272+BNAndras@users.noreply.github.com> Date: Tue, 27 Feb 2024 23:28:53 -0800 Subject: [PATCH] Add list-ops (#393) --- config.json | 8 + .../practice/list-ops/.docs/instructions.md | 19 ++ exercises/practice/list-ops/.meta/config.json | 17 ++ .../.meta/src/reference/groovy/ListOps.groovy | 61 ++++++ exercises/practice/list-ops/.meta/tests.toml | 106 ++++++++++ exercises/practice/list-ops/build.gradle | 18 ++ exercises/practice/list-ops/gradlew | 183 ++++++++++++++++++ exercises/practice/list-ops/gradlew.bat | 103 ++++++++++ .../list-ops/src/main/groovy/ListOps.groovy | 35 ++++ .../src/test/groovy/ListOpsSpec.groovy | 106 ++++++++++ settings.gradle | 1 + 11 files changed, 657 insertions(+) create mode 100644 exercises/practice/list-ops/.docs/instructions.md create mode 100644 exercises/practice/list-ops/.meta/config.json create mode 100644 exercises/practice/list-ops/.meta/src/reference/groovy/ListOps.groovy create mode 100644 exercises/practice/list-ops/.meta/tests.toml create mode 100644 exercises/practice/list-ops/build.gradle create mode 100644 exercises/practice/list-ops/gradlew create mode 100644 exercises/practice/list-ops/gradlew.bat create mode 100644 exercises/practice/list-ops/src/main/groovy/ListOps.groovy create mode 100644 exercises/practice/list-ops/src/test/groovy/ListOpsSpec.groovy diff --git a/config.json b/config.json index e785ca2e..c5763bd0 100644 --- a/config.json +++ b/config.json @@ -813,6 +813,14 @@ "practices": [], "prerequisites": [], "difficulty": 3 + }, + { + "slug": "list-ops", + "name": "List Ops", + "uuid": "42f2b1b6-75bf-4587-957d-0c55f9d046bb", + "practices": [], + "prerequisites": [], + "difficulty": 2 } ] }, diff --git a/exercises/practice/list-ops/.docs/instructions.md b/exercises/practice/list-ops/.docs/instructions.md new file mode 100644 index 00000000..ebc5dffe --- /dev/null +++ b/exercises/practice/list-ops/.docs/instructions.md @@ -0,0 +1,19 @@ +# Instructions + +Implement basic list operations. + +In functional languages list operations like `length`, `map`, and `reduce` are very common. +Implement a series of basic list operations, without using existing functions. + +The precise number and names of the operations to be implemented will be track dependent to avoid conflicts with existing names, but the general operations you will implement include: + +- `append` (_given two lists, add all items in the second list to the end of the first list_); +- `concatenate` (_given a series of lists, combine all items in all lists into one flattened list_); +- `filter` (_given a predicate and a list, return the list of all items for which `predicate(item)` is True_); +- `length` (_given a list, return the total number of items within it_); +- `map` (_given a function and a list, return the list of the results of applying `function(item)` on all items_); +- `foldl` (_given a function, a list, and initial accumulator, fold (reduce) each item into the accumulator from the left_); +- `foldr` (_given a function, a list, and an initial accumulator, fold (reduce) each item into the accumulator from the right_); +- `reverse` (_given a list, return a list with all the original items, but in reversed order_). + +Note, the ordering in which arguments are passed to the fold functions (`foldl`, `foldr`) is significant. diff --git a/exercises/practice/list-ops/.meta/config.json b/exercises/practice/list-ops/.meta/config.json new file mode 100644 index 00000000..ad2b7891 --- /dev/null +++ b/exercises/practice/list-ops/.meta/config.json @@ -0,0 +1,17 @@ +{ + "authors": [ + "BNAndras" + ], + "files": { + "solution": [ + "src/main/groovy/ListOps.groovy" + ], + "test": [ + "src/test/groovy/ListOpsSpec.groovy" + ], + "example": [ + ".meta/src/reference/groovy/ListOps.groovy" + ] + }, + "blurb": "Implement basic list operations." +} diff --git a/exercises/practice/list-ops/.meta/src/reference/groovy/ListOps.groovy b/exercises/practice/list-ops/.meta/src/reference/groovy/ListOps.groovy new file mode 100644 index 00000000..6d0b5f85 --- /dev/null +++ b/exercises/practice/list-ops/.meta/src/reference/groovy/ListOps.groovy @@ -0,0 +1,61 @@ +class ListOps { + + static append(list1, list2) { + if(list1.size == 0) { + list2 + } else { + def first = list1.pop() + [first] + append(list1, list2) + } + } + + static concatenate(lists) { + foldl(lists, this.&append, []) + } + + static filter(list, fn) { + def results = [] + for (elt in list) { + if(fn(elt)) { + results << elt + } + } + results + } + + static length(list) { + foldl(list, { acc, elt -> acc + 1 }, 0) + } + static map(list, fn) { + def results = [] + for (elt in list) { + results << fn(elt) + } + results + } + + static foldl(list, fn, initial) { + def results = initial + for (item in list) { + results = fn(results, item) + } + results + } + + static foldr(list, fn, initial) { + if(list.size == 0) { + initial + } else { + def last = list.removeLast() + foldr(list, fn, fn(initial, last)) + } + } + + static reverse(list) { + def results = [] + def end = list.size -1 + end.step(-1, -1) { results << list[it] } + results + } + +} diff --git a/exercises/practice/list-ops/.meta/tests.toml b/exercises/practice/list-ops/.meta/tests.toml new file mode 100644 index 00000000..08b1edc0 --- /dev/null +++ b/exercises/practice/list-ops/.meta/tests.toml @@ -0,0 +1,106 @@ +# This is an auto-generated file. +# +# Regenerating this file via `configlet sync` will: +# - Recreate every `description` key/value pair +# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications +# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) +# - Preserve any other key/value pair +# +# As user-added comments (using the # character) will be removed when this file +# is regenerated, comments can be added via a `comment` key. + +[485b9452-bf94-40f7-a3db-c3cf4850066a] +description = "append entries to a list and return the new list -> empty lists" + +[2c894696-b609-4569-b149-8672134d340a] +description = "append entries to a list and return the new list -> list to empty list" + +[e842efed-3bf6-4295-b371-4d67a4fdf19c] +description = "append entries to a list and return the new list -> empty list to list" + +[71dcf5eb-73ae-4a0e-b744-a52ee387922f] +description = "append entries to a list and return the new list -> non-empty lists" + +[28444355-201b-4af2-a2f6-5550227bde21] +description = "concatenate a list of lists -> empty list" + +[331451c1-9573-42a1-9869-2d06e3b389a9] +description = "concatenate a list of lists -> list of lists" + +[d6ecd72c-197f-40c3-89a4-aa1f45827e09] +description = "concatenate a list of lists -> list of nested lists" + +[0524fba8-3e0f-4531-ad2b-f7a43da86a16] +description = "filter list returning only values that satisfy the filter function -> empty list" + +[88494bd5-f520-4edb-8631-88e415b62d24] +description = "filter list returning only values that satisfy the filter function -> non-empty list" + +[1cf0b92d-8d96-41d5-9c21-7b3c37cb6aad] +description = "returns the length of a list -> empty list" + +[d7b8d2d9-2d16-44c4-9a19-6e5f237cb71e] +description = "returns the length of a list -> non-empty list" + +[c0bc8962-30e2-4bec-9ae4-668b8ecd75aa] +description = "return a list of elements whose values equal the list value transformed by the mapping function -> empty list" + +[11e71a95-e78b-4909-b8e4-60cdcaec0e91] +description = "return a list of elements whose values equal the list value transformed by the mapping function -> non-empty list" + +[613b20b7-1873-4070-a3a6-70ae5f50d7cc] +description = "folds (reduces) the given list from the left with a function -> empty list" +include = false + +[e56df3eb-9405-416a-b13a-aabb4c3b5194] +description = "folds (reduces) the given list from the left with a function -> direction independent function applied to non-empty list" +include = false + +[d2cf5644-aee1-4dfc-9b88-06896676fe27] +description = "folds (reduces) the given list from the left with a function -> direction dependent function applied to non-empty list" +include = false + +[36549237-f765-4a4c-bfd9-5d3a8f7b07d2] +description = "folds (reduces) the given list from the left with a function -> empty list" +reimplements = "613b20b7-1873-4070-a3a6-70ae5f50d7cc" + +[7a626a3c-03ec-42bc-9840-53f280e13067] +description = "folds (reduces) the given list from the left with a function -> direction independent function applied to non-empty list" +reimplements = "e56df3eb-9405-416a-b13a-aabb4c3b5194" + +[d7fcad99-e88e-40e1-a539-4c519681f390] +description = "folds (reduces) the given list from the left with a function -> direction dependent function applied to non-empty list" +reimplements = "d2cf5644-aee1-4dfc-9b88-06896676fe27" + +[aeb576b9-118e-4a57-a451-db49fac20fdc] +description = "folds (reduces) the given list from the right with a function -> empty list" +include = false + +[c4b64e58-313e-4c47-9c68-7764964efb8e] +description = "folds (reduces) the given list from the right with a function -> direction independent function applied to non-empty list" +include = false + +[be396a53-c074-4db3-8dd6-f7ed003cce7c] +description = "folds (reduces) the given list from the right with a function -> direction dependent function applied to non-empty list" +include = false + +[17214edb-20ba-42fc-bda8-000a5ab525b0] +description = "folds (reduces) the given list from the right with a function -> empty list" +reimplements = "aeb576b9-118e-4a57-a451-db49fac20fdc" + +[e1c64db7-9253-4a3d-a7c4-5273b9e2a1bd] +description = "folds (reduces) the given list from the right with a function -> direction independent function applied to non-empty list" +reimplements = "c4b64e58-313e-4c47-9c68-7764964efb8e" + +[8066003b-f2ff-437e-9103-66e6df474844] +description = "folds (reduces) the given list from the right with a function -> direction dependent function applied to non-empty list" +reimplements = "be396a53-c074-4db3-8dd6-f7ed003cce7c" + +[94231515-050e-4841-943d-d4488ab4ee30] +description = "reverse the elements of the list -> empty list" + +[fcc03d1e-42e0-4712-b689-d54ad761f360] +description = "reverse the elements of the list -> non-empty list" + +[40872990-b5b8-4cb8-9085-d91fc0d05d26] +description = "reverse the elements of the list -> list of lists is not flattened" diff --git a/exercises/practice/list-ops/build.gradle b/exercises/practice/list-ops/build.gradle new file mode 100644 index 00000000..e16cd99d --- /dev/null +++ b/exercises/practice/list-ops/build.gradle @@ -0,0 +1,18 @@ +apply plugin: "groovy" + +repositories { + mavenCentral() +} + +dependencies { + testImplementation "org.spockframework:spock-core:2.0-M2-groovy-3.0" + implementation "org.codehaus.groovy:groovy-all:3.0.2" +} + +test { + useJUnitPlatform() + testLogging { + exceptionFormat = 'full' + events = ["passed", "failed", "skipped"] + } +} \ No newline at end of file diff --git a/exercises/practice/list-ops/gradlew b/exercises/practice/list-ops/gradlew new file mode 100644 index 00000000..2fe81a7d --- /dev/null +++ b/exercises/practice/list-ops/gradlew @@ -0,0 +1,183 @@ +#!/usr/bin/env sh + +# +# Copyright 2015 the original author or authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn () { + echo "$*" +} + +die () { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin or MSYS, switch paths to Windows format before running java +if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=`expr $i + 1` + done + case $i in + 0) set -- ;; + 1) set -- "$args0" ;; + 2) set -- "$args0" "$args1" ;; + 3) set -- "$args0" "$args1" "$args2" ;; + 4) set -- "$args0" "$args1" "$args2" "$args3" ;; + 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=`save "$@"` + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +exec "$JAVACMD" "$@" diff --git a/exercises/practice/list-ops/gradlew.bat b/exercises/practice/list-ops/gradlew.bat new file mode 100644 index 00000000..62bd9b9c --- /dev/null +++ b/exercises/practice/list-ops/gradlew.bat @@ -0,0 +1,103 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/exercises/practice/list-ops/src/main/groovy/ListOps.groovy b/exercises/practice/list-ops/src/main/groovy/ListOps.groovy new file mode 100644 index 00000000..eb708e2a --- /dev/null +++ b/exercises/practice/list-ops/src/main/groovy/ListOps.groovy @@ -0,0 +1,35 @@ +class ListOps { + + static append(list1, list2) { + throw new UnsupportedOperationException('method not implemented.') + } + + static concatenate(lists) { + throw new UnsupportedOperationException('method not implemented.') + } + + static filter(list, fn) { + throw new UnsupportedOperationException('method not implemented.') + } + + static length(list) { + throw new UnsupportedOperationException('method not implemented.') + } + + static map(list, fn) { + throw new UnsupportedOperationException('method not implemented.') + } + + static foldl(list, fn, initial) { + throw new UnsupportedOperationException('method not implemented.') + } + + static foldr(list, fn, initial) { + throw new UnsupportedOperationException('method not implemented.') + } + + static reverse(list) { + throw new UnsupportedOperationException('method not implemented.') + } + +} diff --git a/exercises/practice/list-ops/src/test/groovy/ListOpsSpec.groovy b/exercises/practice/list-ops/src/test/groovy/ListOpsSpec.groovy new file mode 100644 index 00000000..7854c3b0 --- /dev/null +++ b/exercises/practice/list-ops/src/test/groovy/ListOpsSpec.groovy @@ -0,0 +1,106 @@ +import spock.lang.* + +class ListOpsSpec extends Specification { + + @Unroll + def "append entries to a list and return the new list - #label"() { + expect: + ListOps.append(list1, list2) == expected + + where: + label || list1 || list2 || expected + "empty lists" || [] || [] || [] + "list to empty list" || [] || [1, 2, 3, 4] || [1, 2, 3, 4] + "empty list to list" || [1, 2, 3, 4] || [] || [1, 2, 3, 4] + "non-empty-lists" || [1, 2] || [2, 3, 4, 5] || [1, 2, 2, 3, 4, 5] + } + + @Ignore + @Unroll + def "concatenate a list of lists - #label"() { + expect: + ListOps.concatenate(lists) == expected + + where: + label || lists || expected + "empty list" || [] || [] + "list of lists" || [[1, 2], [3], [], [4, 5, 6]] || [1, 2, 3, 4, 5, 6] + "list of nested lists" || [[[1], [2]], [[3]], [[]], [[4, 5, 6]]] || [[1], [2], [3], [], [4, 5, 6]] + } + + @Ignore + @Unroll + def "filter list returning only values that satisfy the filter function - #label"() { + expect: + ListOps.filter(list, { x -> x % 2 == 1 }) == expected + + where: + label || list || expected + "empty list" || [] || [] + "non-empty list" || [1, 2, 3, 5] || [1, 3, 5] + } + + @Ignore + @Unroll + def "return the length of a list - #label"() { + expect: + ListOps.length(list) == expected + + where: + label || list || expected + "empty list" || [] || 0 + "non-empty list" || [1, 2, 3, 5] || 4 + } + + @Ignore + @Unroll + def "return a list of elements whose values equal the list value transformed by the mapping function - #label"() { + expect: + ListOps.map(list, { x -> x + 1 }) == expected + + where: + label || list || expected + "empty list" || [] || [] + "non-empty list" || [1, 3, 5, 7] || [2, 4, 6, 8] + } + + @Ignore + @Unroll + def "folds (reduces) the given list from the left with a function - #label"() { + expect: + ListOps.foldl(list, fn, initial) == expected + + where: + label || list || fn || initial || expected + "empty list" || [] || { acc, el -> el * acc } || 2 || 2 + "direction independent function applied to non-empty list" || [1, 2, 3, 4] || { acc, el -> el + acc } || 5 || 15 + "direction dependent function applied to non-empty list" || [2, 5] || { acc, el -> acc.intdiv(el) } || 5 || 0 + } + + @Ignore + @Unroll + def "folds (reduces) the given list from the right with a function - #label"() { + expect: + ListOps.foldr(list, fn, initial) == expected + + where: + label || list || fn || initial || expected + "empty list" || [] || { acc, el -> el * acc } || 2 || 2 + "direction independent function applied to non-empty list" || [1, 2, 3, 4] || { acc, el -> el + acc } || 5 || 15 + "direction dependent function applied to non-empty list" || [2, 5] || { acc, el -> acc.intdiv(el) } || 5 || 0 + } + + @Ignore + @Unroll + def "reverse the elements of the list - #label"() { + expect: + ListOps.reverse(list) == expected + + where: + label || list || expected + "empty list" || [] || [] + "non-empty list" || [1, 3, 5, 7] || [7, 5, 3, 1] + "list of lists is not flattened" || [[1, 2], [3], [], [4, 5, 6]] || [[4, 5, 6], [], [3], [1, 2]] + } + +} diff --git a/settings.gradle b/settings.gradle index 7960247a..6ff19dee 100644 --- a/settings.gradle +++ b/settings.gradle @@ -24,6 +24,7 @@ include 'exercises:practice:isbn-verifier' include 'exercises:practice:isogram' include 'exercises:practice:leap' include 'exercises:practice:linked-list' +include 'exercises:practice:list-ops' include 'exercises:practice:luhn' include 'exercises:practice:nucleotide-count' include 'exercises:practice:matching-brackets'