-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added the Array.sort_by{} method, which sorts an array by a specifi…
…c value. Example: [4,3,1,2].sort_by { _ }; # same as .sort(); [4,3,1,2].sort_by {|n| -n }; # reverse sorting %w(foo fo f).sort_by { .len } # sort array by length - Added two new Sidef scripts.
- Loading branch information
trizen
committed
Oct 25, 2015
1 parent
7bdfafe
commit 0687ff3
Showing
5 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/usr/bin/ruby | ||
|
||
# | ||
## http://rosettacode.org/wiki/Left_factorials#Sidef | ||
# | ||
|
||
func left_fact(k) { | ||
range(0, k-1).map { _! }.sum \\ 0; | ||
} | ||
|
||
[range(0, 10), range(20, 50).by(10)].each { |r| | ||
r.each { |i| | ||
printf("!%d = %s\n", i, left_fact(i)); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#!/usr/bin/ruby | ||
|
||
# | ||
## http://rosettacode.org/wiki/Left_factorials#Sidef | ||
# | ||
|
||
func left_fact(n) { | ||
static cached = 0; | ||
static factorial = 1; | ||
static leftfact = 0; | ||
|
||
if (n < cached) { | ||
cached = 0; | ||
factorial = 1; | ||
leftfact = 0; | ||
} | ||
|
||
while (n > cached) { | ||
leftfact += factorial; | ||
factorial *= ++cached; | ||
} | ||
|
||
leftfact; | ||
} | ||
|
||
[range(0, 10), range(20, 50).by(10)].each { |r| | ||
r.each { |i| | ||
printf("!%d = %s\n", i, left_fact(i)); | ||
} | ||
} |