当前位置: 首页 > article >正文

Python每日一练(20230420)

目录

1. 数组逐位判断  🌟

2. 交错字符串  🌟🌟

3. 二进制求和  🌟

🌟 每日一练刷题专栏 🌟

Golang每日一练 专栏

Python每日一练 专栏

C/C++每日一练 专栏

Java每日一练 专栏


1. 数组逐位判断

比如有以下数组:
a1: 1,0,0,1,0,0,0,1
a2: 0,0,0,0,1,1,1,1
a3: 0,1,0,1,0,1,0,0
a4: 1,0,1,1,1,1,0,0
a5: .......

抓取三个数组进行判断, if ((a1第一位or a2第一位 or a3第一位=1 )and (a1第二位 or a2 第二位 or a3第二位=1)and.... 直到判断完所有位数为止,所有位都有了1的话就输出当前这三个数组,已输出的数组不参与之后的判断。

出处:

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

代码:

# -*- coding: UTF-8 -*-
from itertools import combinations
a1=[ 1,0,0,1,0,0,0,1]
a2=[ 0,0,0,0,1,1,1,1]
a3=[ 0,1,0,1,0,1,0,0]
a4=[ 1,0,1,1,1,1,0,0]
a5=[ 1,1,1,1,1,1,1,0]
a6=[ 0,0,0,0,0,0,0,1]
a=[a1,a2,a3,a4,a5,a6]
al = list(combinations(a,3))
for i in al:
    flag = True
    for j in range(len(i[0])):
        if (i[0][j] + i[1][j] + i[2][j] == 0):
            flag = False
            break
    if flag:
        print(i)

输出:

([1, 0, 0, 1, 0, 0, 0, 1], [0, 0, 0, 0, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 0])
([1, 0, 0, 1, 0, 0, 0, 1], [0, 1, 0, 1, 0, 1, 0, 0], [1, 1, 1, 1, 1, 1, 1, 0])
([1, 0, 0, 1, 0, 0, 0, 1], [1, 0, 1, 1, 1, 1, 0, 0], [1, 1, 1, 1, 1, 1, 1, 0])
([1, 0, 0, 1, 0, 0, 0, 1], [1, 1, 1, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0, 1])
([0, 0, 0, 0, 1, 1, 1, 1], [0, 1, 0, 1, 0, 1, 0, 0], [1, 0, 1, 1, 1, 1, 0, 0])
([0, 0, 0, 0, 1, 1, 1, 1], [0, 1, 0, 1, 0, 1, 0, 0], [1, 1, 1, 1, 1, 1, 1, 0])
([0, 0, 0, 0, 1, 1, 1, 1], [1, 0, 1, 1, 1, 1, 0, 0], [1, 1, 1, 1, 1, 1, 1, 0])
([0, 0, 0, 0, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0, 1])
([0, 1, 0, 1, 0, 1, 0, 0], [1, 1, 1, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0, 1])
([1, 0, 1, 1, 1, 1, 0, 0], [1, 1, 1, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0, 1])


2. 交错字符串

给定三个字符串 s1s2s3,请你帮忙验证 s3 是否是由 s1 和 s2 交错 组成的。

两个字符串 s 和 t 交错 的定义与过程如下,其中每个字符串都会被分割成若干 非空 子字符串:

  • s = s1 + s2 + ... + sn
  • t = t1 + t2 + ... + tm
  • |n - m| <= 1
  • 交错 是 s1 + t1 + s2 + t2 + s3 + t3 + ... 或者 t1 + s1 + t2 + s2 + t3 + s3 + ...

提示:a + b 意味着字符串 a 和 b 连接。

示例 1:

输入:s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"
输出:true

示例 2:

输入:s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc"
输出:false

示例 3:

输入:s1 = "", s2 = "", s3 = ""
输出:true

提示:

  • 0 <= s1.length, s2.length <= 100
  • 0 <= s3.length <= 200
  • s1s2、和 s3 都由小写英文字母组成

出处:

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

代码:

class Solution(object):
    def isInterleave(self, s1, s2, s3):
        """
        :type s1: str
        :type s2: str
        :type s3: str
        :rtype: bool
        """
        if len(s1) + len(s2) != len(s3):
            return False
        queue = [(0, 0), (-1, -1)]
        visited = set()
        isSuccess = False
        index = 0
        while len(queue) != 1 or queue[0][0] != -1:
            p = queue.pop(0)
            if p[0] == len(s1) and p[1] == len(s2):
                return True
            if p[0] == -1:
                queue.append(p)
                index += 1
                continue
            if p in visited:
                continue
            visited.add(p)
            if p[0] < len(s1):
                if s1[p[0]] == s3[index]:
                    queue.append((p[0] + 1, p[1]))
            if p[1] < len(s2):
                if s2[p[1]] == s3[index]:
                    queue.append((p[0], p[1] + 1))
        return False
# %%
s = Solution()
print(s.isInterleave(s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"))
print(s.isInterleave(s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc"))
print(s.isInterleave(s1 = "", s2 = "", s3 = ""))

输出:

True
False
True


3. 二进制求和

给你两个二进制字符串,返回它们的和(用二进制表示)。

输入为 非空 字符串且只包含数字 1 和 0

示例 1:

输入: a = "11", b = "1"
输出: "100"

示例 2:

输入: a = "1010", b = "1011"
输出: "10101"

提示:

  • 每个字符串仅由字符 '0' 或 '1' 组成。
  • 1 <= a.length, b.length <= 10^4
  • 字符串如果不是 "0" ,就都不含前导零。

出处:

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

代码:

class Solution(object):
    def addBinary(self, a, b):
        res = ''
        lsa, lsb = len(a), len(b)
        pos, plus, curr = -1, 0, 0
        while (lsa + pos) >= 0 or (lsb + pos) >= 0:
            if (lsa + pos) >= 0:
                curr += int(a[pos])
            if (lsb + pos) >= 0:
                curr += int(b[pos])
            res = str(curr % 2) + res
            curr //= 2
            pos -= 1
        if curr == 1:
            res = '1' + res
        return res
# %%
s = Solution()
print(s.addBinary(a = "11", b = "1"))
print(s.addBinary(a = "1010", b = "1011"))

输出:

100
10101


🌟 每日一练刷题专栏 🌟

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

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

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

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

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

Golang每日一练 专栏

Python每日一练 专栏

C/C++每日一练 专栏

Java每日一练 专栏


http://www.kler.cn/a/14202.html

相关文章:

  • 【MySQL笔记】MySQL数据库分库分表解读
  • java版UWB人员定位系统源码,提供位置实时显示、历史轨迹回放、电子围栏、行为分析、智能巡检等功能
  • css-设置单行文本溢出省略号,使用overflow:hidden属性之后的出现的问题几解决办法。
  • Hilt在Android中的使用
  • 深入理解 JavaScript 中的原型和原型链
  • 数据库和缓存一致性问题
  • 盘点C# 9.0中好用的特性
  • Win7系统USB串口安装驱动出现“系统找不到指定的文件“解决方案
  • 计算机组成原理 指令系统(1)
  • itop-3568开发板驱动学习笔记(22)设备树(一)设备树基础
  • L1-094 剪切粘贴
  • 测试月入30K,Soeasy?测试这一行涨薪机制被我摸透了...
  • 【Android FrameWork(四)】- binder service_manager ServiceManager
  • 【Linux命令行与Shell脚本编程】第五章 理解 Shell 父子关系 后台进程 协程
  • 计算机类大学生竞赛经验分享
  • JavaSE 09 File 类 IO 流 - Part 01
  • 搜索引擎找外贸客户
  • 【产品经理】系统上线自查清单
  • TCP协议的相关特性(续)
  • GDB调试gdb