in reply to IBM Cloud Challenge.
Initially I thought that in the change problem they gave you denominations, but the way I read it now seems to mean they give you a list of coins, so that you can't use two of a denomination if they only list it once. In the sample data you don't need more than one coin of each denomination. So that makes the difference if you have say 7,5,2:14 Is that using all coins or two 7s?
Mojolicious::Lite just shines when it comes to making small encapsulated web apps
Question 2:
#!/usr/bin/env perl use Mojolicious::Lite; sub makeChange { my $line = shift; my @available = split /[,:]/,$line; my $changeNeeded = pop @available; @available = sort { $b <=> $a } @available; my %coins; while ($changeNeeded > 0 && @available > 0 ) { my $denom = shift @available; if ($denom <= $changeNeeded) { $changeNeeded -= $denom; $coins{$denom}++; #unshift @available,$denom; #Multiple larger c +oins? } } warn "Could not make change correctly: $line" if ($changeNeede +d != 0); return %coins; } get '/' => sub { my $c = shift; $c->render(template => 'index'); }; post '/make/change' => sub { my $c = shift; my $changeToMake = $c->param('data'); my @lines = split /\r?\n/, $changeToMake; my @answer = map { { makeChange($_) } } @lines; my @formattedAnswers; for my $coin (sort { $b <=> $a } @answer) { my @formatted; for (sort { $b <=> $a } keys %{$coin}) { push @formatted, "$_". 'x' . "$coin->{$_}"; } unshift @formattedAnswers, [ @formatted ]; } $c->render( answer => \@answer, formatted=>\@formattedAnswers, template => 'makeChange' ); }; app->start; __DATA__ @@ index.html.ep % layout 'default'; % title 'Welcome'; <h2>Question 2 Data Input</h2> <form action="/make/change" method="post" > <textarea rows=30 cols=80 name="data" id="comment" value="" required > </textarea> <input type="submit" value="Run"></input> </form> @@ makeChange.html.ep <h2>Question 2 Results</h2> % for my $answer (@{$formatted}) { %== join ',',@{$answer} <br> % } @@ layouts/default.html.ep <!DOCTYPE html> <html> <head><title><%= title %></title></head> <body><%= content %></body> </html>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: IBM Cloud Challenge.
by BrowserUk (Patriarch) on Aug 30, 2015 at 17:00 UTC | |
by trippledubs (Deacon) on Aug 30, 2015 at 21:26 UTC | |
by Anonymous Monk on Aug 30, 2015 at 21:58 UTC | |
by trippledubs (Deacon) on Aug 31, 2015 at 00:12 UTC |