Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HW8 solution #17

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

HW8 solution #17

wants to merge 2 commits into from

Conversation

vladspirin
Copy link
Owner

@vladspirin vladspirin commented Nov 4, 2024

I optimized my solution six times. I don't know; maybe it can be even shorter.

@vladspirin vladspirin requested a review from antohaUa November 4, 2024 11:43
@vladspirin vladspirin self-assigned this Nov 4, 2024
lesson_08/homework8.py Outdated Show resolved Hide resolved
lesson_08/homework8.py Outdated Show resolved Hide resolved
@vladspirin vladspirin requested a review from antohaUa November 6, 2024 13:03
@vladspirin
Copy link
Owner Author

vladspirin commented Nov 6, 2024

As you said in the comments, convert_and_replace() can be improved using list comprehension (and I still strongly feel that I skipped smth), but I have only this solution for now. I'm still wondering what I should change here. I'm unsure about the function's name because it's not only "convert and replace" but also its sum.

Comment on lines +35 to +41
result = []
for elem in lst:
try:
# Added strip() in case of unpredictable spaces
result.append([int(num.strip()) for num in elem.split(',')])
except ValueError:
result.append("I can't do that!") # Add msg if unable to convert
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to iterate over the list twice
In direct action we return sum of list comprehension in case of error we return phrase

Comment on lines +45 to +60
def convert_and_replace(lst):
"""
Process a list by summing sublists or replacing with a message.

Args:
lst (list): List of sublists or messages.
Returns:
list: List with either sums of integers or replacement message.
"""
for indx, sublist in enumerate(lst):
if isinstance(sublist, list): # Check if sublist is list
try:
lst[indx] = sum(sublist) # Sum if list with int
except TypeError:
lst[indx] = "I can't do that!"
return lst
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According prev comment this function will be absolutely redundant .
It just adds complexity with additional iterations and errors handling

Comment on lines +64 to +66
req_lst = ['1,2,3,4', '1,2,3,4,50', 'qwerty1,2,3']
# Call the functions
result = convert_and_replace(split_and_convert(req_lst))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you could have list comprehension with target function call for each element to get sum or text

@vladspirin vladspirin added the need to improve There is a possibility of improving the code label Nov 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
need to improve There is a possibility of improving the code
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

2 participants