new tasks

This commit is contained in:
Ingy döt Net 2013-04-09 00:46:50 -07:00
parent 2a4d27cea0
commit 80737d5a6a
1194 changed files with 15353 additions and 1 deletions

View file

@ -0,0 +1,3 @@
{{clarified-review}}Collections are abstractions to represent sets of values. In statically-typed languages, the values are typically of a common data type.
Create a collection, and add a few values to it.

View file

@ -0,0 +1,2 @@
---
note: Basic language learning

View file

@ -0,0 +1 @@
split("one two three",a)

View file

@ -0,0 +1 @@
print a[0]

View file

@ -0,0 +1 @@
for(i in a) print i":"a[i]

View file

@ -0,0 +1 @@
a[0]="hello"

View file

@ -0,0 +1,12 @@
int* ar; /* Collection<Integer> ar; */
int arSize;
arSize = (rand() % 6) + 1;
ar = calloc(arSize, sizeof(int) ); /* ar = new ArrayList<Integer>(arSize); */
ar[0] = 1; /* ar.set(0, 1); */
int* p; /* Iterator<Integer> p; Integer pValue; */
for (p=ar; /* p=ar.itereator(); for( pValue=p.next(); */
p<(ar+arSize); /* p.hasNext(); */
p++) { /* pValue=p.next() ) { */
printf("%d\n",*p); /* System.out.println(pValue); */
} /* } */

View file

@ -0,0 +1,11 @@
#define cSize( a ) ( sizeof(a)/sizeof(a[0]) ) /* a.size() */
int ar[10]; /* Collection<Integer> ar = new ArrayList<Integer>(10); */
ar[0] = 1; /* ar.set(0, 1); */
ar[1] = 2;
int* p; /* Iterator<Integer> p; Integer pValue; */
for (p=ar; /* for( p = ar.itereator(), pValue=p.next(); */
p<(ar+cSize(ar)); /* p.hasNext(); */
p++) { /* pValue=p.next() ) { */
printf("%d\n",*p); /* System.out.println(pValue); */
} /* } */

View file

@ -0,0 +1,3 @@
'(1 4 7) ; a linked list
(list 1 4 7)
(cons 1 (cons 4 '(7)))

View file

@ -0,0 +1,3 @@
['a 4 11] ; somewhere between array and list
(vector 'a 4 11)
(cons ['a 4] 11) ; vectors add at the *end*

View file

@ -0,0 +1,3 @@
#{:pig :dog :bear}
(assoc #{:pig :bear} :dog)
(set [:pig :bear :dog])

View file

@ -0,0 +1,4 @@
{1 "a", "Q" 10} ; commas are treated as whitespace
(hash-map 1 "a" "Q" 10) ; equivalent to the above
(let [my-map {1 "a"}]
(assoc my-map "Q" 10)) ; "adding" an element

View file

@ -0,0 +1,7 @@
include ffl/dcl.fs
dcl-create dl \ create a double linked list
3 dl dcl-append
1 dl dcl-prepend
2 1 dl dcl-insert \ dl[0] = 1 dl[1] = 2 dl[2] = 3

View file

@ -0,0 +1,7 @@
include ffl/hct.fs
10 hct-create ht \ create a hashtable with initial size 10
1 s" one" ht hct-insert \ ht["one"] = 1
2 s" two" ht hct-insert \ ht["two"] = 2
3 s" three" ht hct-insert \ ht["three"] = 3

View file

@ -0,0 +1,7 @@
include ffl/car.fs
10 car-create ar \ create a dynamic array with initial size 10
2 0 ar car-set \ ar[0] = 2
3 1 ar car-set \ ar[1] = 3
1 0 ar car-insert \ ar[0] = 1 ar[1] = 2 ar[2] = 3

View file

@ -0,0 +1,10 @@
package main
import "fmt"
func main() {
var a []interface{}
a = append(a, 3)
a = append(a, "apples", "oranges")
fmt.Println(a)
}

View file

@ -0,0 +1 @@
1 : [2, 3, 4]

View file

@ -0,0 +1 @@
[1, 2] ++ [3, 4]

View file

@ -0,0 +1 @@
concat [[1, 2], [3, 4], [5, 6, 7]]

View file

@ -0,0 +1 @@
[1, 2, 3, 4, 5]

View file

@ -0,0 +1,4 @@
//loop through myarrlist to sum each entry
for ( i = 0; i < myarrlist.size(); i++) {
sum += myarrlist.get(i);
}

View file

@ -0,0 +1,3 @@
for(int i : myarrlist) {
sum += i;
}

View file

@ -0,0 +1,5 @@
//remove the last entry in the ArrayList
myarrlist.remove(myarrlist.size()-1)
//clear the ArrayList
myarrlist.clear();

View file

@ -0,0 +1,14 @@
List arrayList = new ArrayList();
arrayList.add(new Integer(0));
// alternative with primitive autoboxed to an Integer object automatically
arrayList.add(0);
//other features of ArrayList
//define the type in the arraylist, you can substitute a proprietary class in the "<>"
List<Integer> myarrlist = new ArrayList<Integer>();
//add several values to the arraylist to be summed later
int sum;
for(int i = 0; i < 10; i++) {
myarrlist.add(i);
}

View file

@ -0,0 +1,5 @@
var map = {};
map['foo'] = 'xyz'; //equivalent to: map.foo = 'xyz';
map['bar'] = new MyClass; //equivalent to: map.bar = new MyClass;
map['1x; ~~:-b'] = 'text'; //no equivalent
alert( map['1x; ~~:-b'] );

View file

@ -0,0 +1,5 @@
var array = [];
array.push('abc');
array.push(123);
array.push(new MyClass);
alert( array[2] );

View file

@ -0,0 +1,8 @@
collection = {0, '1'}
print(collection[1]) -- prints 0
collection = {["foo"] = 0, ["bar"] = '1'} -- a collection of key/value pairs
print(collection["foo"]) -- prints 0
print(collection.foo) -- syntactic sugar, also prints 0
collection = {0, '1', ["foo"] = 0, ["bar"] = '1'}

View file

@ -0,0 +1,10 @@
<?php
$a = array();
# add elements "at the end"
array_push($a, 55, 10, 20);
print_r($a);
# using an explicit key
$a['one'] = 1;
$a['two'] = 2;
print_r($a);
?>

View file

@ -0,0 +1,18 @@
use strict;
my @c = (); # create an empty "array" collection
# fill it
push @c, 10, 11, 12;
push @c, 65;
# print it
print join(" ",@c) . "\n";
# create an empty hash
my %h = ();
# add some pair
$h{'one'} = 1;
$h{'two'} = 2;
# print it
foreach my $i ( keys %h ) {
print $i . " -> " . $h{$i} . "\n";
}

View file

@ -0,0 +1,14 @@
: (setq Lst (3 4 5 6))
-> (3 4 5 6)
: (push 'Lst 2)
-> 2
: (push 'Lst 1)
-> 1
: Lst
-> (1 2 3 4 5 6)
: (insert 4 Lst 'X)
-> (1 2 3 X 4 5 6)

View file

@ -0,0 +1,20 @@
collection = [0, '1'] # Lists are mutable (editable) and can be sorted in place
x = collection[0] # accessing an item (which happens to be a numeric 0 (zero)
collection.append(2) # adding something to the end of the list
collection.insert(0, '-1') # inserting a value into the beginning
y = collection[0] # now returns a string of "-1"
collection.extend([2,'3']) # same as [collection.append(i) for i in [2,'3']] ... but faster
collection += [2,'3'] # same as previous line
collection[2:6] # a "slice" (collection of the list elements from the third up to but not including the sixth)
len(collection) # get the length of (number of elements in) the collection
collection = (0, 1) # Tuples are immutable (not editable)
collection[:] # ... slices work on these too; and this is equivalent to collection[0:len(collection)]
collection[-4:-1] # negative slices count from the end of the string
collection[::2] # slices can also specify a stride --- this returns all even elements of the collection
collection="some string" # strings are treated as sequences of characters
x = collection[::-1] # slice with negative step returns reversed sequence (string in this case).
collection[::2] == "some string"[::2] # True, literal objects don't need to be bound to name/variable to access slices or object methods
collection.__getitem__(slice(0,len(collection),2)) # same as previous expressions.
collection = {0: "zero", 1: "one"} # Dictionaries (Hash)
collection['zero'] = 2 # Dictionary members accessed using same syntax as list/array indexes.
collection = set([0, '1']) # sets (Hash)

View file

@ -0,0 +1,2 @@
integer(5)
c(1L, -2L, 99L);

View file

@ -0,0 +1,2 @@
logical(5)
c(TRUE, FALSE)

View file

@ -0,0 +1,2 @@
character(5)
c("abc", "defg", "")

View file

@ -0,0 +1,3 @@
matrix(1:12, nrow=3)
array(1:24, dim=c(2,3,4)) #output not shown

View file

@ -0,0 +1 @@
list(a=123, b="abc", TRUE, 1:5, c=list(d=runif(5), e=5+6))

View file

@ -0,0 +1,13 @@
$a
[1] 123
$b
[1] "abc"
[[3]]
[1] TRUE
[[4]]
[1] 1 2 3 4 5
$c
$c$d
[1] 0.6013157 0.5011909 0.7106448 0.3882265 0.1274939
$c$e
[1] 11

View file

@ -0,0 +1 @@
data.frame(name=c("Alice", "Bob", "Carol"), age=c(23, 35, 17))

View file

@ -0,0 +1,3 @@
numeric(5)
1:10
c(1, 3, 6, 10, 7 + 8, sqrt(441))

View file

@ -0,0 +1 @@
pr.0=14 /*number of entries in the stemmed array.*/

View file

@ -0,0 +1,5 @@
do j=1 while pr.j\==0
say 'prime' j 'is' pr.j
end /*j*/
/*at this point, J=15. */
j=j-1 /*J now has the count of primes stored.*/

View file

@ -0,0 +1,10 @@
primeList='2 3 5 7 11 13 17 19 23 29 31 37 41 43' /* or ... */
primeList= 2 3 5 7 11 13 17 19 23 29 31 37 41 43
/*in this case, the quotes (') can be dropped. */
primes=words(primeList)
do j=1 for primes /*can also be coded: do j=1 to primes */
say 'prime' j 'is' word(primeList,j)
end /*j*/

View file

@ -0,0 +1,31 @@
pr.=0 /*define a default for all elements for the stemmed array.*/
pr.2 =1
pr.3 =1
pr.5 =1
pr.7 =1
pr.11=1
pr.13=1
pr.17=1
pr.19=1
pr.23=1
pr.29=1
pr.31=1
pr.37=1
pr.41=1
pr.43=1
primes=0
do j=1 for 10000 /*this method isn't very efficent*/
if pr.j==0 then iterate
primes=primes+1
end /*j*/
say '# of primes in list:' primes
Ps=0 /*another inefficient method. */
do k=1 for 10000 /*this method isn't very efficent*/
Ps = Ps + (pn.k\==0) /*more obtuse, if ya like that. */
say 'prime' Ps "is:" k /*might as well echo the prime #.*/
end /*k*/
say 'number of primes found in the list is' Ps

View file

@ -0,0 +1,36 @@
pr.=0 /*define a default for all elements for the stemmed array.*/
pr.1 =2 /*note that this array starts at 1 (one). */
pr.2 =3
pr.3 =5
pr.4 =7
pr.6 =11
pr.6 =13
pr.7 =17
pr.8 =19
pr.9 =23
pr.10=29
pr.11=31
pr.12=37
pr.13=41
pr.14=43
y.=0 /*define a default for all years. */
y.1985 = 6020
y.1986 = 7791
y.1987 = 8244
y.1988 = 10075
x = y.2000 /*X will have a value of zero (0). */
fib.0 = 0 /*arrays may start with zero (0). */
fib.1 = 1
fib.2 = 1
fib.3 = 2
fib.4 = 3
fib.5 = 5
fib.6 = 8
fib.7 =17
do n=-5 to 5 /*define an array from -5 ──► 5 */
sawtooth.n=n
end /*n*/ /*eleven elements will be defined. */

View file

@ -0,0 +1,9 @@
# creating an empty hash
h = {} # => {}
h["a"] = 1 # => {"a" => 1}
h["test"] = 2.4 # => {"a" => 1, "test" => 2.4}
h[3] = "Hello" # => {"a" => 1, "test" => 2.4, 3 => "Hello"}
# creating a hash with the constructor
h = Hash.new # => {}

View file

@ -0,0 +1,8 @@
# creating an empty array and adding values
a = [] # => []
a[0] = 1 # => [1]
a[3] = 2 # => [1, nil, nil, 2]
# creating an array with the constructor
a = Array.new # => []

View file

@ -0,0 +1,37 @@
scala> // Immutable collections do not change the original object
scala> // A List
scala> val list = Nil // Empty List
list: scala.collection.immutable.Nil.type = List()
scala> val list2 = List(1, 2) // List with two elements
list2: List[Int] = List(1, 2)
scala> val list3 = 3 :: list2 // prepend 3 to list2, using a special operator
list3: List[Int] = List(3, 1, 2)
scala> // A Set
scala> val set = Set.empty[Char] // Empty Set of Char type
set: scala.collection.immutable.Set[Char] = Set()
scala> val set1 = set + 'c' // add an element
set1: scala.collection.immutable.Set[Char] = Set(c)
scala> // A Map
scala> val map = Map(1 -> "A")
map: scala.collection.immutable.Map[Int,java.lang.String] = Map(1 -> A)
scala> val map1 = map + (2 -> "Map")
map1: scala.collection.immutable.Map[Int,java.lang.String] = Map(1 -> A, 2 -> Map)
scala> // Mutable collections can be modified
scala> val queue = new scala.collection.mutable.Queue[Int]
queue: scala.collection.mutable.Queue[Int] = Queue()
scala> queue += 4
res0: queue.type = Queue(4)
scala> queue += 5
res1: queue.type = Queue(4, 5)
scala> queue
res2: scala.collection.mutable.Queue[Int] = Queue(4, 5)
scala> val set = scala.collection.mutable.Set('a')
set: scala.collection.mutable.Set[Char] = Set(a)
scala> set += 'b'
res3: set.type = Set(b, a)
scala> val map = scala.collection.mutable.Map(1 -> "one")
map: scala.collection.mutable.Map[Int,java.lang.String] = Map(1 -> one)
scala> map += (2 -> "two")
res4: map.type = Map(2 -> two, 1 -> one)
scala> map
res5: scala.collection.mutable.Map[Int,java.lang.String] = Map(2 -> two, 1 -> one)

View file

@ -0,0 +1,4 @@
(display (list 1 2 3))
(newline)
(display (list))
(newline)

View file

@ -0,0 +1,2 @@
(1 2 3)
()

View file

@ -0,0 +1 @@
(cons obj lst)

View file

@ -0,0 +1,2 @@
(display (cons 0 (list 1 2 3)))
(newline)

View file

@ -0,0 +1 @@
(0 1 2 3)

View file

@ -0,0 +1 @@
(append lst ...)

View file

@ -0,0 +1,2 @@
(display (append (list 1 2 3) (list 4 5 6)))
(newline)

View file

@ -0,0 +1 @@
(1 2 3 4 5 6)

View file

@ -0,0 +1 @@
(list obj ...)

View file

@ -0,0 +1,32 @@
|anOrdered aBag aSet aSorted aSorted2 aDictionary|
anOrdered := OrderedCollection new.
anOrdered add: 1; add: 5; add: 3.
anOrdered printNl.
aBag := Bag new.
aBag add: 5; add: 5; add: 5; add: 6.
aBag printNl.
aSet := Set new.
aSet add: 10; add: 5; add: 5; add: 6; add: 10.
aSet printNl.
aSorted := SortedCollection new.
aSorted add: 10; add: 9; add: 8; add: 5.
aSorted printNl.
"another sorted with custom comparator: let's sort
the other collections according to their size (number of
elements)"
aSorted2 := SortedCollection sortBlock: [ :a :b |
(a size) < (b size) ].
aSorted2 add: anOrdered; add: aBag; add: aSet; add: aSorted.
aSorted2 printNl.
aDictionary := Dictionary new.
aDictionary at: 'OrderedCollection' put: anOrdered;
at: 'Bag' put: aBag;
at: 'Set' put: aSet;
at: 'SortedCollection' put: { aSorted. aSorted2 }.
aDictionary printNl.

View file

@ -0,0 +1,24 @@
# create an empty array
array set h {}
# add some pair
set h(one) 1
set h(two) 2
# add more data
array set h {three 3 four 4 more {5 6 7 8}}
# iterate over it in a couple of ways
foreach key [array names h] {puts "$key -> $h($key)"}
foreach {key value} [array get h] {puts "$key -> $value"}
# pass by name
proc numkeys_byname {arrayName} {
upvar 1 $arrayName arr
puts "array $arrayName has [llength [array names arr]] keys"
}
numkeys_byname h
# pass serialized
proc numkeys_bycopy {l} {
array set arr $l
puts "array has [llength [array names arr]] keys"
}
numkeys_bycopy [array get h]

View file

@ -0,0 +1,9 @@
# create an empty dictionary
set d [dict create]
dict set d one 1
dict set d two 2
# create another
set e [dict create three 3 four 4]
set f [dict merge $d $e]
dict set f nested [dict create five 5 more [list 6 7 8]]
puts [dict get $f nested more] ;# ==> 6 7 8

View file

@ -0,0 +1,12 @@
set c [list] ;# create an empty list
# fill it
lappend c 10 11 13
set c [linsert $c 2 "twelve goes here"]
# iterate over it
foreach elem $c {puts $elem}
# pass to a proc
proc show_size {l} {
puts [llength $l]
}
show_size $c