The Artima Developer Community
Sponsored Link

Python Answers Forum
forget my other post, read this one

4 replies on 1 page. Most recent reply: Jul 16, 2004 11:27 AM by Greg Jorgensen

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
John Avery

Posts: 6
Nickname: kingdud
Registered: Jul, 2004

forget my other post, read this one Posted: Jul 1, 2004 5:14 PM
Reply to this message Reply
Advertisement
Ok, so I messed around with the program and got it working...kindof. With that in mind, I am changing my request. Here is the background so you can have a clue how to code it. I have these 16 spaces ok, they are devided like this:

2 spaces with a 72x multiplier (72 * whatever number)
2 spaces with a 36x multiplier (36 * whatever number)
2 spaces with a 21x multiplier (21 * whatever number)
2 spaces with a 14x multiplier (14 * whatever number)
2 spaces with a 10x multiplier (10 * whatever number)
4 spaces with a 8x multiplier (8 * whatever number)

If you need to see how I have it coded ATM here is how I have them devided up:

#inputs number of coins to occupy each value slot
coin1 = input("Number of coins in 72x slots: ")
coin2 = input("Number of coins in 36x slots: ")
coin3 = input("Number of coins in 21x slots: ")
coin4 = input("Number of coins in 14x slots: ")
coin5 = input("Number of coins in 10x slots: ")
coin6 = input("Number of coins in 8x slots: ")

#modifies coin count so you don't have to account for the 2 slots
c1 = coin1 * 2
c2 = coin2 * 2
c3 = coin3 * 2
c4 = coin4 * 2
c5 = coin5 * 2
c6 = coin6 * 4

#adds all the coins used together
sum = (2 + c1 + c2 + c3 + c4 + c5 + c6)

#figures out how much money is spent
money = sum * 100
min = coin6 * 800

#calculates profiets per multiplier
p1 = (coin1 * 100)*72-money
p2 = (coin2 * 100)*36-money
p3 = (coin3 * 100)*21-money
p4 = (coin4 * 100)*14-money
p5 = (coin5 * 100)*10-money
p6 = (coin6 * 100)*8-money

Ok, now that you see the code, what I need is a segment that makes sure that:

1. P1 through P6 values are all positive.

2. If changing one of the P1 through P6 values makes a number above it negative, it goes back and fixes that.

3. Keeps counting up till it finds a match, or until it reaches the maximum value (money = 21600) for this test.

Note: It is possible that this thing might not have a solution, but if it does, I would like to know what it is. Thanks ahead of time.

Peace out,

-Kingdud


Andy

Posts: 28
Nickname: adn
Registered: Jul, 2004

Re: forget my other post, read this one Posted: Jul 14, 2004 10:54 AM
Reply to this message Reply
You could re-work your code to use loops instead of the longhanded way it's in now.

e.g.

NUM_SLOTS = 6
MULTIPLIERS = (72, 36, 21, 14, 10, 8)

coins = []

for slot_index in range(NUM_SLOTS):
coins.append(input("Number of coins in %d slots: " % slot_index))

# etc. etc.

Also there's a builtin sum function:

total = sum(c1, c2 ...)

Anyway ... your questions;

1. Hint - If I have a number and I want to see if it's positive or negative is there some other number I can compare it to?

2. I'm not sure what you're after here. How does it go back and "fix it"

3. What's consititutes a match?

Andy

Posts: 28
Nickname: adn
Registered: Jul, 2004

Re: forget my other post, read this one Posted: Jul 14, 2004 10:58 AM
Reply to this message Reply
Whoops. That should be:

for slot_index in range(NUM_SLOTS):
coins.append(input("Number of coins in %d slots: " % MULTIPLIERS[slot_index]))

Greg Jorgensen

Posts: 65
Nickname: gregjor
Registered: Feb, 2004

Re: forget my other post, read this one Posted: Jul 16, 2004 11:22 AM
Reply to this message Reply
The two posts asking for help writing this program are great examples of typical problems confronting programmers.

1. Incomplete (in this case nonexistent) description of the problem. I've read all of the posts more than once and I can't figure out what the problem is, or what a solution will look like. I could be dense, but I'm guessing that no one is posting helpful solutions because they don't know what the problem is either.

2. Diving into implementation too soon. With no clear description of the problem, or how to solve it, the programmer has put together an implementation, then gotten bogged down with problems in that implementation. Instead of discussing the problem and the data structures and algorithms that might be used to solve the problem, the programmer is asking for help with language and semantic issues.

3. The data structures are clearly poorly chosen and are driving the implementation. Like most modern languages Python has data structures like lists and tuples that are frequently better choices than variables named p1, p2, etc. And the code begs for some generality that would be obvious with more appropriate data structures.

4. Learning just enough of the language to drive into a dead-end. Loops, lists, functions, etc. are all available in Python to make life easier. Spending the time to learn the language will pay off.

On top of all that I suspect that when the problem is defined someone will recognize it as a textbook algebra or calculus problem that doesn't need a computer program to exhaustively search for a solution.

Greg Jorgensen
PDXperts LLC, Portland, Oregon USA

Greg Jorgensen

Posts: 65
Nickname: gregjor
Registered: Feb, 2004

Re: forget my other post, read this one Posted: Jul 16, 2004 11:27 AM
Reply to this message Reply
> Whoops. That should be:
>

> for slot_index in range(NUM_SLOTS):
> coins.append(input("Number of coins in %d slots: " %
> " % MULTIPLIERS[slot_index]))
>


Even better, just iterate over the list:


multipliers = (72, 36, 21, 14, 10, 8)
coins = []
for mult in multipliers:
coins.append(input("Number of coins in %d slots: " % mult))


Greg Jorgensen
PDXperts LLC, Portland, Oregon USA

Flat View: This topic has 4 replies on 1 page
Topic: random from an array Previous Topic   Next Topic Topic: stupid Python tricks

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use