Python Syntax

homework
python

#1

Kindly help me execute this syntax
The questions says
Create a function get_algorithm_result to implement the algorithm below

Get a list of numbers L1, L2, L3....LN as argument Assume L1 is the largest, Largest = L1 Take next number Li from the list and do the following If Largest is less than Li Largest = Li If Li is last number from the list then return Largest and come out Else repeat same process starting from step 3

Create a function prime_number that does the following
Takes as parameter an integer and
Returns boolean value true if the value is prime or
Returns boolean value false if the value is not prime

but this is what i have written so far

def get_algorithm_result(numList):
    numblist = range(10)
    for i in numList:
        if L1 < Li:
            print (Largest = Li)
        elif Li is LN:
            return "Largest"
        continue

#2

@darotudeen,

the FUNCTION talk

def myFunc( param1, param2):
    # Begin of =myFunc= FUNCTION-BODY
    # this =myFunc= function- has 2 PARAMETERS param1 and param2
    # param1 and param2 PARAMETERS are used 
    # as -local- VARIABLES throughout the =myFunc= FUNCTION-BODY
    print( param1 + " and " + param2 )
    #End of =myFunc= FUNCTION-BODY

If you want to call/execute the myFunc function
you will have to add a pair of parentheses to myFunc
like
myFunc()
As the myFunc function was defined
as having 2 parameters
you have to provide 2 arguments
in our case 2 string VALUES "Alena" and "Lauren"
like
myFunc("Alena","Lauren")

some quotes from the outer-world:

**argument is the value/variable/reference being passed in,
parameter is the receiving variable used within the function/block**

OR

**"parameters" are called "formal parameters",
while "arguments" are called "actual parameters".**


#3

@darotudeen,

#Create a function get_algorithm_result
#    def get_algorithm_result()
#to implement the algorithm below

#Get a list of numbers L1, L2, L3....LN as argument
#the function takes a PARAMETER numlist 
def get_algorithm_result(num_list):
#Assume L1 is the largest,
    largest = 0
    # Largest = L1
    for item in range(0,len(num_list)):
        #Take next number Li from the list
        # num_list[item]
        #and do the following
        #If Largest is less than Li
        if largest < num_list[item]:
            #Largest = Li
            largest = num_list[item]
    #end of FOR-loop
    #If Li is last number from the list
    #then return Largest
    #using the return statement 
    # will cause you to leave this function
    return largest
    #and come out
    #
    #== i assume that the following
    #== is part of an other Instruction-set....
    #Else repeat same process starting from step 3
    
print( get_algorithm_result([4,7,1,9,12,2,1]) )


#Create a function prime_number that does the following
# def prime_number()
#Takes as parameter an integer 
def prime_number(integer):
#and 
#Returns boolean value true if the value is prime
#or 
#Returns boolean value false if the value is not prime
    if integer%2==0 and 2!=integer:
        return False
    else:
        return True

print( prime_number(6) )
print( prime_number(5) )

#4

Thanks a lot Leonhard
The code was ok when I ran but still did not pass the test according to the picture below
kindly help me we this.
.


#5

@darotudeen,
I cannot help you...


#6

ok thanks. I will just take it from here


#7

Well Li does probably mean current ListIndex so instead of using Li you might use your couting variable i that goes through your list. Also where does this come from and what does it have to do with your function?

Create a function prime_number that does the following
Takes as parameter an integer and
Returns boolean value true if the value is prime or
Returns boolean value false if the value is not prime


#8

def get_algorithm_result(listNumb):
largest = listNumb[0]
for i in range(1, len(listNumb)):
if largest < listNumb[i]:
largest = listNumb[i]
continue
return largest


#9

@devjumper86430,

You are not using indentations... and these are very important...

Please re-edit your Post

  • leave one blank-line above of your code
  • select your code in the Post
  • then =click= on the </>-symbol-of-this-editor

Your code will then be in a pre-code state
and you will be able to make/present the proper indentations.

or even better use
= http://discuss.codecademy.com/t/using-backticks-to-format-your-code/3697
[extra's]
https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet


#10

this will work fine
def get_algorithm_result(n):
if type(n) == type([]):
largest = n[0]
for item in n:
if largest < item:
largest = item
return max(n)
else:
pass
return largest

def prime_number(integer):
if integer%2==0 and 2!=integer:
return False
else:
return True


#11

I have a problem with this codes
it does not want to run
Here is the question

Create both a recursive function called recursive_factorial and iterative function called iterative_factorial that does the following

Accepts as parameter an Integer n
Computes the factorial of n
Returns the factorial of n

import unittest

class RecursiveTestCase(unittest.TestCase):

def test_recursive_factorial_one(self):
result = recursive_factorial(4)
self.assertEqual(result, 24, msg="Inaccurate value")

def test_recursive_factorial_two(self):
result = recursive_factorial(0)
self.assertEqual(result, 1, msg="Inaccurate value")

def test_iterative_factorial_one(self):
result = iterative_factorial(5)
self.assertEqual(result, 120, msg="Inaccurate value")

def test_iterative_factorial_two(self):
result = iterative_factorial(0)
self.assertEqual(result, 1, msg="Inaccurate value")

def recursive_factorial(n):
if n == 0:
return 1
else:
return n * recursive_factorial(n-1)

def recursive_factorial(n):

def iterative_factorial(n):

def iterative_factorial(n):

what should i do


#12

where is your function?


#14

What do you mean? This is post is older than 3 weeks and it was a question to the OP.


#15

def recursive_factorial (n):
if n == 0:
return 1
else:
return n *recursive_factorial(n-1)
def iterative_factorial (n):
result = 1
for i in range(1,n+1):
result *= i
return result
expectation = true
this was the 0ne i did but it keeps on showing this error maximum recursion depth exceded is the code wrong
pls help me review it if there is any error there


#16

please help me on this code it is giving a lot of headache


#17

hi bro!
I believe you have done Quiz 5 (Algo Lab). please help a brother here o, i have tried different method to write the code even when the test certify it...Ok, it still complains about spec

please help post it here.
Thanks


#18

`def get_algorithm_result(n):
if type(n) == type([]):
largest = n[0]
for item in n:
if largest < item:
largest = item
return max(n)
else:
pass
return largest

def prime_number(integer):
if integer%3==0 and3!=integer:
return False
else:
return True
this is the answer and aiso indent it


#19

Thank you, it works perfectly


#20

Hi @designninja41379 did you ever get a "An error occurred running your script" when answering questions in the ALGO LAB?

this is my code and i keep getting that error message.

def get_algorithm_result(num_list):
if type(num_list) == type([]) and num_list != []:
largest = num_list[0]
n = 0
while (n+1) < len(num_list):
if largest < num_list[n+1]:
largest = num_list[n+1]
n = n + 1
return largest
else:
pass


#21

def get_algorithm_result(n):
if type(n) == type([]):
largest = n[0]
for item in n:
if largest < item:
largest = item
return max(n)
else:
pass
return largest

def prime_number(integer):
if integer%3==0 and3!=integer:
return False
else:
return True

the above code didnt work for me and i am stuck
is there anything i'm missing?
please help me. My deadline is in 2 days