MITx: 6.00.1x Alphabetical Substrings (python),mitxalphabetical,Assumesis
MITx: 6.00.1x Alphabetical Substrings (python),mitxalphabetical,Assumesis
Assumesis a string of lower case characters.
Write a program that prints the longest substring ofsin which the letters occur in alphabetical order. For example, if
s = ‘azcbobobegghakl‘, then your program should print
Longest substring in alphabetical order is: beggh
In the case of ties, print the first substring. For example, ifs = ‘abcbcd‘, then your program should print
Longest substring in alphabetical order is: abc
For problems such as these, do not includeraw_inputstatements or define the variable
sin any way. Our automated testing will provide a value of
sfor you - so the code you submit in the following box should assume
sis already defined. If you are confused by this instruction, please review L4 Problems 10 and 11 before you begin this problem set.
Note: This problem is fairly challenging. We encourage you to work smart. If you‘ve spent more than a few hours on this problem, we suggest that you move on to a different part of the course. If you have time, come back to this problem after you‘ve had a break and cleared your head.
# from string import lowercases = raw_input("Please input a String:")from itertools import countdef find_substring(input_string):maxsubstr = input_string[0:0]for start in range(len(input_string)):for end in count(start+len(maxsubstr)+1):substr = input_string[start:end]if len(substr)!=(end - start):breakif sorted(substr) == list(substr):maxsubstr = substrreturn maxsubstrlog_substr = find_substring(s)print ("Longest substring in alphabetical order is:"+log_substr)#lowe = 'abcdefghijklmnopqrstuvwxyz' # cont = []# sub = []# for i in s:# if len(sub) >= 1 and lowe.index(sub[-1]) + 1 != lowe.index(i):# cont.append(''.join(sub))# sub = []# sub.append(i) # cont = sorted(cont, key = len, reverse=True)# print cont[0]# cont = []# sub = []# for i in s:# if len(sub) >= 1 and lowercase.index(sub[-1]) + 1 != lowercase.index(i):# cont.append(''.join(sub))# sub = []# sub.append(i) # cont = sorted(cont, key = len, reverse=True)# print cont[0]# counter = 0# i = 0# length = len(s)# temp = s[0]# substr = s[0]# while (i<=length-1):# if s[i]<s[i+1]:# temp += s[i+1]# counter += 1# elif s[i]>s[i+1]:# if len(substr)<len(temp):# substr = temp# temp = s[i+1]# i = i + 1# if len(temp)>len(substr):# substr = temp# print 'Longest substring in alphabetical order is:',substr#s = 'cyqfjhcclkbxpbojgkar'# def long_alphabet(input_string):# maxsubstr = input_string[0:0] # empty slice (to accept subclasses of str)# for start in range(len(input_string)): # O(n)# for end in count(start + len(maxsubstr) + 1): # O(m)# substr = input_string[start:end] # O(m)# if len(substr) != (end - start): # found duplicates or EOS# break# if sorted(substr) == list(substr):# maxsubstr = substr# return maxsubstr# bla = (long_alphabet(s))# print "Longest substring in alphabetical order is: %s" %bla
MITx: 6.00.1x Alphabetical Substrings (python)
相关内容
- python request 接口测试get和post请求,pythonrequest,开发IDE:
- Python 函数式编程--高阶函数Map、Reduce、Filter、Sorted,
- ZABBIX 微信报警 插件(python无图),,ZABBIX可以实现
- python如何支持中文注释,,只要在Python开
- python实现列表中各元素的拼接,python拼接,功能要求:
- Python pandas DataFrame操作,,1. 从字典创建Da
- Python重写C语言程序100例--Part7,python--part7,‘‘‘【程序
- python执行shell指令的几种方法,pythonshell,1.os.syste
- Python 代码优化基础——判断对象类型,,# -*- codi
- python改文件名,python文件名,import os
评论关闭