DEV Community

dev.to staff
dev.to staff

Posted on

Daily Challenge #37 - Name Swap

In today's challenge, we ask that you to write a function that returns a string in which an individual's first name is swapped with their last name.

Example: nameShuffler('Emma McClane'); => "McClane Emma"

Good luck!


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

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

Latest comments (25)

Collapse
 
mrdulin profile image
official_dulin

JavaScript:

function nameShuffler(str){
  return str.split(' ').reverse().join(" ");
}
Collapse
 
peter279k profile image
peter279k

Here is the simple solution with Python:

def name_shuffler(string):
    string_array = string.split(' ')

    return string_array[1] + ' ' + string_array[0]
Collapse
 
kvharish profile image
K.V.Harish

My solution in js


const nameShuffler = (name) => `${name}`.split(' ').reverse().join(' ');

Collapse
 
craigmc08 profile image
Craig McIlwrath
nameShuffler = unwords . reverse . words

Seems pretty simple (Haskell)

Collapse
 
mangelsnc profile image
Miguel Ángel SÑnchez Chordi
 <?php

 echo implode(' ', array_reverse(explode(' ', $argv[1])));
Collapse
 
hectorpascual profile image

Python :

def name_shuffler(name): return ' '.join([name.split()[1], name.split()[0]])
Collapse
 
qm3ster profile image
Mihail Malo

Buffer goes buf-buf

const rename = name => {
    const inBuf = Buffer.from(name, 'utf8')
    const len = inBuf.length
    const outBuf = Buffer.alloc(len, 0x20)
    let prev = 0
  for (let i = 0; i < len; i++) {
    if (inBuf[i] !== 0x20) continue
    inBuf.copy(outBuf, len - i, prev, i)
    prev = i + 1
  }
  inBuf.copy(outBuf, 0, prev)
    return outBuf.toString('utf8')
}
Collapse
 
badrecordlength profile image
Henry πŸ‘¨β€πŸ’» • Edited

Nice and simple python implementation 😁

def nameShuffler(name):
    name = name.split()
    name.reverse()
    print(str(name))
Collapse
 
diek profile image
diek

There is my python solution

Collapse
 
jay profile image
Jay

Rust Playground

fn name_shuffler(name: &str) -> String {
    name.split_whitespace().rev().collect::<Vec<&str>>().join(" ")
}