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的更多相关文章

  1. LightOj 1096 - nth Term (矩阵快速幂,简单)

    题目 这道题是很简单的矩阵快速幂,可惜,在队内比赛时我不知什么时候抽风把模版中二分时判断的 ==1改成了==0 ,明明觉得自己想得没错,却一直过不了案例,唉,苦逼的比赛状态真让人抓狂!!! #incl ...

  2. LightOJ 1096 - nth Term 矩阵快速幂

    http://www.lightoj.com/volume_showproblem.php?problem=1096 题意:\(f(n)  = a * f(n-1) + b * f(n-3) + c, ...

  3. Sum of AP series——AP系列之和

    A series with same common difference is known as arithmetic series. The first term of series is 'a' ...

  4. 10个经典的C语言面试基础算法及代码

    10个经典的C语言面试基础算法及代码作者:码农网 – 小峰 原文地址:http://www.codeceo.com/article/10-c-interview-algorithm.html 算法是一 ...

  5. Project Euler 101 :Optimum polynomial 最优多项式

    Optimum polynomial If we are presented with the first k terms of a sequence it is impossible to say ...

  6. (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 ...

  7. lightoj刷题日记

    提高自己的实力, 也为了证明, 开始板刷lightoj,每天题量>=1: 题目的类型会在这边说明,具体见分页博客: SUM=54; 1000 Greetings from LightOJ [简单 ...

  8. 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 ...

  9. 决战Leetcode: easy part(1-50)

    本博客是个人原创的针对leetcode上的problem的解法,所有solution都基本通过了leetcode的官方Judging,个别未通过的例外情况会在相应部分作特别说明. 欢迎互相交流! em ...

随机推荐

  1. jQuery实现的上下滚动公告栏详细讲解

    之前做项目的时候,一直都想着做一个上下滚动的公告栏,作为展示网站的最新公告信息,因为刚开始自己的思路并不是太清晰,在网上找了很多的源码,但是却发现都不能让自己满意,有的还会出现一些小问题,比如,有时候 ...

  2. mysql 删除某一个数据库下面的所有表,但不删除数据库

    删除某一个数据库下面的所有表,但不删除数据库.该语句经过从concat拼接,最后查询出来的是删除表的语句,然后执行那些查询出来的语句就ok了select concat(‘drop table ‘,ta ...

  3. 2017 先知创新大会:有 ZHI 而来

    先知创新大会( XIANZHI INNOVATION CONFERENCE ) 是聚焦安全行业创新的行业盛事, 旨在推动安全技术的进步和发展. 先知大会的主题是“极致·眼界·创新” 极致:追求技术的极 ...

  4. 通过代码管理工具 git 完成一次完整的代码管理过程

    1.从公共远程fork一份自己的本地远程之后,从本地远程 clone 到本地 2.将本地代码跟公共远程代码做关联配置 git remote add upstream https://github.co ...

  5. 利用 NGINX 最大化 Python 性能,第二部分:负载均衡和监控

    [编者按]本文主要介绍 NGINX 的主要功能以及如何通过 Nginx 优化 Python 应用性能.本文系国内 ITOM 管理平台 OneAPM 编译呈现. 本文上一篇系: 利用 NGINX 最大化 ...

  6. leetCode题解之字符最短路径解法2

    1.题目描述 2.分析 之前使用的大循环再向两边寻找的算法是 O(n^2)复杂度的,可以利用 multimap降低其复杂度. 3.代码 vector<int> shortestToChar ...

  7. geogebra几何画图工具用法

    1. intersectPath :该命令可以自动“算出”对应多边形的交汇区域 2. 静态文本可以指定到一个对象的中间这样将来动态变化对象大小时也不出现问题 3.export worksheet 4. ...

  8. Mysqlbinlog工具及导出数据并转换编码导入

    2014 - binlog是通过记录二进制文件方式来备份数据,然后在从二进制文件将数据恢复到某一时段或某一操作点. 1.使用mysqlbinlog工具来恢复 Mysqlbinlog日志如何开启? 在m ...

  9. 由于使用JDBC ResultSet的滚动功能而导致的内存溢出

    前天一去公司,老大说,服务器全挂了! 最后排查了半天,结论是内存溢出! 在WAS的DUMP日志中,看得我头晕眼花,终于找到了罪魁祸首,原来是有同事写代码的时候使用了可滚动的结果集导致内存溢出. 什么是 ...

  10. [翻译] USING GIT IN XCODE [4] 在XCODE中使用GIT[4]

    USING GIT IN XCODE LOOKING AT HISTORY Xcode provides a Versions editor, which has three different pe ...