Skip to main content
Code Review

Return to Question

Commonmark migration
Source Link

I found the following challenge online:

Create a function that takes a string and returns that string with the first half lowercased and the last half uppercased.

eg: foobar == fooBAR

If it is an odd number then 'round' it up to find which letters to uppercase.

That sounded easy enough but I found the last requirement a little tricky. Eventually this is what I came up with:

def sillycase(silly):
 length=len(silly)
 firstHalf = silly[:length / 2 + (length % 2)].lower()
 secondHalf = silly[length / 2 + (length % 2):].upper()
 return firstHalf + secondHalf 

but that seems repetitive and inefficient.

What could I have done to make it slicker, without sacrificing readability? I was actually a little surprised python didn't have a built in method to split a string in half.

I found the following challenge online:

Create a function that takes a string and returns that string with the first half lowercased and the last half uppercased.

eg: foobar == fooBAR

If it is an odd number then 'round' it up to find which letters to uppercase.

That sounded easy enough but I found the last requirement a little tricky. Eventually this is what I came up with:

def sillycase(silly):
 length=len(silly)
 firstHalf = silly[:length / 2 + (length % 2)].lower()
 secondHalf = silly[length / 2 + (length % 2):].upper()
 return firstHalf + secondHalf 

but that seems repetitive and inefficient.

What could I have done to make it slicker, without sacrificing readability? I was actually a little surprised python didn't have a built in method to split a string in half.

I found the following challenge online:

Create a function that takes a string and returns that string with the first half lowercased and the last half uppercased.

eg: foobar == fooBAR

If it is an odd number then 'round' it up to find which letters to uppercase.

That sounded easy enough but I found the last requirement a little tricky. Eventually this is what I came up with:

def sillycase(silly):
 length=len(silly)
 firstHalf = silly[:length / 2 + (length % 2)].lower()
 secondHalf = silly[length / 2 + (length % 2):].upper()
 return firstHalf + secondHalf 

but that seems repetitive and inefficient.

What could I have done to make it slicker, without sacrificing readability? I was actually a little surprised python didn't have a built in method to split a string in half.

Tweeted twitter.com/#!/StackCodeReview/status/588218168583290880
edited tags
Link
200_success
  • 145.6k
  • 22
  • 190
  • 479
edited title
Link
Phrancis
  • 20.5k
  • 6
  • 69
  • 155

Return a Split string with the firstin half upercased and the last half lowercasedchange case accordingly

Source Link
Seth
  • 213
  • 1
  • 4
  • 13
Loading
lang-py

AltStyle によって変換されたページ (->オリジナル) /