Quick actions

cmd+k|ctrl+k

Navigation

Languages

discrete and cominatorial math chapter 1_1

Snippet info

Language

Raku

Visibility

public

Author

frank576frank576frank576

Created

2017-07-09T14:50:34Z

Updated

2017-07-09T14:50:34Z

#/usr/bin/env perl6
use v6;

"The rule of sum".say;

say q:to/EOF/;
	Ex: 
		Frank wants to go to Taipei. 
		He can choose from bus services or train services to head from home to Hsinchu . 
		From there, he can choose from 2 bus services or 3 train services to head to Taipei. 
		{ bus: Kuo-Kuang Greyhound-Line , train: Amtrak Krtco Metro-Taipei }
		How many ways are there for Hsinchu to get to Taipei?
	EOF

my Sub @byBus = gather for < Kuo-Kuang Greyhound-Line > -> $name {
	take &trafficMethod( :way( $name ) )
}

my Sub @byTrain = gather for < Amtrak Krtco Metro-Taipei > -> $name {
	take &trafficMethod( :way( $name ) )
}

say "We have { &count( @byBus, @byTrain) :rule("sum")}  way to reach it\n";

"The rule of product".say;
say q:to/EOF/;
	EX:
		How many situation of "rock paper scissors" game for two player 
	EOF
my @player := <rock paper scissors> ;

say "We have { &count( @player, @player ) } situation";


sub trafficMethod ( Str :$way!, Str :$from? = 'Hsinchu', Str :$to? = 'Taipei' --> Sub ) {
	return sub { "form $from to $to by $way".say  }
}

sub count( List $a where $a.elems > 0, List $b where $b.elems > 0, :$rule? = 'product' --> Int ) {
	given $rule { 
		when 'product' { $a.elems * $b.elems }
		when 'sum' { $a.elems + $b.elems }
		default { die "I don't know the option of $rule mean !\n" }
	}
}

INFO