K

@kenbitanga

PHP: Stack Implementation Using a Linked List

PHP
2 years ago
<?php class Node { public $data; public $next; public function __construct($data) { $this->data = $data; }

C#: Stack Implementation Using a Linked List

C#
2 years ago
using System; namespace MyCompiler { class Node { public int data; public Node next = null; public Node(int value) { data = value; } public override string ToString() {

Ruby: Stack Implementation Using Linked List

Ruby
2 years ago
class Node attr_reader :data attr_accessor :next def initialize(data) @data = data end def to_s output = (@next != nil) ? "#{@next}, " : ""

Kotlin: Stack Implementation

Kotlin
2 years ago
class Node(val data: Int, var next: Node? = null) { override fun toString(): String { // displays stack in reverse order // where top of stack is the right-most element var output = if (next != null) "$ne

PHP: Random Quote Generator

PHP
2 years ago
<?php $quotes = [ [ 'author' => 'Vincent Van Gogh', 'text' => 'Where friendship blooms, life is reborn.', ], [ 'author' => 'Robert Black', 'text' => 'Find someone you care enough about to help you control your drinking. Preferably yourse

PHP: elvis operator (ternary shortcut)

PHP
2 years ago
<?php $balance = 0; $availableBalance = $balance ?: 'zero'; echo 'Your balance is ' . $availableBalance . PHP_EOL; var_dump(empty($balance));

PHP: Scope & the 'use' keyword

PHP
2 years ago
<?php $config = [ 'separator' => '_', ]; // 'use ()' must be used in a function expression $fullname = function ($first, $last) use ($config) { return "{$first}{$config['separator']}{$last}"; };

PHP: match(true)

PHP
2 years ago
<?php $age = 108; $result = match (true) { $age > 0 && $age < 13 => 'child', $age >= 13 && $age < 18 => 'teen', $age >= 18

PHP: spread operator, array_sum()

PHP
2 years ago
<?php function add(float ...$numbers) { return array_sum($numbers); } echo add(1,2,3.5);

PHP: func_get_args(), is_numeric()

PHP
2 years ago
<?php function add() { $total = 0; foreach (func_get_args() as $number) { if (!is_numeric($number)) { continue; }

PHP: Looping through nested assoc arrays

PHP
2 years ago
<?php $topics = [ [ 'id' => 1, 'title' => 'Learn PHP the right way!', 'posts' => [ ['body' => 'Practice a lot.'], ['body' => 'Work on a project.'], ]

PHP: Dice roll

PHP
2 years ago
<?php $numberIWant = 3; while (($diceRoll = rand(1, 6)) != $numberIWant) { echo "You rolled a {$diceRoll}, we need a {$numberIWant}.\n"; } echo "You rolled a {$numberIWant}!";

Java: HashMap

Java
2 years ago
import java.util.HashMap; // The main method must be in a class named "Main". class Main { public static void main(String[] args) { HashMap<String, String> map = new HashMap<>(); map.put("sunny", "blue");

PHP: if-elseif vs. switch-case vs. match vs. associative array

PHP
2 years ago
<?php $color = null; $weather = 'sunny'; // using if-elseif-else if ($weather == 'sunny') { $color = 'blue'; } elseif ($weather == 'cloudy') { $color = 'grey';

PHP: Associative array in place of if-elseif-else

PHP
2 years ago
<?php $colors = [ 'sunny' => 'blue', 'cloudy' => 'grey', ]; $weather = 'cloudy'; echo $colors[$weather] ?? 'invalid weather';

PHP: Using an expression in switch-case

PHP
2 years ago
<?php $weather = 'sunny'; $color = null; switch (true) { case $weather == 'sunny'; $color = 'blue'; break; case $weather == 'cloudy';

TS: Select from tuple

TypeScript
2 years ago
/* Write a function that takes an array of tuples. Each tuple consists of a name (a string) and an age (a number). Your function should return only the names. You'll need to use a loop. It can be a regular for loop, a call to .forEach, or a call

PHP: Stack, __toString() Method

PHP
2 years ago
<?php class Node { public ?int $data = null; public ?Node $next = null; public function __toString() : string { return "$this->data $this->next";

PHP: Stack, Properties Initialized to Null, No Null Coalescing Operator Used

PHP
2 years ago
<?php class Node { // always initialize properties public mixed $data = null; public ?Node $next = null; public function __construct(mixed $data) {

PHP: Queue, Null Coalescing Operator

PHP
2 years ago
<?php class Node { // always initialize properties public mixed $data = null; public ?Node $next = null; public function __construct(mixed $data) { $this->data = $data;