What would the two lines of code be if we wanted to print the first and last number of an integer instead of the first and last letter? I’ve not had any luck in coming up with the solution on my own.
The constraint put in the exercise " assuming the string is at least 2 characters long", shouldn’t be somehow written in the code? Maybe by using an if statement?
I tried to put a string one-character long, e.g. ‘H’ and the output is ‘HH’ ; a result which I think is not thorougly consistent with the way the exercise was expressed.
However, when I added … if len(x)>2 else ‘None’ and wrote again print(mylambda(‘H’)),
then the output was ‘None’.
The article says the output should look like ‘Tg’ (notice the quotes). I had to add those manually to make it look like the article’s output. Was there some other way I should be doing it? Also, I had to put an else behavior to add the >2 check.
mylambda = lambda x: "'"+x[0]+x[-1]+"'" if len(x) > 2 else "String too short"
print(mylambda('This is a string'))
I did a letter count of the following functions (not including spaces and line breaks)
def say_hello(name):
print(f"Hi there {name}!!")
say_hello = lambda name: print(f"Hi there {name}!!")
lambda functions are 46 letters, 2 letters longer than normal functions so which one is better (I know it’s just two letters but still, I am just asking)
@noot-noot it’s not about character counts. I think efficiency in Python is more dictated by lines. It also has the ability of being declared without a name so good option for one use throw away functions.