第一次编程练习 [英] first time programming-exercise

查看:71
本文介绍了第一次编程练习的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有史以来第一次编程......我正在尝试做这个练习......:

First time programming ever... I'm trying to do this exercise to.. :

编写一个程序,打印 s 中字母按字母顺序出现的最长子串.例如,如果 s = 'azcbobobegghakl',那么你的程序应该打印

Write a program that prints the longest substring of s in which the letters occur in alphabetical order. For example, if s = 'azcbobobegghakl', then your program should print

按字母顺序排列的最长子串是:beggh

Longest substring in alphabetical order is: beggh

我在这里......在开始惊慌失措之前:

I was here..before starting to freak out:

s = 'abcdezcbobobegghakl'
n = len(s) 
x = 0


x += 1
lengh = s[x-1]
if s[x] >= s[x-1]:
    lengh = lengh + s[x]


if s[x+1] < s[x]:
    n = len(lengh)
if x > n:
    break 

print('Longest substring in alphabetical order is: ' + str(lengh)) 

我知道这段代码很糟糕..我正在尝试按字母顺序查找子字符串,并以某种方式保留最长的!我知道这可能是正常的,因为我以前从未编程过,但我感到非常沮丧......有什么好主意/帮助吗??

I know this code is bad..I m trying to find substring in alphabetical order and is some way keep the longest one! I know could be normal, because I never programmed before but i feel really frustrated...any good idea/help??

推荐答案

def longest(s):
    buff = ''
    longest = ''

    s += chr(255)
    for i in range(len(s)-1):
        buff += s[i]
        if not s[i] < s[i+1]:
            if len(buff) > len(longest):
                longest = buff
            buff = ''
    if len(buff) > len(longest):
        longest = buff

    return longest

这篇关于第一次编程练习的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆