Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialDavid Wright
4,437 PointsI'm being silly on the SillyCase Challenge
Please, help with this silliness!
# The first half of the string, rounded with round(), should be lowercased.
# The second half should be uppercased.
# E.g. "Treehouse" should come back as "treeHOUSE"
def sillycase(string):
string_length = len(string)
half_string = string_length / 2
first_half = string[:half_string]
result = first_half.extend(string[half_string:])
return result
3 Answers
Chris Freeman
Treehouse Moderator 68,457 PointsYou are very close. A few corrections to you code. extend()
works on lists but not on strings.
def sillycase(string):
string_length = len(string)
# need to round when length is odd
half_string = round(string_length / 2)
first_half = string[:half_string]
# lower first half and upper second half
result = first_half.lower() + string[half_string:].upper()
return result
Innocent Ngwaru
7,554 PointsHow abou this. Where am i wrong if I decide to use this method
def sillycase(input_string):
pivot_index = [(len(input_string)+1)/2]
char_list = [char.lower() if index < pivot else char.upper() for index, char in enumerate(input_string)]
return sillycase(input_string.join(char_list))
[MOD: added ```python markdown formatting -cf]
Chris Freeman
Treehouse Moderator 68,457 PointsInnocent, your code does not seem it pass. pivot
isn't defined, the pivot value should be an int
not a list, and the pivot value needs to adjust for 0-based indexing:
def sillycase(input_string):
pivot = (len(input_string))/2
char_list = [char.lower() if index <= pivot else char.upper() for index, char in enumerate(input_string)]
return ''.join(char_list)
Your approach has an interesting list compression which works but might be less readable than using a simple list slice. It would be interesting to time it to see which is faster.
Innocent Ngwaru
7,554 PointsThank you Chris. I took a leaf from your response above. I was doing it the hard way. Thank you!
Chris Freeman
Treehouse Moderator 68,457 PointsAfter using timeit
to compare your solution and the best answer, your code takes 3x more time:
import timeit
setup="""def sillycase(input_string):
pivot = (len(input_string))/2
char_list = [char.lower() if index <= pivot else char.upper() for index, char in enumerate(input_string)]
return ''.join(char_list)
"""
setupBA = """def sillycaseOP(string):
string_length = len(string)
# need to round when length is odd
half_string = round(string_length / 2)
first_half = string[:half_string]
# lower first half and upper second half
result = first_half.lower() + string[half_string:].upper()
return result
"""
print( timeit.timeit(stmt="sillycaseBA('treehouse')", setup=setupBA, number=100000) )
print( timeit.timeit(stmt="sillycase('treehouse')", setup=setup, number=100000) )
3.149674166692421
10.27628900000127