我不明白为什么我通过更改放置行获得差异答案,如果在代码中

时间:2016-09-05 17:57:01

标签: python

我会编写一个程序来打印s中最长的子字符串,其中字母按字母顺序出现。例如s = 'azcbobobegghakl' 将打印出'beggh'

s = 'azcbobobegghakl'
result = ''
word ='a'
sen = ''
for alpha in s:
  if alpha < word:
        word ='a'
        sen = ''   
  if alpha >= word:   
        word = alpha
        sen += alpha
        if len(result) <len(sen):
            result = sen

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

答案是

Logest substring in alphabetical order is: beggh

但是只需将代码放在不同的行上。我的回答错了!!!

s = 'azcbobobegghakl'
result = ''
word ='a'
sen = ''
for alpha in s:  
  if alpha >= word:   
       word = alpha
       sen += alpha
       if len(result) <len(sen):
           result = sen
  if alpha < word:
     word ='a'
     sen = '' 
print('Longest substring in alphabetical order is: '+ result)

回答

Longest substring in alphabetical order is: eggh

这个问题让我发疯了!!!

2 个答案:

答案 0 :(得分:0)

您正在丢弃新序列的第一个字符

    var SearchResultComponent = Vue.extend({
    template:
        '<div class="table-responsive">' +

            '<h3 class="selectedItemsHeader" style="margin-top:0;margin-bottom:0;">{{ language.resultsItems }}</h3>' +

            '<table class="table">' +

                '<thead>' +
                    '<tr>' +
                        '<th style="width:15%">' +
                            '<a href="javascript:void(0)" style="color:#333;" class="sort-btn companyrate" @click="sortBy = \'rate\'">{{ language.company }} <i class="fa fa-caret-down"></i></a>' +
                        '</th>' +
                        '<th style="width:30%">{{ language.service }}</th>' +
                        '<th style="width:10%">{{ language.servicePrice }}</th>' +
                        '<th style="width:10%">{{ language.discount }}</th>' +
                        '<th style="width:15%">{{ language.info }}</th>' +
                        '<th style="width:10%">' +
                            '<a href="javascript:void(0)" class="sort-btn totalVat" @click="sortBy = \'total\'">{{ language.totalVat }} <i class="fa fa-caret-up"></i></a>' +
                        '</th>' +
                        '<th></th>' +
                    '</tr>' +
                '</thead>' +

                '<tbody v-if="items.length > 0">' +
                    '<tr v-for="item in items | orderBy itemByTotalPrice">' +
                        '<td class="clearfix">{{ item.companyName }}'+
                            '<span class="rate" v-if="item.companyRate"><span class="star"></span><span class="point">{{ item.companyRate }} / 5</span></span>'+
                        '</td>' +
                        '<td class="small-text item-name">{{{ getItemName(item) }}}</td>' +
                        '<td>{{ totalPriceNoDiscount(item) }} &euro;</td>' +
                        '<td>{{ totalPriceDiscount(item) }} &euro;</td>' +
                        '<td class="small-text">{{ item.companyNote || \' - \' }}</td>' +
                        '<td>{{ totalPrice(item) }} &euro;</td>' +
                        '<td>'+
                            '<button class="btn btn-primary btn-sm" @click="select(item, $event)">{{ language.select }}</button>' +
                        '</td>' +
                    '</tr>' +
                '</tbody>' +


                '<tbody v-if="items.length == 0">' +
                    '<tr>' +
                        '<td colspan="7" style="font-size: 12pt;color: #777;padding-top: 60px;">{{ language.noResultsFound }}</td>' +
                    '</tr>' +
                '</tbody>' +

            '</table>' +

            '<h3 class="selectedItemsHeader" v-if="selectedItems.length > 0">{{ language.selectedItems }}</h3>' +

            '<table class="table selectedItems" v-if="selectedItems.length > 0">' +
                '<tbody>' +
                    '<tr v-for="item in selectedItems">' +
                        '<td style="width:15%">{{ item.selectedItem.companyName }}' +
                        '</td>' +
                        '<td style="width:30%" class="small-text item-name">{{{ getItemName(item.selectedItem) }}}</td>' +
                        '<td style="width:10%">{{ totalPriceNoDiscount(item.selectedItem) }} &euro;</td>' +
                        '<td style="width:10%">{{ totalPriceDiscount(item.selectedItem) || \'0.00\' }} &euro;</td>' +
                        '<td style="width:15%" class="small-text">{{ item.selectedItem.companyNote || \' - \' }}</td>' +
                        '<td style="width:10%">{{ totalPrice(item.selectedItem) }} &euro;</td>' +
                        '<td>'+
                            '<button class="btn btn-danger btn-sm" @click="remove(item)"><i class="fa fa-remove"></i></button>' +
                        '</td>' +
                    '</tr>' +
                '</tbody>' +
            '</table>' +
        '</div>',
    props: ['items','selectedItems','removeUrl'],
    data: function(){ return { sortBy: 'total' } },
    computed: {
        'language' : function() { return language; }
    },
    methods: {
        'select' : function(item, ev) {
            this.$dispatch('onCompanySelect', item);
            $(ev.target).attr('disabled', true).addClass('disabled');
        },
        'remove' : function(item) {
            this.$dispatch('onSelectedItemRemove', {
                removeUrl: this.removeUrl,
                item: item
            });
        },
        'totalPriceNoDiscount' : function(item) {
            return item.priceNoDiscount.toFixed(2);
        },
        'totalPriceDiscount' : function(item) {
            return (item.priceNoDiscount - this.totalPrice(item)).toFixed(2);
        },
        'totalPrice' : function(item) {
            return item.extraServicesPriceVat ? (item.priceVat + item.extraServicesPriceVat).toFixed(2) : item.priceVat.toFixed(2);
        },
        'itemByTotalPrice' : function(a, b) {
            if(this.sortBy == 'total') {
                $('a.companyrate').css('color', '#333');
                $('a.totalVat').css('color', '#337ab7');
                return this.totalPrice(a) > this.totalPrice(b);
            } else if(this.sortBy == 'rate') {
                $('a.totalVat').css('color', '#333');
                $('a.companyrate').css('color', '#337ab7');
                return a.companyRate < b.companyRate;
            }
        }
    }

});

注意改变:

s = 'azcbobobegghakl'
result = ''
word ='a'
sen = ''
for alpha in s:  
  print alpha,word,sen
  if alpha >= word:   
       word = alpha
       sen += alpha
       if len(result) <len(sen):
           result = sen
  if alpha < word:
     word = alpha
     sen = alpha 
print('Longest substring in alphabetical order is: '+ result)

你应该保存新序列的第一个字符

答案 1 :(得分:0)

好的,我们假设您有几个函数f1和f2,并且您正在跟踪for循环的每次迭代,如下所示:

def f1(s):
    result = ''
    word = 'a'
    sen = ''
    for index, alpha in enumerate(s):
        if alpha < word:
            word = 'a'
            sen = ''
        if alpha >= word:
            word = alpha
            sen += alpha
            if len(result) < len(sen):
                result = sen

        print("{0}-{1} => {2}".format(index, alpha, result))

    return result


def f2(s):
    result = ''
    word = 'a'
    sen = ''
    for index, alpha in enumerate(s):
        if alpha >= word:
            word = alpha
            sen += alpha
            if len(result) < len(sen):
                result = sen
        if alpha < word:
            word = 'a'
            sen = ''

        print("{0}-{1} => {2}".format(index, alpha, result))

    return result

s = 'azcbobobegghakl'
print('-' * 80)
print('f1: Longest substring in alphabetical order is: ' + f1(s))
print('-' * 80)
print('f2: Longest substring in alphabetical order is: ' + f2(s))

现在,运行此脚本将产生:

--------------------------------------------------------------------------------
0-a => a
1-z => az
2-c => az
3-b => az
4-o => az
5-b => az
6-o => az
7-b => az
8-e => az
9-g => beg
10-g => begg
11-h => beggh
12-a => beggh
13-k => beggh
14-l => beggh
f1: Longest substring in alphabetical order is: beggh
--------------------------------------------------------------------------------
0-a => a
1-z => az
2-c => az
3-b => az
4-o => az
5-b => az
6-o => az
7-b => az
8-e => az
9-g => az
10-g => egg
11-h => eggh
12-a => eggh
13-k => eggh
14-l => eggh
f2: Longest substring in alphabetical order is: eggh

正如你所看到的,一切都是相同的,直到它到达迭代9,f1结果变为9-g => beg而f2变为9-g => az,这就是顺序的变化如何影响结果,希望现在有意义。