Python学习笔记-初级(三):数据结构
来源:互联网 发布:linux usleep 头文件 编辑:程序博客网 时间:2024/06/04 18:19
一、问题背景
“Python语言导论“课程作业。
二、实现环境
Windows 8, Python 3.5 IDLE
三、题目
1、 输出1~100之间不能被7整除的数,每行输出10个数字,要求应用字符串对齐函数美化输出格式。
(1)代码
'''Author: WJTDate: 24/10/2016function: output all the numbers can't be divided bynumbers between 1 and 100'''count = 0for 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: WJTDate: 10/24/2016function: output longest substring in alphabetical order''''''# this version of imeplemention is of low efficiencydef 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 implementiondef 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')# testbenchs = 'azcbobobegghakl'longest_sub(s)s = 'abcbcd'longest_sub(s)
3、 假定n1和n2为正整数,编写函数:findDivisors(n1, n2),返回包含n1、n2公因子的元组。
(1)代码
'''Author: WJTDate: 24/10/2016function: 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: WJTDate: 10/24/2016Function: 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# testbenchanimals = { '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)运行结果
0 0
- Python学习笔记-初级(三):数据结构
- Python学习笔记(三)-- 数据结构
- Python初级学习笔记
- Python 初级学习笔记
- JAVA学习笔记(初级)三
- 数据结构学习笔记(三)
- 数据结构学习笔记(三)
- 数据结构学习笔记(三)
- 数据结构学习笔记(三)
- 【python入门笔记】(三)基本数据结构
- Python入门(三,初级)
- Python学习笔记(三)——Python数据结构之序列
- 数据结构学习笔记三
- Python学习笔记-初级(二):模块与函数
- Python学习笔记-初级(四):文件处理
- Python学习笔记-初级(五):面向对象
- python学习笔记-数据结构
- python学习笔记(三)
- IOS:聊一聊UIImage几点知识
- CodeForces 733A Grasshopper And the String(蚂蚱跳字符)
- Java 判断日期是否在系统本月内
- 求一组成对出现的数据中的单身狗
- HDU 5863 cjj's string game(矩阵快速幂)
- Python学习笔记-初级(三):数据结构
- C语言排序算法集锦:选择排序,冒泡排序,插入排序,希尔排序,归并排序,堆排序,快排序
- jquery源码学习--资源清单
- 网易视频云受邀参加中国视频云峰会,有哪些看点?
- 排座椅
- 树莓派3b学习笔记
- git用法,clone依赖的库
- LeetCode学习篇十四——House Robber
- 统计文件中单词出现的行号及打印出该行内容