Python每日一练(20230407) 四舍六入五成双、编辑距离、分割回文串

简介: Python每日一练(20230407) 四舍六入五成双、编辑距离、分割回文串

1. 四舍六入五成双规则

输入:1234

输出:1234

12 12.0

4 4.00

0.2 0.200

0.32 0.320

1.3 1.30

1.235 1.24

1.245 1.24

1.2451 1.25

出处:

https://edu.csdn.net/practice/26046539

代码:

nums = [1234,12,4,0.2,0.32,1.3,1.235,1.245,1.2451]
for n in nums:
    a = str(n)
    if '.' in a:
        a = float(a)
        if a*1000%10!=5:
            a = '%.2f'%(a)
        else:
            if len(str(a).split(".")[1])>3:
                a = '%.2f'%(a)
            else:
                if int(a*100%10%2)==1:
                    a = float('%.2f'%float(int(a*100)/100))+0.01
                else:
                    a = '%.2f'%float(int(a*100)/100)
        print(a)
    else:
        a = int(a)
        if a>99:
            print(a)
        else:
            if 0 < a < 10: 
                print('%.2f'%a)
            else:
                print(float(a))

输出:

1234

12.0

4.00

0.20

0.32

1.30

1.24

1.24

1.25

四舍六入五成双规则:

对于位数很多的近似数,当有效位数确定后,其后面多余的数字应该舍去,只保留有效数字最末一位,也即“4舍6入5凑偶”

“四”是指≤4 时舍去,

“六”是指≥6时进上,

“五”指的是根据5后面的数字来定:

1. 当5后有数时,舍5入1;

2. 当5后无有效数字时,需要分两种情况来讲:

(1)5前为奇数,舍5入1;

(2)5前为偶数,舍5不进(0是偶数)。


2. 编辑距离

给你两个单词 word1word2,请你计算出将 word1 转换成 word2 所使用的最少操作数 。

你可以对一个单词进行如下三种操作:

  • 插入一个字符
  • 删除一个字符
  • 替换一个字符

示例 1:

输入:word1 = "horse", word2 = "ros"

输出:3

解释:horse -> rorse (将 'h' 替换为 'r')rorse -> rose (删除 'r')rose -> ros (删除 'e')

示例 2:

输入:word1 = "intention", word2 = "execution"

输出:5

解释:intention -> inention (删除 't')inention -> enention (将 'i' 替换为 'e')enention -> exention (将 'n' 替换为 'x')exention -> exection (将 'n' 替换为 'c')exection -> execution (插入 'u')


提示:

  • 0 <= word1.length, word2.length <= 500
  • word1word2 由小写英文字母组成

以下程序实现了这一功能,请你填补空白处内容:

```
class Solution(object):
    def minDistance(self, word1, word2):
        ls_1, ls_2 = len(word1), len(word2)
        dp = list(range(ls_1 + 1))
        for j in range(1, ls_2 + 1):
            pre = dp[0]
            dp[0] = j
            for i in range(1, ls_1 + 1):
                temp = dp[i]
                if word1[i - 1] == word2[j - 1]:
                    dp[i] = pre
                else:
                    ____________________________;
                pre = temp
        return dp[ls_1]
if __name__ == '__main__':
    s = Solution()
    print (s.minDistance("horse","ros"))        
    print (s.minDistance("intention","execution"))   
```

出处:

https://edu.csdn.net/practice/24915040

代码:

class Solution(object):
    def minDistance(self, word1, word2):
        ls_1, ls_2 = len(word1), len(word2)
        dp = list(range(ls_1 + 1))
        for j in range(1, ls_2 + 1):
            pre = dp[0]
            dp[0] = j
            for i in range(1, ls_1 + 1):
                temp = dp[i]
                if word1[i - 1] == word2[j - 1]:
                    dp[i] = pre
                else:
                    dp[i] = min(pre + 1, dp[i] + 1, dp[i - 1] + 1)
                pre = temp
        return dp[ls_1]
if __name__ == '__main__':
    s = Solution()
    print (s.minDistance("horse","ros"))        
    print (s.minDistance("intention","execution"))

输出:

3

5


3. 分割回文串

给你一个字符串 s,请你将 s 分割成一些子串,使每个子串都是 回文串 。返回 s 所有可能的分割方案。

回文串 是正着读和反着读都一样的字符串。

示例 1:

输入:s = "aab"

输出:[["a","a","b"],["aa","b"]]


示例 2:

输入:s = "a"

输出:[["a"]]


提示:

  • 1 <= s.length <= 16
  • s 仅由小写英文字母组成

出处:

https://edu.csdn.net/practice/24915041

代码:

class Solution(object):
    def partition(self, s):
        """
        :type s: str
        :rtype: List[List[str]]
        """
        if len(s) == 0:
            return []
        else:
            res = []
            self.dividedAndsel(s, [], res)
        return res
    def dividedAndsel(self, s, tmp, res):
        if len(s) == 0:
            res.append(tmp)
        for i in range(1, len(s) + 1):
            if s[:i] == s[:i][::-1]:
                self.dividedAndsel(s[i:], tmp + [s[:i]], res)
if __name__ == '__main__':
    s = Solution()
    print (s.partition("aab"))        
    print (s.partition("a"))

输出:

[['a', 'a', 'b'], ['aa', 'b']]

[['a']]

方法2:

from typing import List
class Solution:
    def partition(self, s: str) -> List[List[str]]:
        res = []
        self.dfs(s, [], res)
        return res
    def dfs(self, s, path, res):
        if not s:
            res.append(path)
            return
        for i in range(1, len(s)+1):
            if self.is_palindrome(s[:i]):
                self.dfs(s[i:], path+[s[:i]], res)
    def is_palindrome(self, s):
        return s == s[::-1]
if __name__ == '__main__':
    s = Solution()
    print (s.partition("aab"))        
    print (s.partition("a"))

🌟 每日一练刷题专栏 🌟

持续,努力奋斗做强刷题搬运工!

👍 点赞,你的认可是我坚持的动力!

🌟 收藏,你的青睐是我努力的方向!

评论,你的意见是我进步的财富!  

主页:https://hannyang.blog.csdn.net/


相关实践学习
Docker镜像管理快速入门
本教程将介绍如何使用Docker构建镜像,并通过阿里云镜像服务分发到ECS服务器,运行该镜像。
阿里云资源编排ROS使用教程
资源编排(Resource Orchestration)是一种简单易用的云计算资源管理和自动化运维服务。用户通过模板描述多个云计算资源的依赖关系、配置等,并自动完成所有资源的创建和配置,以达到自动化部署、运维等目的。编排模板同时也是一种标准化的资源和应用交付方式,并且可以随时编辑修改,使基础设施即代码(Infrastructure as Code)成为可能。 产品详情:https://www.aliyun.com/product/ros/
目录
相关文章
|
21天前
|
存储 自然语言处理 数据可视化
【办公自动化】用Python按时间分割txt文件中的数据
【办公自动化】用Python按时间分割txt文件中的数据
67 1
|
21天前
|
数据处理 Python
Python中按指定数量分割列表字符串的方法
Python中按指定数量分割列表字符串的方法
13 1
|
21天前
|
Python
怎么理解python中的拼接字符串和分割字符串
怎么理解python中的拼接字符串和分割字符串
20 0
|
21天前
|
Python
利用Python生成字符串分割
利用Python生成字符串分割
15 0
|
21天前
|
Python 人工智能
讯飞星火、文心一言和通义千问同时编“贪吃蛇”游戏,谁会胜出?
讯飞星火、文心一言和通义千问同时编“贪吃蛇”游戏,谁会胜出?
56 1
讯飞星火、文心一言和通义千问同时编“贪吃蛇”游戏,谁会胜出?
|
21天前
|
Shell Unix Linux
Linux 终端命令之文件浏览(3) less
Linux 终端命令之文件浏览(3) less
38 0
Linux 终端命令之文件浏览(3) less
|
21天前
|
Rust
Rust 编程小技巧摘选(8)
Rust 编程小技巧摘选(8)
106 0
Rust 编程小技巧摘选(8)
|
21天前
|
算法 C++ 机器人
力扣 C++|一题多解之动态规划专题(1)
力扣 C++|一题多解之动态规划专题(1)
43 0
力扣 C++|一题多解之动态规划专题(1)
|
4天前
|
存储 索引 Python
元组(Tuple)在Python编程中的应用与实例
元组(Tuple)在Python编程中的应用与实例
16 2
|
4天前
|
机器学习/深度学习 数据可视化 数据挖掘
Python编程的深入探索与实用案例
Python编程的深入探索与实用案例
15 3