tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
1
Task/Pick-random-element/0DESCRIPTION
Normal file
1
Task/Pick-random-element/0DESCRIPTION
Normal file
|
|
@ -0,0 +1 @@
|
|||
Demonstrate how to pick a random element from a list.
|
||||
2
Task/Pick-random-element/1META.yaml
Normal file
2
Task/Pick-random-element/1META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
note: Basic language learning
|
||||
6
Task/Pick-random-element/ACL2/pick-random-element.acl2
Normal file
6
Task/Pick-random-element/ACL2/pick-random-element.acl2
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
:set-state-ok t
|
||||
|
||||
(defun pick-random-element (xs state)
|
||||
(mv-let (idx state)
|
||||
(random$ (len xs) state)
|
||||
(mv (nth idx xs) state)))
|
||||
30
Task/Pick-random-element/Ada/pick-random-element.ada
Normal file
30
Task/Pick-random-element/Ada/pick-random-element.ada
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
with Ada.Text_IO, Ada.Numerics.Float_Random;
|
||||
|
||||
procedure Pick_Random_Element is
|
||||
|
||||
package Rnd renames Ada.Numerics.Float_Random;
|
||||
Gen: Rnd.Generator; -- used globally
|
||||
|
||||
type Char_Arr is array (Natural range <>) of Character;
|
||||
|
||||
function Pick_Random(A: Char_Arr) return Character is
|
||||
-- Chooses one of the characters of A (uniformly distributed)
|
||||
begin
|
||||
return A(A'First + Natural(Rnd.Random(Gen) * Float(A'Last)));
|
||||
end Pick_Random;
|
||||
|
||||
Vowels : Char_Arr := ('a', 'e', 'i', 'o', 'u');
|
||||
Consonants: Char_Arr := ('t', 'n', 's', 'h', 'r', 'd', 'l');
|
||||
Specials : Char_Arr := (',', '.', '?', '!');
|
||||
|
||||
begin
|
||||
Rnd.Reset(Gen);
|
||||
for J in 1 .. 3 loop
|
||||
for I in 1 .. 10 loop
|
||||
Ada.Text_IO.Put(Pick_Random(Consonants));
|
||||
Ada.Text_IO.Put(Pick_Random(Vowels));
|
||||
end loop;
|
||||
Ada.Text_IO.Put(Pick_Random(Specials) & " ");
|
||||
end loop;
|
||||
Ada.Text_IO.New_Line;
|
||||
end Pick_Random_Element;
|
||||
11
Task/Pick-random-element/Aime/pick-random-element.aime
Normal file
11
Task/Pick-random-element/Aime/pick-random-element.aime
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
list l;
|
||||
|
||||
l_append(l, 'a');
|
||||
l_append(l, 'b');
|
||||
l_append(l, 'c');
|
||||
l_append(l, 'd');
|
||||
l_append(l, 'e');
|
||||
l_append(l, 'f');
|
||||
|
||||
o_byte(l_query(l, drand(5)));
|
||||
o_byte('\n');
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
list := ["abc", "def", "gh", "ijklmnop", "hello", "world"]
|
||||
Random, randint, 1, % list.MaxIndex()
|
||||
MsgBox % List[randint]
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
list := "abc,def,gh,ijklmnop,hello,world"
|
||||
StringSplit list, list, `,
|
||||
Random, randint, 1, %list0%
|
||||
MsgBox % List%randint%
|
||||
13
Task/Pick-random-element/BASIC/pick-random-element.basic
Normal file
13
Task/Pick-random-element/BASIC/pick-random-element.basic
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
'setup
|
||||
DIM foo(10) AS LONG
|
||||
DIM n AS LONG, x AS LONG
|
||||
FOR n = LBOUND(foo) TO UBOUND(foo)
|
||||
foo(n) = INT(RND*99999)
|
||||
NEXT
|
||||
RANDOMIZE TIMER
|
||||
|
||||
'random selection
|
||||
x = INT(RND * ((UBOUND(foo) - LBOUND(foo)) + 1))
|
||||
|
||||
'output
|
||||
PRINT x, foo(x)
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
DIM list$(5)
|
||||
list$() = "The", "five", "boxing", "wizards", "jump", "quickly"
|
||||
chosen% = RND(6)
|
||||
PRINT "Item " ; chosen% " was chosen which is '" list$(chosen%-1) "'"
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
blsq ) "ABCDEFG"123456 0 6rn-]!!
|
||||
'G
|
||||
13
Task/Pick-random-element/C/pick-random-element.c
Normal file
13
Task/Pick-random-element/C/pick-random-element.c
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
int main(){
|
||||
char array[] = { 'a', 'b', 'c' };
|
||||
|
||||
srand( time( 0 ) );
|
||||
|
||||
printf( "%c\n", array[ rand() % 3 ] );
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
(rand-nth coll)
|
||||
|
|
@ -0,0 +1 @@
|
|||
(nth coll (rand-int (count coll)))
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
array = [1,2,3]
|
||||
console.log array[Math.floor(Math.random() * array.length)]
|
||||
7
Task/Pick-random-element/D/pick-random-element.d
Normal file
7
Task/Pick-random-element/D/pick-random-element.d
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
import std.stdio, std.random;
|
||||
|
||||
void main() {
|
||||
auto items = ["foo", "bar", "baz"];
|
||||
auto r = items[uniform(0, $)];
|
||||
writeln(r);
|
||||
}
|
||||
1
Task/Pick-random-element/Deja-Vu/pick-random-element.djv
Normal file
1
Task/Pick-random-element/Deja-Vu/pick-random-element.djv
Normal file
|
|
@ -0,0 +1 @@
|
|||
print choose [ "one" "two" "chicken" ]
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
constant s = {'a', 'b', 'c'}
|
||||
puts(1,s[rand($)])
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
( scratchpad ) { "a" "b" "c" "d" "e" "f" } random .
|
||||
"a"
|
||||
14
Task/Pick-random-element/Go/pick-random-element.go
Normal file
14
Task/Pick-random-element/Go/pick-random-element.go
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
var list = []string{"bleen", "fuligin", "garrow", "grue", "hooloovoo"}
|
||||
|
||||
func main() {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
fmt.Println(list[rand.Intn(len(list))])
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
def list = [25, 30, 1, 450, 3, 78]
|
||||
def random = new Random();
|
||||
|
||||
(0..3).each {
|
||||
def i = random.nextInt(list.size())
|
||||
println "list[${i}] == ${list[i]}"
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
import Random (randomRIO)
|
||||
|
||||
pick :: [a] -> IO a
|
||||
pick xs = randomRIO (0, length xs - 1) >>= return . (xs !!)
|
||||
|
||||
x <- pick [1 2 3]
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
import Data.Random
|
||||
import Data.Random.Source.DevRandom
|
||||
import Data.Random.Extras
|
||||
|
||||
x <- runRVar (choice [1 2 3]) DevRandom
|
||||
4
Task/Pick-random-element/Icon/pick-random-element.icon
Normal file
4
Task/Pick-random-element/Icon/pick-random-element.icon
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
procedure main()
|
||||
L := [1,2,3] # a list
|
||||
x := ?L # random element
|
||||
end
|
||||
2
Task/Pick-random-element/J/pick-random-element.j
Normal file
2
Task/Pick-random-element/J/pick-random-element.j
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
({~ ?@#) 'abcdef'
|
||||
b
|
||||
4
Task/Pick-random-element/Java/pick-random-element.java
Normal file
4
Task/Pick-random-element/Java/pick-random-element.java
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
import java.util.Random;
|
||||
...
|
||||
int[] array = {1,2,3};
|
||||
return array[new Random().nextInt(array.length)]; // if done multiple times, the Random object should be re-used
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
var array = [1,2,3];
|
||||
return array[Math.floor(Math.random() * array.length)];
|
||||
2
Task/Pick-random-element/K/pick-random-element.k
Normal file
2
Task/Pick-random-element/K/pick-random-element.k
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
1?"abcdefg"
|
||||
,"e"
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
list$ ="John Paul George Ringo Peter Paul Mary Obama Putin"
|
||||
wantedTerm =int( 10 *rnd( 1))
|
||||
print "Selecting term "; wantedTerm; " in the list, which was "; word$( list$, wantedTerm, " ")
|
||||
1
Task/Pick-random-element/Logo/pick-random-element.logo
Normal file
1
Task/Pick-random-element/Logo/pick-random-element.logo
Normal file
|
|
@ -0,0 +1 @@
|
|||
pick [1 2 3]
|
||||
3
Task/Pick-random-element/Lua/pick-random-element.lua
Normal file
3
Task/Pick-random-element/Lua/pick-random-element.lua
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
math.randomseed(os.time())
|
||||
local a = {1,2,3}
|
||||
print(a[math.random(1,#a)])
|
||||
2
Task/Pick-random-element/MATLAB/pick-random-element-1.m
Normal file
2
Task/Pick-random-element/MATLAB/pick-random-element-1.m
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
list = {'a','b','c'};
|
||||
list{ceil(rand(1)*length(list))}
|
||||
2
Task/Pick-random-element/MATLAB/pick-random-element-2.m
Normal file
2
Task/Pick-random-element/MATLAB/pick-random-element-2.m
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
list = 1:1000;
|
||||
list(ceil(rand(1)*length(list)))
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
RandomChoice[{a, b, c}]
|
||||
->c
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
;; from list containnig only literals and literal constants
|
||||
user-message one-of [ 1 3 "rooster" blue ]
|
||||
;; from list containing variables and reporters
|
||||
user-message one-of (list (red + 2) turtles (patch 0 0) )
|
||||
13
Task/Pick-random-element/NetRexx/pick-random-element.netrexx
Normal file
13
Task/Pick-random-element/NetRexx/pick-random-element.netrexx
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
/* NetRexx */
|
||||
options replace format comments java crossref savelog symbols nobinary
|
||||
|
||||
iArray = [ 1, 2, 3, 4, 5 ] -- a traditional array
|
||||
iList = Arrays.asList(iArray) -- a Java Collection "List" object
|
||||
iWords = '1 2 3 4 5' -- a list as a string of space delimited words
|
||||
|
||||
|
||||
v1 = iArray[Random().nextInt(iArray.length)]
|
||||
v2 = iList.get(Random().nextInt(iList.size()))
|
||||
v3 = iWords.word(Random().nextInt(iWords.words()) + 1) -- the index for word() starts at one
|
||||
|
||||
say v1 v2 v3
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
let list_rand lst =
|
||||
let len = List.length lst in
|
||||
List.nth lst (Random.int len)
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
let array_rand ary =
|
||||
let len = Array.length ary in
|
||||
ary.(Random.int len)
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
values := [1, 2, 3];
|
||||
value := values[(Float->Random() * 100.0)->As(Int) % values->Size()];
|
||||
|
|
@ -0,0 +1 @@
|
|||
pick(v)=v[random(#v)+1]
|
||||
2
Task/Pick-random-element/PHP/pick-random-element.php
Normal file
2
Task/Pick-random-element/PHP/pick-random-element.php
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
$arr = array('foo', 'bar', 'baz');
|
||||
$x = $arr[array_rand($arr)];
|
||||
3
Task/Pick-random-element/PL-I/pick-random-element.pli
Normal file
3
Task/Pick-random-element/PL-I/pick-random-element.pli
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
declare t(0:9) character (1) static initial
|
||||
('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j');
|
||||
put ( t(10*random()) );
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
Program PickRandomElement (output);
|
||||
|
||||
const
|
||||
s: array [1..5] of string = ('1234', 'ABCDE', 'Charlie', 'XB56ds', 'lala');
|
||||
|
||||
begin
|
||||
randomize;
|
||||
writeln(s[low(s) + random(length(s))]);
|
||||
end.
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
(1..6).roll; # return 1 random value in the range 1 through 6
|
||||
(1..6).roll(3); # return a list of 3 random values in the range 1 through 6
|
||||
(1..6).roll(*); # return a lazy infinite list of random values in the range 1 through 6
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
# define the deck
|
||||
constant deck = 2..9, <J Q K A> X~ <♠ ♣ ♥ ♦>;
|
||||
deck.pick; # Pick a card
|
||||
deck.pick(5); # Draw 5
|
||||
deck.pick(*); # Get a shuffled deck
|
||||
|
|
@ -0,0 +1 @@
|
|||
@array[@array * rand]
|
||||
|
|
@ -0,0 +1 @@
|
|||
say Bool.pick; # returns either True or False
|
||||
2
Task/Pick-random-element/Perl/pick-random-element.pl
Normal file
2
Task/Pick-random-element/Perl/pick-random-element.pl
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
my @array = qw(a b c);
|
||||
print $array[ rand @array ];
|
||||
1
Task/Pick-random-element/PicoLisp/pick-random-element.l
Normal file
1
Task/Pick-random-element/PicoLisp/pick-random-element.l
Normal file
|
|
@ -0,0 +1 @@
|
|||
(get Lst (rand 1 (length Lst)))
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
Procedure.s pickRandomElement(List source.s())
|
||||
Protected x = ListSize(source())
|
||||
|
||||
If x > 0
|
||||
SelectElement(source(), Random(x - 1)) ;element numbering is zero - based
|
||||
ProcedureReturn source()
|
||||
EndIf
|
||||
EndProcedure
|
||||
|
||||
;initialize list elements
|
||||
DataSection
|
||||
elements:
|
||||
Data.s "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"
|
||||
EndDataSection
|
||||
|
||||
#elementCount = 10
|
||||
NewList item.s()
|
||||
|
||||
Restore elements
|
||||
Define i
|
||||
For i = 1 To #elementCount
|
||||
AddElement(item())
|
||||
Read.s item()
|
||||
Next
|
||||
|
||||
If OpenConsole()
|
||||
Print("Source list: ")
|
||||
ForEach item()
|
||||
Print(item() + " ")
|
||||
Next
|
||||
PrintN(#CRLF$)
|
||||
|
||||
Print("Random picks from list: ")
|
||||
For i = 1 To 10
|
||||
Print(pickRandomElement(item()) + " ")
|
||||
Next
|
||||
|
||||
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
|
||||
CloseConsole()
|
||||
EndIf
|
||||
3
Task/Pick-random-element/Python/pick-random-element.py
Normal file
3
Task/Pick-random-element/Python/pick-random-element.py
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
>>> import random
|
||||
>>> random.choice(['foo', 'bar', 'baz'])
|
||||
'baz'
|
||||
12
Task/Pick-random-element/R/pick-random-element.r
Normal file
12
Task/Pick-random-element/R/pick-random-element.r
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
# a vector (letters are builtin)
|
||||
letters
|
||||
# [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
|
||||
# [20] "t" "u" "v" "w" "x" "y" "z"
|
||||
|
||||
# picking one element
|
||||
sample(letters, 1)
|
||||
# [1] "n"
|
||||
|
||||
# picking some elements with repetition, and concatenating to get a word
|
||||
paste(sample(letters, 10, rep=T), collapse="")
|
||||
# [1] "episxgcgmt"
|
||||
21
Task/Pick-random-element/REXX/pick-random-element-1.rexx
Normal file
21
Task/Pick-random-element/REXX/pick-random-element-1.rexx
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
/*REXX program to pick a random element from a list (tongue in cheek). */
|
||||
|
||||
_= 'hydrogen helium lithium beryllium boron carbon nitrogen oxygen'
|
||||
_=_ 'fluorine neon sodium magnesium aluminum silicon phosphorous sulfur'
|
||||
_=_ 'chlorine argon potassium calcium scandium titanium vanadium chromium'
|
||||
_=_ 'manganese iron cobalt nickel copper zinc gallium germanium arsenic'
|
||||
_=_ 'selenium bromine krypton rubidium strontium yttrium zirconium'
|
||||
_=_ 'niobium molybdenum technetium ruthenium rhodium palladium silver'
|
||||
_=_ 'cadmium indium tin antimony tellurium iodine xenon cesium barium'
|
||||
_=_ 'lanthanum cerium praseodymium neodymium promethium samarium europium'
|
||||
_=_ 'gadolinium terbium dysprosium holmium erbium thulium ytterbium'
|
||||
_=_ 'lutetium hafnium tantalum tungsten rhenium osmium irdium platinum'
|
||||
_=_ 'gold mercury thallium lead bismuth polonium astatine radon francium'
|
||||
_=_ 'radium actinium thorium protactinium uranium neptunium plutonium'
|
||||
_=_ 'americium curium berkelium californium einsteinum fermium mendelevium'
|
||||
_=_ 'nobelium lawrencium rutherfordium dubnium seaborgium bohrium hassium'
|
||||
_=_ 'meitnerium darmstadtium roentgenium copernicium Ununtrium'
|
||||
|
||||
w=words(_)
|
||||
say 'random element =' word(_,random(1,w))
|
||||
/*stick a fork in it, we're done.*/
|
||||
9
Task/Pick-random-element/REXX/pick-random-element-2.rexx
Normal file
9
Task/Pick-random-element/REXX/pick-random-element-2.rexx
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
/* REXX ***************************************************************
|
||||
* 18.10.2012 Walter Pachl Not only the list of elements shortened:-)
|
||||
**********************************************************************/
|
||||
wl='hydrogen helium lithium beryllium boron carbon nitrogen oxygen',
|
||||
'fluorine neon sodium magnesium aluminum silicon phosphorous sulfur',
|
||||
'...',
|
||||
'meitnerium darmstadtium roentgenium copernicium Ununtrium'
|
||||
|
||||
Say word(wl,random(1,words(wl)))
|
||||
4
Task/Pick-random-element/Ruby/pick-random-element-1.rb
Normal file
4
Task/Pick-random-element/Ruby/pick-random-element-1.rb
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
irb(main):001:0> %w(north east south west).sample
|
||||
=> "west"
|
||||
irb(main):002:0> (1..100).to_a.sample(2)
|
||||
=> [17, 79]
|
||||
2
Task/Pick-random-element/Ruby/pick-random-element-2.rb
Normal file
2
Task/Pick-random-element/Ruby/pick-random-element-2.rb
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
irb(main):001:0> %w(north east south west).choice
|
||||
=> "south"
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
list$ = "a,b,c,d,e,f,g,h,i,j"
|
||||
letter = rnd(1) * 10
|
||||
print "Selected letter:"; word$(list$,letter,",")
|
||||
6
Task/Pick-random-element/Seed7/pick-random-element.seed7
Normal file
6
Task/Pick-random-element/Seed7/pick-random-element.seed7
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
$ include "seed7_05.s7i";
|
||||
|
||||
const proc: main is func
|
||||
begin
|
||||
writeln(rand([] ("foo", "bar", "baz")));
|
||||
end func;
|
||||
|
|
@ -0,0 +1 @@
|
|||
x := #(1 2 3) atRandom.
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
$$ MODE TUSCRIPT
|
||||
list="John'Paul'George'Ringo'Peter'Paul'Mary'Obama'Putin"
|
||||
sizeList=SIZE(list)
|
||||
selectedNr=RANDOM_NUMBERS (1,sizeList,1)
|
||||
selectedItem=SELECT(list,#selectednr)
|
||||
PRINT "Selecting term ",selectedNr," in the list, which was ",selectedItem
|
||||
3
Task/Pick-random-element/TXR/pick-random-element.txr
Normal file
3
Task/Pick-random-element/TXR/pick-random-element.txr
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
@(do (defun randelem (vec)
|
||||
(vecref vec (random nil (length vec)))))
|
||||
@(bind x @(randelem #("a" "b" "c" "d")))
|
||||
4
Task/Pick-random-element/Tcl/pick-random-element.tcl
Normal file
4
Task/Pick-random-element/Tcl/pick-random-element.tcl
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
proc randelem {list} {
|
||||
lindex $list [expr {int(rand()*[llength $list])}]
|
||||
}
|
||||
set x [randelem {1 2 3 4 5}]
|
||||
5
Task/Pick-random-element/XPL0/pick-random-element.xpl0
Normal file
5
Task/Pick-random-element/XPL0/pick-random-element.xpl0
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
code Ran=1, Text=12;
|
||||
int List;
|
||||
[List:= ["hydrogen", "helium", "lithium", "beryllium", "boron"]; \(Thanks REXX)
|
||||
Text(0, List(Ran(5)));
|
||||
]
|
||||
Loading…
Add table
Add a link
Reference in a new issue