- The snippets are under the CC-BY-SA license.
- Please consider keeping a bookmark
- (instead of printing)
Ada | |||
---|---|---|---|
1 |
Print a literal string on standard output
|
||
2 |
Loop to execute some code a constant number of times
|
|
|
3 |
Like a function which doesn't return any value, thus has only side effects (e.g. Print to standard output)
|
||
4 |
Create a function which returns the square of an integer
|
||
5 |
Declare a container type for two floating-point numbers x and y
|
||
6 |
Do something with each item x of the list (or array) items, regardless indexes.
|
||
7 |
Print each index i with its value x from an array-like collection items
|
||
8 |
Create a new map object x, and provide some (key, value) pairs as initial content.
|
||
9 |
The structure must be recursive because left child and right child are binary trees too. A node has access to children nodes, but not to its parent.
|
||
11 |
The list x must be non-empty.
|
|
|
12 |
Check if the list contains the value x.
list is an iterable finite container. |
||
13 |
Access each key k with its value x from an associative array mymap, and print them.
|
||
15 |
Pick a random integer greater than or equals to a, inferior or equals to b. Precondition : a < b.
|
||
19 |
Reverse the order of the elements of the list x.
This may reverse "in-place" and destroy the original ordering. |
||
20 |
Implement a function search which looks for item x in a 2D matrix m.
Return indices i, j of the matching cell. Think of the most idiomatic way in the language to return the two values at the same time. |
|
|
21 |
Swap the values of the variables a and b
|
||
22 |
Extract the integer value i from its string representation s (in radix 10)
|
||
25 |
Share the string value "Alan" with an existing running process which will then display "Hello, Alan"
|
||
26 |
Declare and initialize a matrix x having m rows and n columns, containing real numbers.
|
||
27 |
Declare and initialize a 3D array x, having dimensions boundaries m, n, p, and containing real numbers.
|
||
28 |
Sort the elements of the list (or array-like collection) items in ascending order of x.p, where p is a field of the type Item of the objects in items.
|
||
29 |
Remove i-th item from list items.
This will alter the original list or return a new list, depending on which is more idiomatic. Note that in most languages, the smallest valid value for i is 0. |
||
31 |
Create the recursive function f which returns the factorial of the non-negative integer i, calculated from f(i-1)
|
||
38 |
Find substring t consisting in characters i (included) to j (excluded) of string s.
Character indices start at 0 unless specified otherwise. Make sure that multibyte characters are properly handled. |
||
39 |
Set the boolean ok to true if the string word is contained in string s as a substring, or to false otherwise.
|
||
43 |
Look for a negative value v in 2D integer matrix m. Print it and stop searching.
|
|
|
45 |
Sleep for 5 seconds in current thread, before proceeding with the next instructions.
|
||
46 |
Create the string t consisting of the 5 first characters of the string s.
Make sure that multibyte characters are properly handled. |
||
48 |
Assign to variable s a string literal consisting in several lines of text, including newlines.
|
||
50 |
Write a loop that has no end clause.
|
||
51 |
Determine whether the map m contains an entry for the key k
|
||
53 |
Concatenate elements of string list x joined by the separator ", " to create a single string y.
|
||
54 |
Calculate the sum s of the integer list or array x.
|
||
55 |
Create the string representation s (in radix 10) of the integer value i.
|
||
57 |
Create the list y containing the items from the list x that satisfy the predicate p. Respect the original ordering. Don't modify x in-place.
|
||
59 |
Print the message "x is negative" to standard error (stderr), with integer x value substitution (e.g. "-2 is negative").
|
||
61 |
Assign to the variable d the current date/time value, in the most standard type.
|
|
|
69 |
Use seed s to initialize a random generator.
If s is constant, the generator output will be the same each time the program runs. If s is based on the current value of the system clock, the generator output will be different each time. |
||
71 |
Basic implementation of the Echo program: Print all arguments except the program name, separated by space, followed by newline.
The idiom demonstrates how to skip the first argument if necessary, concatenate arguments as strings, append newline and print it to stdout. |
||
77 |
Declare a complex x and initialize it with value (3i - 2). Then multiply it by i.
|
||
78 |
Execute a block once, then execute it again as long as boolean condition c is true.
|
||
79 |
Declare the floating point number y and initialize it with the value of the integer x .
|
||
80 |
Declare integer y and initialize it with the value of floating point number x . Ignore non-integer digits of x .
Make sure to truncate towards zero: a negative x must yield the closest greater integer (not lesser). |
|
|
81 |
Declare the integer y and initialize it with the rounded value of the floating point number x .
Ties (when the fractional part of x is exactly .5) must be rounded up (to positive infinity). |
|
|
85 |
Write boolean function addingWillOverflow which takes two integers x, y and return true if (x+y) overflows.
An overflow may be above the max positive value, or below the min negative value. |
||
88 |
Create a new bytes buffer buf of size 1,000,000.
|
||
90 |
Expose a read-only integer x to the outside world while being writable inside a structure or a class Foo.
|
||
93 |
Implement the procedure control which receives one parameter f, and runs f.
|
||
95 |
Assign to variable x the length (number of bytes) of the local file at path.
|
|
|
96 |
Set the boolean b to true if string s starts with prefix prefix, false otherwise.
|
||
99 |
Assign to the string x the value of the fields (year, month, day) of the date d, in format YYYY-MM-DD.
|
|
|
100 |
Sort elements of array-like collection items, using a comparator c.
|
|
|
105 |
1
|
||
106 |
Assign to string dir the path of the working directory.
(This is not necessarily the folder containing the executable itself) |
||
109 |
Set n to the number of bytes of a variable t (of type T).
|
||
110 |
Set the boolean blank to true if the string s is empty, or null, or contains only whitespace ; false otherwise.
|
||
117 |
Set n to the number of elements of the list x.
|
||
122 |
Create an enumerated type Suit with 4 possible values SPADES, HEARTS, DIAMONDS, CLUBS.
|
||
131 |
Execute f1 if condition c1 is true, or else f2 if condition c2 is true, or else f3 if condition c3 is true.
Don't evaluate a condition when a previous condition was true. |
||
137 |
Set the boolean b to true if the string s contains only characters in the range '0'..'9', false otherwise.
|
||
144 |
Set boolean b to true if file at path fp exists on filesystem; false otherwise.
Beware that you should not do this and then in the next instruction assume the result is still valid, this is a race condition on any multitasking OS. |
|
|
147 |
Create string t from string s, keeping only ASCII characters
|
|
|
152 |
Create string s containing only the character c.
|
||
153 |
Create the string t as the concatenation of the string s and the integer i.
|
||
155 |
Delete from filesystem the file having path filepath.
|
|
|
157 |
Initialize a constant planet with string value "Earth".
|
||
161 |
Multiply all the elements of the list elements by a constant c
|
||
165 |
Assign to the variable x the last element of the list items.
|
||
169 |
Assign to the integer n the number of characters of the string s.
Make sure that multibyte characters are properly handled. n can be different from the number of bytes of s. |
|
|
190 |
Declare an external C function with the prototype
void foo(double *a, int n); and call it, passing an array (or a list) of size 10 to a and 10 to n. Use only standard features of your language. |
|
|
205 |
Read an environment variable with the name "FOO" and assign it to the string variable foo. If it does not exist or if the system does not support environment variables, assign a value of "none".
|
|
|
211 |
Create the folder at path on the filesystem
|
|
|
212 |
Set the boolean b to true if path exists on the filesystem and is a directory; false otherwise.
|
|
|
226 |
Remove the last element from the list items.
|
|
|
228 |
Copy the file at path src to dst.
|
|
|
252 |
Assign to the variable x the string value "a" if calling the function condition returns true, or the value "b" otherwise.
|
||
256 |
Print the numbers 5, 4, ..., 0 (included), one line per number.
|
||
261 |
Assign to the string x the value of fields (hours, minutes, seconds) of the date d, in format HH:MM:SS.
|
|
|
266 |
Assign to the string s the value of the string v repeated n times, and write it out.
E.g. v="abc", n=5 ⇒ s="abcabcabcabcabc" |
||
269 |
Given the enumerated type t with 3 possible values: bike, car, horse.
Set the enum value e to one of the allowed values of t. Set the string s to hold the string representation of e (so, not the ordinal value). Print s. |
||
278 |
Read one line into the string line.
Explain what happens if EOF is reached. |
|
|
279 |
Read all the lines (until EOF) into the list of strings lines.
|
||
289 |
Create the string s by concatenating the strings a and b.
|
||
294 |
Given an array a containing the three values 1, 12, 42, print out
"1, 12, 42" with a comma and a space after each integer except the last one. |
||
295 |
Given the enumerated type T, create a function TryStrToEnum that takes a string s as input and converts it into an enum value of type T.
Explain whether the conversion is case sensitive or not. Explain what happens if the conversion fails. |
|
|
299 |
Write a line of comments.
This line will not be compiled or executed. |
|
|
303 |
Declare an array a of integers with six elements, where the first index is 42 and consecutive elements have the indices 43, 44, 45, 46, 47.
|
||
306 |
Preallocate memory in the list x for a minimum total capacity of 200 elements.
This is not possible in all languages. It is only meant as a performance optimization, should not change the length of x, and should not have any effect on correctness. |
|
|
336 |
Compute x = b ⁿ
b raised to the power of n is equal to the product of n terms b × b × ... × b |