Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions 100+ Python challenging programming exercises.txt
Original file line number Diff line number Diff line change
Expand Up @@ -407,14 +407,14 @@ Use a list comprehension to square each odd number in a list. The list is input
Suppose the following input is supplied to the program:
1,2,3,4,5,6,7,8,9
Then, the output should be:
1,3,5,7,9
1, 2, 9, 4, 25, 6, 49, 8, 81

Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.

Solution:
values = raw_input()
numbers = [x for x in values.split(",") if int(x)%2!=0]
values = raw_input() # input() in python3
numbers = [int(x)**2 if int(x) % 2 != 0 else int(x) for x in values.split(",")]
print ",".join(numbers)

Choose a reason for hiding this comment

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

Great solution but here numbers list contains integers whereas join() method is only applicable with iterables that contain either strings or unicode object. You need to convert values in numbers to string first to be able to use join().

#----------------------------------------#

Expand Down