A topic ( the number of specified types of characters in the string of statistics ):
Assuming that all the characters are divided into three categories: letters, numbers and other characters. write a function, determining the type of the specified character and type count the number of the character strings. enter a main program
description:
Input formats:
The first line of the input string, the second line enter a character, not to any message.
Output formats:
Outputting a specified number of character types, such as input-output example.
Example O:
analysis:
First, we need to traverse the first line of the input string, and then determine which character type of each character, numeric or alphabetic (letter but also consider the case), or a special symbol, then the same character type of a character the number of the last character of the second row and the number of characters for input, and outputs the same to its type.
Code:
def function(s,ch): a=0 b=0 c=0 for i in s: if ord('0')<=ord(i)<=ord('9'): a=a+1 elif ord('a')<=ord(i)<=ord('z') or ord('A')<=ord(i)<=ord('Z'): b=b+1 else: c=c+1 if ord('0')<=ord(ch)<=ord('9'): return a elif ord('a')<=ord(ch)<=ord('z') or ord('A')<=ord(ch)<=ord('Z'): return b else: return c s=input() CH = INPUT () Print ( " with the same type of characters% c There are% d. " % (CH, function (S, CH)))
Title bis (Standard Deviation):
Write a function to calculate the standard deviation of a series of numbers.
Standard deviation is defined: a set of data S = S 0 , S . 1 , S 2 , ..., S n--. 1 , which is represented as a standard deviation ,
wherein several main
description:
Input formats:
In several same number of input lines, comma separated.
Output formats:
Standard deviation:
Example O:
analysis:
First, we need to calculate m, according to the formula, we can make m = SUM / length (SUM = SUM + X), so that B = B + (XM) ** 2 , it is possible to obtain d = sqrt (b / (length- 1)) , is the standard deviation.
Code:
from math import * def fd(*a): sum = 0 length = len(a) for x in a: sum+=x m = sum/length b = 0 for x in a: b+=(x-m)**2 return sqrt(b/(length-1)) nums=eval(input()) print("标准差为%.1f"%fd(*nums))
题目三(判断闰年):
编写函数leap,根据“四年闰百年不闰,四百年又闰”判断是否闰年。在主程序输入一个年份,调用leap函数判断其是否为闰年,并输出判断结果。
描述:
输入格式:
输入一个代表年份的整数。
输出格式:
输出年份是否闰年的判断结果。
输入输出示例:
分析:
明确本题的判断条件,即y%4==0 and y%100!=0返回True,或y%4==0 and y%400==0返回True,之后再判断,若leap(y)==True,则为闰年,反之则不是。
代码:
def leap(y): if y%4==0: if y%100!=0: return True elif y%400==0: return True else: return False y=int(input()) if leap(y)==True: print("%d年是闰年"%y) else: print("%d年不是闰年"%y)
题目四(斐波那契数列):
大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项。
描述:
输入格式:
10
输出格式:
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
分析:
n=int(input()) def feibo(num): result=[0,1] for i in range(num-2): result.append(result[-2]+result[-1]) return result print(feibo(n))