Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Test Your Skills: Python Thread
Index -> Python
Goto page Previous  1, 2
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
zuzelvp




PostPosted: Sat May 22, 2010 10:39 am   Post subject: Re: Test Your Skills: Python Thread

[quote="[Gandalf] @ Thu Oct 02, 2008 8:00 am"]Roman, you couldn't do that because you're just using the join function and printing the result. The point is the re-implement it completely.

Here's my try:
Python:
def join(lst, sep):
    return reduce(lambda x, y: x + sep + y, lst)


What I don't understand is, if we're not supposed to return anything or modifying any arguments, then what is the function supposed to do? Of course, there's probably some 'trick' that I'm missing completely. Wink

!!![/quote]

join = lambda lst, sep: reduce(lambda x, y: x + sep + y, lst, '').replace(sep, '', 1)
Sponsor
Sponsor
Sponsor
sponsor
zuzelvp




PostPosted: Sat May 22, 2010 10:49 am   Post subject: (No subject)

Hikaru79 @ Tue Nov 29, 2005 8:15 pm wrote:
Okay, my turn to give one Smile This is my first try at this, so be gentle. I'll come up with more soon Smile Here it is.

Add one line of code so that the following line executes properly (and gives the result that one would intuitively assume).
Python:
Dict(foo=5, bar=12, foobar=9)


I hope this isn't too easy, hard, pointless, etc Embarassed


Anyone coming from languages like C# will need to learn this one Wink
DtY




PostPosted: Sat May 22, 2010 2:15 pm   Post subject: (No subject)

Hikaru79 @ Tue Nov 29, 2005 8:15 pm wrote:
Okay, my turn to give one Smile This is my first try at this, so be gentle. I'll come up with more soon Smile Here it is.

Add one line of code so that the following line executes properly (and gives the result that one would intuitively assume).
Python:
Dict(foo=5, bar=12, foobar=9)


I hope this isn't too easy, hard, pointless, etc Embarassed
What do you mean "properly", I assume returns the dictionary that contains those?

Python:
Dict = dict
# Or
def Dict(**params): return params


Here's another, if anyone's interested;

In one line, write a function that will transpose a two dimensional list, that is, if the input were to look like this as a grid:
code:
A B C
D E F
G H I

The output looks like this:
code:
A D G
B E H
C F I
.
Some languages have a function that will do this, python doesn't, without using some syntax that may appear odd.
I'm pretty sure I learned how to do this from here, so at least one person should know right away.
cavetroll




PostPosted: Sat May 22, 2010 10:37 pm   Post subject: (No subject)

DtY @ Sat May 22, 2010 2:15 pm wrote:
Hikaru79 @ Tue Nov 29, 2005 8:15 pm wrote:
Okay, my turn to give one Smile This is my first try at this, so be gentle. I'll come up with more soon Smile Here it is.

Add one line of code so that the following line executes properly (and gives the result that one would intuitively assume).
Python:
Dict(foo=5, bar=12, foobar=9)


I hope this isn't too easy, hard, pointless, etc Embarassed
What do you mean "properly", I assume returns the dictionary that contains those?

Python:
Dict = dict
# Or
def Dict(**params): return params


Here's another, if anyone's interested;

In one line, write a function that will transpose a two dimensional list, that is, if the input were to look like this as a grid:
code:
A B C
D E F
G H I

The output looks like this:
code:
A D G
B E H
C F I
.
Some languages have a function that will do this, python doesn't, without using some syntax that may appear odd.
I'm pretty sure I learned how to do this from here, so at least one person should know right away.


I think for the transposing of a list, you can just do
Python:

zip(*lst)


Where lst is your initial two dimensional list. Although it will actually return a tuple of tuples, I assume that's what you were looking for?
tedying




PostPosted: Fri Jan 20, 2012 11:43 pm   Post subject: (No subject)

Hikaru79 @ Tue Nov 29, 2005 8:15 pm wrote:
Okay, my turn to give one Smile This is my first try at this, so be gentle. I'll come up with more soon Smile Here it is.

Add one line of code so that the following line executes properly (and gives the result that one would intuitively assume).
Python:
Dict(foo=5, bar=12, foobar=9)


I hope this isn't too easy, hard, pointless, etc Embarassed


def Dict(**x):
??return x
tedying




PostPosted: Fri Jan 20, 2012 11:45 pm   Post subject: (No subject)

DtY @ Sat May 22, 2010 2:15 pm wrote:
Hikaru79 @ Tue Nov 29, 2005 8:15 pm wrote:
Okay, my turn to give one Smile This is my first try at this, so be gentle. I'll come up with more soon Smile Here it is.

Add one line of code so that the following line executes properly (and gives the result that one would intuitively assume).
Python:
Dict(foo=5, bar=12, foobar=9)


I hope this isn't too easy, hard, pointless, etc Embarassed
What do you mean "properly", I assume returns the dictionary that contains those?

Python:
Dict = dict
# Or
def Dict(**params): return params


Here's another, if anyone's interested;

In one line, write a function that will transpose a two dimensional list, that is, if the input were to look like this as a grid:
code:
A B C
D E F
G H I

The output looks like this:
code:
A D G
B E H
C F I
.
Some languages have a function that will do this, python doesn't, without using some syntax that may appear odd.
I'm pretty sure I learned how to do this from here, so at least one person should know right away.


My python definitely has this function:

>>> a=((1,2,3),(4,5,6),(7,8,9))
>>> zip(*a)
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]
tedying




PostPosted: Fri Jan 20, 2012 11:49 pm   Post subject: (No subject)

wtd @ Tue Nov 29, 2005 8:43 pm wrote:
Python:
for line in file("foo.txt"): print line


Add one line of code to this such that it prints "Whoops! Empty file." if there's nothing in the file.



for line in open("/dev/null"):
  print(line)
else:
  print("null")
Display posts from previous:   
   Index -> Python
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 2 of 2  [ 22 Posts ]
Goto page Previous  1, 2
Jump to:   


Style:  
Search: