Jump to content

redefining the alphabet then sorting


poboy

Recommended Posts

If we redefined the alphabet as:

 

DEFGHIJKMLNOPQTSRUVWXYABZC

 

Can you write a function that sorts the following array alphabetically according to the new alphabet definition:

 

array('A','J','R','U','Z','A','H','F','U','E','W','H','J','A','B','C','C','C','X','Y','A','G','T','R','U','V','X')

 

 

So I basically need to assign a key to each letter of this new alphabet then pass that into the array then sort by key.  

 

I'm not quite sure how to tackle this.  I was able to create the array then just run sort(array) but of course that sorts according to our existing alphabet.  Can someone point me in the right direction please?

Link to comment
Share on other sites

This works but it's not a function.  :/    

<?php

$aa = 'DEFGHIJKMLNOPQTSRUVWXYABZC';
$aa = str_split($aa);
$bb = ['A','J','R','U','Z','A','H','F','U','E','W','H','J','A','B','C','C','C','X','Y','A','G','T','R','U','V','X'];
$ordered = [];

foreach ($aa as $a) {
    foreach ($bb as $b) {
        if ($b == $a) {
            $ordered[] = $b;
        }
    }
}
var_dump($ordered); 

?>
Link to comment
Share on other sites

And, problem solved I believe.

<?php
function resort($aa)
{
	$aa = str_split($aa);
	$bb = ['A','J','R','U','Z','A','H','F','U','E','W','H','J','A','B','C','C','C','X','Y','A','G','T','R','U','V','X'];
	$ordered = [];
	foreach ($aa as $a) {
	    foreach ($bb as $b) {
	        if ($b == $a) {
	            $ordered[] = $b;
	        }
	    }
	}
	return $ordered;
}

var_dump(resort('DEFGHIJKMLNOPQTSRUVWXYABZC'));

?>
Link to comment
Share on other sites

Depends on how you define “solved”. This is dumb brute force and extremely inefficient. For example, you need 26 comparisons to sort an array with just one character. As your teacher/professor/instructor, I would reject this.

 

PHP has usort() to sort an array with a custom comparison function. That function takes two array elements $ch_1 and $ch_2, and you need to return -1 if $ch_1 is smaller than $ch_2 , 0 if $ch_1 and $ch_2 are equal, and +1 if $ch_1 is bigger than $ch_2 . When you go back to your initial idea of mapping the characters to their indexes, this should be doable:

<?php

$alphabet = 'DEFGHIJKMLNOPQTSRUVWXYABZC';
$char_indexes = array_flip(str_split($alphabet));    // map characters to their corresponding indexes (D => 0, E => 1, ...)

$input = ['A','J','R','U','Z','A','H','F','U','E','W','H','J','A','B','C','C','C','X','Y','A','G','T','R','U','V','X'];

usort($input, function ($ch_1, $ch_2) use ($char_indexes) {
    // TODO: return -1, 0 or +1 depending on the comparison
});

// var_dump($input);

Hint: If you have PHP 7, the spaceship operator will save you a lot of code.

Link to comment
Share on other sites

Slight correction: the function to usort() needs only return negative, zero, or positive. It doesn't have to be strictly -1/0/+1. The spaceship operator does work that way, but that was just a stupid decision made by the PHP devs for that particular operator.

Link to comment
Share on other sites

Depends on how you define “solved”. This is dumb brute force and extremely inefficient. For example, you need 26 comparisons to sort an array with just one character. As your teacher/professor/instructor, I would reject this.

 

PHP has usort() to sort an array with a custom comparison function. That function takes two array elements $ch_1 and $ch_2, and you need to return -1 if $ch_1 is smaller than $ch_2 , 0 if $ch_1 and $ch_2 are equal, and +1 if $ch_1 is bigger than $ch_2 . When you go back to your initial idea of mapping the characters to their indexes, this should be doable:

<?php

$alphabet = 'DEFGHIJKMLNOPQTSRUVWXYABZC';
$char_indexes = array_flip(str_split($alphabet));    // map characters to their corresponding indexes (D => 0, E => 1, ...)

$input = ['A','J','R','U','Z','A','H','F','U','E','W','H','J','A','B','C','C','C','X','Y','A','G','T','R','U','V','X'];

usort($input, function ($ch_1, $ch_2) use ($char_indexes) {
    // TODO: return -1, 0 or +1 depending on the comparison
});

// var_dump($input);

Hint: If you have PHP 7, the spaceship operator will save you a lot of code.

 

 

 

 

Thank you so much for the feedback, but when I var_dump $input this is the array I get:  

array (size=27)
  0 => string 'X' (length=1)
  1 => string 'Y' (length=1)
  2 => string 'C' (length=1)
  3 => string 'C' (length=1)
  4 => string 'C' (length=1)
  5 => string 'A' (length=1)
  6 => string 'G' (length=1)
  7 => string 'V' (length=1)
  8 => string 'X' (length=1)
  9 => string 'U' (length=1)
  10 => string 'R' (length=1)
  11 => string 'T' (length=1)
  12 => string 'B' (length=1)
  13 => string 'A' (length=1)
  14 => string 'Z' (length=1)
  15 => string 'A' (length=1)
  16 => string 'U' (length=1)
  17 => string 'R' (length=1)
  18 => string 'J' (length=1)
  19 => string 'H' (length=1)
  20 => string 'F' (length=1)
  21 => string 'H' (length=1)
  22 => string 'J' (length=1)
  23 => string 'W' (length=1)
  24 => string 'E' (length=1)
  25 => string 'U' (length=1)
  26 => string 'A' (length=1)

The array that I got, which I believe to be correct was :

0 => string 'E' (length=1)
  1 => string 'F' (length=1)
  2 => string 'G' (length=1)
  3 => string 'H' (length=1)
  4 => string 'H' (length=1)
  5 => string 'J' (length=1)
  6 => string 'J' (length=1)
  7 => string 'T' (length=1)
  8 => string 'R' (length=1)
  9 => string 'R' (length=1)
  10 => string 'U' (length=1)
  11 => string 'U' (length=1)
  12 => string 'U' (length=1)
  13 => string 'V' (length=1)
  14 => string 'W' (length=1)
  15 => string 'X' (length=1)
  16 => string 'X' (length=1)
  17 => string 'Y' (length=1)
  18 => string 'A' (length=1)
  19 => string 'A' (length=1)
  20 => string 'A' (length=1)
  21 => string 'A' (length=1)
  22 => string 'B' (length=1)
  23 => string 'Z' (length=1)
  24 => string 'C' (length=1)
  25 => string 'C' (length=1)
  26 => string 'C' (length=1)

Apologies if I'm going about this the wrong way.

Link to comment
Share on other sites

Looks like there's no way to go back and edit posts?

Only allowed for a couple minutes after posting. It's to prevent people from editing their post after someone has already seen and is starting to reply to the original version. And other reasons.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.