[1,1000]定义函数求最大值函数公式是什么

编写一个函数,交换指定字典的key和value。
例如:dict1={'a':1, 'b':2, 'c':3}
-->
dict1={1:'a', 2:'b', 3:'c'}
def exchange(x: dict):
"""
交换指定字典的key和value
:param x: 给定的字典
:return: 交换key和value后的新字典
"""
dict1 = {x[i]: i for i in x}
print(dict1)
return dict1
exchange({'a': 1, 'b': 2, 'c': 3})
编写一个函数,提取指定字符串中所有的字母,然后拼接在一起产生一个新的字符串
例如: 传入'12a&bc12d-+'
-->
'abcd'
def letter(str1:str):
"""
提取指定字符串中所有的字母,然后拼接在一起产生一个新的字符串
:param str1: 指定字符串
:return: 新的字符串
"""
str2 = ''
for x in str1:
if 'a' <= x <= 'z' or 'A' <= x <= 'Z':
str2 += x
print(str2)
return str2
letter('12a&bc12d-+')
写一个自己的capitalize函数,能够将指定字符串的首字母变成大写字母
例如: 'abc' -> 'Abc'
'12asd'
--> '12asd'
def capital(str1: str):
"""
能够将指定字符串的首字母变成大写字母
:param str1: 给定的字符串
:return: 首字母变成大写字母后的新字符串
"""
if 'a' <= str1[0] <= 'z':
str1 = chr(ord(str1[0]) - 32) + str1[1:]
print(str1)
else:
print(str1)
return str1
capital('abc')
写一个自己的endswith函数,判断一个字符串是否已指定的字符串结束
例如: 字符串1:'abc231ab' 字符串2:'ab' 函数结果为: True
字符串1:'abc231ab' 字符串2:'ab1' 函数结果为: False
def endswith_str(str1: str, str2: str):
"""
判断一个字符串是否已指定的字符串结束
:param str1: 一个字符串
:param str2: 以指定字符结束的字符串
:return:判断一个字符串是否已指定的字符串结束 True 与 False
"""
a = len(str1) - len(str2)
print(str2 == str1[a:])
return (str2 == str1[a:])
endswith_str('abc231ab', 'ab')
写一个自己的isdigit函数,判断一个字符串是否是纯数字字符串
例如: '1234921'
结果: True
'23函数'
结果: False
'a2390'
结果: False
def isdigit_num(str1: str):
"""
判断一个字符串是否是纯数字字符串
:param str1: 需要判断的字符串
:return: None
"""
for i in str1:
if not '0' <= i <= '9':
print(False)
break
else:
print(True)
isdigit_num('1234921')
写一个自己的upper函数,将一个字符串中所有的小写字母变成大写字母
例如: 'abH23好rp1'
结果: 'ABH23好RP1'
def upper_letter(str1: str):
"""
将一个字符串中所有的小写字母变成大写字母
:param str1: 给定字符串
:return: 变化后的字符串
"""
str2 = ''
for i in str1:
if 'a' <= i <= 'z':
i = chr(ord(i) - 32)
str2 += i
else:
str2 += i
print(str2)
return str2
upper_letter('abH23好rp1')
写一个自己的rjust函数,创建一个字符串的长度是指定长度,原字符串在新字符串中右对齐,剩下的部分用指定的字符填充
例如: 原字符:'abc'
宽度: 7
字符:'^'
结果: '^^^^abc'
原字符:'你好吗'
宽度: 5
字符:'0'
结果: '00你好吗'
写一个自己的index函数,统计指定列表中指定元素的所有下标,如果列表中没有指定元素返回-1
例如: 列表: [1, 2, 45, 'abc', 1, '你好', 1, 0]
元素: 1
结果: 0,4,6
列表: ['赵云', '郭嘉', '诸葛亮', '曹操', '赵云', '孙权']
元素: '赵云'
结果: 0,4
列表: ['赵云', '郭嘉', '诸葛亮', '曹操', '赵云', '孙权']
元素: '关羽'
结果: -1
写一个自己的len函数,统计指定序列中元素的个数
例如: 序列:[1, 3, 5, 6]
结果: 4
序列:(1, 34, 'a', 45, 'bbb')
结果: 5
序列:'hello w'
结果: 7
def len_content(x):
"""
统计指定序列中元素的个数
:param x: 给定的序列
:return: 序列中元素的个数
"""
count = 0
for i in x:
count += 1
print(count)
return count
len_content([1, 3, 5, 6])
len_content((1, 34, 'a', 45, 'bbb'))
len_content('hello w')
写一个自己的max函数,获取指定序列中元素的最大值。如果序列是字典,取字典值的最大值
例如: 序列:[-7, -12, -1, -9]
结果: -1
序列:'abcdpzasdz'
结果: 'z'
序列:{'小明':90, '张三': 76, '路飞':30, '小花': 98}
结果: 98
def max_content(x):
"""
获取指定序列中元素的最大值。如果序列是字典,取字典值的最大值
:param x: 指定序列
:return: 序列中元素的最大值
"""
max_num = 0
if type(x) in (str, list):
max_num = max(x)
print(max_num)
elif type(x) == dict:
max_num = max([x[i] for i in x])
print(max_num)
return max_num
max_content('abcdpzasdz')
max_content([-7, -12, -1, -9])
max_content({'小明': 90, '张三': 76, '路飞': 30, '小花': 98})
写一个函数实现自己in操作,判断指定序列中,指定的元素是否存在
例如: 序列: (12, 90, 'abc')
元素: '90'
结果: False
序列: [12, 90, 'abc']
元素: 90
结果: True
def content_in(x, y):
"""
判断指定序列中,指定的元素是否存在
:param x: 指定的序列
:param y: 指定的元素
:return: 判断是否存在的布尔值
"""
result = True
for i in x:
if i == y:
print(result)
break
else:
result = False
print(result)
return result
content_in((12, 90, 'abc'), '90')
content_in([12, 90, 'abc'], 90)
写一个自己的replace函数,将指定字符串中指定的旧字符串转换成指定的新字符串
例如: 原字符串: 'how are you? and you?'
旧字符串: 'you'
新字符串:'me'
结果: 'how are me? and me?'
def replace_str(str1: str, str2: str, str3: str):
"""
将指定字符串中指定的旧字符串转换成指定的新字符串
:param str1: 指定的字符串
:param str2: 被转换的旧字符
:param str3: 转换的新字符
:return: 新字符串
"""
str4 = ''
x = 0
while x < len(str1):
if str1[x:x+len(str2)] == str2:
str4 += str3
x += len(str2)
else:
str4 += str1[x]
x += 1
print(str4)
return str4
replace_str('how are you? and you?', 'you', 'me')
加一个题:定义一个自己的update函数,将一个字典中所有的键值对添加到另外一个字典中 例如:dict1={'a':1, 'b':2, 'c':3}
-->
dict2={4:'d'}
def add_dict(dict1: dict, dict2: dict):
"""
将一个字典中所有的键值对添加到另外一个字典
:param dict1: 指定的字典
:param dict2: 键值对被加入的字典
:return: 新字典
"""
for key, value in enumerate(dict1):
dict2.setdefault(value, dict1[value])
print(dict2)
return dict2
add_dict({'a': 1, 'b': 2, 'c': 3}, {4: 'd'})
一些小练习中很matlab的做法--即能用数组+现成函数解决的运算就避免使用循环结构,也对一部分些基础题目做一个记录,顺便补全一些常用函数、矩阵。1.在1000-3000中自小到大的整数中,找出第二个和第五个能被41整除的整数组成的行向量。(length、find、rem+数组)function s=myfun
v=1000:3000;
%生成行向量
idx =find(rem(v,41)==0); %利用rem、find函数找出满足条件的数并返回一个行向量
if length(idx)<5,
error
% 找到满足条件的元素不够多, 与设计的题目不符合
end
s=v(idx([2 5]));
%然后访问第二个和第五个数赋给输出参数。2.请计算
其中q=1.09,请返回计算结果s(sum+数组)
function s=myfun
p = 2:2:46;
%生成指数的行向量,步长为2
s =
方法1:自己#include<stdio.h>
void main(void){
int s[10] = {10, 23, 45, 1, 34, 76,100, 32, 456,54};
int i,j;
int max;
//求数组s元素的最大值
for(i = 0; i < 10; i++){
for(j = 0; j < i; j++){
if(s[i] > s[i]){
max = s[i];
}else{
max = s[j];
}
}
}
printf("数组s的值分别为:");
for(i = 0; i < 10; i++){
printf("%d ", s[i]);
}
printf("\n其中最大值为:%d\n\n", max);
}方法2:老师#include<stdio.h>
void main(void){
int s[10] = {10, 23, 45, 1, 34, 76,100, 32, 456,54};
int i;
int max = s[0];
//求数组s元素的最大值
for(i = 0; i < 10; i++){
if(s[i] > max){
max = s[i];
}
}
printf("数组s的值分别为:");
for(i = 0; i < 10; i++){
printf("%d ", s[i]);
}
printf("\n其中最大值为:%d\n\n", max);
}
阅读终点,创作起航,您可以撰写心得或摘录文章要点写篇博文。去创作
分类专栏
您愿意向朋友推荐“博客详情页”吗?
强烈不推荐
不推荐
一般般
推荐
强烈推荐

我要回帖

更多关于 最大值函数公式是什么 的文章