Split a number per unit of token python function [closed] - python-3.x

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I'm trying to create a function that receive as argument a number and return an array of 3 numbers max.
I have 3 tokens. 1 unit, 5 unit and 25 unit.
calculateUnit(4) should = [4,0,0]
calculateUnit(7) should = [2,1,0] (because 2 unit of 1 and 1 unit of 5 = 7)
calculateUnit(36) should = [1,2,1] (because 1 unit of 1, 2 unit of 5 and 1 unit of 25 = 36)
I have a basic code and I think I need to use modulo division, I already tried to search here and every other resources I have but I may not use the correct terms.

You can reduce your solution to:
def convertInToken(am):
return [am//25, (am%25)//5, am%5]
This leverages integer-division (3.x upwards, also named floor division) and modulo division.
Floor division returns the full integer that woud have been returned if you did a normal division and floored it.
Modulu division returns the "remainder" of a division.

I managed to do that, but thanks anyway :)
# your code goes here
import math
def convertInToken(am):
result = [];
#need to use 25
if am >= 25:
amount25 = math.floor((am/25))
amount5 = math.floor((am-(amount25*25))/5)
amount1 = math.floor(((am-(amount25*25)-(amount5*5))/1))
result = result+[amount1]
result = result+[amount5]
result = result+[amount25]
#need to use 5
elif am >= 5:
amount5 = math.floor((am/5))
amount1 = math.floor(((am-(amount5*5))/1))
result = result+[amount1]
result = result+[amount5]
result = result+[0]
#need to use 1
elif am < 5:
result = result+[am]
result = result+[0]
result = result+[0]
return result
print(convertInToken(4))
print(convertInToken(7))
print(convertInToken(12))
print(convertInToken(37))

Related

code for input of small numbers: can any please explain why is not iterating after the second input? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
This is the code I came up with. It is not iterating after the second input, it's only printing after 3rd, why is it so?
smallest = None
while True:
num = input('Enter a number:')
if smallest is None:
smallest = num
continue
elif num < smallest:
smallest = num
print(smallest)
elif num == "done":
break
The problem with your code is that you are taking input.
input() function in python takes input as a string, so you need to typecast this. I am assuming you wanted to compare the integral values so for that, you should use instead int(input("YOUR MSSG")) or if you want to typecast to float then just replace int with float.
Scenarios where your code will show awkward behavior: -
-> Let's say that you want to compare 11 and 2, then if you consider this as an integer, obviously 11 > 2 but when you consider the same thing as a string and do a string comparison then you will see that "11" < "2". As the code in the question is taking the inputs as string and doing the string comparison that's why you are not getting the expected result.
The below code should work perfectly for you: -
smallest = None
while True:
num = int(input('Enter a number:'))
if smallest is None:
smallest = num
continue
elif num < smallest:
smallest = num
print(smallest)
elif num == "done":
break
You can modify the code as per your requirement.
As Brian said input returns a string, so you would have to pass elif int(num) < int(smallest): to allow it to compare the values as numbers.
If you do this however I would recommend moving elif num == "done": before that statement as int("done") will otherwise cause an error.

Python: Separate things of string [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have the following string in my database: items = '1;2 | 3;4'
And I have to get information, like quantity and product, separating the information, I have to get the following, for example:
1;2 = Product 1, quantity 2
3;4 = Product 3, quantity 4
I tried anyway with split, partition with join, but it doesn't work.
I can get the first one with [0], but if there are others in the string I can't do the transformation.
x = "1;2 | 3;4" # string
data = x.split("|") # data = ["1;2", "3;4"]
data = [i.split(";") for i in data] # data = [["1","2"], ["3","4"]]
data = [[int(i) for i in j] for j in data] # converts strings to ints
Now, data[0][0] gives the number of the first product and data[0][1] gives the quantity of the first product. Similarly for data[1][0] and data[1][1]
Edit
To clarify Andre's comment:
x = '1;2 | 2;4 | 3;8 | 45;1'
data = x.split("|")
data = [i.split(";") for i in data]
data = [[int(i) for i in j] for j in data]
for i in data:
print("Product number: " + str(i[0]))
print("Quantity: " + str(i[1]))
txt = '1;2 | 3;4'
x = txt.split('|')
for i in x:
print(i.split(';'))

Out of bounds error due to list assignment? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
array1 = []
array2 = []
size = int(input("Enter the size of the array: "))
print("Enter the elements in the array: ")
for item in range(size):
element = int(input())
array1.append(element)
array2[0] = -1
for item in range(1, 8):
x = item-1
if array1[item] < array1[x]:
array2.append(-1)
elif array1[item] > array1[x]:
array2.append(array1[x])
elif array1[item] == array1[x]:
array2.append(array2[x])
print(array2)
Expected output: a proper execution of the code
Received output:
Traceback (most recent call last)
array2[0] = -1
IndexError: list assignment index out of range
First of all, note that your problem is in array2, so most of the code is superfluous to this issue. The minimal, complete example to reproduce your error is:
array2 = []
array2[0] = -1
When looking at this example, it's easier to see the problem - array2 is initialized with size 0, so its 0-th index is already out of bound.
Going back to your code, you can just initialize it as array2 = [-1] instead of how it's written - after all, array2[0] = -1 is the first time that array2 is accessed. Or you can change the array2[0]=-1 to an .append.
u set array2 as [],so when u define array2[0] = -1 the index is out of range

Is there a way to re-write math.ceil function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
For this function, I must find all the "Egyptian fractions" that add up to a fraction using a greedy algorithm. All Egyptian fractions have a numerator value of 1, they are distinct, and the sum = (numerator/denominator). I understand how to find one using division and math.ceil. However, the function never seems to resolve after I try with values for numerator and denominator. Is there a way to re-write my code using no division (i.e. no ceiling, division, or floor), just multiplication and subtraction? I can assume the numerator is always < denominator, and both are positive integers.
def egypt(numerator, denominator):
fracs = []
while numerator != 0:
n = int(numerator)
d = int(denominator)
c = math.ceil(d / n)
fracs.append(c)
n = (c*n) - d
d = c*d
return fracs
math.ceil is OK. The problem is that you reinitialize the cycle each time. Here is the fixed function:
def egypt(numerator, denominator):
fracs = []
n = int(numerator)
d = int(denominator)
while n != 0:
c = math.ceil(d / n)
fracs.append(c)
n = (c*n) - d
d = c*d
return fracs

Answer by python language [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
A positive integer n is said to be perfect if the sum of the factors of n, other than n itself, add up to n. For instance 6 is perfect since the factors of 6 are {1,2,3,6} and 1+2+3=6. Likewise, 28 is perfect because the factors of 28 are {1,2,4,7,14,28} and 1+2+4+7+14=28.
Write a Python function perfect(n) that takes a positive integer argument and returns True if the integer is perfect, and False otherwise.
Here are some examples to show how your function should work.
perfect(6)
True
perfect(12)
False
perfect(28)
True
def perfect(x):
factor_sum = 0
for i in range(1, x-1):
if x % i == 0:
factor_sum = factor_sum + i
if(factor_sum == x):
return True
return False
print perfect(6) #Prints True
print perfect(12) #Prints False
print perfect(28) #Prints True

Resources