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 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: Thu Sep 29, 2005 2:54 pm   Post subject: Test Your Skills: Python Thread

Challenge #1: Join

Lists and other iterable objects are very important in Python. It's often also important for us to be able to output a nicely formatted representation of these important objects.

The join function represents one important means of doing this.

code:
>>> ', '.join(['1', '2', '3'])
'1, 2, 3'


Reimplement it

Now, the challenge is to reimplement this function.

The Reward

50 bits, and my admiration. Admiration only if the bits system is borked.

The rules


  • The function must accept two arguments: 'lst', the list to join; and 'sep', the separator to place between the items.
  • Separators may be more than one character.
  • The function may not use the keywords 'return' or 'if', or any semi-colons. Semi-colons are permitted as the separator string passed to the function as an argument.
  • The function may not modify any arguments passed to it.
  • The function may not exceed one line of code.


Bonus

50 additional bits or simply added admiration if you can improve upon the standard library's "join" and make it work with lists containing elements that are not strings.
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Sat Nov 26, 2005 6:20 pm   Post subject: (No subject)

Given the snippet of code from the interactive interpreter:

code:
>>> foo()
bar
>>>


Write the code which makes this possible. Your code may not include any kind of input/output operation.
wtd




PostPosted: Mon Nov 28, 2005 11:22 am   Post subject: (No subject)

wtd wrote:
Given the snippet of code from the interactive interpreter:

code:
>>> foo()
bar
>>>


Write the code which makes this possible. Your code may not include any kind of input/output operation.


The solution, since no one has expressed interest, is:

Python:
class foo(object):
   def __repr__(self):
      return "bar"
[Gandalf]




PostPosted: Mon Nov 28, 2005 4:40 pm   Post subject: (No subject)

I have not looked into Python too much, so forgive any mistakes, but...
Python:
   def foo():
      return "bar"

Could you not just do that?
wtd




PostPosted: Mon Nov 28, 2005 5:48 pm   Post subject: (No subject)

[Gandalf] wrote:
I have not looked into Python too much, so forgive any mistakes, but...
Python:
   def foo():
      return "bar"

Could you not just do that?


In that case, what'd you'd see would be:

code:
>>> foo()
"bar"
>>>
Hikaru79




PostPosted: Tue Nov 29, 2005 8:15 pm   Post subject: (No subject)

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
wtd




PostPosted: Tue Nov 29, 2005 8:43 pm   Post subject: (No subject)

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.
bugzpodder




PostPosted: Thu Sep 07, 2006 12:20 pm   Post subject: (No subject)

if os.path.getsize("foo.txt") = 0 : print "empty file"
wtd




PostPosted: Thu Sep 07, 2006 12:46 pm   Post subject: (No subject)

Not what I was looking for. Smile

What I am looking for is part of the loop.
bugzpodder




PostPosted: Fri Sep 08, 2006 3:29 pm   Post subject: (No subject)

hint
wtd




PostPosted: Fri Sep 08, 2006 10:43 pm   Post subject: (No subject)

See the Tutorial under the Documentation section on python.org. It has the answer.
Roman




PostPosted: Sat Aug 09, 2008 1:09 am   Post subject: Re: Test Your Skills: Python Thread

wtd @ Thu Sep 29, 2005 2:54 pm wrote:
Challenge #1: Join


code:
>>> ', '.join(['1', '2', '3'])
'1, 2, 3'




I'm very new at this, but couldn't you just do

code:

def joinFctn(sep, lst):
    print sep.join(lst)


P.S. Only three years late x_x
[Gandalf]




PostPosted: Thu Oct 02, 2008 8:00 am   Post subject: Re: Test Your Skills: Python Thread

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

!!!
Zeroth




PostPosted: Thu Oct 02, 2008 9:05 am   Post subject: Re: Test Your Skills: Python Thread

Your mixing up the challenges Gandalf. One of the challenges, you cannot return anything. The other you can.
SS_198




PostPosted: Sun Feb 22, 2009 2:32 pm   Post subject: Re: Test Your Skills: Python Thread

Here a way to make your python program write something without typing return : or print


python : 3.0

def helloWorld():
print ("helloWorld") # this line does not print helloworld

helloWorld() # This line print hello world


# you can type helloWorld() as many times as you like, and will print > HelloWorld <
Display posts from previous:   
   Index -> Python
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

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


Style:  
Search: