12346 - Python_mooc_2   

Description

Define a subclass of tuple such that it supports the __contains__ special method
(corresponding to the in operator in reverse) that works similar to the str version.
For your information,

But tuple does not support slice containment:

Define a subclass of tuple named STuple such that containment works for both
elements and slices:

''' you should use the template of following.'''

import ast

class STuple(tuple):
    '''inherit constructor'''
    def __contains__(self, item):
    #coding on here

    #coding on here

    #coding on here

if __name__ == '__main__':
    pat = ast.literal_eval(input()) # pattern input
    testSeq = ast.literal_eval(input()) # test case input
    for p in pat:
        for t in testSeq:
            print(f'{p} in STuple{t} =', p in STuple(t))

Input

['o', 's', ('s', 'c', 'h'), (), ('o', 'o')]
[(), ('s', 'c', 'h', 'o', 'o', 'l'), ('s', 'c', 'h'), ('k', 'o' 'o', 'l'), ('o', 'o', 'l')]

Output

o in STuple() = False
o in STuple('s', 'c', 'h', 'o', 'o', 'l') = True
o in STuple('s', 'c', 'h') = False
o in STuple('k', 'oo', 'l') = False
o in STuple('o', 'o', 'l') = True
s in STuple() = False
s in STuple('s', 'c', 'h', 'o', 'o', 'l') = True
s in STuple('s', 'c', 'h') = True
s in STuple('k', 'oo', 'l') = False
s in STuple('o', 'o', 'l') = False
('s', 'c', 'h') in STuple() = False
('s', 'c', 'h') in STuple('s', 'c', 'h', 'o', 'o', 'l') = True
('s', 'c', 'h') in STuple('s', 'c', 'h') = True
('s', 'c', 'h') in STuple('k', 'oo', 'l') = False
('s', 'c', 'h') in STuple('o', 'o', 'l') = False
() in STuple() = True
() in STuple('s', 'c', 'h', 'o', 'o', 'l') = True
() in STuple('s', 'c', 'h') = True
() in STuple('k', 'oo', 'l') = True
() in STuple('o', 'o', 'l') = True
('o', 'o') in STuple() = False
('o', 'o') in STuple('s', 'c', 'h', 'o', 'o', 'l') = True
('o', 'o') in STuple('s', 'c', 'h') = False
('o', 'o') in STuple('k', 'oo', 'l') = False
('o', 'o') in STuple('o', 'o', 'l') = True

Sample Input  Download

Sample Output  Download

Tags




Discuss