Posts: 30
Threads: 15
Joined: Sep 2016
"""
Create a function called list2str , that takes two arguments - a list of
strings L, and a delimeter D. The function must:
1. Return a single string, containing all of the input strings joined together
with D as a delimeter between them. The result should not start or end with
the delimeter D - D should be used only between the substrings.
2. When inspected, would return the following help string:
Join the list L using the delimeter D
For example:
list2str( ['a','b','c'], ',') -> 'a,b,c'
list2str('We','are','the','location','cloud','.') -> 'We are the location cloud.'
"""
Posts: 2,168
Threads: 35
Joined: Sep 2016
Same applies as to your other post, this is your homework assignment that you copy pasted.
What is your question ?
Posts: 3,458
Threads: 101
Joined: Sep 2016
You really should try before posting. We don't do people's work for them, only help when they have an issue.
That said, the second test case doesn't match the explanation given. Why isn't the list of strings a single list, instead of individual arguments? And why isn't the output "We.are.the.location.cloud"?
Posts: 30
Threads: 15
Joined: Sep 2016
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
def list2str( * args):
x = args[ 0 ]
y = args[ 1 ]
p = len (x)
z = ''
for i in range (p):
q = x[i] + y
z + = str (q)
R = str (z)[: - 1 ]
print ( )
a = [ 'We' , 'are' , 'the' , 'location' , 'cloud' ]
b = '.'
list2str(a,b)
|
Posts: 2,342
Threads: 62
Joined: Sep 2016
Nov-29-2016, 05:56 PM
(This post was last modified: Nov-30-2016, 04:20 AM by micseydel.)
Stop posting code with no details. Give a natural language explanation of the problem. Say your expected vs. actual output and describe what's wrong. You've also been told in another thread that using *args that way is not in line with your assignment, but it's also much less readable. Lastly, using single-letter variable names, especially as many as you are here, is very difficult to read - please use meaningful names (it's ok to use names prescribed by your assignment, as in another thread, but that's not what's happening here).
Posts: 3,458
Threads: 101
Joined: Sep 2016
That fails the example output, though:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
>>> def list2str( * args):
... x = args[ 0 ]
... y = args[ 1 ]
... p = len (x)
... z = ''
... for i in range (p):
... q = x[i] + y
... z + = str (q)
... R = str (z)[: - 1 ]
... print ( )
...
>>> a = [ 'We' , 'are' , 'the' , 'location' , 'cloud' ]
>>> b = '.'
>>> list2str(a,b)
'We.are.the.location.cloud'
|
The output of yours is "We.are.the.location.cloud" when it should be "We are the location cloud.". Also, you're calling it differently than the example usage, as you're doing list2str(['We', 'are', 'the', 'location', 'cloud'], '.'), while the example usage is list2str('We', 'are', 'the', 'location', 'cloud', '.'). That said, yours does work fine for the first test case:
1 2 |
>>> list2str( [ 'a' , 'b' , 'c' ], ',' )
'a,b,c'
|
You should either contact your professor to see if that expected input/output is correct for the second case, or rework your function to handle both situations.
|