#🔒 im trying to define a var inside a function that doesnt explicitly return the var

66 messages · Page 1 of 1 (latest)

wanton vine
#
#im using a midpoint reimann sum casue the asnwer will be natural numbers
#8/n*sum(k=1 to n, sqrt(k*(n-k))
#        n
#  8    _____
#_____  \          ___________
#  n     /        \|k ( n - k )
#       _____
#        k = 1
from logic import add,subtract,divide,multiply

def binary_search(f, a, b, tol=1e-7):
    if multiply(f(a),f(b)) >= 0: raise ValueError()
    while abs(subtract(b,a) > tol:
        mid = divide(add(a,b),2)
        a, b = (mid, b) if multiply(f(mid),f(a)) < 0 else (a, mid)
    return divide(add(a,b),2)

def summation(start, end, function):
    return sum(function(index) for index in range(start, end + 1))
def func_sum(n):
    
    
def ans(k):
   def riemann(x):
      multiply(x,x)-multiply(k,subtract(n,k))
   binary_search(riemann,1,multiply(k,subtract(n,k)))
summation(1, n, rei
regal lindenBOT
#

@wanton vine

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

hearty ore
#

hello!

wanton vine
#

yo

hearty ore
#

what is your problem?

wanton vine
#
def ans(k):
   def riemann(x):
      multiply(x,x)-multiply(k,subtract(n,k))
   binary_search(riemann,1,multiply(k,subtract(n,k)))
summation(1, n, riemann

thisline

#

i need to use a var in the reimann function without returning it inside the func

hearty ore
#

i assume the indentation is discord's fault

wanton vine
#

yes

hearty ore
#

ik the answer

#

buttttt

#

i dont wanna spoon feed

#

google "python anynomous functions"

#

that could help :)

spark scarab
#

you don't need to worry about spoonfeeding this guy

wanton vine
#

ima just do a global var

hearty ore
#

JUST USE LAMBDA

#

OMG

#

LAMBDA: RIEMANN(ARGS)

hearty ore
#

. . . . .

spark scarab
# wanton vine what?

I was saying you're more than capable of taking suggestions and learning from it.

wanton vine
#

thanks

hearty ore
#

binary_search(lambda: riemann(your_arguments_here),1,multiply(k,subtract(n,k)))
lambda is an anynomous function, so you basically create a function to call a function

wanton vine
#

casue i saw someone say i should just define it instead of lambda

spark scarab
wanton vine
#

yeah thats diffrent

#

ima use lambda anywasy

hearty ore
#

do you know lambdas?

wanton vine
#

i tore my hair out aobut lambda

#

but yes

hearty ore
#

it behaves a lot like normal functions, but types etc are less explicit

spark scarab
#

IMO it can make code messy if the lambda gets to be too complex

hearty ore
#

lambda a, b: a | b returns a union of a and b which can be provided as keyword or positional arguments

hearty ore
#

use lambda

wanton vine
#

this is so confusing im trying to use binary search to solve a sqrt in sokme code and constantly increment a value each time its run

#

brb

spark scarab
#

note that it was my opinion. I wasn't stating a hard fact

hearty ore
#

mainly for Callable parameters of lib's functions

#

like tkinter

#

hold on lemme make an example

spark scarab
#

i don't need an example. I'm well aware of good use cases for lambda

wanton vine
#

bro

spark scarab
#

i'm also quite aware of cases where lambdas make code messy

wanton vine
#

look at esoteric python

spark scarab
#

that's all i'm saying

wanton vine
#

it can make code messy and people do abuse the rediculous look of pure lambda code

hearty ore
#
import tkinter as tk
window = tk.Tk()
button = tk.Button(window, text="say hello", command=lambda: print("Hello world!"))
button.pack()
window.mainloop()
``` something like this could occur
#

i wanted to try lambda: window.destroy() but it doesnt need any arguments

wanton vine
#

its doneeee

#
#im using a midpoint reimann sum casue the asnwer will be natural numbers
#8/n*sum(k=1 to n, sqrt(k*(n-k))
#        n
#  8    _____
#_____  \          ___________
#  n     /        \|k ( n - k )
#       _____
#        k = 1
from logic import add, subtract, divide, multiply

def binary_search(f, a, b, tol=1e-7):
    if multiply(f(a), f(b)) >= 0:
        raise ValueError("Function must have different signs at the endpoints.")
    while abs(subtract(b, a)) > tol:
        mid = divide(add(a, b), 2)
        if multiply(f(mid), f(a)) < 0:
            b = mid
        else:
            a = mid
    return divide(add(a, b), 2)

def summation(start, end, function):
    return sum(function(index) for index in range(start, end + 1))

def func_sum(n):
    return summation(1, n, lambda k: sqrt(multiply(k, subtract(n, k))))

def ans(n):
    def riemann(k):
        return multiply(8, divide(func_sum(n), n))
    return binary_search(lambda x: subtract(riemann(x), x), 0, n)

# Example usage:
n = 10  # Replace with your desired value of n
result = ans(n)
print("Approximate square root:", result)
hearty ore
#

gg

wanton vine
#

finally

hearty ore
#

:D

spark scarab
#

nice

wanton vine
#

wai nvm

#

i forgot to imple,ment sqrt

#

ok closed i guess

#

wai nvm

#
#im using a midpoint reimann sum casue the asnwer will be natural numbers
#8/n*sum(k=1 to n, sqrt(k*(n-k))
#        n
#  8    _____
#_____  \          ___________
#  n     /        \|k ( n - k )
#       _____
#        k = 1
from logic import add, subtract, divide, multiply
sqrt = binart_search(lambda x: x**2 - multiply(k, subtract(n, k))
def binary_search(f, a, b, tol=1e-7):
    if multiply(f(a), f(b)) >= 0:
        raise ValueError("Function must have different signs at the endpoints.")
    while abs(subtract(b, a)) > tol:
        mid = divide(add(a, b), 2)
        if multiply(f(mid), f(a)) < 0:
            b = mid
        else:
            a = mid
    return divide(add(a, b), 2)

def summation(start, end, function):
    return sum(function(index) for index in range(start, end + 1))

def func_sum(n):
    return summation(1, n, lambda k: sqrt(multiply(k, subtract(n, k))))

def ans(n):
    def riemann(k):
        return multiply(8, divide(func_sum(n), n))
    return binary_search(lambda x: subtract(riemann(x), x), 0, n)

# Example usage:
n = 10  # Replace with your desired value of n
result = ans(n)
print("Approximate square root:", result)

i have to access the "k" and "n" var in the "sqrt =" line so i can properly change what the lambda function outputs but if i change it my algorithm doesnt work

regal lindenBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.