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
<?php | |
/** | |
* Define $output on multiple $lines | |
* Use Herodoc syntax. | |
* | |
* output = | |
* She said "This is John's test" | |
* on multiple rows | |
*/ |
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
<?php | |
/** | |
* Define getMaxValue() method | |
* Use static syntax | |
*/ | |
class MyClass { | |
const MAX_VALUE = 100; |
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
<?php | |
/** | |
* array_map() | |
* | |
* Round up array values | |
* Use array_map() with built-in ceil functions | |
* The result array $mapped will be [2, 3, 4] | |
*/ |
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
<?php | |
/** | |
* Define class constructor and ... | |
* Display the constructor method name | |
*/ | |
$obj = new A(); | |
class A | |
{ |
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
<?php | |
/** | |
* Compose $m octal value using myFunc() recursively | |
* Use reminder and quontient values | |
* | |
* 97 (decimal) = 141 (octal) | |
* 97 % 8 => reminder 1 | |
* 12 % 8 => reminder 4 | |
* 1 % 8 => reminder 1 |
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
<?php | |
/** | |
* Given the users $role and access $rights ... | |
* Check if Mike has admin rights and downgrade him | |
* Wrong approach: | |
* if ($is_admin && $can_edit_articles ... | |
*/ | |
$rights = (object) ["read"=>1, "write"=>2, "readwrite"=>16, "admin"=> 32]; | |
$role = (object) ["jim"=>96, "mike"=>32]; |
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
<?php | |
/** | |
* Replace | |
* {b} abc {/b} with: | |
* <b> abc </b> | |
* Use preg_replace() function | |
*/ | |
$str = "{b} abc {/b}"; |
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
# Write a function convert() that returns | |
# days, hours, minutes, seconds since UTC | |
# The time module provides a function, also named time, | |
# that returns the current GMT in “the epoch” | |
import time | |
today = time.time() | |
# SOLUTION |
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
# A Caesar cypher is a weak form on encryption | |
# It involves "rotating" each letter by a number (shift it through the alphabet) | |
# A rotated by 3 is D; Z rotated by 1 is A | |
# In a SF movie the computer is called HAL, which is IBM rotated by -1 | |
# Write a function rotate_word() | |
# Use built-in functions ord (char to code_number), chr (codes to char) | |
# SOLUTION |
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
# Get the words with three consecutive double letters | |
# https://github.com/AllenDowney/ThinkPython2/blob/master/code/words.txt | |
def has3_2consecutive(word): | |
i = 0; count = 0 | |
while i < len(word) - 1: | |
if word[i] == word[i+1]: | |
count = count + 1 | |
if count == 3: | |
return True |
OlderNewer