In: Computer Science
Python
1. Change the order of an input string based on Unicode equivalent.
Write a function Reorder that will take an input string of a maximum length of 20 characters and create a new string that reorders the string based on it’s binary Unicode character equivalent, e.g. ‘a’ = 0x61, ‘A’ = 0x41, from smallest value to largest value. In this example, ‘A’ would come before ‘a’, as 0x41 is a smaller number than ox61. Note, use python logical operators to do the comparison. Do not try to create a Unicode lookup table. For your code, use the string “sum=(x*259)/average” as your input string to your function.
Python Code:
#Function to sort the string
def sortChar(stringinp):
#Declare and initialize sorted string with first character
of input string. We can compare with this later
sortedString = stringinp[0]
#Loop through input string
for ch in stringinp[1:]:
#Loop through the sorted string
for i in range(0,len(sortedString)):
#Comparing the unicode charater value of characters of 2
strings. If value is lower, then insert the character in before the
larger character
if hex(ord(ch)) < hex(ord(sortedString[i])):
sortedString = sortedString[0:i] + ch + sortedString[i:]
break
else:
#If it is the largest character encountered append it at
the end
sortedString = sortedString + ch
#Print the sorte string
print(f'Sorted string is {sortedString}')
sortChar('sum=(x*259)/average')
Sample Output:
Sorted string is ()*/259=aaeegmrsuvx
#Function to sort the string def sortChar(stringinp): #Declare and initialize sorted string with first character of input string. We can compare with this later sortedString = stringinp[0] #Loop through input string for ch in stringinp[1:]: #Loop through the sorted string for i in range(0, len(sortedString)): #Comparing the unicode charater value of characters of 2 strings. If value is lower, then insert the character in before the larger character if hex(ord(ch)) < hex(ord(sortedString[i])): sortedString = sortedString[0:1] + ch + sortedString[i:] break else: #If it is the largest character encountered append it at the end sortedString = sortedString + ch #Print the sorte string print('Sorted string is sortedString}') sortCharl'sum=(x*259)/average') ||