Find matching numbers in an array and print result to new array [closed] - python

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 reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Trying to figure out why the code I have produced is not the answer to the following question:
Given two arrays, one with user inputs and another of correct inputs, return an array that denotes when the user input matches the correct input with the number 1, and incorrect inputs with -1.
My code below
newArr = []
def correct_stream(user, correct):
for i in user:
for j in correct:
if i == j:
newArr.append(1)
else:
newArr.append(-1)
return newArr

Your return statement is inside of your second for loop and will only execute once.
newArr = []
def correct_stream(user, correct):
for i in user:
for j in correct:
if i == j:
newArr.append(1)
else:
newArr.append(-1)
return newArr #this executes after the if statement runs only once, leading to an array with size 1
Python is built upon proper syntax, and while some aspects are flexible, indentation is strict. Brackets don't split apart scopes, indentation does.
newArr = []
def correct_stream(user, correct):
for i in user:
for j in correct:
if i == j:
newArr.append(1)
else:
newArr.append(-1)
return newArr #this now executes after the whole for loop above is finished

Related

How can I solve the functions of large number problem in 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 reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I'm trying to find the factors of 600851475143. my code isn't working.
def factor(a):
factor = []
for i in range (1, a+1):
if(a%i==0):
factor.append(i)
print(factor)
factor.clear()
factor(600851475143)
You can use return keyword to return a list of factors.
def factor(a):
factor = []
for i in range (1, a + 1):
if a % i == 0:
factor.append(i)
return factor
print(factor(int(input())))
Your code have some problems, first, the if statement in python don't use (), it should be only if a%i == 0:. Your code is not correctly indented, the code bellow the function is not part of it. You made a global variable with the same name as the function name, they overwrite each other.If you want the function to complete:
factorlist = []
def factor(a):
for i in range(1, a + 1):
if a % i == 0:
factorlist.append(i)
factor(input("Fators of: "))
print(factorlist)
factorlist.clear()
If you want to return the end list, use the #Ratery 's code.
First of all the indentation is not correct. You've to indent the inner code of the function correctly. It should come inside the defining statement. The next thing is, you're printing the factor list outside the function which is a local variable and not global variable. If you want to print it, add a print statement inside the function and then call the function. Your corrected code:
def factor(a):
factor_1= []
for i in range (1, a+1):
if(a%i==0):
factor_1.append(i)
print(factor_1)
factor(600851475143)
Also, don't keep your function name and the list name same. It might give error
Your function should return the list of factors (not print it). Because you are going through all potential divisors from 1 to a, it will take along time to produce a result with a very large number such as 600851475143.
To make this run faster, you can extract the factors in pairs so that you only have to look at divisors up to the square root of your number. Every time you find a factor (f) of the number (N), you know there is a corresponding factor which is the result of dividing N by f (N/f). This means that every factor <= √N has a corresponding one that is >= √N so you can get them in pairs and only check the divisors <= √N
def factors(N):
return [f for d in range(1,int(N**0.5)+1) if N%d==0 for f in {d,N//d}]
Output:
print(factor(600851475143))
[1, 600851475143, 8462696833, 71, 716151937, 839, 408464633, 1471,
6857, 87625999, 59569, 10086647, 104441, 5753023, 1234169, 486847]

How to code this condition in Python? I am new to 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 reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I am new to python. so any help will be appreciated.
I have two arrays A = [1,2,4,2,3,5,3] and B = [0,4,4,4,1,1,1]
for the function if I give A, B as input then I should get output as = [1,(2+4+2),(3+5+3)] = [1,8,11](if numbers are repeating in B then corresponding values in A should be added together).
This should do the trick:
def bla(list1:list, list2:list):
prev = list2[0] - 1
final_list = []
for ele, pos in zip(list1, list2):
if prev != pos:
final_list.append(ele)
else:
final_list[-1] += ele
prev = pos
return final_list

I'm returning a defaultdict(list), but randomly choosing between the two, why does it return nothing sometimes? [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 reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
output = ""
numberList = [0, 1]
print(random.choice(numberList))
if(random.choice(numberList) == 0):
if len(slots) > 0:
output = templates[state][0].replace("<num_classes>", str(slots[0][1]))
else:
output = templates[state][0]
elif(random.choice(numberList) == 1):
if len(slots) > 0:
output = templates2[state][0].replace("<num_classes>", str(slots[0][1]))
else:
output = templates2[state][0]
return output
expected answer
sometimes get this
Sometimes it returns nothing or no dictionary... Why?
With
elif(random.choice(numberList) == 1)
you will again choose a brand new random number. And if that isn't 1 then there's no else that will set output.
Instead of elif you should have a plain else.

While loop testing equality with 0 will not break [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 reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
This while loop won't end. Any suggestions for this Python program?
[Edit: Upon request I quoted the entire program. It's supposed to find the largest palindrome produced by two n digit decimals.]
def palindrome(n):
first = 1
second = 1
largestPalindrome = 1
palindrome = True
while(first < 10**n):
while(second < 10**n):
number = []
candidate = 1
while candidate!=0:
number.append(candidate%10)
candidate = candidate // 10
print("in")
i = 0
ub = len(number)//2
while(i<ub):
if(number[i]!=number[len(number)-1-i]):
palindrome = False
i += 1
if palindrome == True:
largestPalindrome = first*second
print(largestPalindrome)
Your external while loops
while(first < 10**n):
while(second < 10**n):
...
are checking if the variables first and second are under a certain value (10**n). The problem is that, inside these loops, you never increment either first or second, so the condition is always satisfied and your loops keep going on forever.

Python: How do I write a program that reads 10 different integers and prints the one that has the largest value? [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 reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
What is a method, in python, that can be used to read the largest number in a list? My attempt is below:
list = [a, b, c, d, e, f, g, h, i, j]
print max(list)
EDIT (Solution): Use a for loop.
userInput = []
for i in range(10):
userInput.append(int(input("Enter a number")))
print(max(userInput))
You can use a loop if you want to do something repeatedly. You want to have an empty list, and then get a number from the user 10 times, so you put all of the requests for a number from the user in a loop. input gives you a string, so you need to convert it to an int.
user_inputs = []
for i in range(10):
user_inputs.append(int(input("Enter a number")))
print(max(user_inputs))
If you don't understand loops, you should look into learning about them.
I think along with JHobern, you could probably do something like
print(max([int(raw_input("Enter a number: ")) for x in range(10)]))
That should do it!
To explain, you know what print and max should do.
raw_input asks the user for an input and stores it.
max() returns the maximum value in a list of things.
[ ______ for x in range(10)] is a list comprehension where it does something 10 times. Doesn't necessarily use the variable x in this case though.
So basically, I'm using list comprehension to create a list of 10 integers that is provided y the user and then prints out the maximum all in one line :)
You can use a while loop to achieve this:
This code will prompt the user 10 times to input numbers. Each time the number will be appended to the list nums. Also, each time the highest number in nums will be printed.
nums = []
while len(nums) < 10:
nums.append(int(input('Enter a number: ')))
print (max(nums))
If you only want to print the highest number at the end, one time, after the 10th input, then you can do this:
nums = []
while len(nums) < 10:
nums.append(int(input('Enter a value: ')))
print (max(nums))
You shouldn't use list as a variable because list is a built in function

Categories