Yeah, see this is what I think the question implies, but it's not actually what the example Python code does. The Python code given, if followed, makes the problem needlessly complex. My first instinct was to do something like your solution, but I ended up trying to stick as closely to the prompt as possible, and that includes returning a partially-defined function from const.
Some notes on yours:
Defining Pair<T> with only one generic type means that a and bmust be the same type. They can't be Integer and Double, for instance. Maybe try expanding it so a and b can have independent types?
Your return statements from cdr() and car() have explicit type-casting:
return(T)p.b;
...if you instead declare the type of Pair in the argument, you can avoid this. Instead of:
publicstatic<T>Tcdr(Pairp){return(T)p.b;}
...write:
publicstatic<T>Tcdr(Pair<T>p){returnp.b;}
Also, I think you can drop the <T> in the methods, since you already declare T in the class signature:
publicstaticTcdr(Pair<T>p){returnp.b;}
Other than that, that's basically what I would have written, if we needed to make a Pair class. Nice work!
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Yeah, see this is what I think the question implies, but it's not actually what the example Python code does. The Python code given, if followed, makes the problem needlessly complex. My first instinct was to do something like your solution, but I ended up trying to stick as closely to the prompt as possible, and that includes returning a partially-defined function from
const
.Some notes on yours:
Defining
Pair<T>
with only one generic type means thata
andb
must be the same type. They can't beInteger
andDouble
, for instance. Maybe try expanding it soa
andb
can have independent types?Your return statements from
cdr()
andcar()
have explicit type-casting:...if you instead declare the type of
Pair
in the argument, you can avoid this. Instead of:...write:
Also, I think you can drop the
<T>
in the methods, since you already declareT
in the class signature:Other than that, that's basically what I would have written, if we needed to make a
Pair
class. Nice work!