【每日一题1111】灯组排列

:mage:t2: 假设一个灯组有ABC一共3盏灯,每盏灯的状态只有ON/OFF两种状态。假设A、C灯亮,则输出字典{'A': 1, 'B': 0, 'C': 1},其中1表示ON开,0表示OFF关。

请编写一个函数func,接收灯组的字母作为参数(支持任意个),返回该灯组的所有开关状态的组合字典(不限顺序)。

示例:
输入:"A",输出:{0: {'A': 0}, 1: {'A': 1}}
输入:"B",输出:{0: {'A': 0, 'B': 0}, 1: {'A': 0, 'B': 1}, 2: {'A': 1, 'B': 0}, 3: {'A': 1, 'B': 1}}

题目难度:中等
题目来源:20211110面试真题

def func(*input) -> dict:
    # your code here

assert func("A") == {0: {'A': 0}, 1: {'A': 1}}
assert func("A", "B") == {0: {'A': 0, 'B': 0}, 
                          1: {'A': 0, 'B': 1}, 
                          2: {'A': 1, 'B': 0}, 
                          3: {'A': 1, 'B': 1}}
def func(*input_l) -> dict:
    from itertools import product

    # 先获取单个灯和0,1开关的所有组合:[[('A', 0), ('A', 1)], [('B', 0), ('B', 1)]]
    temp_1 = [list(product(x, [0, 1])) for x in input_l]
    # for x in input_l:
    #     res = list(product(x, [0, 1]))
    #     temp.append(res)

    # 再获取灯与其他灯的所有组合:[(('A', 0), ('B', 0)), (('A', 0), ('B', 1)), (('A', 1), ('B', 0)), (('A', 1), ('B', 1))]
    temp_2 = list(product(*temp_1))

    # 最后调整一下数据结构
    res = {x: {k: v for k, v in temp_2[x]} for x in range(len(temp_2))}
    # for x in range(len(temp)):
    #     res[x] = {k: v for k, v in temp[x]}
    return res


assert func("A") == {0: {'A': 0},
                     1: {'A': 1}}
assert func("A", "B") == {0: {'A': 0, 'B': 0},
                          1: {'A': 0, 'B': 1},
                          2: {'A': 1, 'B': 0},
                          3: {'A': 1, 'B': 1}}
assert func("A", "B", "C") == {0: {'A': 0, 'B': 0, 'C': 0},
                               1: {'A': 0, 'B': 0, 'C': 1},
                               2: {'A': 0, 'B': 1, 'C': 0},
                               3: {'A': 0, 'B': 1, 'C': 1},
                               4: {'A': 1, 'B': 0, 'C': 0},
                               5: {'A': 1, 'B': 0, 'C': 1},
                               6: {'A': 1, 'B': 1, 'C': 0},
                               7: {'A': 1, 'B': 1, 'C': 1}}
def func(*input) -> dict:
    light_dict = {}
    for i in range(len(input) ** 2 if len(input) > 1 else 2):
        index = 0
        one_dict = {}
        bin_str = '{:0' + str(len(input)) + 'b}'
        bin_num = bin_str.format(i)
        if len(bin_num) == len(input):
            for j in bin_num:
                one_dict[input[index]] = int(j)
                index += 1
            light_dict[i] = one_dict
    return light_dict


assert func("A") == {0: {'A': 0}, 1: {'A': 1}}
assert func("A", "B") == {0: {'A': 0, 'B': 0},
                          1: {'A': 0, 'B': 1},
                          2: {'A': 1, 'B': 0},
                          3: {'A': 1, 'B': 1}}
assert func('A', 'B', 'C') == {0: {"A": 0, "B": 0, "C": 0},
                               1: {"A": 0, "B": 0, "C": 1},
                               2: {"A": 0, "B": 1, "C": 0},
                               3: {"A": 0, "B": 1, "C": 1},
                               4: {"A": 1, "B": 0, "C": 0},
                               5: {"A": 1, "B": 0, "C": 1},
                               6: {"A": 1, "B": 1, "C": 0},
                               7: {"A": 1, "B": 1, "C": 1},
                               }

def func(*input) → dict:
‘’’
假设一个灯组有ABC一共3盏灯,每盏灯的状态只有ON/OFF两种状态。假设A、C灯亮,则输出字典{‘A’: 1, ‘B’: 0, ‘C’: 1},其中1表示ON开,0表示OFF关。

请编写一个函数func,接收灯组的字母作为参数(支持任意个),返回该灯组的所有开关状态的组合字典(不限顺序)。

示例:
输入:"A",输出:{0: {'A': 0}, 1: {'A': 1}}
输入:"A", "B",输出:{0: {'A': 0, 'B': 0}, 1: {'A': 0, 'B': 1}, 2: {'A': 1, 'B': 0}, 3: {'A': 1, 'B': 1}}
:param input:
:return:
'''
# 考虑输入的字母不同
# 用递归.没有实现
#  if len(input) == 0:
#      return {}
#  if len(input) == 1:
#      data = input[0]
#      return {0: {f'{data}': 0}, 1: {f'{data}': 1}}
#  elif len(input) == 2:
#      dataA = input[0]
#      dataB = input[1]
#      return {0: {f'{dataA}': 0, f'{dataB}': 0}, 1: {f'{dataA}': 0, f'{dataB}': 1}, 2: {f'{dataA}': 1, f'{dataB}': 0}, 3: {f'{dataA}': 1, f'{dataB}': 1}}
#  else:
#      dataA = input[0]
#      dataB= input[1:]
#      func(dataA, dataB)

#     利用数字
input_len = len(input)
max_num = pow(2, input_len)
index = 0
result_dict = {}
num_list = []
for i in range(0, pow(2, input_len)):
    num_list.append(i)
for i in range(0, len(num_list)):
    bin_num = bin(num_list[i])
    bin_num = bin_num[2:]
    bin_str = str(bin_num)
    cur_index = 0
    cur_dict = {}
    for j in range(0 ,input_len):
        bin_len = len(bin_num)
        if j < bin_len:
            cur_dict[input[j]] = int(bin_num[bin_len-j-1])
        else:
            cur_dict[input[j]] = 0
    result_dict[i] = cur_dict
return result_dict

def test_func():
assert func(“A”) == {0: {‘A’: 0}, 1: {‘A’: 1}}
# assert func(“A”, “B”) == {0: {‘A’: 0, ‘B’: 0}, 1: {‘A’: 0, ‘B’: 1}, 2: {‘A’: 1, ‘B’: 0}, 3: {‘A’: 1, ‘B’: 1}}
assert func(“A”, “B”, “C”) == {0: {‘A’: 0, ‘B’: 0, ‘C’: 0}, 1: {‘A’: 1, ‘B’: 0, ‘C’: 0}, 2: {‘A’: 0, ‘B’: 1, ‘C’: 0}, 3: {‘A’: 1, ‘B’: 1, ‘C’: 0}, 4: {‘A’: 0, ‘B’: 0, ‘C’: 1}, 5: {‘A’: 1, ‘B’: 0, ‘C’: 1}, 6: {‘A’: 0, ‘B’: 1, ‘C’: 1}, 7: {‘A’: 1, ‘B’: 1, ‘C’: 1}}

Python 参考题解

from itertools import product

def func(*inputs):
    # 组合:[(0, 0), (0, 1), (1, 0), (1, 1)]
    nums = list(product(range(2), repeat=len(inputs)))
    
    # 复制 [('A', 'B'), ('A', 'B'), ('A', 'B'), ('A', 'B')]
    data = list([inputs]*len(nums))
    
    # 对位组成
    sub = list(map(lambda x,y:dict(zip(x,y)), data, nums))
    
    # 组装成大字典
    result = dict(zip(range(len(sub)), sub))
    return result
    

assert func("A") == {0: {'A': 0}, 1: {'A': 1}}
assert func("A", "B") == {0: {'A': 0, 'B': 0}, 
                          1: {'A': 0, 'B': 1}, 
                          2: {'A': 1, 'B': 0}, 
                          3: {'A': 1, 'B': 1}}

Python参考题解2

def comp(*data):
    from itertools import product
    rp = len(data)
    lis_temp = []
    for i in product(range(2), repeat=rp):
        lis_temp.append(dict(zip(data, i)))
    print(dict(enumerate(lis_temp,1)))

贡献者:蚊子

Python题解3

def func(*input):
    from itertools import product
    return dict(enumerate([dict(zip(input, i)) for i in product(range(2), repeat=len(input))], 1))

贡献者:蚊子

def func(*inputs):
    from itertools import product
    nums = list(product(range(2), repeat=len(inputs)))
    print(nums)

    data = list([inputs] * len(nums))

    sub = list(map(lambda x, y: dict(zip(x, y)), data, nums))

    result = dict(zip(range(len(sub)), sub))
    return result


assert func("A") == {0: {'A': 0}, 1: {'A': 1}}
assert func("A", "B") == {0: {'A': 0, 'B': 0},
                          1: {'A': 0, 'B': 1},
                          2: {'A': 1, 'B': 0},
                          3: {'A': 1, 'B': 1}}
from itertools import product


def func(*input) -> dict:
    length = 2 ** len(input)
    # [(0, 0), (0, 1), (1, 0), (1, 1)]
    data = list(product(range(2), repeat=len(input)))
    result = {}
    for i in range(length):
        result[i] = {}
        for x in input:
            # 计算x在input的索引0或1或。。。
            j = input.index(x)
            # x的
            result[i][x] = data[i][j]
    return result


assert func("A") == {0: {'A': 0}, 1: {'A': 1}}
assert func("A", "B") == {0: {'A': 0, 'B': 0},
                          1: {'A': 0, 'B': 1},
                          2: {'A': 1, 'B': 0},
                          3: {'A': 1, 'B': 1}}