DEV Community

Daily Challenge #66- Friend List

dev.to staff on September 14, 2019

John has invited some friends. His list is: s = "Fred:Corwill;Wilfred:Corwill;Barney:Tornbull;Betty:Tornbull;Bjon:Tornbull;Raphael:Corwill;Alfred:...
Collapse
 
ynndvn profile image
La blatte •

Lets do it:

s
.split(';')
.map(n=>n.split(':').reverse().join(':'))
.sort()
.map(n=>`(${n.split(':')[0]}, ${n.split(':')[1]})`.toUpperCase())
.join``

It's pretty straightforward:

  • split(';') gets us all Name:Surname
  • .map(n=>n.split(':').reverse().join(':')) reverses them to be Surname:Name
  • .sort() sorts them alphabetically
  • .map(n=>(${n.split(':')[0]}, ${n.split(':')[1]}).toUpperCase()) reformats every name (uppercase it, place parentheses and commas)
  • The last step is equivalent to .join(''), which transforms the name array to an output string

Here is the output:

"Fred:Corwill;Wilfred:Corwill;Barney:Tornbull;Betty:Tornbull;Bjon:Tornbull;Raphael:Corwill;Alfred:Corwill"
.split(';')
.map(n=>n.split(':').reverse().join(':'))
.sort()
.map(n=>`(${n.split(':')[0]}, ${n.split(':')[1]})`.toUpperCase())
.join``;


// "(CORWILL, ALFRED)(CORWILL, FRED)(CORWILL, RAPHAEL)(CORWILL, WILFRED)(TORNBULL, BARNEY)(TORNBULL, BETTY)(TORNBULL, BJON)"
Collapse
 
blindfish3 profile image
Ben Calder • • Edited

Same but different:

  const s = "Fred:Corwill;Wilfred:Corwill;Barney:Tornbull;Betty:Tornbull;Bjon:Tornbull;Raphael:Corwill;Alfred:Corwill";
  return s.toUpperCase().split(';')
          .map(name => name.split(':').reverse())
          .sort()
          .reduce((accumulator, name) => ( accumulator += `(${name[0]}, ${name[1]})`), "");

You don't need the arbitrary join(':') after reverse() since sort() coerces the nested arrays to strings for you. That means you can avoid having to split again later.

But Amin is right:

  const s = "Fred:Corwill;Wilfred:Corwill;Barney:Tornbull;Betty:Tornbull;Bjon:Tornbull;Raphael:Corwill;Alfred:Corwill";
  return s.toUpperCase().split(';')
             .map(name=> `(${name.split(':')[1]}, ${name.split(':')[0]}`)
             .sort()
             .join('');
Collapse
 
ynndvn profile image
La blatte •

That's way shorter! Thanks a lot for the input

Collapse
 
idanarye profile image
Idan Arye •
s="Fred:Corwill;Wilfred:Corwill;Barney:Tornbull;Betty:Tornbull;Bjon:Tornbull;Raphael:Corwill;Alfred:Corwill"

echo $s \
    | tr ';' '\n' \
    | awk -F: '{print toupper("("$2":"$1")")}' \
    | sort \
    | paste -sd '' -
Collapse
 
brightone profile image
Oleksii Filonenko •

Rust:

fn meeting(list: &str) -> String {
    let list = list.to_uppercase();
    let list: Vec<_> = list.split(|c| [';', ':'].contains(&c)).collect();
    let mut list: Vec<_> = list
        .chunks_exact(2)
        .map(|chunk| format!("({}, {})", chunk[1], chunk[0]))
        .collect();
    list.sort();
    list.join("")
}
Collapse
 
peledzohar profile image
Zohar Peled •

Not much of a challenge, I'm afraid. Pretty straight forward split, split, join, sort and join again. A one liner in most modern languages, I suspect.
In c# it would look like this:

string meeting(string s)
{
    return "(" + string.Join(")(", 
        s.
        ToUpper().
        Split(';').
        Select(n => n.Split(':')).
        Select(n => string.Join(":", n.Reverse())).
        OrderBy(n => n)
    ) + ")"; 
}
Collapse
 
choroba profile image
E. Choroba •

Perl solution:

#!/usr/bin/perl
use warnings;
use strict;
use feature qw{ say };

my $in = 'Fred:Corwill;Wilfred:Corwill;Barney:Tornbull;Betty:Tornbull;Bjon:Tornbull;Raphael:Corwill;Alfred:Corwill';

say join "",
    map "($_->[1], $_->[0])",
    sort { $a->[1] cmp $b->[1] || $a->[0] cmp $b->[0] }
    map [split /:/],
    split /;/,
    uc $in;
 
therealkevinard profile image
Kevin Ard • • Edited

Anyone who says rex is too clever clearly hasn't bumped into bit-shifting code-golf lol

Rex is a very appropriate amount of cleverness 😉