Codewars算法题(7)
来源:互联网 发布:360wifi搜索不到网络 编辑:程序博客网 时间:2024/05/22 00:24
第23题:(Convert string to camel case)
问题:
Complete the method/function so that it converts dash/underscore delimited words into camel casing. The first word within the output should be capitalized only if the original word was capitalized.
Examples:
# returns "theStealthWarrior"to_camel_case("the-stealth-warrior") # returns "TheStealthWarrior"to_camel_case("The_Stealth_Warrior")
问题描述:就是将给定字符串变为:第一个单词不变。之后的每个单词的首字母大写。类似于驼峰命名自己代码:
def to_camel_case(text): new1 = text.replace("-", "_") new2 = new1.split("_") new='' if text=='': return '' else: for s in new2: new += s[:1].upper() + s[1:].lower() return text[0]+''.join(new)[1:]还是一如既往的运用最简单的if else for语句~~~~~~~真是让人脑仁疼!!
评分较高代码:
(1)
def to_camel_case(s): return s[0] + s.title().translate(None, "-_")[1:] if s else stitle()的作用是将每个单词的首字母大写;而capitalize()的作用是将首单词的首字母大写;
(2)
def to_camel_case(text): return text[0] + ''.join([w[0].upper() + w[1:] for w in text.replace("_", "-").split("-")])[1:] if text else ''我感觉这位仁兄的想法和我的差不多,只不过他写成了一个合着的语句。
(3)
import redef to_camel_case(text): return reduce(lambda p, n: p + n[0].upper() + n[1:], re.split('[-_]', text))
第24题:(Bouncing Balls)
问题:
A child plays with a ball on the nth floor of a big building. The height of this floor is known:
(float parameter "h" in meters, h > 0)
.
He lets out the ball. The ball rebounds for example to two-thirds:
(float parameter "bounce", 0 < bounce < 1)
of its height.
His mother looks out of a window that is 1.5 meters from the ground:
(float parameters window < h).
How many times will the mother see the ball either falling or bouncing in front of the window
(return a positive integer unless conditions are not fulfilled in which case return -1)
?
Note
You will admit that the ball can only be seen if the height of the rebouncing ball is stricty greater than the window parameter.
Example:
问题描述:给定楼层数(从该楼层将球扔出),给定弹跳消减系数,给定窗户高度(当球的高度>窗户高度时才可看 到球),求当球从某层扔出后,能看到球的次数?h = 3, bounce = 0.66, window = 1.5, result is 3
h = 3, bounce = 1, window = 1.5, result is -1
自己代码:
def bouncingBall(h, bounce, window): n=0 if bounce>=1 or bounce<=0: return -1 while (h>window): h=h*bounce n+=1 return 1+(n-1)*2
第25题:(Write Number in Expanded Form)(以扩展形式写数字)
问题:
You will be given a number and you will need to return it as a string in Expanded Form. For example:
expanded_form(12) # Should return '10 + 2'expanded_form(42) # Should return '40 + 2'expanded_form(70304) # Should return '70000 + 300 + 4'
自己代码:def expanded_form(num): s=[] lnum1=map(int,str(num)) lnum2=list(lnum1) for i in range(0,len(lnum2)): if lnum2[-(i+1)]!=0: s.insert(0,str(lnum2[-(i+1)]*10**(i))) return ' + '.join(s)
代码解析:
首先将利用map()函数将整数转化为列表,其实在这里lnum1已经是列表了,但是在程序执行过程中仍报错,所以我又转化了一下。其实我是逆向将数字插入到列表中s中的,比如:1234,我先处理4,再处理3(通过insert()函数将处理的结果30插入4之前),再处理2,再处理1。最终再将列表转化为字符串形式。
评分较高代码:
def expanded_form(n): result = [] for a in range(len(str(n)) - 1, -1, -1): current = 10 ** a quo, n = divmod(n, current) if quo: result.append(str(quo * current)) return ' + '.join(result)代码解析:通过divmod()函数,求得商和余数(实现了一个小循环,上次的余数,当做本次的除数)
201707271623 持续更新~~~~~
- Codewars算法题(7)
- codewars算法题(3)
- Codewars算法题(4)
- Codewars算法题(5)
- Codewars算法题(6)
- Codewars算法题(8)
- codewars算法题(求因子)
- codewars算法题(找零钱)
- codewars算法题-求因子
- codewars算法题-Array.diff
- codewars算法题-酒局划拳
- codewars算法题-求两数组元素长度差值极大值
- codewars算法题-Sum without highest and lowest number
- CodeWars---Python第二题
- CodeWars---Python第三题
- CodeWars---Python第四题
- 菜圈的codewars(三),水题好多!
- Rust: codewars的Simple Substitution Cipher Helper算法题、N种不简单的解法集
- QT5 VS2010安装与配置
- 运维技巧
- ajax数据获取成功,却从error返回
- CSS常见布局解决方案
- 学习ECMAScript(2017年7月27日)
- Codewars算法题(7)
- JDK、JRE、JVM三者间的关系
- NS-3学习笔记 2
- Java基础之String,StringBuffer与StringBuilder的区别及应用场景
- scrapy 抓取的中文结果乱码,请问如何解决?
- 微服务好文章
- 树莓派3 远程控制方法
- linux免密码登录
- jquery datatables之Requested unknown parameter '' for row column