# Raku

**TASK #4.2**

**open a .txt file, and remove any line that has a particular key word in it.**

**TASK #4.1**

**Open a .txt file consisting of several tabulated columns and remove the 2nd column. **

**TASK #3.2**

**Generate the numbers of a Pascal triangle and print them. **

Tough one this one! This took me a lot of time mainly because I didnt see the pattern. Basically a pascal triangle can be done by taking a row, shifting it to the right 1 place, then adding it to itself. There are many ways to do this, this one is the shortest but possibly the ugliest . I did a google search and I noticed that people have done this task properly tabulated so its a perfect pyramid! Mine doesn’t do that! I’d have to think about how to achieve that. I guess the easiest would be to have the first row shift along lets say 30 spaces and print, the next row then 30-2 before printing, the next 30-4 and so on.

`my @row = 1;`

say 1;

for 1..7 {

@row = (|@row, 0) >>+<< (0, |@row);

say @row.join(' ');

}

**TASK #3.1**

**This all the prime numbers up to 100. **

`my $num = 0;`

loop {

if $num.is-prime {say $num};

$num++;

last if $num > 100;

}

The above is the standard way to do this. But its long winded. Here is a far more raku-fied version using a lazy list.

`my @num = grep {.is-prime}, 1..*;`

say @num[^25];

**TASK #2.2**

**Write a script that can convert integers to and from a base35 representation, using the characters 0-9 and A-Y.**

This one stumped me for a while when doing the base35 to base10 conversation because it didn’t like the base35 type. Casting the value didn’t help either. Research yielded that there is a specific convertor that you have use to parse the number (parse-base) After that it was easy.

`sub MAIN ($input1, $input2) {`

if $input1 == 1

{put "converting base10 to base35."; put $input2.base: 35;}

elsif $input1 == 2

{put "converting base35 to base10."; my $number=$input2.parse-base: 35; put $number;}

else {put "error in syntax, either 1 or 2"} }

**TASK #2.1:**

**Write a script or one-liner to remove leading zeros from positive numbers.**

This one is an odd challenge because of its brevity. Any Integer by default already removes leading zeros. For example if you attempt to add ‘**0002+0002**‘ you will see.

`Leading 0 has no meaning. If you meant to create an octal number, use '0o' prefix; like, '0o002'. If you meant to create a string, please add quotation marks.`

------> put 0002+0002<HERE>;

4

So it whinges, but it still gives the correct answer (4).

Hmm.. doing a simple “prompt” to receive an input and checking its name afterwards reveals its a “**IntStr**” object.

`my $n = prompt('enter number '); say "$n"; say $n.WHAT`

so I entered “00000010”. and yup, its a **IntStr**. Which also meant I can run methods on it.

`IntStr.^methods`

(new Int ACCEPTS Numeric Real Str raku BUILDALL Complex Range ACCEPTS chr sinh unival sign Real Str acos Int atan cis gist tanh roots log2 cosh sqrt truncate polymod rand sech FatRat WHICH is-prime asinh log DUMP new cos ceiling raku Bool atanh tan Capture succ acotanh acosec isNaN cosech abs sin acotan asech conj floor sec Numeric pred atan2 cotan log10 base Bridge acosech Rat acosh msb unpolar narrow asec lsb Num cotanh asin round cosec exp expmod BUILDALL codes Date lc substr Numeric word-by-word trim tc pred comb contains Bool index raku rindex samecase wordcase Capture indices match ...)

doing a “`p6doc IntStr`

” also yielded some useful info. I’m really loving this language already, its so easy to find info straight off the bat.

`> IntStr.^mro`

((IntStr) (Int) (Str) (Cool) (Any) (Mu))

So its of type **Cool**. Looking at the docs we can see all the Cool methods that are prebuilt. Okay, maybe something there might be useful for later but nothing that is immediately helpful. We could go for a reading of the chars one at a time, we can put the IntStr into an array and scan along the array one char at a time, thats longwinded but will work but that feels way too much like Java. (the most over verbose language ever!). Poking around more into the p6doc I discover a explicit “**Numeric**” type by use of the “+” symbol. So you can cast the IntStr into a Numeric and when you print it, it displays it as per a normal number.

`my $n = prompt('enter number ');`

say $n;

say +$n;

**TASK #1.2:**

**Write a one-liner to solve the FizzBuzz problem and print the numbers 1 through 20. However, any number divisible by 3 should be replaced by the word ‘fizz’ and any divisible by 5 by the word ‘buzz’. Those numbers that are both divisible by 3 and 5 become ‘fizzbuzz’.**

`loop {`

state $n=1;

if $n %% 5 && $n %% 3 {put "fizzbuzz"}

elsif $n %% 3 {put "fizz"}

elsif $n %% 5 {put "buzz"}

else {put $n;}

$n++;

last if $n > 20;

}

**TASK #1.1:
Write a script to replace the character “e” with “E” in the string “Perl Weekly Challenge”.
Also print the number of times the character ‘e’ is found in the string.**

`my $pwc = "Perl Weekly Challenge";`

say $pwc;

my $changes = ( $pwc = "Perl Weekly Challenge") ~~ tr/e/E/;

say $pwc;

say +$changes;