Created
April 22, 2024 08:33
-
-
Save wesolowski/ed3dce8632cebaa51afd5f6921933c27 to your computer and use it in GitHub Desktop.
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 | |
/* Sum Arrays | |
Write a function that takes an array of numbers and returns the sum of the numbers. The numbers can be negative or non-integer. If the array does not contain any numbers then you should return 0. | |
#### Examples | |
Input: [-2.398] Output: -2.398 | |
Input: [1, 5.2, 4, 0, -1] Output: 9.2 | |
Input: [] Output: 0 | |
#### Assumptions | |
* You can assume that you are only given numbers. | |
* You cannot assume the size of the array. | |
* You can assume that you do get an array and if the array is empty, return 0. */ | |
function sum(array $a) { | |
$sum = 0; | |
// Your code here | |
return $sum; | |
} | |
echo sum([]); //show 0 | |
echo PHP_EOL; | |
echo sum([1, 5.2, 4, 0, -1]); // show 9.2 | |
/* ------------------------------ */ | |
/* Sum of positive | |
You get an array of numbers, return the sum of all of the positives ones. | |
Example [1,-4,7,12] => 1 + 7 + 12 = 20 | |
Note: if there is nothing to sum, the sum is default to 0. */ | |
function positive_sum($arr) { | |
$sum = 0; | |
// Your code here | |
return $sum; | |
} | |
echo positive_sum([1,-4,7,12]); // show 20 | |
/* ------------------------------ */ | |
/* Number of People in the Bus | |
There is a bus moving in the city which takes and drops some people at each bus stop. | |
You are provided with a list (or array) of integer pairs. Elements of each pair represent the number of people that get on the bus (the first item) and the number of people that get off the bus (the second item) at a bus stop. | |
Your task is to return the number of people who are still on the bus after the last bus stop (after the last array). Even though it is the last bus stop, the bus might not be empty and some people might still be inside the bus, they are probably sleeping there :smile: | |
Take a look on the test cases. | |
Please keep in mind that the test cases ensure that the number of people in the bus is always >= 0. So the returned integer can't be negative. | |
The second value in the first pair in the array is 0, since the bus is empty in the first bus stop. */ | |
function number($busStops) { | |
$sum = 0; | |
// Your code here | |
return $sum; | |
} | |
echo number([[3,0],[9,1],[4,8],[12,2],[6,1],[7,8]]); // return 21 | |
echo PHP_EOL; | |
echo number([[10,0],[3,5],[5,8]]); // return 5 | |
echo PHP_EOL; | |
echo number([[3,0],[9,1],[4,10],[12,2],[6,1],[7,10]]); // return 17 | |
echo PHP_EOL; | |
echo number([[0,0]]); // return 0 | |
echo PHP_EOL; | |
/* ------------------------------ */ | |
/* Total amount of points | |
Our football team has finished the championship. | |
Our team's match results are recorded in a collection of strings. Each match is represented by a string in the format "x:y", where x is our team's score and y is our opponents score. | |
For example: ["3:1", "2:2", "0:1", ...] | |
Points are awarded for each match as follows: | |
- if x > y: 3 points (win) | |
- if x < y: 0 points (loss) | |
- if x = y: 1 point (tie) | |
We need to write a function that takes this collection and returns the number of points our team (x) got in the championship by the rules given above. | |
Notes: | |
- you can use php function explode*/ | |
function points($games) { | |
$sum = 0; | |
// Your code here | |
return $sum; | |
} | |
echo points(['1:0','2:0','3:0','4:0','2:1','3:1','4:1','3:2','4:2','4:3']); // show 30 | |
echo PHP_EOL; | |
echo points(['1:1','2:2','3:3','4:4','2:2','3:3','4:4','3:3','4:4','4:4']); // show 10 | |
echo PHP_EOL; | |
echo points(['0:1','0:2','0:3','0:4','1:2','1:3','1:4','2:3','2:4','3:4']); // show 0 | |
echo PHP_EOL; | |
echo points(['1:0','2:0','3:0','4:0','2:1','1:3','1:4','2:3','2:4','3:4']); // show 15 | |
echo PHP_EOL; | |
echo points(['1:0','2:0','3:0','4:4','2:2','3:3','1:4','2:3','2:4','3:4']); // show 12 | |
echo PHP_EOL; | |
/* ------------------------------ */ | |
/* Schreibe ein Programm mit einer foreach-Schleife, das im Browser / Console dynamisch eine Liste ausgibt. Diese Liste soll Folgendes enthalten: | |
Aufgabe 1: den Namen und Nachnamen von Personen, die Lehrer sind | |
Aufgabe 2: alle Berufe, die in diesem Array vorkommen, wobei die Berufe nicht doppelt angezeigt werden dürfen. | |
*/ | |
$user = [ | |
["name" => "Hans", "lastname" => "Schmidt", "job" => "Ingenieur", "age" => 35], | |
["name" => "Anna", "lastname" => "Müller", "job" => "Programmierer", "age" => 28], | |
["name" => "Max", "lastname" => "Weber", "job" => "Lehrer", "age" => 42], | |
["name" => "Julia", "lastname" => "Maier", "job" => "Designerin", "age" => 25], | |
["name" => "Markus", "lastname" => "Bauer", "job" => "Verkäufer", "age" => 31], | |
["name" => "Sabine", "lastname" => "Krause", "job" => "Ärztin", "age" => 49], | |
["name" => "Anton", "lastname" => "Brot", "job" => "Programmierer", "age" => 32], | |
["name" => "Leon", "lastname" => "Krause", "job" => "Lehrer", "age" => 42], | |
]; | |
/* ------------------------------ */ | |
/* String repeat | |
Write a function that accepts an integer n and a string s as parameters, and returns a string of s repeated exactly n times. | |
Examples (input -> output) | |
6, "I" -> "IIIIII" | |
5, "Hello" -> "HelloHelloHelloHelloHello" | |
/* ------------------------------ */ | |
/* Sum of Digits / Digital Root | |
Given n, take the sum of the digits of n. If that value has more than one digit, continue reducing in this way until a single-digit number is produced. The input will be a non-negative integer. | |
Examples | |
16 --> 1 + 6 = 7 | |
942 --> 9 + 4 + 2 = 15 --> 1 + 5 = 6 | |
132189 --> 1 + 3 + 2 + 1 + 8 + 9 = 24 --> 2 + 4 = 6 | |
493193 --> 4 + 9 + 3 + 1 + 9 + 3 = 29 --> 2 + 9 = 11 --> 1 + 1 = 2*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment