Hello. Found a nice python script that gives ways to sum to N and prints these arrays. I have a minor issue with it. I need these arrays
in descending order. So instead of 1 1 1 2 2 3 I need it like this 3 2 2 1 1 1.
Already made a python script and I'll try to merge these two in order to see which array is better. Any idea? 💡Tnx
Here's how it looks.
# Python3 program for the above approach
# Function to print the values stored
# in vector arr
def printVector(arr):
if (len(arr) != 0):
# Traverse the vector arr
for i in range(len(arr)):
print(arr[i], end = " ")
print()
# Recursive function to print different
# ways in which N can be written as
# a sum of at 2 or more positive integers
def findWays(arr, i, n):
# If n is zero then print this
# ways of breaking numbers
if (n == 0):
printVector(arr)
# Start from previous element
# in the representation till n
for j in range(i, n + 1):
# Include current element
# from representation
arr.append(j)
# Call function again
# with reduced sum
findWays(arr, j, n - j)
# Backtrack to remove current
# element from representation
del arr[-1]
# Driver Code
if __name__ == '__main__':
# Given sum N
n = 8
# To store the representation
# of breaking N
arr = []
# Function Call
findWays(arr, 1, n)
change the "printVector" function to traverse the array in reverse
def printVector(arr):
if len(arr) != 0:
arr = arr[::-1]
for i in range(len(arr)):
print(arr[i], end=" ")
print()
Thank you! It worked like a charm 💯 👍
Hello again. I have one more question. I'm having this python input syntax in my script.
m7, m6, m5, m4 = map(int, input().split())
Now this works very well but script expects 4 inputs. All I want is if only one input is provided the rest to be considered input = 0.
e.g. instead of input 3 0 0 0 just input 3 meaning m7=3 and the rest m6=m5=m4=0
I just need to reduce to a minimum what user has to input to make the script more slick and not that boring. Found a way but wasn't
that compact and looked bloated/impractical so I thought to ask python guys.
Tnx in advance 🤞🏻 😺
@suren ☮️
method 1:
var1 = inputs[0] if len(inputs) >= 1 else 0
var2 = inputs[1] if len(inputs) >= 2 else 0
var3 = inputs[2] if len(inputs) >= 3 else 0
var4 = inputs[3] if len(inputs) >= 4 else 0
method 2:
variables = [0, 0, 0, 0]
for i in range(min(len(inputs), len(variables))):
variables[i] = inputs[i]
glad to help
p.s.
python does not make any sense
fib = {1: 1,2: 1,3: 2,4: 3}
print(fib.get(4, 0) + fib.get(7, 5))
Tried method 2 but saw it has indents so idk where that should go. From my tries also complains about inputs and not input.
Think I have to perform surgery on this
1st
m7, m6, m5, m4 = map(int, input().split())
Tried this approach
inp = int(input("Enter the inputs : ") or "0")
but could not see how to make it work with the 1st or combine them.
Yeah it's kinda autistic programming language. Don't understand how such simple thing is so insurmountable hard to do at least at my
n00b level.
The following is the full implementation of two methods. depending on either line 55 or 54 is commented.
code still need to be modified to use all the input, this only calculate "printWays" for first input. but hold the in input values in var1, var2, var3, var4. also in "variables" array.
note: in both methods you are limited to a maximum 4 input in line.
# Function to print the values stored
# in vector arr in descending order
def printVector(arr):
if len(arr) != 0:
# Reverse the order of elements
arr = arr[::-1]
# Traverse the vector arr
for i in range(len(arr)):
print(arr[i], end=" ")
print()
# Recursive function to print different
# ways in which N can be written as
# a sum of at 2 or more positive integers
def findWays(arr, i, n):
# If n is zero then print this
# ways of breaking numbers
if n == 0:
printVector(arr)
# Start from previous element
# in the representation till n
for j in range(i, n + 1):
# Include current element
# from representation
arr.append(j)
# Call function again
# with reduced sum
findWays(arr, j, n - j)
# Backtrack to remove current
# element from representation
del arr[-1]
# Driver Code
if __name__ == '__main__':
# Input statement
inputs = list(map(int, input().split()))
# Assigning values to variables
var1 = inputs[0] if len(inputs) >= 1 else 0
var2 = inputs[1] if len(inputs) >= 2 else 0
var3 = inputs[2] if len(inputs) >= 3 else 0
var4 = inputs[3] if len(inputs) >= 4 else 0
# Assigning values to variables
variables = [0, 0, 0, 0]
for i in range(min(len(inputs), len(variables))):
variables[i] = inputs[i]
# Given sum N
#n = var1
n = variables[0]
# To store the representation
# of breaking N
arr = []
# Function Call
findWays(arr, 1, n)
Forgot to add second script. So here it is. I need to just input some values, if no other input those values should be =0
It's very annoying to have to input 0 values (too much user input) so if I provide no input, 0 should be considered.
print("enter your T and S")
T, S = map(int, input().split())
print("your ems")
print("insert your ems, insert 0 where's not the case, e.g. 3 0 0 0")
m7, m6, m5, m4 = map(int, input().split())
print("insert your ens, insert 0 where's not the case, e.g. 2 0 0 0")
print("your ens")
n7, n6, n5, n4 = map(int, input().split())
ens = int(n7)+int(n6)+int(n5)+int(n4)
kmn = int(m7)*int(n7)+int(m6)*int(n6)+int(m5)*int(n5)+int(m4)*int(n4)
Z = int(ens)+(S-kmn)
print('Your Z is:',int(Z))
import math
a = math.factorial(T)
b = math.factorial(T-Z)
c = math.factorial(S)
d = math.factorial(m7)**n7*math.factorial(n7)*math.factorial(m6)**n6*math.factorial(n6)*math.factorial(m5)**n5*math.factorial(n5)*math.factorial(m4)**n4*math.factorial(n4)*math.factorial(S-kmn)
e = int(a)/int(b)
f = int(c)/int(d)
g = int(e)*int(f)
print(format(g, "e"))
print(f"{g:,}")
Think I got it
the first two methods can still work here, but I would use another method here
ems_inputs = list(map(int, input().split())) + [0, 0, 0, 0] # if this has one input, the values in "ems_inputs" will be [8, 0, 0, 0, 0]
m7, m6, m5, m4 = ems_inputs[:4] # and this will be m7 = 8, m6 = 0, m5 = 0, m4 = 0
replace this with
m7, m6, m5, m4 = map(int, input().split())
Yeah but this way the script appends another m instead of having 4 I'm having 5
Also there's a second round of inputs of ens that the script needs.
Wonder if it's possible to make it for an undetermined number of ems so not be capped at 4 values in case there are more needed.
Same for ens. But I guess is simply pure rocket science 🚀
what you want is normally achieved with array to store values
and to access values you can use a for loop, the general idea is like this:
user_input = input("Enter integers separated by spaces: ")
values = list(map(int, user_input.split()))
for value in values:
print("Processing value:", value)
and if you need to iterate two array and do calculations against each other, you can you a module called "itertools"
here I have a snippet for you
from itertools import zip_longest
user_input1 = input("Enter integers for the first array separated by spaces: ")
array1 = list(map(int, user_input1.split()))
user_input2 = input("Enter integers for the second array separated by spaces: ")
array2 = list(map(int, user_input2.split()))
for value1, value2 in zip_longest(array1, array2, fillvalue=0):
print("Processing values:", value1, value2)
so just change the print inside the for loop to function you need
this works too:
user_input1 = input("Enter integers for the first array separated by spaces: ")
array1 = list(map(int, user_input1.split()))
user_input2 = input("Enter integers for the second array separated by spaces: ")
array2 = list(map(int, user_input2.split()))
max_size = max(len(array1), len(array2))
array1 = array1 + [0] * (max_size - len(array1))
array2 = array2 + [0] * (max_size - len(array2))
print (array1)
print (array2)
Great I'm gonna take it little easy cos it's hard to digest so much python 😬 in such short period of time. Sure I'm gonna find the better
way out of all these examples/methods you gave here. And thank you very much 👍. Primarily indentations are those that scares me the most, but I'll get their trick soon 🤞🏻.
Have fun coding,
print("enter your T and S")
T, S = map(int, input().split())
print("your ems")
print("insert your ems, insert 0 where's not the case, e.g. 3")
array1 = list(map(int, input().split()))
print("insert your ens, insert 0 where's not the case, e.g. 2")
print("your ens")
array2 = list(map(int, input().split()))
# Resize the arrays by filling with zeros
array1 = array1 + [0, 0, 0, 0]
array2 = array2 + [0, 0, 0, 0]
m7, m6, m5, m4 = array1[:4]
n7, n6, n5, n4 = array2[:4]
ens = int(n7) + int(n6) + int(n5) + int(n4)
kmn = int(m7) * int(n7) + int(m6) * int(n6) + int(m5) * int(n5) + int(m4) * int(n4)
Z = int(ens) + (S - kmn)
print('Your Z is:', int(Z))
import math
a = math.factorial(T)
b = math.factorial(T - Z) if T - Z >= 0 else 1 # Add check for non-negative argument
c = math.factorial(S)
d = (
math.factorial(m7) ** n7
* math.factorial(n7)
* math.factorial(m6) ** n6
* math.factorial(n6)
* math.factorial(m5) ** n5
* math.factorial(n5)
* math.factorial(m4) ** n4
* math.factorial(n4)
* math.factorial(S - kmn) if S - kmn >= 0 else 1 # Add check for non-negative argument
)
e = int(a) / int(b)
f = int(c) / int(d)
g = int(e) * int(f)
print(format(g, "e"))
print(f"{g:,}")
Absolute perfection! Works exactly how it should 🎯 👍 Now there's a need just for a minimum input from user. Thank you 💯 🤩
Hmm, it looks like the script messes the d.
It doesn't give same results when i'm doing same calc on paper. Think those factorials aren't grouped in the way the formula intended.
(https://i.postimg.cc/d1mXPc3f/Screenshot-2023-07-09-16-34-42.png)
edit: discovered while checking things that there's no error it's just that for large numbers python precision drops. So I got it, sorry.
I forgot how this script works, and I don't understand what each variable meant to represent.
can you give me the formula or explain each of these variables and what's their purpose, like z, d, ems, ...
It's ok the script but because large numbers precision drops. How can I increase its precision? I've verified it but needs to have higher precision.
After 15-17 digits python lose accuracy.
For example A(94,12)=94!/82!= 228.743.740.236.102.111.820.800 in gnome-calculator
but in my script gives 94!/82!= 228.743.740.236.102.111.330.304 in the script
It loses about 490.496
And because the script has not one but two places where divides huge numbers (depending what's used) things lose accuracy each time it calculates numbers exceeding 17 digits.
You can check it out here (https://forum.artixlinux.org/index.php/topic,4690.msg34170.html#msg34170)
might be because
print("is 0.1+0.2 equal to 0.3")
print (0.1+0.2==0.3)
print("=================")
print("9999999999999999.0-9999999999999998.0=",9999999999999999.0-9999999999999998.0)
print ("================")
print ("what the hell??????")
#this mistake cause by binary system that defined by IEEE754
#you can read about it in (https://en.wikipedia.org/wiki/IEEE_754?wprov=sfla1)
or might not be
for example:
import math
result = math.factorial(94) / math.factorial(82)
print(result)
gives:
2.287437402361021e+23
that is equal to:
228743740236102111330304
so you see there's no problem with code, but with python itself (or all PL in general)
this might come handy if you want to fix this: percition py (https://docs.sympy.org/latest/index.html)
the question is, are you willing to rewrite the code in this library
Yeah, no code problem I realize after calculated by hand with gnome calculator. You need to choose the option 'fixed' from gnome calc to have the whole number and not cropped. Tried to make a 94^16 that gives about 231 slices and after trying to add slices results it gave some trillion in minus. When using the script in the lower order of magnitude range always it adds up exactly to T^S.
Also awk summation function when using big numbers has huge margin of error similar with python.
I would wanna rewrite it if I would know how :D simply python rounds every number beyond 10^15 .
Also python tricked me cos at start I gave big credit those numbers, thought are perfection but those were rounded pretty nasty.
well, I got an amazing news for you.
I just remembered something.
import mpmath
# Set the desired precision
mpmath.mp.dps = 50
result = mpmath.factorial(94) / mpmath.factorial(82)
print(result)
yupiii, I'm going to try an see how it performs 🥳 tnx a lot 🙏🏻
Haha, just tested it and it works like Swiss watch. 8) Tnx once again and a big round of applaud 👏🏻👏🏻👏🏻
Had to choose precision, not 50 not 100 but no more no less than 1000 and now gives exact results (https://github.com/B1u3l1ght/Old-School-Pass/blob/main/94x16) what formula should give.
All slices summed gives 🎯 94^16 🎯 mapped a 16 long pwd with a 94 search depth (without space char).
With 100 precision still lost 22 out of 94^16