【发布时间】:2016-04-26 10:36:34
【问题描述】:
对于这个 Kata,我获得了 PEP8 格式的随机函数名称,我要将它们转换为 camelCase。
(输入)get_speed == (输出)getSpeed .... (输入)set_distance == (输出)setDistance
我对用伪代码编写的一种方法有所了解:
loop through the word,
if the letter is an underscore
then delete the underscore
then get the next letter and change to a uppercase
endIf
endLoop
return the resultant word
但我不确定最好的方法是创建一个 char 数组并循环遍历元素,然后在查找下划线时删除该元素并获取下一个索引并更改为大写,这样会不会更有效。
或者使用递归会更好:
function camelCase takes a string
if the length of the string is 0,
then return the string
endIf
if the character is a underscore
then change to nothing,
then find next character and change to uppercase
return the string taking away the character
endIf
finally return the function taking the first character away
请有任何想法,寻找处理此问题的有效方法。谢谢:)
【问题讨论】:
标签: java string loops recursion