Python学习笔记-初级(三):数据结构

一、问题背景

“Python语言导论“课程作业。


二、实现环境

Windows 8, Python 3.5 IDLE


三、题目

1、 输出1~100之间不能被7整除的数,每行输出10个数字,要求应用字符串对齐函数美化输出格式。

(1)代码

'''
Author: WJT
Date: 24/10/2016
function: output all the numbers can't be divided by
numbers between 1 and 100
'''
count = 0
for i in range(1, 101):
        if (i % 7 != 0):
                print("% 4d" % i, end=' ')
                count += 1
                if (count%10 == 0):
                        print('\n')

(2)运行结果



2、假定s是小写字母的字符串。编写程序,输出s的最长子串(子串必须是字母顺序)。

如:s = 'azcbobobegghakl',则应输出:

Longestsubstring in alphabetical order is: beggh

又如:s = 'abcbcd',则应输出第一个子串:

Longestsubstring in alphabetical order is: abc

 

(1)代码

'''
Author: WJT
Date: 10/24/2016
function: output longest substring in alphabetical order
'''

'''
# this version of imeplemention is of low efficiency
def longest_sub(string):
        # each character is a substring of length 1
        length=[]
        for i in range(0, len(string)):
                length.append(1)

        for i in range(0, len(string)-1):
                for j in range(i, len(string)-1):
                        if (string[j] <= string[j+1]):
                                length[i] += 1
                        else:
                                break

        max_length = max(length)
        max_index = length.index(max_length)

        # for debug
        #print(length)
        #print(max_length, max_index)

        print("Longest substring in alphabetetic order is: ", end=' ')
        for i in range(max_index, max_index + max_length):
                print(string[i], end='')
        print('\n')
'''


# another version of implemention
def longest_sub(string):
    i = 0
    max_length = 1
    temp_length = 1
    while(i < len(string) - 1):
        if(string[i] <= string[i + 1]):
            temp_length += 1
            if(temp_length > max_length):
                max_length = temp_length
                # record the index of the last element in longest sub-string
                max_index = i + 1
                      
        else:
            temp_length = 1

        i += 1
        
    # for debug
    #print(max_length, max_index)

    print("Longest substring in alphabetetic order is: ", end=' ')
    for i in range(max_index - max_length + 1, max_index + 1):
        print(string[i], end='')
    print('\n')


# testbench
s = 'azcbobobegghakl'
longest_sub(s)
s = 'abcbcd'
longest_sub(s)


(2)运行结果



3、 假定n1和n2为正整数,编写函数:findDivisors(n1, n2),返回包含n1、n2公因子的元组。

 (1)代码

'''
Author: WJT
Date: 24/10/2016
function: return the tuple whose elements are common divisors of n1 and n2
'''

def findDivisors(n1, n2):
    return_tuple = ( )
    if (n1 < n2):
        min_num = n1
        max_num = n2
    else:
        min_num = n2
        max_num = n1

    for i in range(1, min_num):
        if ((min_num % i == 0) and (max_num % i == 0)):
            # add element to a tuple
            return_tuple = return_tuple + (i,)

    return return_tuple


(2)运行结果



4、假定已经执行了以下语句序列:

animals = { 'a': ['aardvark'], 'b': ['baboon'], 'c':['coati']}   #值都是列表

animals['d'] = ['donkey']

animals['d'].append('dog')

animals['d'].append('dingo')

编写函数:bigest(Dict),返回其值包含元素个数最多的键值。


(1)代码

'''
Author: WJT
Date: 10/24/2016
Function: return the key of the dictionary value which has the most elements
'''

def biggest(Dict):
    max_len = 0
    for k in Dict.keys():
        if (max_len < len(Dict[k])):
            max_len = len(Dict[k])
            max_len_key = k
    print(max_len_key)
    #return max_len_key



# testbench
animals = { 'a': ['aardvark'], 'b': ['baboon'], 'c': ['coati']}   #值都是列表
animals['d'] = ['donkey']
animals['d'].append('dog')
animals['d'].append('dingo')
biggest(animals)

biggest({'a': [3, 3, 18], 'c': [3, 15, 12, 10, 0], \
         'b': [10, 19, 14, 5, 16, 20, 11, 6], \
         'd': [5, 16, 8, 16, 6, 1]})


(2)运行结果


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值