美文网首页
python 马丁 quiz3

python 马丁 quiz3

作者: 33jubi | 来源:发表于2019-03-17 22:11 被阅读0次

QUIZ 3

COMP9021 PRINCIPLES OF PROGRAMMING
$ python quiz_3.py

Enter three integers: 0 3 4
L_1 is: [3, 3, 0, 2]
The length of the longest increasing sequence
of members of L_1, possibly wrapping around, is: 4.
L_2 is: [3, 3, 2, 3]
The maximum integer built from L_2 by jumping
as directed by its members, from some starting member
and not using any member more than once, is: 33
$ python3 quiz_3.py

Enter three integers: 0 10 5
L_1 is: [6, 6, 0, 4, 8]
The length of the longest increasing sequence
of members of L_1, possibly wrapping around, is: 3.
L_2 is: [3, 3, 2, 3, 2]
The maximum integer built from L_2 by jumping
as directed by its members, from some starting member
and not using any member more than once, is: 33
$ python quiz_3.py

Enter three integers: 1 9 6
L_1 is: [2, 9, 1, 4, 1, 7]
The length of the longest increasing sequence
of members of L_1, possibly wrapping around, is: 2.
L_2 is: [3, 3, 5, 3, 1, 0]
The maximum integer built from L_2 by jumping
as directed by its members, from some starting member
and not using any member more than once, is: 5033
$ python3 quiz_3.py

Enter three integers: 1 8 7
L_1 is: [2, 1, 4, 1, 7, 7, 7]
The length of the longest increasing sequence
of members of L_1, possibly wrapping around, is: 4.
L_2 is: [5, 3, 6, 1, 0, 3, 0]
The maximum integer built from L_2 by jumping
as directed by its members, from some starting member
and not using any member more than once, is: 605313
Date: Trimester 1, 2019.
2 COMP9021 PRINCIPLES OF PROGRAMMING
$ python3 quiz_3.py

Enter three integers: 0 6 10
L_1 is: [6, 3, 6, 3, 0, 2, 4, 3, 3, 6]
The length of the longest increasing sequence
of members of L_1, possibly wrapping around, is: 4.
L_2 is: [4, 7, 5, 9, 3, 8, 2, 4, 2, 1]
The maximum integer built from L_2 by jumping
as directed by its members, from some starting member
and not using any member more than once, is: 439174
$ python3 quiz_3.py

Enter three integers: 2 4 10
L_1 is: [0, 0, 0, 2, 1, 2, 2, 4, 1, 4]
The length of the longest increasing sequence
of members of L_1, possibly wrapping around, is: 4.
L_2 is: [0, 9, 2, 6, 6, 8, 5, 8, 7, 8]
The maximum integer built from L_2 by jumping
as directed by its members, from some starting member
and not using any member more than once, is: 65878
$ python3 quiz_3.py

Enter three integers: 0 8 20
L_1 is: [6, 6, 0, 4, 8, 7, 6, 4, 7, 5, 3, 8, 2, 4, 2, 1, 4, 8, 2, 4]
The length of the longest increasing sequence
of members of L_1, possibly wrapping around, is: 4.
L_2 is: [3, 2, 10, 15, 17, 3, 11, 13, 10, 19, 6, 17, 15, 14, 16, 8, 1, 17, 0, 2]
The maximum integer built from L_2 by jumping
as directed by its members, from some starting member
and not using any member more than once, is: 13141612106111717
$ python3 quiz_3.py

Enter three integers: 0 2 25
L_1 is: [1, 1, 0, 1, 2, 1, 1, 1, 1, 1, 2, 0, 2, 0, 1, 0, 0, 2, 1, 2, 2, 2, 0, 1, 0]
The length of the longest increasing sequence
of members of L_1, possibly wrapping around, is: 6.
L_2 is: [23, 2, 21, 10, 15, 17, 3, 11, 13, 10, 19, 20, 6, 17, 15, 14, 16, 8, 1,...
...17, 0, 2, 23, 12, 22]
The maximum integer built from L_2 by jumping
as directed by its members, from some starting member
and not using any member more than once, is: 1120023126310191781317

Quiz 3
1.25 marks for passing all tests for length_of_longest_increasing_sequence()

1.25 mark for passing all tests for max_int_jumping_in()

# Written by Eric Martin for COMP9021

import sys
from random import seed, randint, randrange


try:
    arg_for_seed, upper_bound, length =\
            (int(x) for x in input('Enter three integers: ').split())
except ValueError:
    print('Incorrect input, giving up.')
    sys.exit()


def length_of_longest_increasing_sequence(L):
    if L==[]:
        return 0
    max_count=1
    for i in range(0,len(L)):
        count=1
        while True:
            if i==len(L)-1:
                if L[i]<=L[0] and count<len(L):
                    count=count+1
                    i=0
                else:
                    break
            elif L[i]<=L[i+1]:
                count=count+1
                i=i+1
            else:
                break
        max_count=max(max_count,count)
    return max_count

def max_int_jumping_in(L):
    if L==[]:
        return None
    #str=''
    lmax=0
    list=[]
    ll=len(L)-1
    for i in range(0,len(L)):
        x=i
        s=L[i]
        str=''
        c=0
        #list2=[]
        while True:           
            try:
                v=L[i]
            except ValueError and IndexError:
                sys.exit()            
            if v==x:
                str+=f'{v}'
                c=c+1
                break;
            elif v!=s and str.find(f',{v},')==-1:#the same index means the pre one has been already got tiwce
                str+=f'{v},'
                c=c+1
                i=v  
            elif v==s and str.find(f'{v},')==-1:
                str+=f'{v},'
                c=c+1
                i=v           
            else:
                str+=f'{v}'
                c=c+1
                break         
        if lmax<c:
            lmax=c
            list.append(''.join(str.split(',')))
    return list[-1]
           
seed(arg_for_seed)
L_1 = [randint(0, upper_bound) for _ in range(length)]
print('L_1 is:', L_1)
print('The length of the longest increasing sequence\n'
      '  of members of L_1, possibly wrapping around, is:',
      length_of_longest_increasing_sequence(L_1), end = '.\n\n'
     )
L_2 = [randrange(length) for _ in range(length)]
print('L_2 is:', L_2)
print('The maximum integer built from L_2 by jumping\n'
      '  as directed by its members, from some starting member\n'
      '  and not using any member more than once, is:',
      max_int_jumping_in(L_2)
     )

···

有时间会更list的解法,这个string的不是满分解法哦,有数据漏洞的

相关文章

网友评论

      本文标题:python 马丁 quiz3

      本文链接:https://www.haomeiwen.com/subject/szvpmqtx.html