fork(2) download
  1. <?php
  2.  
  3.  
  4. function is10x($userInput) {
  5. $input = trim($userInput);
  6. $binStr = sprintf('%b', intval($input,2));
  7. if( $binStr !== $input) return FALSE;
  8. return array_sum(str_split($binStr)) === 1;
  9. }
  10.  
  11. // tests
  12. function test($userInput, $expected) {
  13. printf( "%-10s \t%s\n", '>'.$userInput.'<', is10x($userInput) === $expected ?'passed':'failed');
  14. }
  15. for($i=0; $i<8; $i++) test( (String)pow(10, $i), TRUE);
  16. for($i=0; $i<8; $i++) test( pow(10, $i), TRUE);
  17. test( '10000001', FALSE);
  18. test( '12345', FALSE);
  19. test( ' 0001 ', FALSE);
  20. test( 'a0b11', FALSE);
  21. test( '0b1111', FALSE);
  22. test( '000', FALSE);
  23. test( '0', FALSE);
  24.  
Success #stdin #stdout 0.02s 52472KB
stdin
Standard input is empty
stdout
>1<        	passed
>10<       	passed
>100<      	passed
>1000<     	passed
>10000<    	passed
>100000<   	passed
>1000000<  	passed
>10000000< 	passed
>1<        	passed
>10<       	passed
>100<      	passed
>1000<     	passed
>10000<    	passed
>100000<   	passed
>1000000<  	passed
>10000000< 	passed
>10000001< 	passed
>12345<    	passed
> 0001  <  	passed
>a0b11<    	passed
>0b1111<   	passed
>000<      	passed
>0<        	passed