My attempts at exercism.org
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

at main 147 lines 3.8 kB view raw view rendered
1# Language List 2 3Welcome to Language List on Exercism's PHP Track. 4If you need help running the tests or submitting your code, check out `HELP.md`. 5If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :) 6 7## Introduction 8 9## Arrays 10 11Arrays in PHP are ordered maps. 12A map is a data structure that associates a key to a value. 13Arrays are versatile and can be treated as a linear array (like in `C` or `Java`), list [vector], hash-table, dictionary, collection, stack, or a queue. 14 15A key is optional, but if specified must either be an `int` or a `string`. 16When a key is not provided, PHP defaults to integer keys in increasing order, starting at `0`. 17 18```php 19$no_keys = ["my", "first", "array"]; 20$integer_keys = [0 => "my", 1 => "first", 2 => "array"]; 21 22$no_keys == $integer_keys // => equal returns true 23$no_keys === $integer_keys // => strictly equal returns true 24``` 25 26An array can store values of all types. 27Each value can have a different type. 28 29### Using Arrays 30 31Arrays can be declared as a literal (written in code, as done above) or created and manipulated as functions. 32Access, assign, append values using the index operator: 33 34```php 35$prime_numbers = [2, 3, 5, 6]; 36 37$prime_numbers[3] = 7; // replace 6 with 7 38 39$prime_numbers[] = 11; // array now contains [2, 3, 5, 7, 11] 40``` 41 42## Variable-Length Arguments 43 44Function arguments can be specified such that it can take any number of arguments: 45 46```php 47<?php 48 49function actOnItems(...$items) { 50 // $items is an array containing 0 or more values 51 // used to call the function. 52} 53 54actOnItems(1, 2, 3, 4); // $items => [1, 2, 3, 4] 55``` 56 57## Instructions 58 59In this exercise you need to implement some functions to manipulate a list of programming languages. 60 61## 1. Define a function to return an empty language list 62 63Define the `language_list` function that takes no arguments and returns an empty list. 64 65```php 66<?php 67 68language_list(); 69// => [] 70``` 71 72## 2. Modify function to create a list from any number of languages 73 74Modify the `language_list` function, so it takes a variadic argument of languages (strings). 75It should return the resulting list with the languages in the list. 76 77```php 78<?php 79 80$language_list = language_list(); 81// => [] 82$language_list = language_list("Clojure", "PHP"); 83// => ["Clojure", "PHP"] 84$language_list = language_list("PHP", "Haskell", "Java", "C++", "Rust") 85// => ["PHP", "Haskell", "Java", "C++", "Rust"] 86``` 87 88## 3. Define a function to add a language to the list 89 90Define the `add_to_language_list` function that takes 2 arguments, an array of languages, and the new language. 91 92```php 93<?php 94 95$language_list = language_list(); 96// => [] 97$language_list = add_to_language_list($language_list, "Clojure"); 98// => ["Clojure"] 99``` 100 101## 4. Define a function to remove an item from the language list 102 103Define the `prune_language_list` function to remove the first language from the array of languages. 104 105```php 106<?php 107 108$language_list = language_list("PHP"); 109// => ["PHP"] 110$language_list = prune_language_list($language_list); 111// => [] 112``` 113 114## 5. Define a function to get the first item in the list 115 116Define the `current_language` function that takes 1 argument (a _language list_). 117It should return the first language in the list. 118Assume the list will always have at least one item. 119 120```php 121<?php 122 123$language_list = language_list("PHP", "Prolog"); 124// => ["PHP", "Prolog"] 125$first = current_language($language_list); 126// => "PHP" 127``` 128 129## 6. Define a function to return how many languages are in the list 130 131Define the `language_list_length` function that takes 1 argument (a _language list_). 132It should return the number of languages in the list. 133 134```php 135<?php 136 137$language_list = language_list("PHP", "Prolog", "Wren"); 138// => ["PHP", "Prolog", "Wren"] 139language_list_length($language_list); 140// => 3 141``` 142 143## Source 144 145### Created by 146 147- @neenjaw