Looping problem Elden ring runes per level calculation - python

This code is currently showing the total amount needed to reach the next level, but what I would like it to do is show the complete total amount so for example if you pick 50 the total amount needed from level 1 to 50. Looping through the calculation from 50 to 1 and only showing the sum of that total. and hereby I mean only showing the total amount of runes needed to reach level 50 for example, but I seem to get the entire list for each level. I, unfortunately, can't seem to find the right way to do this online, so I would try my luck here. any help is appreciated.
def total_runes_per_lvl(lvl):
list = []
for i in range(lvl):
runes = 0.02*(lvl)**3 + 3.06*(lvl)**2 + 105.6*(lvl) - 895
list.append(runes)
lvl -= 1
print(sum(list))
total_runes_per_lvl(50)
15102.68
14535.0
28514.440000000002
41950.32
54854.520000000004
67238.8
etc`
should only be one number: 277.571

Your identation is incorrect, and you're decrementing lvl even though there's already an iterator on it.
def total_runes_per_lvl(lvl):
total = 9381
for i in range(13,lvl+1):
runes = 0.02*(i+1)**3 + 3.06*(i+1)**2 + 105.6*(i+1) - 895
total += int(runes)
print(total)
total_runes_per_lvl(16) # 15605
total_runes_per_lvl(50) # 277574
Edit: since the formula works accurately after level 12, I've hardcoded the value for the total of first 12 levels. The formula works as expected, though it still isn't accurate.

Related

How do I fix infinite loop bugs in Python?

I have a problem regarding a competition question I'm attempting to do. Here is the question (its a bit long)
""""
Welcome aboard, Captain! Today you are in charge of the first ever doughnut-shaped spaceship, The
Circular. There are N cabins arranged in a circle on the spaceship. They are numbered from 1 to N in
a clockwise direction around the ship. The ith and the (i + 1)th cabins are connected. So too are cabin
1 and cabin N.
Currently the ith cabin has Ai crewmates, however the spaceship cannot depart unless there are exactly
Bi crewmates in this cabin.
To achieve this, you have the power to pay crewmates to change cabins. You can pay a crewmate $1 to
move to an adjacent cabin. A crewmate can be asked to move multiple times, provided that you pay
them $1 each time.
What is the fewest dollars you must pay before you can depart? It is always be possible to depart.
""""
https://orac2.info/problem/aio22spaceship/ (the link to the intereactive Qs)
I searched the web and i found no solutions to the Q. My code seems to be infinite looping i guess but im not sure as i cant see what cases the sit uses to determine if my code is right.
Heres my code
#!/usr/bin/env python
import sys
sys.setrecursionlimit(1000000000)
#
# Solution Template for Spaceship Shuffle
#
# Australian Informatics Olympiad 2022
#
# This file is provided to assist with reading and writing of the input
# files for the problem. You may modify this file however you wish, or
# you may choose not to use this file at all.
#
# N is the number of cabins.
N = None
# A contains the initial number of crewmates in each cabin. Note that here the
# cabins are numbered starting from 0.
A = []
# B contains the desired number of crewmates in each cabin. Note that here the
# cabins are numbered starting from 0.
B = []
answer = 0
# Open the input and output files.
input_file = open("spacein.txt", "r")
output_file = open("spaceout.txt", "w")
# Read the value of N.
N = int(input_file.readline().strip())
# Read the values of A and B.
input_line = input_file.readline().strip()
A = list(map(int, input_line.split()))
input_line = input_file.readline().strip()
B = list(map(int, input_line.split()))
AM = A
#AM is my modifying set
# TODO: This is where you should compute your solution. Store the fewest
# dollars you must pay before you can depart into the variable
while AM != B:
#Check if the set is correct
#notfound is a testing variable to see if my code was looping due to input error
notfound = True
for i in range(N):
#Check which places needs people to be moved
while AM[i]>B[i]:
notfound = False
#RV and LV check the "neediness" for each half's people requirements. I check how many people
#are needed on one side compared to the other and subtract the "overflow of people"
RV = 0
LV = 0
for j in range(int(N/2-0.5)):
#The range thing makes sure that if N is odd, im splitting the middle but if N is even, i leave out the end pod
RV += B[(i+j+1)%N]-AM[(i+j+1)%N]
LV += B[(i-j-1)%N]-AM[(i-j-1)%N]
answer +=1
if RV>LV:
AM[i]+=-1
AM[(i+1)%N]+=1
else:
AM[i]+=-1
AM[(i-1)%N]+=1
print(AM,B)
if notfound:
break
print(answer)
# Write the answer to the output file.
output_file.write("%d\n" % (answer))
# Finally, close the input/output files.
input_file.close()
output_file.close()
please help i really neeed to know the answer, driving me mad ngl
Welp, there aren't any resources online and I've tried everything. I think the problem might be that because of my solving method, passengers may be flicked between two pods indefinitely. Not sure since i could make a case that demoed this.
also my post probably is messy since this is my first time posting

How to make my program start at 1 and double that amount and also print the total

My problem is that my code starts 1 day at 2 pennies, it should start at 1. I also would like to know how to print the total amount of pennies. Here is the code I have so far:
daysworked = int(input("Enter the amount of days you worked: "))
pay = 1
print("Day\tPay")
print("-----------------")
for daysworked in range (1, daysworked+1):
endpay = pay * 2
print(daysworked, "\t", endpay)
pay = endpay
If you want to print out the total amount of money the user has earned over the course of the days they worked, you can create a variable: total, which is the sum of all the payments earned per day.
Also, since on the first day, you don't want to double your value, we can simply print out the first value before the loop (skipping an iteration of your loop, hence the -1), or start our variable pay at 0.5.
To integrate this into your code, we can utilize the for loop you implemented and simply add up all the endpay values.
Other minor changes you could make: As #S3DEV mentioned, it is unnecessary to create a secondary variable to store updated payment, you can simply update the variable pay! We could also edit the for loop to just take in one parameter, the daysworked, instead of a starting and ending value. In Python, it is also recommended that if your variable names are multiple words long, separate them with an underscore.
With these changes included, your code could look something like this:
days_worked = int(input("Enter the amount of days you worked: "))
pay = 1
total = 1
print("Day\tPay")
print("-----------------")
print(1,pay)
for days_worked in range (days_worked - 1):
pay *= 2
total += pay
print(days_worked + 1, "\t", pay)
print("Total Payment:" , total)
Different Solution: (This solution will result in float answers since pay starts at .5, so you can cast to an int):
days_worked = int(input("Enter the amount of days you worked: "))
pay = .5
total = 0
print("Day\tPay")
print("-----------------")
for days_worked in range (days_worked):
pay *= 2
total += pay
print(days_worked, "\t", int(pay))
print("Total Payment:" , int(total))
I hope this helped with your problem! Let me know if you need any further clarification or details :)
I'm guessing you don't want to print in every iteration, and that's what you are asking. Just unindent that print line so it executes at the end of the loop. Also I removed superfluous variable.
daysworked = int(input("Enter the amount of days you worked: "))
pay = 1
print("Day\tPay")
print("-----------------")
for daysworked in range (1, daysworked+1):
pay = pay * 2
print(daysworked, "\t", pay)
daysworked = int(input("Enter the amount of days you worked: "))
pay = 1
endpay = 0
print("Day\tPay")
print("-----------------")
for dayworked in range (1, daysworked+1):
pay *= 2
print(dayworked, "\t", endpay)
endpay += pay
print(endpay)
Have a look at two things here.
First, you need to add one more variable to keep track of the sum, endpay in this case, which you have to print at the end. Although, in this case, endpay will always be the double of the last value of pay less two. ;) The last line could as well have been written print(2 * pay - 2) and get rid of the lines using 'endpay'.
Second the difference between daysworked and dayworked (you could have used i or any other variable name here as well).

How to convert bar count to time, in midi? (music)

Given a midi file, how can one convert the bar count to time?
Generally, how can one easily map the bar count, in entire numbers, to the time in seconds in the song
Using pretty midi, my solution
import pretty_midi as pm
def get_bar_to_time_dict(self,song,id):
def get_numerator_for_sig_change(signature_change,id):
# since sometime pretty midi count are wierd
if int(signature_change.numerator)==6 and int(signature_change.denominator)==8:
# 6/8 goes to 2 for sure
return 2
return signature_change.numerator
# we have to take into account time-signature-changes
changes = song.time_signature_changes
beats = song.get_beats()
bar_to_time_dict = dict()
# first bar is on first position
current_beat_index = 0
current_bar = 1
bar_to_time_dict[current_bar] = beats[current_beat_index]
for index_time_sig, _ in enumerate(changes):
numerator = get_numerator_for_sig_change(changes[index_time_sig],id)
# keep adding to dictionary until the time signature changes, or we are in the last change, in that case iterate till end of beats
while index_time_sig == len(changes) - 1 or beats[current_beat_index] < changes[index_time_sig + 1].time:
# we have to increase in numerator steps, minus 1 for counting logic of natural counting
current_beat_index += numerator
if current_beat_index > len(beats) - 1:
# we labeled all beats so end function
return bar_to_time_dict
current_bar += 1
bar_to_time_dict[current_bar] = beats[current_beat_index]
return bar_to_time_dict
song = pm.PrettyMIDI('some_midi_file.midi')
get_bar_to_time_dict(song)
If anyone knows a function in pretty midi or music21 that solves the same issue please let me know, couldn't find one.
EDIT: There was also an issue with 6/8 beats, I think this covers all edge cases(not 100% sure)

I am doing my NEA dice game and I can't make the rounds add up

I've tried many ways and still can't find a way to make it work!
I've tried making a variable which adds the total after each round to itself. endtotal = endtotal + totalscore.
I haven't tried other ways yet but I thought I would ask on here first so I'm not trying different methods and failing every time.
Regardless of OP posting code: use a while-loop and variables to be set outside. In the example of using vars outside the loop the endtotal is each round increased by the defined totalscore and printed as a sum at the end.
count_rounds = 0
rounds = 10
endtotal = 0
totalscore = 69
while count_rounds < rounds :
endtotal = endtotal + totalscore
count_rounds += 1
print ("I'm done with the rounds counting. End score: %s" % endtotal)
You'll get the hang of it and how to implement when you'll check out the example. (I assume you know how to work this out using defs.). Enjoy.

Infinite loop in simulation

I'm starting out in python.. The details I have written in the below.. It goes to an infinite loop and give me an error when I try to call the function inside itself.. Is this kind of recursion not allowed ?
Posting code below.. Thanks for all your help :)
The program assumes that we have 100 passengers boarding a plane. Assuming if the first one has lost his boarding pass, he finds a random seat and sits there. Then the other incoming passengers sit in their places if unoccupied or some other random seat if occupied.
The final aim is to find the probability with which the last passenger will not sit in his/her own seat. I haven't added the loop part yet which
would make it a proper simulation. The question above is actually a puzzle in probability. I am trying to verify the answer as I don't really follow the reasoning.
import random
from numpy import zeros
rand = zeros((100,3))
# The rows are : Passenger number , The seat he is occupying and if his designated seat is occupied. I am assuming that the passengers have seats which are same as the order in which they enter. so the 1st passenger enter has a designated seat number 1, 2nd to enter has no. 2 etc.
def cio(r): # Says if the seat is occupied ( 1 if occupied, 0 if not)
if rand[r][2]==1:
return 1
if rand[r][2]==0:
return 0
def assign(ini,mov): # The first is passenger no. and the second is the final seat he gets. So I keep on chaning the mov variable if the seat that he randomly picked was occupied too.
if cio(rand[mov][2])== 0 :
rand[mov][2] = 1
rand[mov][1] = ini
elif cio(rand[mov][2])== 1 :
mov2 = random.randint(0,99)
# print(mov2) Was used to debug.. didn't really help
assign(ini,mov2) # I get the error pointing to this line :(
# Defining the first passenger's stats.
rand[0][0] = 1
rand[0][1] = random.randint(1,100)
m = rand[0][1]
rand[m][2]= 1
for x in range(99):
rand[x+1][0] = x + 2
for x in range(99):
assign(x+1,x+1)
if rand[99][0]==rand[99][1] :
print(1);
else :
print(0);
Please tell me if y'all get the same error.. ALso tell me if I am breaking any rules coz thisi sthe first question I'm posting.. Sorry if it seems too long.
This is how it should've been...
The code does work fine in this case with the following mods :
def assign(ini,mov):
if cio(mov)== 0 : """Changed here"""
rand[mov][2] = 1
rand[mov][1] = ini
elif cio(mov)== 1 : """And here"""
mov2 = random.randint(0,99)
assign(ini,mov2)
I am using Python 2.6.6 on Windows 7, using a software from Enthought Academic Version of Python.
http://www.enthought.com/products/getepd.php
Also the answer to this puzzle is 0.5 which is actually what I am getting(almost) by running it 10000 times.
I didn't see it here but it had to be available online..
http://www.brightbubble.net/2010/07/10/100-passengers-and-plane-seats/
Recursion, while allowed, isn't your best first choice for this.
Python enforces an upper bound on recursive functions. It appears that your loop exceeds the upper bound.
You really want some kind of while loop in assign.
def assign(ini,mov):
"""The first is passenger no. and the second is the final seat he gets. So I keep on chaning the mov variable if the seat that he randomly picked was occupied too.
"""
while cio(rand[mov][2])== 1:
mov = random.randint(0,99)
assert cio(rand[mov][2])== 0
rand[mov][2] = 1
rand[mov][1] = ini
This may be more what you're trying to do.
Note the change to your comments. Triple-quoted string just after the def.
you may be able to find the exact solution using dynamic programming
http://en.wikipedia.org/wiki/Dynamic_programming
For this you will need to add memoization to your recursive function:
What is memoization and how can I use it in Python?
If you just want to estimate the probability using simulation with random numbers then I suggest you break out of your recursive function after a certain depth when the probability is getting really small because this will only change some of the smaller decimal places (most likely.. you may want to plot the change in result as you change the depth).
to measure the depth you could add an integer to your parameters:
f(depth):
if depth>10:
return something
else: f(depth+1)
the maximum recursion depth allowed by default is 1000 although you can change this you will just run out of memory before you get your answer

Categories

Resources