python内置函数值 -- chr() ord()
chr()接收一个数字, 找到这个数字对应的ascii里的元素(只能接受数字)
a = chr(65)
print(a) #结果: A
ord()接收一个字符,返回这个字符对应的数字.(只能接受一个字符)
b = ord('a')
print(b) #结果: 97
举例:实现字符串的反转,要求不使用任何系统方法,且时间复杂度最小
思路:异或
时间复杂度O(N)
不需要中间变量
代码如下:
def reverseStr(str):
lens = len(str)
ch = list(str)
i = 0
j = lens-1
while i < j:
ch[i] = chr(ord(ch[i])^ord(ch[j]))
ch[j] = chr(ord(ch[i])^ord(ch[j]))
ch[i] = chr(ord(ch[i])^ord(ch[j]))
i += 1
j -= 1
return ''.join(ch) res = reverseStr('abc')
print(res)
结果:cba
python内置函数值 -- chr() ord()的更多相关文章
- 内置函数值 -- chr() ord() -- 字符和ascii的转换
英文文档: chr(i) Return the string representing a character whose Unicode code point is the integer i. F ...
- Python内置函数(16)——ord
英文文档: ord(c) Given a string representing one Unicode character, return an integer representing the U ...
- Python内置函数(48)——ord
英文文档: ord(c) Given a string representing one Unicode character, return an integer representing the U ...
- python内置函数
python内置函数 官方文档:点击 在这里我只列举一些常见的内置函数用法 1.abs()[求数字的绝对值] >>> abs(-13) 13 2.all() 判断所有集合元素都为真的 ...
- python 内置函数和函数装饰器
python内置函数 1.数学相关 abs(x) 取x绝对值 divmode(x,y) 取x除以y的商和余数,常用做分页,返回商和余数组成一个元组 pow(x,y[,z]) 取x的y次方 ,等同于x ...
- python内置函数的归集
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明. Python内置(built-in)函数随着python解释器的运行而创建.在Pytho ...
- Python补充03 Python内置函数清单
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明. Python内置(built-in)函数随着python解释器的运行而创建.在Pytho ...
- Python内置函数进制转换的用法
使用Python内置函数:bin().oct().int().hex()可实现进制转换. 先看Python官方文档中对这几个内置函数的描述: bin(x)Convert an integer numb ...
- Python 内置函数笔记
其中有几个方法没怎么用过, 所以没整理到 Python内置函数 abs(a) 返回a的绝对值.该参数可以是整数或浮点数.如果参数是一个复数,则返回其大小 all(a) 如果元组.列表里面的所有元素都非 ...
随机推荐
- checkbox做全部选中,全部取消效果
批量选中.取消多个checkbox的用法很简单,这个功能也很常用.这里做个总结. 在HTML中的写法: <div id="ftpFileDownTr"> <but ...
- nowcoder 211B - 列队 - [(伪·良心贪心)真·毒瘤暴力]
题目链接:https://www.nowcoder.com/acm/contest/211/B 题目描述 炎热的早上,gal男神们被迫再操场上列队,gal男神们本来想排列成x∗x的正方形,可是因为操场 ...
- Vue.js最佳实践
Vue.js最佳实践 第一招:化繁为简的Watchers 场景还原: created(){ this.fetchPostList() }, watch: { searchInputValue(){ t ...
- confd template src格式和 templates 语法
Template Resources Template resources are written in TOML and define a single template resource. Tem ...
- monit安装配置
环境centos5(32bit),monit-5.17.1,下载地址 https://bitbucket.org/tildeslash/monit/downloads/ 1.tar zxvf moni ...
- js的序列化和反序列化
(1)序列化 即js中的Object转化为字符串 1.使用toJSONString var last=obj.toJSONString(); //将JSON对象转化为JSON字符 2.使用strin ...
- LeetCode 762 Prime Number of Set Bits in Binary Representation 解题报告
题目要求 Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a ...
- 自动化测试:java + testng + maven + reportng + jenkins + selenium (一)_基于win环境
集成环境:jdk1.7 + tomcat1.7+ eclipse mars + maven + testng6.14.2 + selenium-java2.40.0 + reportng1.1.4 + ...
- falsk 与 django 捕获异常
falsk捕获异常@app.errorhandler(405)def internal_server_error(e): return '这个接口不能被GET请求到,只能post' django 捕获 ...
- 【Python全栈-HTML】HTML入门
HTML入门介绍 参考: https://www.bilibili.com/video/av21663728/?p=339 http://www.cnblogs.com/yuanchenqi/arti ...