Python Forum
Optimizing string capitalization challenge
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Optimizing string capitalization challenge
#3
(Dec-30-2018, 03:57 PM)Gribouillis Wrote: Hint:
>>> "mac".capitalize()
'Mac'

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 reformatted
When 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. haha

Instead 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.
Reply


Messages In This Thread
RE: Optimizing string capitalization challenge - by Drone4four - Jan-01-2019, 04:45 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Team Chooser Challenge kanchiongspider 3 2,458 Jun-02-2020, 04:02 AM
Last Post: kanchiongspider
  Meal cost challenge Emekadavid 3 2,953 Jun-01-2020, 02:01 PM
Last Post: Emekadavid
  Appending To Files Challenge erfanakbari1 3 3,040 Mar-27-2019, 07:55 AM
Last Post: perfringo
  Problem with a basic if challenge erfanakbari1 2 2,053 Oct-12-2018, 08:04 AM
Last Post: erfanakbari1
  trying an online challenge tozqo 8 6,089 Jun-21-2017, 07:07 AM
Last Post: Kebap

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020