M
M
Maxim Kalina2015-04-13 20:27:07
Python
Maxim Kalina, 2015-04-13 20:27:07

Search for combinations in a string where one character plays the role of a universal?

Good afternoon, dear members of the forum, there is a task to test combinations of the "One-armed bandit" lottery,
in response from the server I receive a string of 15 characters. in the form of a game drum
(Example:
1BCDE
FG1BC
DEFG1)

line = '1BCDEFG1BCDEFG1'
values = {'HHH': 10, 'HHHH': 25, 'HHHHH': 150,
          'GGG': 10, 'GGGG': 25, 'GGGGG': 150,
          'FFF': 150, 'FFFF': 500, 'FFFFF': 2000,
          'EEE': 100, 'EEEE': 200, 'EEEEE': 2000,
          'DDD': 300, 'DDDD': 1000, 'DDDDD': 2000,
          'CCC': 300, 'CCCC': 1000, 'CCCCC': 5000,
          'BBB': 300, 'BBBB': 1500, 'BBBBB': 500,
          '111': 50, '1111': 500, '11111': 5000
          }
Нужно реализовать распознавание строки выигрышной комбинации по таким параметрам <img src="//habrastorage.org/files/4e8/9d5/740/4e89d5740d06465ca31aba4a02adb98e.png" alt="image"/>
Каким лучше методом реализовать данный тест в Pyt<blockquote>hon, были идеи использовать обращение к линии с помощью
line[0] == line[1] and line[1] == line[2] and line[2] == line[3] and line[3] == line[4]
или же с помощью регулярных выражений.
Прошу помощи у опытных коллег, каким лучше методом воспользоваться для решения этой задачи.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Andrey Dugin, 2015-04-13
@dayzz

def checksum(code, awards={'A': 15, 'B': 20, 'C': 30, '': 40}, joker='D'):
    return sum(
        awards.get(''.join(set(triplet)-{joker}), 0) for triplet in zip(*[iter(code)]*3)
    )

checksum('ABCAABDCA')  # 0
checksum('ADAAABDCA')  # 15

Or like this, for example:
def checksum2(code, awards={'A': 15, 'B': 20, 'C': 30, '': 40}, joker='D'):
    return reduce(
        lambda xsum, triplet: xsum + awards.get(
            ''.join(set(triplet).difference(joker)), 0
        ),
        (code[0:3], code[3:6], code[6:9]), 0
    )

B
Bulat Kurbangaliev, 2015-04-13
@ilov3

if len(set(line[:3])) == 1:
    income_amount += values[line[:3]]
elif 'D' in set(line[:3]) and len(set(line[:3])) == 2:
    income_amount += values[line[:3].replace('D', set(line[:3])-{'D'})]
if len(set(line[3:6])) == 1:
    income_amount += values[line[3:6]]
elif 'D' in set(line[3:6]) and len(set(line[3:6])) == 2:
    income_amount += values[line[3:6].replace('D', set(line[3:6])-{'D'})]

but regular is probably easier)

V
Vov Vov, 2015-07-02
@balamut108

Use itertools, it's a great tool for all sorts of combinations. pythonworld.ru/moduli/modul-itertools.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question