Please place your programs in seperate files named as described in the problem. Improperly named programs will not count. Turn in your files by placing them in the dropbox. Please be mindful of the late policy.
Correct | 80% |
Commented | 10% |
Attempted | 10% |
Nested lists can be useful, but sometimes we just want to know all of the items in a list. Create a list flattener, which will take a nested list and turn it into a simple list.
flatten(lst)
where lst
is the list to be
flattened.type(lst) ==
type([])
). You should return a new list with that item in it,
because flatten
should always return
the same
type.lst
is a list, then create an empty list and
iterate through lst
, appending the results of calling
flatten
on each of the items in lst
. (Don't
forget to return
the results!)append
method will take its argument and append it
to the end. If its argument is a list, it will append the list to the
end as an entry in the list... that is, you will get a nested list!
Instead, use the extend
method, which appends a whole list
as seperate items. If you don't understand, play with
append
and extend
until you do
understand.flatten.py
.
Also in that file should be some tests, including the following:
print flatten([1,2,[3,4]])
# [1, 2, 3, 4]
print flatten([[1,4],[5,4],[27,4]])
#[1, 4, 5, 4, 27, 4]
print flatten([[1,2,[5,4],[34,[5],2]],34])
# [1, 2, 5, 4, 34, 5, 2, 34]
Write an interactive data analyzer. Data analysis can be a painstaking effort, and sometimes it is nice to be able to do things step by step without having to do them by hand. In this problem, we will develop a program that will allow users to analyze data with various mathematical functions step by step.
analyze.py
map
square
: argument squaredscale
: argument scaled by the variable
scalar
reduce
:
+
: adds two arguments*
: multiplies two argumentsfilter
:
even
: True iff argument is even>
: True iff argument is greater than variable
threshold
range
: returns range(n)
where
n,ignore
are arguments
(ignore
is the place where the
previousResult
is passed, even
though it is not used.)random
: returns n
random floats using
random.random()
, where
n,ignore
are arguments
(ignore
is the place where the
previousResult
is passed, even
though it is not used.)set
: sets name
to [value]
in
the variables
dictionary. Returns
previousResult
.show
: shows variable name
. Returns
previousResult
.store
: sets name
equal to
previousResult
in
variables
. Returns
previousResult
.load
: returns name
from
variables
.list
: lists all variables in variables
and their values. Returns
previousResult
.clear
: clears all variables in variables
.
Returns []
help
: shows all available operations. Returns
previousValue
.userFunctions
+
, range
,
and store
:
def add(x,y):
return x+y
userFunctions['+'] = (reduce, add)
def genRange(n, ignore):
return range(int(n))
userFunctions['range'] = (None, genRange)
def store(name, previousValue):
variables[name] = previousValue
return previousValue
userFunctions['store'] = (None, store)
Note that because range
and store
are not in
one of the three functional categories above, they have
None
as their functional procedures. >
rather than > threshold
.process(userResponse, previousResult)
that
processes user input in the following way:
userResponse
is a list that contains the user's
command.previousResult
is what the last result was.userResponse[0]
is in the dictionary; print
an error and return previousResult
if it is not.None
, in which
case we want to pass the rest of userReponse
as well as
previousResult
to the function object.previousResult
to the functional procedure.apply
takes a function and a single list
of arguments and returns the resultresult = process(userResponse, result)
.result
should be initialized to []
before
the loop starts.Here is an example run that uses everything at least once:
? help
even,load,square,set,help,show,+,*,list,random,range,scale,clear,store,>
Result: []
? random 5
Result: [0.0079262051586875826, 0.9649961654385576, 0.29754284663124386, 0.57334520230032837, 0.78848891112000485]
? set threshold 0.5
Result: [0.0079262051586875826, 0.9649961654385576, 0.29754284663124386, 0.57334520230032837, 0.78848891112000485]
? >
Result: [0.9649961654385576, 0.57334520230032837, 0.78848891112000485]
? set scalar 10.0
Result: [0.9649961654385576, 0.57334520230032837, 0.78848891112000485]
? scale
Result: [9.6499616543855762, 5.7334520230032835, 7.8848891112000485]
? even
Result: []
? range 5
Result: [0, 1, 2, 3, 4]
? scale
Result: [0.0, 10.0, 20.0, 30.0, 40.0]
? set threshold 0
Result: [0.0, 10.0, 20.0, 30.0, 40.0]
? >
Result: [10.0, 20.0, 30.0, 40.0]
? *
Result: [240000.0]
? store aa
Result: [240000.0]
? range 10
Result: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
? store ab
Result: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
? load aa
Result: [240000.0]
? load bb
No variable bb
Result: [240000.0]
? load ab
Result: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
? list
threshold: [0.0]
aa: [240000.0]
scalar: [10.0]
ab: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Result: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
? show aa
aa: [240000.0]
Result: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
? clear
Variables cleared.
Result: []
? list
Result: []
? range 10
Result: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
? even
Result: [0, 2, 4, 6, 8]
? +
Result: [20]
? range 10
Result: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
? even
Result: [0, 2, 4, 6, 8]
? square
Result: [0, 4, 16, 36, 64]
? +
Result: [120]
? dance
Unknown operation.
? quit
Goodbye!