CS1101S S5
Extra questions:
-
Write a Source function
every_second
that takes a list and returns another list containing the elements at odd indexes. For example:// returns ["x", ["y", ["z", null]]]every_second(list("a", "x", "b", "y", "c", "z", "d")); -
Write a Source function
sums
that takes a list of numbers and returns a list, whose first item is the sum of all even indexed items, and the second item is the sum of all odd indexed items.// returns: [9, [6, null]]sums(list(1, 2, 3, 4, 5)); -
Strings can be represented as lists of characters. This allows us to perform list operations on strings. Write a function
lexico
that takes in two lists of charactersxs
andys
and returnstrue
ifxs > ys
andfalse
otherwise. The relation>
here is the lexicographic order, i.e. the way words are sorted in dictionaries. So ifxs
comes afterys
in a dictionary, returntrue
.// returns: truelexico(list("a", "b", "c"), list("a", "b", "b"));// returns: falselexico(list("a", "b", "c"), list("a", "b", "c")); -
Write a function
substr
that takes in lists of charactersxs
andys
and returnstrue
iffys
is a substring ofxs
. For example:// returns: truesubstr(list("h", "e", "l", "l", "o"), list("h", "e", "l", "l"));