-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
43
concepts/1_arrays_and_strings/permutation/permutation2.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
28
concepts/1_arrays_and_strings/permutation/permutation3.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |