How would I write this function in 1 return statement?
Question:
So I’m having trouble with writing this Cipher Function in one line. I’ve tried using the result methods with the if and else statements. For instance I know that the I need to do the else statement first before the if statement buts I could be wrong. So I’m trying to figure this out.
How would I write this function in 1 return statement?
def cipher(self):
string = ""
for i in self.string:
if i.isalpha():
if i.isupper():
alphabet = (ord(i) - 65 + len(self.string)) % (26)
alphabet += 65
if i.islower():
alphabet = (ord(i) - 97 + len(self.string)) % (26)
alphabet += 97
letter = chr(alphabet)
else:
letter = i
string += letter
return string
return string
I’ve tried multiple times but it would always show an error
Answers:
I agree with barmar. this is totally unreadable and not advised…
But here is a list comprehension version anyway. I split it into multiple lines to try to improve readability a little bit, but i still wouldn’t recommend this.
text = "Hello World"
def cipher(text):
return ''.join(
[i if not i.isalpha() else
chr((((ord(i) - 65 + len(text)) % 26) + 65)
if i.isupper() else
(((ord(i)-97 + len(text)) %26) +97))
for i in text])
cipher(text)
Output:
Spwwz Hzcwo
with all the caveats of Barmar’s super valid comment (and this seems very Code Golf-y, which you might enjoy), but here we go:
In [7]: string = "hello my #1 person!"
In [8]: ''.join([(chr((ord(i) - (65 if i.isupper() else 97) + len(string)) % (26) + (65 if i.isupper() else 97)) if i.isalpha() else i) for i in string])
Out[8]: 'axeeh fr #1 ixklhg!'
So I’m having trouble with writing this Cipher Function in one line. I’ve tried using the result methods with the if and else statements. For instance I know that the I need to do the else statement first before the if statement buts I could be wrong. So I’m trying to figure this out.
How would I write this function in 1 return statement?
def cipher(self):
string = ""
for i in self.string:
if i.isalpha():
if i.isupper():
alphabet = (ord(i) - 65 + len(self.string)) % (26)
alphabet += 65
if i.islower():
alphabet = (ord(i) - 97 + len(self.string)) % (26)
alphabet += 97
letter = chr(alphabet)
else:
letter = i
string += letter
return string
return string
I’ve tried multiple times but it would always show an error
I agree with barmar. this is totally unreadable and not advised…
But here is a list comprehension version anyway. I split it into multiple lines to try to improve readability a little bit, but i still wouldn’t recommend this.
text = "Hello World"
def cipher(text):
return ''.join(
[i if not i.isalpha() else
chr((((ord(i) - 65 + len(text)) % 26) + 65)
if i.isupper() else
(((ord(i)-97 + len(text)) %26) +97))
for i in text])
cipher(text)
Output:
Spwwz Hzcwo
with all the caveats of Barmar’s super valid comment (and this seems very Code Golf-y, which you might enjoy), but here we go:
In [7]: string = "hello my #1 person!"
In [8]: ''.join([(chr((ord(i) - (65 if i.isupper() else 97) + len(string)) % (26) + (65 if i.isupper() else 97)) if i.isalpha() else i) for i in string])
Out[8]: 'axeeh fr #1 ixklhg!'