Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. <?php
  2.  
  3. // Make this work (without using any built-in functions, only a for loop, return the next binary number in a string or as an array).
  4.  
  5. // next_binary_number([1,0]) // [1,1]
  6.  
  7. // possible test cases:
  8. // [1,0] => [1,1]
  9. // [1,1] => [1,0,0]
  10. // [1,1,0] => [1,1,1]
  11. // .......
  12. // [1,0,0,0,0,0,0,0,0,1] => [1,0,0,0,0,0,0,0,1,0]
  13.  
  14. // Your solution:
  15.  
  16. // if we type in our console your function and next_binary_number([1,0,0,0,0,0,0,0,0,1]) then the result should look like 1,0,0,0,0,0,0,0,1,0 (or as an array).
  17.  
  18.  
  19. function nextGreater($num) {
  20.  
  21. $l = strlen($num);
  22.  
  23. for ($i = $l - 1; $i >= 0; $i--)
  24. {
  25. if ($num[$i] == '0')
  26. {
  27. $num[$i] = '1';
  28. break;
  29. }
  30.  
  31. else
  32. $num[$i] = '0';
  33. }
  34.  
  35. if ($i < 0)
  36. $num = "1" . $num;
  37.  
  38. return $num;
  39. }
  40.  
  41. ?>
  42.  
  43. <?php
  44.  
  45. // Task 2:
  46. //
  47. // Make this work (no vowels, lowercase except the first letter):
  48. //
  49. // reformat("TyPEqaSt DeveLoper TeST") //Typqst dvlpr tst
  50. //
  51. // Your solution:
  52. //
  53. // if we type in our CLI your function and reformat("TyPEqaSr DeveLoper TeST") then the result should be Typqst dvlpr tst
  54.  
  55. function reformat(string $sentance) {
  56.  
  57. $vowels = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"];
  58.  
  59. $result = str_replace($vowels, "", ucfirst(strtolower($sentance)));
  60.  
  61. return $result;
  62. }
  63.  
  64. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement