diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2019-06-24 19:27:06 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-06-24 19:27:06 +0100 |
| commit | 7c9a9c8ade6c19acdaf5e44d2d0977d100f44207 (patch) | |
| tree | f96bae2953600b6ff0ccc94ea7b30337e22bd78a /challenge-014 | |
| parent | 10f0cd8851835cfa2f1f8962894ffb887de0202c (diff) | |
| parent | 26a921b3ccb89bcc4d7f7d047c5131196bda8543 (diff) | |
| download | perlweeklychallenge-club-7c9a9c8ade6c19acdaf5e44d2d0977d100f44207.tar.gz perlweeklychallenge-club-7c9a9c8ade6c19acdaf5e44d2d0977d100f44207.tar.bz2 perlweeklychallenge-club-7c9a9c8ade6c19acdaf5e44d2d0977d100f44207.zip | |
Merge pull request #298 from oWnOIzRi/new-branch
add solution to challenge 1
Diffstat (limited to 'challenge-014')
| -rw-r--r-- | challenge-014/steven-wilson/perl5/ch-1.pl | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/challenge-014/steven-wilson/perl5/ch-1.pl b/challenge-014/steven-wilson/perl5/ch-1.pl new file mode 100644 index 0000000000..77f1873153 --- /dev/null +++ b/challenge-014/steven-wilson/perl5/ch-1.pl @@ -0,0 +1,53 @@ +#!/usr/bin/env perl +# Author: Steven Wilson +# Date: 2019-06-24 +# Week: 014 +# Challenge: #1 +# +# Write a script to generate Van Eck’s sequence starts with 0. For more +# information, please check out wikipedia page. This challenge was +# proposed by team member Andrezgz. +# https://en.wikipedia.org/wiki/Van_Eck%27s_sequence + +use strict; +use warnings; +use feature qw / say /; + +my $length_of_sequence = 30; +my $current_position = 0; +my $current_int = 0; +my %last_int_position; +my $next_int; + +say "Printing the first $length_of_sequence values in Van Eck's sequence:"; +while ( $current_position < $length_of_sequence ) { + print "$current_int, "; + if ( exists $last_int_position{$current_int} ) { + $next_int = $current_position - $last_int_position{$current_int}; + } + else { + $next_int = 0; + } + $last_int_position{$current_int} = $current_position; + $current_int = $next_int; + $current_position++; +} +print "...\n"; + +# Alternate method using an array to hold sequence: +# +# my @sequence = (0); +# while((scalar @sequence) < $length_of_sequence){ +# if(exists $last_int_position{$sequence[-1]}){ +# $next_int = ((scalar @sequence) - $last_int_position{$sequence[-1]}); +# } +# else { +# $next_int = 0; +# } +# $last_int_position{$sequence[-1]} = scalar @sequence; +# push @sequence, $next_int; +# } +# for (@sequence){ +# print "$_, "; +# } +# print "...\n"; |
