How do i implement these algorithms below

2024/11/22 12:21:51

Alogrithm 1:

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

Algorithm 2:

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

So far my code is :

def get_algorithm_result(num_list):    largest =num_list[0]        for item in range(0,len(num_list)):    if largest < num_list[item]:                largest = num_list[item]    return largestdef prime_number(integer):    if integer%2==0:return Falseelse:return True

After executing the code i get

"Test Spec FailedYour solution failed to pass all the tests" 

where am I going wrong?

Answer

Is what you mean with the first one find the largest number? Then you should use max() like

list = [1,2,4,5,3]
print max(list)
>>> 5

This should help with the second one:

def prime_number(n):if n > 1:for x in range(2,n):if (n % x) == 0:return Falsebreakelse:return True

If a number is prime, then the factors are only 1 and itself. If there is any other factor from 2 to the number, it is not prime. n % x finds the remainder when n is divided by x. If x is a factor of n, then n % x has a remainder of 0.

https://en.xdnf.cn/q/120692.html

Related Q&A

How to run a shell script once a day?

I am trying to run this particular shell script only one time, daily. Heres my code for runLucene.py:#!/usr/bin/env pythonimport os from extras.download_datos_desambiguar import news_Lucenex=datetime.t…

How to fix Error: Please select a valid Python interpreter in Pycharm?

Error:Error: Please select a valid Python interpreterScreenshot:How to fix this?

Tuples conversion into JSON with python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 8 years ago.Improve…

Python continue with while [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

Create MySQL database with python [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Basic Python repetition exercise

I was trying to learn Python when I came upon this question. I am not asking for you to answer the question for me, I just need some help. Note: I am only allowed to use loops and if statements etc. No…

Python 3.3 socket programming error [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.Questions asking for code must demonstrate a minimal understanding of the problem being solved. Incl…

ValueError: math domain error

I wrote this codedef partE():e = 3 * 10 // 3 + 10 % 3print("e).", e)partE()and python comes back with this error message when I try to run it. I do not understand why. Can someone please expl…

how to access objects in python with for loop in different files

This is my file1.json: {"count": 1,"next": null,"previous": null,"results": [{"id": 5883,"url": "https://some.api.com/api/ipam/ip-addres…

multiplicative digital root of a number using loops

I need to find the multiplicative digital root of a number in python using only loops.something that does the same as the below code but using loops:print("multiplicative digital root of a number …