四个python面试自己的解答

Posted 枫奇

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了四个python面试自己的解答相关的知识,希望对你有一定的参考价值。

共两题,要求为纸上写代码,总时间 30 min,请合理分配时间
(若在线答题,要求在注释中阐述设计思路,这样做的优缺点)

1,实现函数,输入字符串,输出其对应的对象:

Input:
string1 = '["ITEM0001 x 1", "ITEM0013 x 2", "ITEM0022 x 1"]'

Output:
obj1 = {
    "ITEM0001": 1,
    "ITEM0013": 2,
    "ITEM0022": 1,
}

2,在上题的基础上,增加额外信息
ITEM 指的是商品:
ITEM0001 的价格 为 10;
ITEM0013 的价格 为 20;
ITEM0022 的价格 为 30。

Input:
string1 = '["ITEM0001 x 1", "ITEM0013 x 2", "ITEM0022 x 1"]'

Output:
80


Input:
string1 = '["ITEM0006 x 1"]'

Output:
"ITEM 不合法!"

3,「附加题」在上两题的基础上,增加额外信息
优惠 1:
同时购买 ITEM0001 和 ITEM0022,则该商品半价

优惠 2:
满 100 减 30

优惠只能选一种。自动计算多种优惠的结果,并输出最优结果。

Input:
string1 = '["ITEM0001 x 1", "ITEM0013 x 2", "ITEM0022 x 1"]'

Output:
60
"优惠 1"

Input:
string1 = '["ITEM0001 x 1", "ITEM0013 x 3", "ITEM0022 x 1"]'

Output:
70
"优惠 2"

4,mongodb 数据库表设计:
将第 2 题的商品表放到数据库中,
将第 3 题的优惠结果放到数据库中。

# -*- coding: utf-8 -*-
# @Author  : Fengqi(jmps515@163.com)
# @Time    : 2021/6/8 下午1:54
# @File    : test.py
# @Software: PyCharm
# @Python  : 3.6

import pymongo

# 封装数据库函数
class Mongodb(object):
    def __init__(self):
        self.mongo_url = 'localhost'
        self.mongo_db = 'goods'
        self.client = pymongo.MongoClient(self.mongo_url)
        self.db = self.client[self.mongo_db]

    # 插入字典数据
    def process_item(self, item):
        tablename = 'goods'
        self.db[tablename].insert(dict(item))


    # 插入单列数据
    def insert_record(self, record):
        tablename = 'record'
        self.db[tablename].insert({'record': record})


    def close_client(self):
        self.client.close()


valid_item = {"ITEM0001": 10, "ITEM0013": 20, "ITEM0022": 30}


# 字符串转商品
def devide_goods_num(goods):
    good_list = eval(goods)
    obj = {}
    for i in good_list:
        goods_name, num = i.split('x')
        goods_name = goods_name.strip()
        if goods_name in valid_item:
            obj[goods_name] = int(num)
        else:
            print('ITEM[%s] 不合法' % goods_name)
    return obj

# 全价商品
def get_price(obj):
    price = 0
    for key, value in obj.items():
        if key in valid_item:
            price += valid_item[key] * value
        else:
            print('ITEM[%s] 不合法' % key)

    return price

# 优惠规则1
def rules_1(obj):
    valid_item = {"ITEM0001": 10, "ITEM0013": 20, "ITEM0022": 30}
    discount_items = ['ITEM0001', 'ITEM0022']
    price = 0
    if set(discount_items) < set(obj.keys()):
        for key, value in obj.items():
            if key in valid_item:
                if key in discount_items:
                    price += valid_item[key] * value / 2
                else:
                    price += valid_item[key] * value
            else:
                print('ITEM[%s] 不合法' % key)
                return
        return price
    else:
        return

# 优惠规则2
def rules_2(obj):
    discount_price = 30
    total = get_price(obj)
    return total if total < 100 else total-discount_price

# 折扣价格
def get_discount(obj):
    return min(rules_1(obj), rules_2(obj))



def test1():
    string1 = '["ITEM0001 x 1", "ITEM0013 x 2", "ITEM0022 x 1"]'
    print(devide_goods_num(string1))
    print('test 1 end')


def test2():
    string1 = '["ITEM0002 x 1", "ITEM0013 x 2", "ITEM0022 x 1"]'
    obj = devide_goods_num(string1)
    print(get_price(obj))
    print('test 2 end')


def test3():

    string1 = ['["ITEM0001 x 1", "ITEM0013 x 2", "ITEM0022 x 1"]', '["ITEM0001 x 1", "ITEM0013 x 3", "ITEM0022 x 1"]']
    for goods in string1:
        obj = devide_goods_num(goods)
        discount_price = get_discount(obj)
        print('discount price is %d' % discount_price)

    print('test 3 end')

def test4():
    string1 = '["ITEM0002 x 1", "ITEM0013 x 2", "ITEM0022 x 1"]'
    obj = devide_goods_num(string1)
    my_db = Mongodb()
    my_db.process_item(obj)

    string1 = ['["ITEM0001 x 1", "ITEM0013 x 2", "ITEM0022 x 1"]', '["ITEM0001 x 1", "ITEM0013 x 3", "ITEM0022 x 1"]']
    for goods in string1:
        obj = devide_goods_num(goods)
        discount_price = get_discount(obj)
        record = ('discount price is %d' % discount_price)
        print('discount price is %d' % discount_price)
        my_db.insert_record(record)


test1()
test2()
test3()
test4()


以上是关于四个python面试自己的解答的主要内容,如果未能解决你的问题,请参考以下文章

四个python面试自己的解答

四个python面试自己的解答

数据仓库开发工程师面试经典问题及解答

Python面试基础篇 - 50道经典面试题(附答案及多种解答)

Python面试基础篇 - 50道经典面试题(附答案及多种解答)

Python面试基础篇 - 50道经典面试题(附答案及多种解答)