简体   繁体   English

大于目标的最小字母

[英]smallest letter greater than target

I have written a code with the intention of returning the smallest letter after the target letter h , so in this case, it is i .我编写了一个代码,目的是返回target字母h之后的最小字母,所以在这种情况下,它是i However, the code below does not produce an output.但是,下面的代码不会产生 output。 Any ideas?有任何想法吗? I have a feeling the problem lies in the first if statement.我感觉问题出在第一个if语句中。

letters = [
    'a',
    'b',
    'c', 'c', 'c', 'c',
    'd',
    'e', 'e', 'e',
    'g',
    'h', 'h', 'h',
    'i',
    'j',
    'k', 'k', 'k',
    'l',
    'm',
    'n',
    'o',
    'p', 'p', 'p',
    'q',
    'r',
    's', 's',
    't'
]

target = 'h'
left = 0
right = len(letters) - 1
mid = left + (right - left) // 2

while left < right:
    if mid == target:
        for i in range(letters[mid], letters[len(letters)]):
            if letters[i] != target:
                print(letters[i])
                break

    if letters[mid] < target:
        left = mid + 1
    elif letters[mid] > target:
        right = mid - 1
letters =     ['a','b','c','c','c','c','d','e','e','e','g','h','h','h','i','j','k','k','k','l','m','n','o','p','p','p','q','r','s','s','t']

target = 'h'

# Find only after target
letters = letters[letters.index(target):]

# Remove target from list
letters = [x for x in letters if x != target]

# Sort list
letters.sort()

# Find char representation of lowest
print(letters[0])

If you like one-liner如果你喜欢单线

letters[len(letters) - letters[::-1].index('h')]

The code gets the index of the first letter 'h' from the reversed list, and use it to find the first one after the letter 'h'代码从反向列表中获取第一个字母“h”的索引,并使用它找到字母“h”之后的第一个

right = len(letters) -1
This will always return the same answer, as it always counts the number of letters in the array:这将始终返回相同的答案,因为它始终计算数组中的字母数:

https://www.w3schools.com/python/ref_func_len.asp https://www.w3schools.com/python/ref_func_len.asp

Another Solution另一个解决方案

An OrderedDict can be used to create the ordered unique list and this list can be used to get the target element index. OrderedDict可用于创建有序唯一列表,此列表可用于获取目标元素索引。

from collections import OrderedDict
ordered_unique_list=list(OrderedDict.fromkeys(letters))
print(ordered_unique_list[ordered_unique_list.index(target)+1])

Output Output

i

This would do the job;)这样就可以了;)

letters = ['a', 'b', 'c', 'c', 'c', 'c', 'd', 'e', 'e', 'e', 'g', 'h', 'h', 'h', 'i', 'j', 'k', 'k', 'k', 'l', 'm', 'n',
       'o', 'p', 'p', 'p', 'q', 'r', 's', 's', 't']

target = 'h'
left = 0
right = len(letters) - 1
mid = (left + right) // 2

while left <= right:
    if letters[mid] == target:
        for i in range(mid, len(letters)):
            if letters[i] != target:
                print(letters[i])
                break
        break

    if letters[mid] < target:
        left = mid + 1
    elif letters[mid] > target:
        right = mid - 1

    mid = (left + right) // 2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM