Python中的词典编辑最小字符串问题 [英] Lexicographical minimal string problem in Python

查看:121
本文介绍了Python中的词典编辑最小字符串问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

字典最小字符串 -



假设我有两个输入字符串 'JACK' 'DENIAL' 的。现在根据问题我想要一个字典最小字符串作为这样的输出(在python中) -

DAJACKNIEL



但是根据我的代码得到了 -

JNJLADAAANAIAEALCDCNCICECLKNK



学习python和尝试下面的代码..



我尝试过:



Lexicographical minimal string--

Suppose I have two input strings 'JACK' and 'DENIAL'. Now as per the problem I want a lexicographical minimal string as an output like this (in python)--
DAJACKNIEL

But am getting, based on my code--
JNJLADAAANAIAEALCDCNCICECLKNK

Am learning python and tried the below code..

What I have tried:

def string_comp(str1, str2):
    empty_str = ''
    for i in range(len(str1)):
        for j in range(len(str2)):
            if str1[i:] < str2[j:]:
                empty_str += str1[i]
                empty_str += str2[j]
    empty_str = empty_str[:-1]
    if i < (len(str1)):
        empty_str += str1[i: -1]
    if j < (len(str2)):
        empty_str += str2[j: -1]
    return empty_str





好​​的,纠正我。



Kindly, correct me.

推荐答案

我认为现在是时候停止猜测你的代码在做什么了。是时候看到你的代码正在执行并确保它能达到预期的效果。



调试器是你的朋友。它会告诉你你的代码到底在做什么。

一步一步地执行,检查变量,你会发现它有一个停止做你期望的点。

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

http://docs.oracle .com / javase / 7 / docs / technotes / tools / windows / jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]



逐行运行程序,看看它停止符合您的预期的位置和原因。
I think it is time for you to stop guessing what your code is doing. It is time to see your code executing and ensuring that it does what you expect.

The debugger is your friend. It will show you what your code is really doing.
Follow the execution step by step, inspect variables and you will see that there is a point where it stop doing what you expect.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

Run the program line by line, and see where and why it stops matching your expectations.


这篇关于Python中的词典编辑最小字符串问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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