codewars-7kyu:Sum of the first nth term of Series
Task:
Your task is to write a function which returns the sum of following series upto nth term(parameter).
Series: 1 + 1/4 + 1/7 + 1/10 + 1/13 + 1/16 +...
Rules:
You need to round the answer to 2 decimal places and return it as String.
If the given value is 0 then it should return 0.00
You will only be given Natural Numbers as arguments.
Examples:
SeriesSum(1) => 1 = "1.00"
SeriesSum(2) => 1 + 1/4 = "1.25"
SeriesSum(5) => 1 + 1/4 + 1/7 + 1/10 + 1/13 = "1.57"
my answer:
def series_sum(n):
# Happy Coding ^_^
temp_sum=1
if n==0:
temp_sum=0
while n>1:
temp_sum+=1/(3*n-2)
n-=1
return '%.2f'%temp_sum
优秀代码:
def series_sum(n):
return '{:.2f}'.format(sum(1.0/(3 * i + 1) for i in range(n)))
这个网站还是很不错的,从级数开始练习,每个阶段安装能力来匹配,我觉得这个模式比LeetCode好很多,因为LeetCode一上手感觉还是很难的,我这种菜鸡还是要先写一写基础的题目
反思:1.pyhton里面不可以用n--这种操作
2.python的round函数有问题,对整数部分是奇数还是偶数有要求,不一定会进位,不如使用
'%.2f'%temp_sum,确保变成浮点型
codewars-7kyu:Sum of the first nth term of Series的更多相关文章
- LightOj 1096 - nth Term (矩阵快速幂,简单)
题目 这道题是很简单的矩阵快速幂,可惜,在队内比赛时我不知什么时候抽风把模版中二分时判断的 ==1改成了==0 ,明明觉得自己想得没错,却一直过不了案例,唉,苦逼的比赛状态真让人抓狂!!! #incl ...
- LightOJ 1096 - nth Term 矩阵快速幂
http://www.lightoj.com/volume_showproblem.php?problem=1096 题意:\(f(n) = a * f(n-1) + b * f(n-3) + c, ...
- Sum of AP series——AP系列之和
A series with same common difference is known as arithmetic series. The first term of series is 'a' ...
- 10个经典的C语言面试基础算法及代码
10个经典的C语言面试基础算法及代码作者:码农网 – 小峰 原文地址:http://www.codeceo.com/article/10-c-interview-algorithm.html 算法是一 ...
- Project Euler 101 :Optimum polynomial 最优多项式
Optimum polynomial If we are presented with the first k terms of a sequence it is impossible to say ...
- (Problem 42)Coded triangle numbers
The nth term of the sequence of triangle numbers is given by, tn = ½n(n+1); so the first ten triangl ...
- lightoj刷题日记
提高自己的实力, 也为了证明, 开始板刷lightoj,每天题量>=1: 题目的类型会在这边说明,具体见分页博客: SUM=54; 1000 Greetings from LightOJ [简单 ...
- Project Euler:Problem 42 Coded triangle numbers
The nth term of the sequence of triangle numbers is given by, tn = ½n(n+1); so the first ten triangl ...
- 决战Leetcode: easy part(1-50)
本博客是个人原创的针对leetcode上的problem的解法,所有solution都基本通过了leetcode的官方Judging,个别未通过的例外情况会在相应部分作特别说明. 欢迎互相交流! em ...
随机推荐
- struts2 上传下载文件,异常处理,数据类型转换
一,web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version=" ...
- Django基础五之django模型层(二)多表操作
一 创建模型 表和表之间的关系 一对一.多对一.多对多 ,用book表和publish表自己来想想关系,想想里面的操作,加外键约束和不加外键约束的区别,一对一的外键约束是在一对多的约束上加上唯一约束. ...
- DOM增删操作(创建删除表格)
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...
- 【小程序】微信小程序绑定企业微信后怎样获取到用户信息
一.获取access_token 1.https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ID&corpsecret=SECRECT Cor ...
- VC++ 共享内存读写操作
此解决方案含两个工程文件,一个是写操作工程文件,即把任意字符串写入创建的共享内存里,另外一个读操作工程文件,则是读取共享内存里的数据,从而实现了进程之间的共享内存读写操作. 源码下载
- ExpandableListView控件实现二级列表
效果图如下: 二级列表附有点击事件. 1.布局文件: 此处加了一个自定义的导航RelativeLayout,记得注activity的时候添加 android:theme="@style/Th ...
- linux 链接命令
ln link /bin/ln -s 创建软链接ln -s [原文件] [链接文件] 软链接 ln -s /etc/issue /tmp/issue.soft硬链接ln /etc/issue /tmp ...
- Oracle存储过程_语法
create or replace procedure procedure_name --存储过程名字 ( --进行输入/输出的量 量_name in out 量_类型 --e.g. username ...
- popup定位引擎popper.js介绍
https://medium.com/@FezVrasta/popper-js-v1-5e8b3acd888c https://survivejs.com/blog/popper-interview/ ...
- 国内一元钱 正常搭建android开发环境
如果你人在gfw之外,那么此篇文章对你来说毫无用处,请自动略过.. 笔者自android出来之后,就一直想尝试一下.可惜,几年来一直未能够定下身心来研究尝试.而所做的工作也与android没有关系,所 ...