python马丁quiz2

Quiz 2
QUIZ 2
COMP9021 PRINCIPLES OF PROGRAMMING
$ python3
...

/>>> from quiz_2 import *
/>>>describe_rule(3)
The rule encoded by 3 is: {(0, 0): 0, (0, 1): 0, (1, 0): 1, (1, 1): 1}
After 0 followed by 0, we draw 0
After 0 followed by 1, we draw 0
After 1 followed by 0, we draw 1
After 1 followed by 1, we draw 1
/>>> describe_rule(4)
The rule encoded by 4 is: {(0, 0): 0, (0, 1): 1, (1, 0): 0, (1, 1): 0}
After 0 followed by 0, we draw 0
After 0 followed by 1, we draw 1
After 1 followed by 0, we draw 0
After 1 followed by 1, we draw 0
/>>> describe_rule(11)
The rule encoded by 11 is: {(0, 0): 1, (0, 1): 0, (1, 0): 1, (1, 1): 1}
After 0 followed by 0, we draw 1
After 0 followed by 1, we draw 0
After 1 followed by 0, we draw 1
After 1 followed by 1, we draw 1
/>>> describe_rule(14)
The rule encoded by 14 is: {(0, 0): 1, (0, 1): 1, (1, 0): 1, (1, 1): 0}
After 0 followed by 0, we draw 1
After 0 followed by 1, we draw 1
After 1 followed by 0, we draw 1
After 1 followed by 1, we draw 0
/>>> draw_line(3, 0, 0, 1)
0
/>>> draw_line(3, 1, 0, 5)
10101
/>>> draw_line(4, 1, 0, 9)
100000000
/>>> draw_line(4, 0, 1, 13)
0110000000000
/>>> draw_line(11, 1, 0, 16)
1010101010101010
/>>> draw_line(11, 1, 1, 19)
1111111111111111111
/>>> draw_line(14, 0, 0, 21)
001101101101101101101
Date: Trimester 1, 2019.
2 COMP9021 PRINCIPLES OF PROGRAMMING
/>>> draw_line(14, 1, 0, 22)
1011011011011011011011
/>>> uniquely_produced_by_rule('1100110011')
12
/>>> uniquely_produced_by_rule('01100000')
4
/>>> uniquely_produced_by_rule('001101101')
14
/>>> uniquely_produced_by_rule('11111111')
-1
/>>> uniquely_produced_by_rule('00011')
-1
/>>> uniquely_produced_by_rule('11001')
-1
/>>> uniquely_produced_by_rule('0010001')
-1
/>>> uniquely_produced_by_rule('0010001')
-1

See comments and test cases in stub.

0.5 mark for passing all tests for describe_rule()

1 mark for passing all tests for draw_line()

1 mark for passing all tests for uniquely_produced_by_rule()

# Written by *** and Eric Martin for COMP9021


def rule_encoded_by(rule_nb):
    values = [int(d) for d in f'{rule_nb:04b}']
    return {(p // 2, p % 2): values[p] for p in range(4)}

def describe_rule(rule_nb):
    rule = rule_encoded_by(rule_nb)
    print(f'The rule encoded by {rule_nb} is:  {rule}')
    print()
    for k in rule.keys():
        print(f'After {k[0]} followed by {k[1]}, we draw {rule[k]}')

def draw_line(rule_nb, first, second, length):
    if rule_nb<0 or rule_nb>15:
        return None
    if first!=0 and first!=1:
        return None
    if second!=0 and second!=1:
        return None
    rule = rule_encoded_by(rule_nb)
    s=[]
    #s=""
    if length==0:
        return None
    if length>0:
        s.append(f'{first}')
        #s=s+int(f'{first}')
    if length>1:
        s.append(f'{second}')
        #s=s+int(f'{second}')
    if length>2:
        for i in range(2,length):
            s.append(f'{rule[(int(s[i-2]),int(s[i-1]))]}')
            #s=s+int('f{rule[(int(s[i-2]),int(s[i-1]))]}')
    print(''.join(s)) 


                                                                    
def uniquely_produced_by_rule(line):
    t=0
    rule=[]
    if len(line)<5:
        return -1
    try:
        x=line[line.index('00')+2]
        rule.append(x)
        t=t+int(x)*8
        x=line[line.index('01')+2]
        rule.append(x)
        t=t+int(x)*4
        x=line[line.index('10')+2]
        rule.append(x)
        t=t+int(x)*2
        x=line[line.index('11')+2]
        rule.append(x)     
        t=t+int(x)
    except (IndexError,ValueError) :
        return -1
        
    for i in range(2,len(line)):
        if line[i-2]=='0':
            if line[i-1]=='0' and line[i]!=rule[0]:
                return -1
            if line[i-1]=='1' and line[i]!=rule[1]:
                return -1
        if line[i-2]=='1':           
            if line[i-1]=='0' and line[i]!=rule[2]:
                return -1
            if line[i-1]=='1' and line[i]!=rule[3]:
                return -1
        
    return t
    
    ```

你可能感兴趣的:(python马丁quiz2)