Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Divide a vector
#1
Hello I have a question i'm doing a program that do this:
Imagine you are a Pokemon trainer and you want to fill your bag with
three types of items; pokeballs, potions and rare candies. Each pokeball cost $200 and gives you 1000 experience points (exp). Potions cost $300 and gives you 1800 exp and finally rare candies cost \verb|$1000| and gives you 7000 exp. You have $25,000 (pokedollars) and obviously, you want to get the maximum possible experience spending as much of your money:
So I made the following program:
#amount of money
pokeball = 200
potion = 300
candy = 1000
#amount of exp
pokeball2 = 1000
potion2 = 1800
candy2 = 7000
#total money
money = 25000
total= 0
exp = 0
#list used
combinations = []
combination= []
lpokeball=[]
lpotion=[]
lcandy=[]
ltotal=[]

n = money / pokeball

for i in range (0,n+1):
    lpokeball.append(i*pokeball)

print('All the combinations of pokeballs are:')
print (lpokeball)

m= money / potion

for i in range (0,m+1):
    lpotion.append(i*potion)

print('All the combinations of potions are:')
print(lpotion)

b = money / candy

for i in range (0,b+1):
    lcandy.append(i*candy)

print('All the combinations of candys are:')

for i in lpokeball:
    for j in lpotion:
        for k in lcandy:
            total = (i+j+k)
            if total <= money and total >= 20000:   #at the beggining
                                                    #I only has if
                                                    #total <= money
                                                    #but that gives me
                                                    #a lot of
                                                    #vectors, so I
                                                    #added the other
                                                    #conditon to give
                                                    #me just the
                                                    #betters
                                                    #combination
                combination.append(i/pokeball)
                combination.append(j/potion)
                combination.append(k/candy)
                combination.append(total)
                exp = (i/pokeball)*pokeball2 + (j/potion)*potion2 + (k/candy)*candy2
                combination.append(exp)
                #combinations.append(combination) I think with this it was going to be separated by vectors but no
#combination.sort()
print(combination)
#print(combinations) 
And my program works but,
it gives me : combinations= (1,0,0,200,1000,1,1,0,500,2800,1,1,1,1500,9800 ... )
but it should be: Combinations= [(1,0,0,200,1000),(1,1,0,500,2800),(1,1,1,1500,9800) ...]
and I don't know how to cut my vector in a vector made by vectors of 4 coordinates each one for short vectors I know that:
if a=[1,2,3,4,5,6,7,8,9]
and I do: b=[(a[0:3]),(a[3:6]),(a[6:10])]
it brings me b=[[1,2,3],[4,5,6],[7,8,9]]
But I don't know how to use that in my program or if there is another way to do it, Thanks!
Reply
#2
The quick fix would be to change following lines:
combination.append(i/pokeball)
combination.append(j/potion)
combination.append(k/candy)
combination.append(total)
exp = (i/pokeball)*pokeball2 + (j/potion)*potion2 + (k/candy)*candy2
combination.append(exp)
to:
exp = (i/pokeball)*pokeball2 + (j/potion)*potion2 + (k/candy)*candy2
combination.append((i/pokeball, j/potion, k/candy, total, exp))
a different and more efficient approach would be to use numpy. You are currently working with lists. computing everything without loops would greatly improve the computational time of your code when you start using huge numbers. This code is not perfect, it could even be improved for numpy, it was just a quick numpy solution :D
here the exact same solution in numpy:
import numpy as np
#amount of money
pokeball = 200
potion = 300
candy = 1000
#amount of exp
pokeball2 = 1000
potion2 = 1800
candy2 = 7000
#total money
money = 25000
total= 0
exp = 0
 
n = int(money / pokeball)

lpokeball = np.array(range(0,n+1)) * pokeball

print('All the combinations of pokeballs are:')
print (lpokeball)
 
m= int(money / potion)

lpotion = np.array(range(0,m+1)) * potion
 
print('All the combinations of potions are:')
print(lpotion)
 
b = int(money / candy)

lcandy = np.array(range(0,b+1)) * candy
 
print('All the combinations of candys are:')
print(lcandy)

n+=1
b+=1
m+=1

# creating three arrays to compute every single possible combination
ball_list = np.array((m*b) * [lpokeball]).flatten()
#ball_list *= pokeball
potion_list = np.array(b*[np.repeat(lpotion, n)]).flatten()
candy_list = np.repeat(lcandy, m*n)
# calculate total
total = ball_list + potion_list + candy_list
# filter elements where total does not match the criteria
total[total > money] = 0
total[total < 20000] = 0
# only select elements where total fits the criteria
pokeball_num_list = (ball_list / pokeball)[total>0]
potion_num_list = (potion_list / potion)[total>0]
candy_num_list = (candy_list / candy)[total>0]
# convert type (looks nicer)
pokeball_num_list = pokeball_num_list.astype(np.int)
potion_num_list = potion_num_list.astype(np.int)
candy_num_list = candy_num_list.astype(np.int)
# claculate experience
exp = pokeball_num_list * pokeball2 + potion_num_list * potion2 + candy_num_list * candy2
total = total[total > 0]
# create combination and get maximum
combination = np.array([pokeball_num_list, potion_num_list, candy_num_list, total, exp])
combination = combination.T
max_combination_index = np.argmax(combination, axis=0)
max_combination = combination[max_combination_index[-1]]

#combination.sort()
print("These are the posible combinations")
print(combination)
print("This is one of the max possible combinations")
print(max_combination)
#print(combinations)  
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Divide a Python Turtle program into subprograms TurtleOHG 5 3,395 Mar-21-2020, 02:07 PM
Last Post: buran
  divide a number iin two marciotos 2 1,916 Jan-20-2020, 12:25 PM
Last Post: marciotos
  vector field plots Larssauerkraut 0 1,524 Oct-15-2019, 11:15 AM
Last Post: Larssauerkraut
  Class for Vector no_named_nobody 4 2,587 Oct-06-2019, 03:27 PM
Last Post: no_named_nobody
  Divide by 0 error kethyar 1 3,138 Jul-16-2017, 05:55 PM
Last Post: snippsat
  Classification by support vector method qwerty 1 2,945 Apr-16-2017, 07:16 PM
Last Post: sparkz_alot
  vector sum of tuples roadrage 7 7,678 Nov-28-2016, 09:54 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020