Question

2. Ancestral Names Given a list of strings comprised of a name and a Roman numeral, sort the list first by name, then by deciConstraints • 1 sn s 50 • Each names[i] is a single string composed of 2 space-separated values: givenName and romanNumeral.Sample Case o Sample Input STDIN Function 2 → names [] size n = 2 Louis IX → names = [Louis IX, Louis VIII] Louis VIII Sa

Can someone code this asap? Use any language that you want.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

As I can use any language, I've used Python

********************

def toDecimal(s): #method to convert roman to decimal
ones={"I":1,"II":2,"III":3,"IV":4,"V":5,"VI":6,"VII":7,"VIII":8,"IX":9} #list storing decimal values for 1-9
tot=0 #value in decimal
pos=0 #position variable
if(s.startswith("L")): #if roman is L i.e., 50
tot=50
return tot #return as 50 is maximum
elif(s.startswith("XL")): #if its 40
tot=40
pos=2
elif(s.startswith("XXX")):
tot=30
pos=3
elif(s.startswith("XX")):
tot=20
pos=2
elif(s.startswith("X")):
tot=10
pos=1
if(pos>=len(s)): #if entire roman string is traversed, return value
return tot
tot=ones[s[pos:]] + tot #else find value for remaining part and add it
return tot

def sortRoman(names):#method to sort name and roman
l=len(names)
nam=[] #three lists for storing name,roman, and decimal
rom=[]
dec=[]
for i in range(l):
det=names[i].split(" ") #split input into name and roman
nam.append(det[0])
rom.append(det[1])
dec.append(toDecimal(rom[i])) #add decimal of roman in list
namdec=[] #new list which consists of names appended with decimal
for i in range(l):
namdec.append(nam[i] + str(dec[i])) #appending name and decimal
(nd,n,r,d)=list(zip(*sorted(zip(namdec,nam,rom,dec)))) #sorting all the lists according to the namedec list
nam=list(n) #converting them to list
rom=list(r)
dec=list(d)
returnlist=[] #new list which will store output
for i in range(l):
returnlist.append(nam[i] + " " + rom[i]) #as the lists are sorted, just append name with roman (as in input)
return returnlist

Code screenshot for indentation help:

def toDecimal(3): #method to convert roman to decimal ones={I:1,II:2,III:3,IV:4,V:5,VI:6,VII:7,VIII:8,IX:9}

def sortRoman (names) : #method to sort name and roman l=len (names) nam=[] #three lists for storing name, roman, and decimal

Driver code:

k=[Steven XL, Steven XVI, David IX, Mary XV, Mary XIII, Mary XX] print (Original list:\n, k) print(\nSorted li

Sample execution:

Original list: [Steven XL, Steven XVI, David IX, Mary XV, Mary XIII, Mary XX] Sorted list: [David IX, Mary XII

In case of any doubt, drop a comment and I'll surely get back to you.

Please give a like if you're satisied with the answer. Thank you.

Add a comment
Know the answer?
Add Answer to:
Can someone code this asap? Use any language that you want. 2. Ancestral Names Given a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT