Last active
March 7, 2022 17:43
-
-
Save sumanstats/5f1a39cce3a481077a8f757fc58b5728 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
my @vals= <1 2 40 40 90 20.5 20.5 20.5 20.5 4 4 6>; | |
my @new = @vals.BagHash.sort(+*.key); | |
my $i=1; | |
for @new { | |
if .value == 1 { | |
say .key => $i; | |
$i+=1 | |
} else { | |
say .key => $i+(.value - 1)/2; | |
$i = $i + .value | |
} | |
} | |
for @vals { | |
$_ = %damn{$_} | |
} |
This file contains hidden or 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
#Recursive solutions | |
#1st | |
sub lucas_number(Int $nth) { | |
given $nth { | |
when 0 { 2 }; | |
when 1 { 1 }; | |
default {lucas_number($nth-1) + lucas_number($nth-2)} | |
} | |
} | |
say lucas_number(20) | |
#2nd recursive solution | |
multi lucas_number(0) {2}; | |
multi lucas_number(1) {1}; | |
multi lucas_number(Int $nth) { | |
lucas_number($nth - 1) + lucas_number($nth - 2) | |
} | |
say lucas_number(20) | |
# Fastest solution | |
multi lucas_number(0) {2}; | |
multi lucas_number(1) {1}; | |
multi lucas_number(Int $nth where * > 1) { | |
my @array = <2 1>; | |
my $i = 1; | |
while $i < $nth { | |
@array.push(@array[$i] + @array[$i-1]); | |
$i = $i + 1 | |
} | |
@array[$i] | |
} | |
say lucas_number(3500) | |
# Suggested by lizmat | |
my @lucas = 0, 1, * + * ... *; | |
for 1..20 {say @lucas[$_]}; | |
say @lucas[3500] |
This file contains hidden or 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
for 1..3 {say "Bio",$_,".jpg"}; | |
.say for "Bio" <<~<< (1..3) >>~>> ".jpg"; | |
"Bio$($_).jpg".say for 1..3; | |
.fmt("Bio%d.jpg").say for 1..3; | |
<Bio .jpg>.join((1..3).join(".jpg\nBio")).say; | |
say ("Bio1.jpg", *.succ ... *).head(3); | |
say "Bio" xx 3 Z~ 1..3 Z~ ".jpg" xx 3; | |
say [Z~] "Bio" xx 3, 1..3, ".jpg" xx 3; | |
say (S/ℵ/$(1..$++)/ given "Bioℵ.jpg") xx 3; |
This file contains hidden or 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
# script to list files in a directory matching a file extension | |
# Main function takes two parameters: folder and a file extension pattern | |
sub MAIN($folder, $pattern) { | |
if $folder.IO.d { | |
# if folder is a directory | |
for $folder.IO.dir -> $filename { | |
say $filename if $filename.IO.f && $filename ~~ /:i $pattern$/; | |
# if filename matches the file extension and the filename is a file | |
} | |
} | |
} | |
#****************************************** | |
# Usage: | |
# raku script.raku <folder> <extension> | |
#****************************************** |
This file contains hidden or 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
# softmax function | |
my @z = [1.0, 2.0, 3.0, 4.0, 1.0, 2.0, 3.0]; | |
sub softmax(@val,$beta) { | |
my @beta_into_n = @val.map: {exp($_*$beta)}; | |
return @val.map: {exp($_)/sum(@beta_into_n)} | |
} | |
say softmax(@z,1) |
This file contains hidden or 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
#Sorting keys as Integer in Hash | |
my %my_hash is Hash[Int, Int] = 9 => 3, 12 => 5, 5 => 7, 3 => 11, 11 => 13; | |
for %my_hash.sort -> (:$key, :$value) { | |
say "'$key' => '$value'"; | |
} | |
my %my_hash = 9 => 3, 12 => 5, 5 => 7, 3 => 11, 11 => 13; | |
for %my_hash.sort(+*.key) -> (:$key, :$value) { | |
say "'$key' => '$value'"; | |
} |
This file contains hidden or 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
srand(1); | |
my @test1 = [6,6,4,6,8,6,8,4,4,6,6,8,8,8,8,8,8,4,4,4,4,8,8,8,8,4,4,4,8,6,8,4]; | |
my @test2 = [160.0,160.0,108.0,258.0,360.0,225.0,360.0,146.7,140.8,167.6,167.6, | |
275.8,275.8,275.8,472.0,460.0,440.0,78.7,75.7,71.1,120.1,318.0,304.0, | |
350.0,400.0,79.0,120.3,95.1,351.0,145.0,301.0,121.0]; | |
my @test3 = @test1.roll(10); | |
my @test4 = @test2.roll(10); | |
my @test5 = @test1.roll(10); | |
# write csv | |
my $fh = 'my-file.csv'.IO.open: :w; | |
$fh.print: "car, mileage, numbers"; # headers | |
for @test3 Z @test4 Z @test5 { | |
$fh.print: "\n", $_.join(", "); | |
} | |
$fh.close; |
This file contains hidden or 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
my @test1 = [6,6,4,6,8,6,8,4,4,6,6,8,8,8,8,8,8,4,4,4,4,8,8,8,8,4,4,4,8,6,8,4]; | |
my @test2 = [160.0,160.0,108.0,258.0,360.0,225.0,360.0,146.7,140.8,167.6,167.6, | |
275.8,275.8,275.8,472.0,460.0,440.0,78.7,75.7,71.1,120.1,318.0,304.0, | |
350.0,400.0,79.0,120.3,95.1,351.0,145.0,301.0,121.0]; | |
# write csv | |
# columns are variables | |
sub write_csv($filename, List :$columns, List :$headers) { | |
# checking | |
die "Provide csv file" if $filename !~~ /\.csv$/; | |
#$headers; | |
die "Provide equal number of headers and columns" if | |
$columns.elems != $headers.elems; | |
my $fh = $filename.IO.open: :w; | |
$fh.print: $headers.join(", "); # headers | |
for [Z] $columns { | |
$fh.print: "\n", $_.join(", "); | |
} | |
$fh.close; | |
} | |
write_csv("my-file.csv", columns=>(@test1, @test2), headers=>("car", "mile")); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment