Kebabize
Instructions
Modify the kebabize
function so that it converts a camel case string into a kebab case.
kebabize('camelsHaveThreeHumps') // camels-have-three-humps
kebabize('camelsHave3Humps') // camels-have-humps
Notes:
- the returned string should only contain lowercase letters
My Submit
def kebabize(string):
for c in string:
if c.isupper():
string = string.replace(c, '-'+c.lower())
elif c.islower():
continue
else:
string = string.replace(c,'')
if string.startswith('-'):
return string[1:]
else:
return string
Best Practices
def kebabize(s):
return ''.join(c if c.islower() else '-' + c.lower() for c in s if c.isalpha()).strip('-')
Summary
python 语法掌握不牢固
Unique In Order
Instructions
Implement the function unique_in_order which takes as argument a sequence and returns a list of items without any elements with the same value next to each other and preserving the original order of elements.
For example:
unique_in_order('AAAABBBCCDAABBB') == ['A', 'B', 'C', 'D', 'A', 'B']
unique_in_order('ABBCcAD') == ['A', 'B', 'C', 'c', 'A', 'D']
unique_in_order([1,2,2,3,3]) == [1,2,3]
My Submit
def unique_in_order(iterable):
list = []
fore = ''
for c in iterable:
if c is fore:
continue
else:
list.append(c)
fore = c
return list
Best Practices
from itertools import groupby
def unique_in_order(iterable):
return [k for (k, _) in groupby(iterable)]
Summary
新姿势get
itertools
Good vs Evil
Instruction
Description
Middle Earth is about to go to war. The forces of good will have many battles with the forces of evil. Different races will certainly be involved. Each race has a certain 'worth' when battling against others. On the side of good we have the following races, with their associated worth:
- Hobbits - 1
- Men - 2
- Elves - 3
- Dwarves - 3
- Eagles - 4
- Wizards - 10
On the side of evil we have:
- Orcs - 1
- Men - 2
- Wargs - 2
- Goblins - 2
- Uruk Hai - 3
- Trolls - 5
- Wizards - 10
Although weather, location, supplies and valor play a part in any battle, if you add up the worth of the side of good and compare it with the worth of the side of evil, the side with the larger worth will tend to win.
Thus, given the count of each of the races on the side of good, followed by the count of each of the races on the side of evil, determine which side wins.
Input:
The function will be given two parameters. Each parameter will be a string separated by a single space. Each string will contain the count of each race on the side of good and evil.
The first parameter will contain the count of each race on the side of good in the following order:
- Hobbits, Men, Elves, Dwarves, Eagles, Wizards.
The second parameter will contain the count of each race on the side of evil in the following order:
- Orcs, Men, Wargs, Goblins, Uruk Hai, Trolls, Wizards.
All values are non-negative integers. The resulting sum of the worth for each side will not exceed the limit of a 32-bit integer.
Output:
Return ""Battle Result: Good triumphs over Evil" if good wins, "Battle Result: Evil eradicates all trace of Good" if evil wins, or "Battle Result: No victor on this battle field" if it ends in a tie.
My Submit
def goodVsEvil(good, evil):
good_worth = [1, 2, 3, 3, 4, 10]
evil_worth = [1, 2, 2, 2, 3, 5, 10]
good_total = 0
evil_total = 0
for g_worth, g_count in zip(good_worth, good.split(' ')):
good_total += g_worth * int(g_count)
for e_worth, e_count in zip(evil_worth, evil.split(' ')):
evil_total += e_worth * int(e_count)
if good_total > evil_total:
return 'Battle Result: Good triumphs over Evil'
elif good_total < evil_total:
return 'Battle Result: Evil eradicates all trace of Good'
else:
return 'Battle Result: No victor on this battle field'
Best Practices
def goodVsEvil(good, evil):
points_good = [1, 2, 3, 3, 4, 10]
points_evil = [1, 2, 2, 2, 3, 5, 10]
good = sum([int(x)*y for x, y in zip(good.split(), points_good)])
evil = sum([int(x)*y for x, y in zip(evil.split(), points_evil)])
result = 'Battle Result: '
if good < evil:
return result +'Evil eradicates all trace of Good'
elif good > evil:
return result + 'Good triumphs over Evil'
else:
return result + 'No victor on this battle field'
Summary
设定很有意思的题目,主要是使用zip来同时遍历两个list
忘记了用sum()
来处理求和
split()
默认使用空格来分割,不需要单独指定