Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Can someone help me make this script more efficient

by jasonk (Parson)
on Apr 13, 2008 at 17:43 UTC ( [id://680126]=note: print w/replies, xml ) Need Help??


in reply to Can someone help me make this script more efficient

Hmm, do you just want it shorter and still produce the wrong total, or shorter and produce the correct total as well?

# Let's see how much money I would have if I just won the # lottery and found a 2 pound coin in the couch cushions... How many 5 pence pieces do you have? 0 How many 10 pence pieces do you have? 0 How many 20 pence pieces do you have? 0 How many 50 pence pieces do you have? 0 How many £1 coins do you have? 1000000000 How many £2 coins do you have? 1 How many £5 notes do you have? 0 How many £10 notes do you have? 0 How many £20 notes do you have? 0 How many £50 notes do you have? 0 You have £2. # Hmm, guess I won't spend my £2 on a lottery ticket then...

strict would have helped you out there... In any case...

#!/usr/bin/perl -w use strict; use warnings; use List::Util qw( sum ); printf( "You have £%.02f\n", sum ( ( map { ask( "$_ pence pieces", $_/100 ) } ( 5, 10, 20, 50 ) ), ( map { ask( "£$_ coins", $_ ) } ( 1, 2 ) ), ( map { ask( "£$_ notes", $_ ) } ( 5, 10, 20, 50 ) ), ) ); sub ask { print "How many $_[0] do you have? " ; <STDIN> * $_[1] }

www.jasonkohles.com
We're not surrounded, we're in a target-rich environment!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://680126]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-24 04:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found