Optimizing string capitalization challenge - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Optimizing string capitalization challenge (/thread-15013.html) |
Optimizing string capitalization challenge - Drone4four - Dec-30-2018 Here is the Udemy instructor's challenge: Quote:Write a function that capitalizes the first and fourth letters of a name Here is my solution: def old_macdonald(name): first_cap = name[0].capitalize() + name[1:3] second_cap = name[3].capitalize() + name[4:] together = first_cap + second_cap return togetherThis produces the desired output: MacDonald but I was wondering how you people would perhaps optimize this or re-write it in a more Pythonic way?For my future reference, the course material I am working is publicly available here: Pierian-Data/Complete-Python-3-Bootcamp. The specific sub- module I am working on is: 03-Methods and Functions/03-Function Practice Exercises.ipynb RE: Optimizing string capitalization challenge - Gribouillis - Dec-30-2018 Hint: >>> "mac".capitalize() 'Mac' RE: Optimizing string capitalization challenge - Drone4four - Jan-01-2019 (Dec-30-2018, 03:57 PM)Gribouillis Wrote: Hint: Thank you for the hint, Gribouillis. Programming is like algebra. In my script name carries the string "macdonald". So I rewrote my function by subsitituing all instances of name with the actual string "macdonald". As a result, I call the script differently as well. Based on your hint, here is what my script now:def old_macdonald(): reformatted = "macdonald"[0].capitalize() + "macdonald"[1:3] + "macdonald"[3].capitalize() + "macdonald"[4:] return reformattedWhen I invoke the function in my interpreter with old_macdonald() , it still successfully prints the expected output: 'MacDonald'. So it still works. However my script is less dynamic because now I can't pass any other string as a parameter. Also, line 2 extends way beyond the right margin of my text editor. So for these two reasons I suppose my new script isn't much of an improvement or any more Pythonic. I'd even say it's kinda less Pythonic. hahaInstead of another hint, I think I am ready to recieve the answer from you people. After all, this isn't for credit. It's just a Udemy course. RE: Optimizing string capitalization challenge - Gribouillis - Jan-01-2019 You didn't understand the hint. I meant def old_macdonald(name): return name[:4].capitalize() + name[4:].capitalize()By the way if you have a very long expression, you can write it on several lines with the help of parentheses result = (foobarbazqux[100034:].capitalize() + x + bazbarquxfoo[800:812].capitalize() + quxbarfooqux("makdoland").upper()) RE: Optimizing string capitalization challenge - Drone4four - Jan-01-2019 Just a minor correction: The return statement using 4 produces 'MacdOnald'. Changing 4 to 3 creates what we are expecting. I see the slicing syntax and why Python parses this as output.Thanks again Gribouillis for your advice. |