loading...

Daily Challenge #245 - Sort Santa's Reindeer

thepracticaldev profile image dev.to staff ・1 min read

Now, Dasher! Now, Dancer! Now, Prancer, and Vixen! On, Comet! On, Cupid! On, Donder and Blitzen! That's the order Santa wanted his reindeer...right? What do you mean he wants them in order by their last names!? Looks like we need your help!

Write a function that accepts a sequence of Reindeer names, and returns a sequence with the Reindeer names sorted by their last names.

Example

for this input:

 [
  "Dasher Tonoyan", 
  "Dancer Moore", 
  "Prancer Chua", 
  "Vixen Hall", 
  "Comet Karavani",        
  "Cupid Foroutan", 
  "Donder Jonker", 
  "Blitzen Claus"
] 

you should return this output:

 [
  "Prancer Chua",
  "Blitzen Claus",
  "Cupid Foroutan", 
  "Vixen Hall", 
  "Donder Jonker", 
  "Comet Karavani",
  "Dancer Moore", 
  "Dasher Tonoyan",
] 

Tests

sort_reindeer(['Kenjiro Mori', 'Susumu Tokugawa', 'Juzo Okita', 'Akira Sanada']),['Kenjiro Mori', 'Juzo Okita', 'Akira Sanada', 'Susumu Tokugawa'])

sort_reindeer(['Yasuo Kodai', 'Kenjiro Sado', 'Daisuke Aihara', 'Susumu Shima', 'Akira Sanada', 'Yoshikazu Okita', 'Shiro Yabu', 'Sukeharu Nanbu', 'Sakezo Yamamoto', 'Hikozaemon Ohta', 'Juzo Mori', 'Saburo Tokugawa']),['Daisuke Aihara', 'Yasuo Kodai', 'Juzo Mori', 'Sukeharu Nanbu', 'Hikozaemon Ohta', 'Yoshikazu Okita', 'Kenjiro Sado', 'Akira Sanada', 'Susumu Shima', 'Saburo Tokugawa', 'Shiro Yabu', 'Sakezo Yamamoto'])


This challenge comes from xDranik on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

Posted on by:

thepracticaldev profile

dev.to staff

@thepracticaldev

The hardworking team behind dev.to ❤️

Discussion

markdown guide
 

Ruby:

def sort_reindeer(reindeer)
  reindeer.sort_by { |r| r.split.last }
end
 
 

Hehe, Ruby can be quite concise at times. A better version would actually be

reindeer.sort_by { |r| r.split.reverse }

which would sort by last names then first names.

🤯

I think last time I saw @ben post a response, it was in Ruby and reactions were pretty much the same.

I think it was combining arrays into 1?

arr1 + arr2

That concatenates them, yes. You can also do union (arr1 | arr2) and intersection (arr1 & arr2) or multiply an array by an integer ([1] * 3 == [1, 1, 1]).

 

Elm

reverseWords : String -> String
reverseWords text =
    text
        |> String.words
        |> List.reverse
        |> String.join " "


sortReindeers : List String -> List String
sortReindeers reindeers =
    List.sortBy reverseWords reindeers
 

Can use sort on the command line with the parameter k (key), in this example with the value 2. Echo the text and pipe it to sort.

$ echo "Dasher Tonoyan
Dancer Moore
Prancer Chua
Vixen Hall
Comet Karavani
Cupid Foroutan
Donder Jonker
Blitzen Claus" | sort -k 2

Prancer Chua
Blitzen Claus
Cupid Foroutan
Vixen Hall
Donder Jonker
Comet Karavani
Dancer Moore
Dasher Tonoyan

 

Python 3 solution

    # solution
    sort_reindeer = lambda r: sorted(r, key=lambda n: n.split(" ")[-1])

    # test cases
    cases = [
     ["Dasher Tonoyan", "Dancer Moore", "Prancer Chua", "Vixen Hall", "Comet Karavani", "Cupid Foroutan", "Donder Jonker", "Blitzen Claus"],
     ['Kenjiro Mori', 'Susumu Tokugawa', 'Juzo Okita', 'Akira Sanada'],
     ['Kenjiro Mori', 'Juzo Okita', 'Akira Sanada', 'Susumu Tokugawa'],
     ['Yasuo Kodai', 'Kenjiro Sado', 'Daisuke Aihara', 'Susumu Shima', 'Akira Sanada', 'Yoshikazu Okita', 'Shiro Yabu', 'Sukeharu Nanbu', 'Sakezo Yamamoto', 'Hikozaemon Ohta', 'Juzo Mori', 'Saburo Tokugawa'],
     ['Daisuke Aihara', 'Yasuo Kodai', 'Juzo Mori', 'Sukeharu Nanbu', 'Hikozaemon Ohta', 'Yoshikazu Okita', 'Kenjiro Sado', 'Akira Sanada', 'Susumu Shima', 'Saburo Tokugawa', 'Shiro Yabu', 'Sakezo Yamamoto']
    ]

    for i, case in enumerate(cases):
     print(f'Test case {1+i}; sorted reindeer:\n{sort_reindeer(case)}\n')

Try it online!

 

ES6


const sort_reindeer = (list)  => [...list].sort((a,b) => {
 const [, surname1] = a.split(" ")
 const [, surname2] = b.split(" ")

 if(surname1 > surname2) return 1;
 if(surname2 > surname1) return -1;
 return 0;

})


 

Javascript

var sorted = [...cases].sort((a,b)=>a.split(' ')[1].localeCompare(b.split(' ')[1]))
 

JS

const sort_reindeer = lst => lst.sort((a,b) => a.substring(a.indexOf(' ')).localeCompare(b.substring(b.indexOf(' '))))