Skip to main content
Topic solved
This topic has been marked as solved and requires no further attention.
Topic: [SOLVED] Cool python script (Read 1113 times) previous topic - next topic
0 Members and 3 Guests are viewing this topic.

[SOLVED] Cool python script

 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.

Code: [Select]
# 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)

Re: Cool python script

Reply #1
change the "printVector" function to traverse the array in reverse

Code: [Select]
def printVector(arr):
if len(arr) != 0:
arr = arr[::-1]
for i in range(len(arr)):
print(arr[i], end=" ")
print()

Re: Cool python script

Reply #2
 
 Thank you! It worked like a charm 💯 👍

Re: [SOLVED] Cool python script

Reply #3

Hello again. I have one more question. I'm having this python input syntax in my script.
Code: [Select]
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  ☮️

Re: [SOLVED] Cool python script

Reply #4
method 1:
Code: [Select]
    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:
Code: [Select]
    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
Code: [Select]
fib = {1: 1,2: 1,3: 2,4: 3}
print(fib.get(4, 0) + fib.get(7, 5))

Re: [SOLVED] Cool python script

Reply #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 
Code: [Select]
 m7, m6, m5, m4 = map(int, input().split())
Tried this approach 
Code: [Select]
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.


Re: [SOLVED] Cool python script

Reply #6
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.

Code: [Select]
# 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)

Re: [SOLVED] Cool python script

Reply #7
 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.

Code: [Select]
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:,}")

Re: [SOLVED] Cool python script

Reply #8

 Think I got it

Re: [SOLVED] Cool python script

Reply #9
the first two methods can still work here, but I would use another method here
Code: [Select]
    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

Code: [Select]
m7, m6, m5, m4 = map(int, input().split())

Re: [SOLVED] Cool python script

Reply #10

  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 🚀

Re: [SOLVED] Cool python script

Reply #11
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:
Code: [Select]
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
Code: [Select]
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

Re: [SOLVED] Cool python script

Reply #12
this works too:
Code: [Select]
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)


Re: [SOLVED] Cool python script

Reply #13
 
 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 🤞🏻.

Re: [SOLVED] Cool python script

Reply #14
Have fun coding,

Code: [Select]
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:,}")