Computer Science Canada Test Your Skills: Python Thread |
Author: | wtd [ 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.
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
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. |
Author: | wtd [ Sat Nov 26, 2005 6:20 pm ] | ||
Post subject: | |||
Given the snippet of code from the interactive interpreter:
Write the code which makes this possible. Your code may not include any kind of input/output operation. |
Author: | wtd [ Mon Nov 28, 2005 11:22 am ] | ||||
Post subject: | |||||
wtd wrote: Given the snippet of code from the interactive interpreter:
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:
|
Author: | [Gandalf] [ Mon Nov 28, 2005 4:40 pm ] | ||
Post subject: | |||
I have not looked into Python too much, so forgive any mistakes, but...
Could you not just do that? |
Author: | wtd [ Mon Nov 28, 2005 5:48 pm ] | ||||
Post subject: | |||||
[Gandalf] wrote: I have not looked into Python too much, so forgive any mistakes, but...
Could you not just do that? In that case, what'd you'd see would be:
|
Author: | Hikaru79 [ Tue Nov 29, 2005 8:15 pm ] | ||
Post subject: | |||
Okay, my turn to give one This is my first try at this, so be gentle. I'll come up with more soon Here it is. Add one line of code so that the following line executes properly (and gives the result that one would intuitively assume).
I hope this isn't too easy, hard, pointless, etc |
Author: | wtd [ Tue Nov 29, 2005 8:43 pm ] | ||
Post subject: | |||
Add one line of code to this such that it prints "Whoops! Empty file." if there's nothing in the file. |
Author: | bugzpodder [ Thu Sep 07, 2006 12:20 pm ] |
Post subject: | |
if os.path.getsize("foo.txt") = 0 : print "empty file" |
Author: | wtd [ Thu Sep 07, 2006 12:46 pm ] |
Post subject: | |
Not what I was looking for. What I am looking for is part of the loop. |
Author: | bugzpodder [ Fri Sep 08, 2006 3:29 pm ] |
Post subject: | |
hint |
Author: | wtd [ Fri Sep 08, 2006 10:43 pm ] |
Post subject: | |
See the Tutorial under the Documentation section on python.org. It has the answer. |
Author: | Roman [ 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
I'm very new at this, but couldn't you just do
P.S. Only three years late x_x |
Author: | [Gandalf] [ 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:
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. !!! |
Author: | Zeroth [ 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. |
Author: | SS_198 [ 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 < |
Author: | zuzelvp [ 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:
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. !!![/quote] join = lambda lst, sep: reduce(lambda x, y: x + sep + y, lst, '').replace(sep, '', 1) |
Author: | zuzelvp [ Sat May 22, 2010 10:49 am ] | ||
Post subject: | |||
Hikaru79 @ Tue Nov 29, 2005 8:15 pm wrote: Okay, my turn to give one This is my first try at this, so be gentle. I'll come up with more soon Here it is.
Add one line of code so that the following line executes properly (and gives the result that one would intuitively assume).
I hope this isn't too easy, hard, pointless, etc Anyone coming from languages like C# will need to learn this one |
Author: | DtY [ Sat May 22, 2010 2:15 pm ] | ||||||||
Post subject: | |||||||||
Hikaru79 @ Tue Nov 29, 2005 8:15 pm wrote: Okay, my turn to give one This is my first try at this, so be gentle. I'll come up with more soon Here it is.
What do you mean "properly", I assume returns the dictionary that contains those?
Add one line of code so that the following line executes properly (and gives the result that one would intuitively assume).
I hope this isn't too easy, hard, pointless, etc
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:
The output looks like this:
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. |
Author: | cavetroll [ Sat May 22, 2010 10:37 pm ] | ||||||||||
Post 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 This is my first try at this, so be gentle. I'll come up with more soon Here it is.
What do you mean "properly", I assume returns the dictionary that contains those?
Add one line of code so that the following line executes properly (and gives the result that one would intuitively assume).
I hope this isn't too easy, hard, pointless, etc
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:
The output looks like this:
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
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? |
Author: | tedying [ Fri Jan 20, 2012 11:43 pm ] | ||
Post subject: | |||
Hikaru79 @ Tue Nov 29, 2005 8:15 pm wrote: Okay, my turn to give one This is my first try at this, so be gentle. I'll come up with more soon Here it is.
Add one line of code so that the following line executes properly (and gives the result that one would intuitively assume).
I hope this isn't too easy, hard, pointless, etc def Dict(**x): ??return x |
Author: | tedying [ Fri Jan 20, 2012 11:45 pm ] | ||||||||
Post 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 This is my first try at this, so be gentle. I'll come up with more soon Here it is.
What do you mean "properly", I assume returns the dictionary that contains those?
Add one line of code so that the following line executes properly (and gives the result that one would intuitively assume).
I hope this isn't too easy, hard, pointless, etc
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:
The output looks like this:
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)] |
Author: | tedying [ Fri Jan 20, 2012 11:49 pm ] | ||
Post subject: | |||
wtd @ Tue Nov 29, 2005 8:43 pm wrote:
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") |