Skip to content

Commit

Permalink
permutation
Browse files Browse the repository at this point in the history
  • Loading branch information
djp424 committed Oct 25, 2016
1 parent 660b622 commit 0c77277
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Terms:
Permutation - the act of arranging all the members of a set into some sequence or order, or if the set is already ordered, rearranging (reordering) its elements, a process called permuting.
8 changes: 8 additions & 0 deletions concepts/1_arrays_and_strings/permutation/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
These programs will check if a string is a permutation of another.

Things one should consider:
* case sensitive? - yes
* whitespace? - yes

Further questions I thought about:
* Would returning a string vs an array slow the program down?
25 changes: 25 additions & 0 deletions concepts/1_arrays_and_strings/permutation/permutation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

echo permutation( 'abcdefg', 'gfedcba' ) . "\n"; // true
echo permutation( 'abcdefg', 'gfedcbaa' ) . "\n"; // false
echo permutation( 'a', 'gafaefoaiwehraoiehjfrpawhefrlfedcbaa' ) . "\n"; // false

function permutation( $string1, $string2 ) {
if ( strlen( $string1 ) !== strlen( $string2 ) ) {
return 'false';
}

if ( string_to_sorted_array( $string1 ) !== string_to_sorted_array( $string2 ) ) {
return 'false';
}

return 'true';
}

function string_to_sorted_array( $string ) {
$chars = array();
for ( $i = 0; $i <= $strlen - 1; $i++ ) {
$chars[ $i ] = substr( $string, $i, 1 );
}
return arsort( $chars );
}
15 changes: 15 additions & 0 deletions concepts/1_arrays_and_strings/permutation/permutation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
def permutation( str1, str2 ):
if len( str1 ) != len( str2 ):
return 'false'

if word_array_sort(str1) != word_array_sort(str2):
return 'false'

return 'true'

def word_array_sort( str ):
return list(str).sort()

print permutation( "abcdefg", "gfedcba" ); # true
print permutation( "abcdefg", "gfedcbaa" ); # false
print permutation( "a", "gafaefoaiwehraoiehjfrpawhefrlfedcbaa" ); # false
43 changes: 43 additions & 0 deletions concepts/1_arrays_and_strings/permutation/permutation2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

echo permutation( 'abcdefgzzdddd', 'gfedcbazzdddd' ) . "\n"; // true
echo permutation( 'abcdefg', 'gfedcbaa' ) . "\n"; // false
echo permutation( 'a', 'gafaefoaiwehraoiehjfrpawhefrlfedcbaa' ) . "\n"; // false

function permutation( $string1, $string2 ) {
$strlen1 = strlen( $string1 );
if ( $strlen1 !== strlen( $string2 ) ) {
return 'false';
}

$array1 = create_array( $string1, $strlen1 );
$array2 = create_array( $string2, $strlen1 );

if ( ! array_compare( $array1, $array2 ) ) {
return 'false';
}

return 'true';
}

function create_array( $string, $strlen ) {
$chars = array();
for ( $i = 0; $i <= $strlen - 1; $i++ ) {
$char = substr( $string, $i, 1 );
if ( ! isset( $chars[ $char ] ) ) {
$chars[ $char ] = 1;
} else {
$chars[ $char ]++;
}
}
return $chars;
}

function array_compare( $array1, $array2 ) {
foreach ( $array1 as $key => $value ) {
if ( $value !== $array2[ $key ] ) {
return false;
}
}
return true;
}
28 changes: 28 additions & 0 deletions concepts/1_arrays_and_strings/permutation/permutation3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

echo permutation( 'abcdefgzzdddd', 'gfedcbazzdddd' ) . "\n"; // true
echo permutation( 'abcdefg', 'gfedcbaa' ) . "\n"; // false
echo permutation( 'a', 'gafaefoaiwehraoiehjfrpawhefrlfedcbaa' ) . "\n"; // false

function permutation( $string1, $string2 ) {
if ( strlen( $string1 ) !== strlen( $string2 ) ) {
return 'false';
}

$array1 = str_split( $string1 );
$array2 = str_split( $string2 );
$chars = array(); // TODO ASCII (128)

foreach ( $array1 as $c ) { // count
$chars[ $c ]++;
}

foreach ( $array2 as $c ) { // reverse
$chars[ $c ]--;
if ( $chars[ $c ] < 0 ) {
return 'false';
}
}

return 'true';
}

0 comments on commit 0c77277

Please sign in to comment.