Skip to content

Commit

Permalink
- Extended the String.first() and String.last() methods to accept a n…
Browse files Browse the repository at this point in the history
…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
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 5 deletions.
3 changes: 3 additions & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ scripts/elementary_cellular_automaton.sf
scripts/ethiopian_multiplication.sf
scripts/Expensive/benford_s_law.sf
scripts/Expensive/binary_multiplier.sf
scripts/Expensive/calendar.sf
scripts/Expensive/closed_form_fibonacci.sf
scripts/Expensive/coin_change.sf
scripts/Expensive/dice_game_solver.sf
Expand Down Expand Up @@ -270,6 +271,7 @@ scripts/pythagorean_means.sf
scripts/reverse_the_gender_of_a_string.sf
scripts/RosettaCode/9_billion_names.sf
scripts/RosettaCode/abc_problem.sf
scripts/RosettaCode/almost_prime.sf
scripts/RosettaCode/arithmetic-geometric_mean.sf
scripts/RosettaCode/arithmetic_complex.sf
scripts/RosettaCode/averages_arithmetic_mean.sf
Expand Down Expand Up @@ -426,6 +428,7 @@ scripts/RosettaCode/polymorphic_copy.sf
scripts/RosettaCode/polymorphism.sf
scripts/RosettaCode/price_fraction.sf
scripts/RosettaCode/primality_by_trial_division.sf
scripts/RosettaCode/priority_queue.sf
scripts/RosettaCode/probabilistic_choice.sf
scripts/RosettaCode/problem_of_apollonius.sf
scripts/RosettaCode/program_name.sf
Expand Down
7 changes: 6 additions & 1 deletion lib/Sidef/Deparse/Sidef.pm
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,12 @@ package Sidef::Deparse::Sidef {

if (exists $call->{keyword}) {
if ($code ne '') {
$code .= ' ';
if (substr($code, 0, 1) ne '(' or substr($code, -1) ne ')') {
$code .= ' ';
}
else {
$code = "($code)";
}
}
$code .= $call->{keyword};
}
Expand Down
8 changes: 4 additions & 4 deletions lib/Sidef/Types/String/String.pm
Original file line number Diff line number Diff line change
Expand Up @@ -263,13 +263,13 @@ package Sidef::Types::String::String {
}

sub first {
my ($self) = @_;
__PACKAGE__->new(CORE::substr($$self, 0, 1));
my ($self, $obj) = @_;
__PACKAGE__->new(CORE::substr($$self, 0, defined($obj) ? $obj->get_value : 1));
}

sub last {
my ($self) = @_;
__PACKAGE__->new(CORE::substr($$self, -1));
my ($self, $obj) = @_;
__PACKAGE__->new(CORE::substr($$self, defined($obj) ? -$obj->get_value : -1));
}

sub char {
Expand Down
58 changes: 58 additions & 0 deletions scripts/Expensive/calendar.sf
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)
23 changes: 23 additions & 0 deletions scripts/RosettaCode/almost_prime.sf
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;
}
}
}
}
36 changes: 36 additions & 0 deletions scripts/RosettaCode/priority_queue.sf
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

0 comments on commit 801ff02

Please sign in to comment.