Switch indexes, create the next highest number using the same exact 9 digits presented

后端 未结 3 1701
自闭症患者
自闭症患者 2020-11-28 16:07

So I received a challenge that states the following: \"Design a program that takes as input a 9 digit number where no digit appears twice and produces as output an arrangeme

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-28 17:04

    input[8] == input[7]
    input[7] == temp
    

    you probably meant:

    input[8] = input[7]
    input[7] = temp
    

    didn't you?

    Which, as stated in the comments, wouldn't work directly on the string, as it is immutable in Python. So, as a first step, you could make a list of characters from that string:

    input = list(input)
    

    and as a last step, get a string back from the modified list:

    input = ''.join(input)
    

    BTW, you might want to benefit from Python tuple unpacking which allows you to swap two variables without having to introduce a third one:

    input[7], input[8] = input[8], input[7]
    

提交回复
热议问题