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 ...
随机推荐
- 关于vue跨域名对接微信授权认证和APP授权认证
这种情况一般也只会出现在前后端分离,跨域名授权的时候吧.耗费了一个前端+一个后台+一个网关,熬夜通宵了两天才整出来一套方法(你们见过凌晨6点的杭州吗,对,我下班的时候天黑了,到家天亮了....),和开 ...
- 个人遗漏知识的回顾-HTML
常用的一些快捷键: Windows + e 我的电脑Ctrl + Tab 网页间不同页面切换F2 重命名Ctrl+Shift+S 另存为 前端的一些常识:前端意义:将效果图生成网页网页组成:文字.图片 ...
- opencv图像处理时使用stringstream批量读取图片,处理后并保存
简介: 同文件输入输出流一样,使用stringstream可以批量读取图片,处理后并进行保存.因为C++中头文件 stringstream既可以从string读数据也可向string写数据,利于其这个 ...
- 使用servicestack连接redis
引言:作为少有的.net架构下的大型网站,stackoverflow曾发表了一篇文章,介绍了其技术体系,原文链接http://highscalability.com/blog/2011/3/3/sta ...
- 2java.lang.NoClassDefFoundError异常
1.错误信息 2.错误分析: JVM读到程序的第三行,会在当前路径 "D:\java" 下寻找com.yangquan.aolun这个包路径下的Cat类,但当前路径下根本就没有co ...
- 带你从零学ReactNative开发跨平台App开发-[react native 仿boss直聘](十三)
ReactNative跨平台开发系列教程: 带你从零学ReactNative开发跨平台App开发(一) 带你从零学ReactNative开发跨平台App开发(二) 带你从零学ReactNative开发 ...
- maven一键部署linux的tomcat(wagon-maven-plugin)
Maven是一个项目管理工具,它包含了一个项目对象模型 (Project Object Model),一组标准集合,一个项目生命周期(Project Lifecycle),一个依赖管理系统(Depen ...
- 探究MySQL MGR的读写分离
1:现有环境 机器 MySQL 环境 172.16.128.240 MGR NODE1 MGR 172.16.128.241 MGR NODE2 MGR 172.16.128.242 MGR NODE ...
- 3. 跟踪标记 (Trace Flag) 1204, 1222 抓取死锁信息
跟踪标记:1204/1222 功能及用途: 捕获SQL Server死锁信息,并自动存放到错误日志(ERRORLOG)中. 举例: USE tempdb GO CREATE TABLE t1(id i ...
- UNIX高级环境编程(4)Files And Directories - umask、chmod、文件系统组织结构和链接
本篇主要介绍文件和文件系统中常用的一些函数,文件系统的组织结构和硬链接.符号链接. 通过对这些知识的了解,可以对Linux文件系统有更为全面的了解. 1 umask函数 之前我们已经了解了每个文件 ...