鱼C论坛

 找回密码
 立即注册
查看: 3531|回复: 40

Python:每日一题 264

[复制链接]
发表于 2019-10-27 14:17:02 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 zltzlt 于 2019-10-27 17:01 编辑

本题由 @阴阳神万物主 提供,感谢 @阴阳神万物主


今天的题目:

我们的目标是从某个神奇仓库中取出一些东西。
字符串 A 是仓库的库存,每一样东西都用首字母大写的单词
(单词中不出现空格,有空格的单词空格用短横线替换,比如:Ice cream 在字符串中为 Ice-cream)表示。
每个单词用空格分开,在字符串中出现的次数即这个东西的数量。
字符串 B 是想要取出的东西的表单,格式与 A 相同
字符串 C 的格式与 A 相同,内容为:A 中可以出现的东西,并且单词不会重复。

要求输出 (a):

第一行为布尔值,能不能从 A 取出 B 中列出的东西。
第二行也是布尔值,A 中存放的物品是不是符合 C 的规矩,符合为真,不符合为假。

往后,当 A 中无法取出 B 中列出的物品时,每一行为:{需要的物品单词} {如果 A 要满足 B 的需求需要进货的最少数量}
如果 A 中没有出现 B 中的某一样东西,而这样东西恰好也是 C 中没有的,则输出 (b) 字符串:Impossible
但如果 A 中出现了,就按照 (a) 规则进行输出。

详细说明见 2#

示例 1:
输入:A = 'Ball Ball Ball Knife Knife Condom Ice-cream',B = 'Ball Condom Ball',C = 'Ball Knife Condom'
输出:
True

False
示例 2:
输入:A = 'Star Chimpanzee Monkey Monkey Little-Turtle Mom Son Ball Ball Ball',B = 'Ball Star Ball',C = 'Star Chimpanzee Monkey Little-Turtle Mom Son Ball'
输出:
True

True


欢迎大家一起答题!

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-10-27 17:00:12 | 显示全部楼层
本帖最后由 阴阳神万物主 于 2019-10-27 17:28 编辑

这锅我背了,我换个好懂的描述吧:
背景

小明同学是我们的执行人,他要去本公司旗下的某家商店采购一些东西,并调查该商店销售的商品是否符合本公司的规定。如果需要采购的东西商店存货不足,则可以向该商店提出进货要求,直到满足采购需求。但是,当要采购的东西商店没有存货,并且这个东西不符合公司规定时,由于小明失落的心情,会直接放弃采购,也不会调查了,因为小明认为在该商店中不可能购买到该商品。

描述

关于规定:当商店销售的商品中,出现规定以外的东西,则应该将该商店查封。
商店销售的商品记为字符串 A ,小明要采购的东西记为字符串 B ,公司规定的商品记为字符串 C 。
这三个字符串的内容均为:首字母大写的英文单词,每个单词用空格分开(在英文中有空格的单词,空格用短横线代替,如:Ice cream 在字符串中为 Ice-cream),某一单词的数量即为该单词代表的商品的数量。但在字符串 C 中不会出现重复的单词。

输入

A B C 三个字符串。

输出格式

当小明失落时,只需输出一个单词:Impossible
但是,他不失落时,需要输出:
第一行:小明是否采购成功?
第二行:按照规定,该商店是否 查封?
若小明采购失败,从第三行开始,往后的每一行(按照在 B 中从左往右出现的顺序)都是:采购失败的物品的单词+一个空格+商店需要进货的最少数量

输入输出样例

输入 #1
输出 #1
A = 'Ball Ball Ball Knife Knife Condom Ice-cream'
B = 'Ball Condom Ball'
C = 'Ball Knife Condom'
True
False


输入 #2 输出 #2
A = 'Ball Ball Ball Ball Ball Ball Ball Ball Ball Ball Ball Ball Ball Ball Ball Ball Ball Ball Ball Ball Star Star Star Sun'
B = 'Ball Moon Star'
C = 'Ball Star Sun Moon'
False
True
Moon 1


输入 #3
输出 #3
A = 'Jin-Yong Country-love-story Country-love-story Country-love-story Guitar Guitar Guitar'
B = 'Little-Turtle'
C = 'Jin-Yong Guitar'
Impossible


点评

我很赞同!: 5.0
我很赞同!: 5
真是辛苦你了  发表于 2019-10-27 17:02
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-10-27 15:35:30 | 显示全部楼层
没太明白什么意思
能不能再给一个以下规则的例子
往后,当 A 中无法取出 B 中列出的物品时,每一行为:{需要的物品单词} {如果 A 要满足 B 的需求需要进货的最少数量}。
如果 A 中没有出现 B 中的某一样东西,而这样东西恰好也是 C 中没有的,则输出 (b) 字符串:Impossible。
但如果 A 中出现了,就按照 (a) 规则进行输出。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-10-27 15:36:26 | 显示全部楼层
Unicorn# 发表于 2019-10-27 15:35
没太明白什么意思
能不能再给一个以下规则的例子

@阴阳神万物主 吧,我也没太看明白
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-10-27 15:36:59 | 显示全部楼层
好像明白了
C 就是 仓库中物品种类集合
是这个意思吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-10-27 15:42:18 | 显示全部楼层
Unicorn# 发表于 2019-10-27 15:36
好像明白了
C 就是 仓库中物品种类集合
是这个意思吗

对的,撒花~~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-10-27 15:44:23 | 显示全部楼层

那C不是A的种类集合的时候还要输出后边的部分吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-10-27 15:50:48 | 显示全部楼层
本帖最后由 阴阳神万物主 于 2019-10-27 15:54 编辑
Unicorn# 发表于 2019-10-27 15:44
那C不是A的种类集合的时候还要输出后边的部分吗


那还得结合 B 的情况:当 A 中出现了 C 中没有的,而且 B 中需要但是 A 中数量不够的,就需要输出后边的。
但是,B 中需要的 A 与 C 都没有时,就只需要输出 Impossible 即可,前面的布尔值啥的都没有
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-10-27 15:52:35 | 显示全部楼层
阴阳神万物主 发表于 2019-10-27 15:50
那还得结合 B 的情况:当 A 中出现了 C 中没有的,而且 B 中需要但是 A 中数量不够的,就需要输出后边的 ...

就是说这样:
输出(a)+补货情况
这样吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-10-27 15:56:32 | 显示全部楼层
本帖最后由 __mrsq__ 于 2019-10-27 16:12 编辑

我觉得可以理解为:

C 是仓库里常规物品的合集
A 是仓库的库存状况(可能会有非常规货品存在,这种情况A就不是C的子集了)
B 是客户需求

如果B的需求在A和C的并集里,而且库存A不满足B需求的情况, 就打印{需要的物品单词} {如果 A 要满足 B 的需求需要进货的最少数量}。
如果B的需求不在A和C的并集里就打印Impossible

这样理解对吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-10-27 16:02:46 | 显示全部楼层
本帖最后由 Unicorn# 于 2019-10-27 16:09 编辑

就按我自己理解的输出:
A能否提供表单B
C是否是A的种类集合
补货情况
def solve(A, B, C):

    def initial(t):
        temp = {}
        for each in t:
            if each not in temp:
                temp[each] = 1
            else:
                temp[each] += 1
        return temp

    A = initial(A.split(' '))
    B = initial(B.split(' '))
    C = initial(C.split(' '))
    addition = {}
    def cmp(A, B):
        temp = False
        for each in B:
            if each not in A:
                temp = True
                continue
            if A[each] < B[each]:
                temp = True
                addition[each] = B[each] - A[each]
        if temp:
            return False
        else:
            return True
    print(cmp(A, B))
    def check(A, C):
        if set(A.keys()) == set(C.keys()):
            return True
        else:
            return False
    print(check(A, C))
    for each in addition:
        if each not in C:
            print('Impossible')
        else:
            print('{%s}{%d}'%(each, addition[each]))
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-10-27 16:06:46 | 显示全部楼层
Unicorn# 发表于 2019-10-27 16:02
就按我自己理解的输出:
A能否提供表单B
C是否是A的种类集合

输入:A='Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef',B='Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Vegetarian Vegetarian Vegetarian Vegetarian Chef Chef Zltzlt',C='Liberal Vegetarian Chef Zltzlt' 出错
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-10-27 16:10:28 | 显示全部楼层
zltzlt 发表于 2019-10-27 16:06
输入:A='Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal L ...

刚刚有点问题已经修改啦
你再试试
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-10-27 16:13:00 | 显示全部楼层
Unicorn# 发表于 2019-10-27 16:10
刚刚有点问题已经修改啦
你再试试

解答错误

输入:A='Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Vegetarian Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef Chef',B='Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Vegetarian Vegetarian Vegetarian Vegetarian Chef Chef Zltzlt',C='Liberal Vegetarian Chef Zltzlt'
输出:False
False
预期结果:False
True
Zltzlt 1
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-10-27 16:20:10 | 显示全部楼层
但如果 A 中出现了,就按照 (a) 规则进行输出。
没太明白这是什么意思
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-10-27 16:20:44 | 显示全部楼层
zltzlt 发表于 2019-10-27 16:13
解答错误

输入:A='Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liberal Liber ...

.....这总行了吧
def solve(A, B, C):

    def initial(t):
        temp = {}
        for each in t:
            if each not in temp:
                temp[each] = 1
            else:
                temp[each] += 1
        return temp

    A = initial(A.split(' '))
    B = initial(B.split(' '))
    C = initial(C.split(' '))
    addition = {}
    def cmp(A, B):
        temp = False
        for each in B:
            if each not in A:
                temp = True
                addition[each] = B[each]
                continue
            if A[each] < B[each]:
                temp = True
                addition[each] = B[each] - A[each]
        if temp:
            return False
        else:
            return True
    print(cmp(A, B))
    def check(A, C):
        for each in A:
            if each not in C:
                return False
        return True
    print(check(A, C))
    for each in addition:
        if each not in A and each not in C:
            print('Impossible')
        else:
            print(each, addition[each])

评分

参与人数 1荣誉 +1 鱼币 +1 贡献 +1 收起 理由
zltzlt + 1 + 1 + 1

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-10-27 16:35:19 | 显示全部楼层
本帖最后由 __mrsq__ 于 2019-10-27 17:05 编辑
def warehouse(A, B, C):
    actual_stock_A = A.split(' ')
    need_B = B.split(' ')
    set_A = set(actual_stock_A)
    set_C= set(C.split(' '))
    Set_AC = set_A.union(set_C)
    res = ''
    visited = []
    for item in need_B:
        if item not in visited:
            visited.append(item)
            if item in Set_AC:
                if need_B.count(item) - actual_stock_A.count(item) > 0:
                    res += f'we need extra {need_B.count(item) -  actual_stock_A.count(item)} {item}\n'
            else:
                return 'Impossible'
    if not res:
        print(True)
    else:
        print(False)
    print(set_A.issubset(set_C))
    print(res)

评分

参与人数 1荣誉 +1 鱼币 +1 贡献 +1 收起 理由
zltzlt + 1 + 1 + 1

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-10-27 16:35:51 | 显示全部楼层
如果 A 中没有出现 B 中的某一样东西,但 C 中有,怎么办?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-10-27 16:39:51 | 显示全部楼层
永恒的蓝色梦想 发表于 2019-10-27 16:35
如果 A 中没有出现 B 中的某一样东西,但 C 中有,怎么办?

打印{需要的物品单词} {如果 A 要满足 B 的需求需要进货的最少数量}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-10-27 16:40:25 | 显示全部楼层
永恒的蓝色梦想 发表于 2019-10-27 16:35
如果 A 中没有出现 B 中的某一样东西,但 C 中有,怎么办?

往后,当 A 中无法取出 B 中列出的物品时,每一行为:{需要的物品单词} {如果 A 要满足 B 的需求需要进货的最少数量}。

是这种情况
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-10-27 16:58:32 | 显示全部楼层
本帖最后由 Stubborn 于 2019-10-27 17:06 编辑
def depot(a, b, c):
    def _check_b(a: dict, b: dict, c: dict) -> bool:
        """b的数量小于a才能被取出"""

        def no_gods(key):
            if c.get(key, False):
                return "Impossible"
            return False

        for key, value in b.items():
            res = a.get(key, False)
            if not res:
                return no_gods(key)
            if res - value < 0:
                # {需要的物品单词} {如果 A 要满足 B 的需求需要进货的最少数量} 进货不满足是这样返回吗?
                return f"{key}{value - res}"
        return True

    def _check_c(a: dict, c: dict):
        """所有的商品,都应该能从c取出"""
        for key, value in a.items():
            res = c.get(key, False)
            if not res:
                return False
        return True

    def to_dict(A: list):
        dic = {}
        for value in A:
            if value not in dic:
                dic.setdefault(value, 1)
            else:
                dic[value] += 1
        return dic

    dict_a = to_dict(a.split(' '))
    dict_b = to_dict(b.split(' '))
    dict_c = to_dict(c.split(' '))

    return _check_b(dict_a, dict_b, dict_c), _check_c(dict_a, dict_c)

评分

参与人数 1荣誉 +1 鱼币 +1 收起 理由
zltzlt + 1 + 1

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-6-24 03:48

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表