-
-
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.
- Extended the String.first() and String.last() methods to accept a n…
…umber as an argument, returning the first or last n characters. Example: say "sidef".first(3); # "sid" say "sidef".last(3); # "def" - Minor bug-fix inside the Sidef deparser regarding the deparsing of the `while` postifx keyword. new file: scripts/Expensive/calendar.sf new file: scripts/RosettaCode/almost_prime.sf new file: scripts/RosettaCode/priority_queue.sf
- Loading branch information
trizen
committed
Dec 15, 2015
1 parent
a07896d
commit 801ff02
Showing
6 changed files
with
130 additions
and
5 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,58 @@ | ||
#!/usr/bin/ruby | ||
|
||
# | ||
## http://rosettacode.org/wiki/Calendar#Sidef | ||
# | ||
|
||
require('DateTime'); | ||
|
||
define months_per_col = 3 | ||
define week_day_names = <Mo Tu We Th Fr Sa Su> | ||
define month_names = <Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec> | ||
|
||
func fmt_month (year, month) { | ||
var str = sprintf("%-20s\n", month_names[month-1]) | ||
str += week_day_names.join(' ')+"\n" | ||
|
||
var dt = %s'DateTime' | ||
var date = dt.new(year => year, month => month) | ||
var week_day = date.day_of_week | ||
str += ([" "] * week_day-1 -> join(" ")) | ||
|
||
var last_day = dt.last_day_of_month(year => year, month => month).day | ||
date.day ... last_day -> each { |day| | ||
date = dt.new(year => year, month => month, day => day) | ||
|
||
str += " " if (week_day ~~ 2...7) | ||
if (week_day == 8) { | ||
str += "\n" | ||
week_day = 1 | ||
} | ||
str += sprintf("%2d", day) | ||
++week_day | ||
} | ||
str += " " if (week_day < 8) | ||
str += ([" "] * 8-week_day -> join(" ")) | ||
str += "\n" | ||
} | ||
|
||
func fmt_year (year) { | ||
var month_strs = 12.of { |i| fmt_month(year, i).lines } | ||
|
||
var str = (' '*30 + year + "\n") | ||
for (var month = 0; month < 12; month += months_per_col) { | ||
while (month_strs[month]) { | ||
months_per_col.times { |i| | ||
month_strs[month + i - 1] || next; | ||
str += month_strs[month + i - 1].shift; | ||
str += ' '*3 | ||
} | ||
str += "\n" | ||
} | ||
str += "\n" | ||
} | ||
|
||
return str | ||
} | ||
|
||
print fmt_year(ARGV.is_empty ? 1969 : ARGV[0].to_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,23 @@ | ||
#!/usr/bin/ruby | ||
|
||
# | ||
## http://rosettacode.org/wiki/Almost_prime#Sidef | ||
# | ||
|
||
func is_k_almost_prime(n, k) { | ||
for (var (p, f) = (2, 0); (f < k) && (p*p <= n); ++p) { | ||
(n /= p; ++f) while (n %% p); | ||
} | ||
f + (n > 1) == k | ||
} | ||
|
||
5.times { |k| | ||
var x = 10 | ||
say gather { | ||
Math.inf.times { |i| | ||
if (is_k_almost_prime(i, k)) { | ||
take(i); (--x).is_zero && break; | ||
} | ||
} | ||
} | ||
} |
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,36 @@ | ||
#!/usr/bin/ruby | ||
|
||
# | ||
## http://rosettacode.org/wiki/Priority_queue#Sidef | ||
# | ||
|
||
class PriorityQueue { | ||
has tasks = [] | ||
|
||
method insert (Number priority { _ >= 0 }, task) { | ||
tasks.len ... priority -> each { |n| | ||
tasks[n] = [] | ||
} | ||
tasks[priority].append(task) | ||
} | ||
|
||
method get { tasks.first { !.is_empty } -> shift } | ||
method is_empty { tasks.all { .is_empty } } | ||
} | ||
|
||
var pq = PriorityQueue() | ||
|
||
[ | ||
[3, 'Clear drains'], | ||
[4, 'Feed cat'], | ||
[5, 'Make tea'], | ||
[9, 'Sleep'], | ||
[3, 'Check email'], | ||
[1, 'Solve RC tasks'], | ||
[9, 'Exercise'], | ||
[2, 'Do taxes'], | ||
].each { |pair| | ||
pq.insert(pair...) | ||
} | ||
|
||
say pq.get while !pq.is_empty |