Python Question: '00101' --> '11010'

Asked 2 years ago, Updated 2 years ago, 14 views

x='00101'

Given a string x consisting of only '0' and '1' with length 5 as above (the value of x is not the same as above). Just an example)

y='11010'

Write a program to find a string y whose digit values of x are converted to '0' and '1' to '0' as shown above.

python

2022-09-20 21:54

2 Answers

x = '000010'
y = ''

for i in x:
    y += str(int(i) ^ 1)

print (y)

It seems good to do your homework on your own.


2022-09-20 21:54

>>> x = '00101'
>>> y = str(int('1' * len(x)) - int(x)).zfill(len(x))
>>> y
'11010'


2022-09-20 21:54

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.