Python Forum
numerical analysis - python program
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
numerical analysis - python program
#1
Hello
I am preparing for a test in numerical analysis and I am new to python and therefore dont know the syntax and all stuff. If anyone can make this program for me it will help a lot as I could use it as a template on the other numerical analysis problems. I know is sounds weird but the other numerical analysis probles is also the same type of problems. I have this following python code:

import auto_test_tools as att


'''

Implement a function that can compute the elements of the recurrence
relation, that we mathematically define as follows

    H(n) = 2 H(n-1) + 1
    H(1) = 1

The function should take as input, N, the number of elements to compute
using the recurrence relation and as output the procedure should return
the list H[0], H[1], H[2],..,H[N-1]. Observe that Python indexing is
different from the mathematical definition given above. Your code must
deal with this.

Implement both a recursive and iterative version of the function.

ADVICE: Submit your solution even if your code is
not running or your score is less than 100%

'''


def generate_from_relation_recursively(N: int, n: int = 0, H=[]) -> list:
    """
    Generate a list of elements from a given recurrence relation using a recursive approach.

    :param N:    The number of elements to generate.
    :param n:    The current index of the element that should be generated.
    :param H:    The current elements that have been generated (holds n-1 elements when invoked).


    :return:        The resulting data list after having added the n-element to the list
    """
    return []  # TODO Write your own code here!


def generate_from_relation_iteratively(N: int) -> list:
    """
    Generate a list of elements from a given recurrence relation using an iterative approach.

    :param N:    The number of elements to generate.


    :return:        The resulting list
    """
    return []  # TODO Write your own code here!


att.start()

att.begin_task('Task 1')
y = [1, 3, 7, 15, 31, 63, 127, 255, 511, 1023]
x = generate_from_relation_recursively(10)
att.list_is_close(x, y, 10e-7, att.get_linenumber(), ' recursive test failed')
att.end_task()


att.begin_task('Task 2')
z = generate_from_relation_iteratively(10)
att.list_is_close(z, y, 10e-7, att.get_linenumber(), ' iterative test failed')
att.end_task()


att.stop()
Reply
#2
(Sep-18-2019, 12:34 AM)restingquarterback Wrote: I am new to python and therefore dont know the syntax and all stuff

How would you do it in a different language?
Or, if you don't know any languages, write plain English to describe how you think it should work (pseudocode).

The whole point of the exercise is to get you to think about the problem. The language you use to express the solution is (or should be) unimportant toward that goal. It's therefore not in our interest to just give you a solution if you don't already understand the reasoning behind it.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020