In Python, I am trying to write two functions. First, I am taking in a list as a parameter and returning it as a single integer. Second, I am taking in a list as a parameter and returning it as a set.
When I attempt to run the program using the given test functions, I have run into two errors I cannot solve. (A) I get a type error that says that "strings = [str(integer) for integer in list_to_int]" is not iterable and (B) I have unreferenced variables for an_integer and converted_set. I have tried to rearrange the assert functions and redo the variables but nothing seems to work.
def list_to_int(list):
"""
:param list
convert list to string
convert string to int
:return: set
"""
strings = [str(integer) for integer in list_to_int]
a_string = "".join(strings)
an_integer = int(a_string)
return an_integer
def list_to_set(list):
"""
:param list:
comvert to set
:return: set
"""
converted_set = set(list_to_set)
return converted_set
# TEST FUNCTIONS
assert list_to_int([1, 2, 3, 4]) == 1234, 'list_to_int([1, 2, 3, 4]) expected 1234'
print("correct")
assert list_to_int([4, 3, 2, 1]) == 4321, 'list_to_int([4, 3, 2, 1]) expected 4321'
print("correct")
assert list_to_int([3, 1, 2, 4]) == 3124, 'list_to_int([3, 1, 2, 4]) expected 3124'
print("correct")
assert list_to_set([1, 2, 3, 4]) == 1234, 'list_to_int([1, 2, 3, 4]) expected 1234'
print("correct")
assert list_to_set([4, 3, 2, 1]) == 4321, 'list_to_int([4, 3, 2, 1]) expected 4321'
print("correct")
assert list_to_set([3, 1, 2, 4]) == 3124, 'list_to_int([3, 1, 2, 4]) expected 3124'
print("correct")
CodePudding user response:
In both functions, you are mistakenly referencing the function name as the iterator in the list concatenation rather than the list parameter.
In other words, in list_to_int, change
strings = [str(integer) for integer in list_to_int]
to
strings = [str(integer) for integer in list]
and in list_to_set change
converted_set = set(list_to_set)
to
converted_set = set(list)
Also, I would recommend against naming function parameters list. The name list is reserved as the default constructor for list objects, so its best to not overwrite it in that context and name the parameter something else (i.e. l, arr, etc.)
CodePudding user response:
You are iterating over list_to_int, which is the function itself... you have to iterate over the list parameter.
Same thing with the second function; you are trying to convert the actual function to a set instead of the function's parameter.
Re-write your code like this:
def list_to_int(list):
"""
:param list
convert list to string
convert string to int
:return: set
"""
strings = [str(integer) for integer in list]
a_string = "".join(strings)
an_integer = int(a_string)
return an_integer
def list_to_set(list):
"""
:param list:
convert to set
:return: set
"""
converted_set = set(list)
return converted_set
# TEST FUNCTIONS
assert list_to_int([1, 2, 3, 4]) == 1234, 'list_to_int([1, 2, 3, 4]) expected 1234'
print("correct")
assert list_to_int([4, 3, 2, 1]) == 4321, 'list_to_int([4, 3, 2, 1]) expected 4321'
print("correct")
assert list_to_int([3, 1, 2, 4]) == 3124, 'list_to_int([3, 1, 2, 4]) expected 3124'
print("correct")
assert list_to_set([1, 2, 3, 4]) == 1234, 'list_to_int([1, 2, 3, 4]) expected 1234'
print("correct")
assert list_to_set([4, 3, 2, 1]) == 4321, 'list_to_int([4, 3, 2, 1]) expected 4321'
print("correct")
assert list_to_set([3, 1, 2, 4]) == 3124, 'list_to_int([3, 1, 2, 4]) expected 3124'
print("correct")
