Python 有没有一种方法可以使用随机大写字母的选择排序?
因此,我一直在研究一种代码,将十六进制转换为ASCII码,然后用选择排序对字母进行排序。但是当我得到一个输入,比如HBbmYa,它输出为B,H,Y,a,B,m。它应该输出为a、B、B、H、m、Y或a、B、B、H、m、Y。有没有办法解决这个问题?这是我现在的代码Python 有没有一种方法可以使用随机大写字母的选择排序?,python,selection-sort,Python,Selection Sort,因此,我一直在研究一种代码,将十六进制转换为ASCII码,然后用选择排序对字母进行排序。但是当我得到一个输入,比如HBbmYa,它输出为B,H,Y,a,B,m。它应该输出为a、B、B、H、m、Y或a、B、B、H、m、Y。有没有办法解决这个问题?这是我现在的代码 #ask for input hex_str = input("Write hexadecimal string here without spaces: ") #format the input so it does
#ask for input
hex_str = input("Write hexadecimal string here without spaces: ")
#format the input so it doesn't have any spaces or commas for the code to work
hex_str = hex_str.replace("," , "")
hex_str = hex_str.replace(" " , "")
#convert input to ASCII
def hexToASCII(hexx):
# initialize the ASCII code string as empty.
asci = ""
for i in range(0, len(hexx), 2):
# extract two characters from hex string
part = hexx[i : i + 2]
# change it into base 16 and
# typecast as the character
ch = chr(int(part, 16))
# add this char to final ASCII string
asci += ch
return asci
#function call
ascii_output = hexToASCII(hex_str)
# print the ASCII string.
print("ASCII output is: {}".format(ascii_output))
def selectionSort(u):
sortedarry = []
def findSmallest(l):
x = l[0]
for i in l:
[ascii_output.lower() for ascii_output in u]
if i < x:
x = i
return l.index(x)
while len(u) > 0:
x = findSmallest(u)
sortedarry.append(u.pop(x))
return sortedarry
u = list(ascii_output)
sortedarry = selectionSort(u)
# print the sorted array
print("The sorted array is: {}".format(sortedarry))
#请求输入
hex_str=input(“在此处写入十六进制字符串,不带空格:”)
#格式化输入,使其不包含任何空格或逗号,以便代码正常工作
十六进制字符串=十六进制字符串替换(“,”,“”)
十六进制字符串=十六进制字符串替换(“,”)
#将输入转换为ASCII码
def hexToASCII(hexx):
#将ASCII代码字符串初始化为空。
asci=“”
对于范围内的i(0,len(hexx),2):
#从十六进制字符串中提取两个字符
部分=hexx[i:i+2]
#将其更改为基数16,然后
#按字符类型转换
ch=chr(内部(第16部分))
#将此字符添加到最终ASCII字符串
asci+=ch
返回asci
#函数调用
ascii_输出=hexToASCII(hex_str)
#打印ASCII字符串。
打印(“ASCII输出为:{}”。格式(ASCII_输出))
def选择排序(u):
SorterDarry=[]
def findSmallest(l):
x=l[0]
对于l中的i:
[ascii_输出。小写()表示u中的ascii_输出]
如果i0时:
x=FindSalest(u)
分拣机附加(u.pop(x))
回程分拣室
u=列表(ascii_输出)
sortedarry=选择排序(u)
#打印已排序的数组
打印(“排序的数组是:{}”。格式(sortedary))
在findsmalest
函数中,您可以通过将比较的两个元素小写来使用不区分大小写的比较:
def findSmallest(l):
x = l[0]
for i in l:
if i.lower() < x.lower():
x = i
return l.index(x)
def findsmalest(l):
x=l[0]
对于l中的i:
如果i.lower()
在findsmalest
函数中,您可以通过将比较的两个元素小写来使用不区分大小写的比较:
def findSmallest(l):
x = l[0]
for i in l:
if i.lower() < x.lower():
x = i
return l.index(x)
def findsmalest(l):
x=l[0]
对于l中的i:
如果i.lower()
问题出在哪里?逗号?请提供预期输出为什么要编写自己的排序例程和十六进制转换器?Python已经有了这两个函数,我自己编写代码的原因是,如果是一个项目,我的老师不允许我们使用内置函数。只有我们自己的代码。有什么问题吗?逗号?请提供预期输出为什么要编写自己的排序例程和十六进制转换器?Python已经有了这两个函数,我自己编写代码的原因是,如果是一个项目,我的老师不允许我们使用内置函数。只有我们自己的代码。哦,好的,谢谢你的建议!哦,好的,谢谢你的建议!