Commit acf87d55 authored by Samarth Joshi's avatar Samarth Joshi

Removing join function and using filter instead

parent 68f931f3
...@@ -12,3 +12,23 @@ print(l) ...@@ -12,3 +12,23 @@ print(l)
L = [["this"],["is"],["the"],["easiest"],["problem"],["of"],["this"],["outlab"]] L = [["this"],["is"],["the"],["easiest"],["problem"],["of"],["this"],["outlab"]]
l=collapse(L) l=collapse(L)
print(l) print(l)
L = [["this"]]
l=collapse(L)
print(l)
L = [[], ["Once", "upon", "a", "time"], ["in", "a", "galaxy", "far", "far", "away"]]
l=collapse(L)
print(l)
L = [[str(2), "+", str(2)], ['='], [str(5)]]
l=collapse(L)
print(l)
L = [[],[],[],[],[],[]]
l=collapse(L)
print(l)
L = [[str(2), "+", str(2)], ['='], [str(5)]]
l=collapse(L)
print(l)
\ No newline at end of file
this is an interesting python programming exercise. this is an interesting python programming exercise.
this is a bad problem this is a bad problem
this is the easiest problem of this outlab this is the easiest problem of this outlab
this
Once upon a time in a galaxy far far away
2 + 2 = 5
2 + 2 = 5
\ No newline at end of file
import functools import functools
import operator
def collapse(L) : def collapse(L) :
flat = functools.reduce(lambda x,y: operator.concat(x,y), L) flat = functools.reduce(lambda x,y: x+y, L)
return " ".join(flat) if flat:
return functools.reduce(lambda x,y: x + " " + y, flat)
else:
return ""
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment