Fetching contributors…
Cannot retrieve contributors at this time
executable file 165 lines (123 sloc) 7.25 KB

# Perl Weekly Challenge #42 Task #1: Octal Numbers

The octal number is printed with `printf` or `sprintf` and the format string `%o`. I provided two solutions, first with a `foreach` loop (Perl5) or `for` loop (Perl6), second with a `map` on one line.

Following are some possibilities to deal with Octal numbers:

• printf, sprintf and "%o" Print with format string used in this solution.

• foreach(0 .. 50) / for (0 .. 50) (Perl5/Perl6) Loop over values from 1 to 50 in Perl5 and Perl6.

• @result = map { ... } (0 .. 50) Loop over values from 1 to 50 with map function.

• my \$number = 0644; Assignment of octal number.

• my \$octal = oct(644); Convert scalar to octal number.

• (s)printf("0%o %d", \$number, \$number); Print number as octal and decimal.

# SYNOPSIS

``````# ./ch-1.pl                     - Program execution
# ./ch-1.p6                     - Program execution
# perldoc ch-1.pod              - POD``````

# Definition Task #1: Octal Number System

Write a script to print decimal number 0 to 50 in Octal Number System.

``````For example:
Decimal 0 = Octal 0
Decimal 1 = Octal 1
Decimal 2 = Octal 2
Decimal 3 = Octal 3
Decimal 4 = Octal 4
Decimal 5 = Octal 5
Decimal 6 = Octal 6
Decimal 7 = Octal 7
Decimal 8 = Octal 10``````

and so on.

# Sourcecode for Perl5 and Perl6

Most important difference between Perl5 and Perl6 in this code is the loop statement `foreach(1 .. 50)` in Perl5 and `for (1 .. 50)` in Perl6. Not only that foreach becomes for, but also the important blank behind the for keyword.

The `printf() / sprintf()` calls are the same. the `map {}, (1 .. 50)` is different in the comma behind the block in the Perl6 part.

 ``` Perl5 1 #!/usr/bin/perl 2 3 use strict; 4 use warnings; 5 6 print "ch-1.pl (Version 1.0) PWC #42 Task #1: Octal Numbers\n"; 7 8 # Version 1: foreach loop with printf 9 foreach (0 .. 50) { 10 printf("Decimal: %d - Octal: %o\n",\$_,\$_); 11 } 12 13 # Version 2: map with sprintf 14 print my @res = map { sprintf("Dec %d - Oct %o\n",\$_,\$_) } (0 .. 50); ``` ``` Perl6 1 #!/home/chuck/rakudo/bin/perl6 2 3 use strict; 4 5 print "ch-1.p6 (Version 1.0) PWC #42 Task #1: Octal Numbers\n"; 6 7 # Version 1: foreach loop with printf 8 for (0 .. 50) { 9 printf("Decimal: %d - Octal: %o\n",\$_,\$_); 10 } 11 12 # Version 2: map with sprintf 13 print my @res = map { sprintf("Dec %d - Oct %o\n",\$_,\$_) }, (0 .. 50); 14 ```

# Sourcecode for Python

In Python I tried to implement the loop and the map way I did in Perl. So below you can see these trials. The `for` loop is very easy and similar. The `map()` way seems to be a little bit more complicated than in Perl.

 ``` Python 1 #!/usr/bin/python 2 3 import array as arr 4 5 print "ch-1.py (Version 1.0) PWC #42 Task #1: Octal numbers." 6 7 for i in range(0,50): 8 print "Decimal: ", i, " - Octal: ", oct(i) 9 10 11 def print_string(n): 12 buf = "Decimal: %d, Octal: %o" % (n, n) 13 return buf 14 15 a = list(range(0,50)) 16 x = map(print_string, a) 17 # print(list(x)) 18 for e in x: print e 19 ```

# AUTHOR

Chuck

You can’t perform that action at this time.