most efficient code for printing odd numbers from 1-99 [closed] - python

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 questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
The task is to print the odd numbers from 1-99 on separate lines.
Codeeval deemed this code partially correct (98 out of 100): (edited)
liszt = (i for i in range(1,100) if i%2!=0)
for i in liszt:
print i
Codeeval deemed the below code completely correct:
liszt = range(1,100)
for i in liszt:
if i%2!=0:
print i
New to Python, so just looking to understand why one method might be considered better than the other. Is the second method more efficient?
Thanks for the help!

In the first code you are iterating over two generators first range(1, 100) and then over liszt whereas in the second case the iteration is only over liszt. Other than that the operation is same in both the cases, so the second method is more efficient.
Since every second number after 1 would be odd, a better solution could be:
for i in range(1, 100, 2):
print(i)

print("\n".join(str(i) for i in range(1,100,2)))

Related

How to count if loops in nested for loops and differentiate how much they are fulfilled? [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 questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I have got a NLP task which is performing fine. It assigns one entry of one dataframe to one entry of another dataframe.
Both have an ID in another column. I need to count how many times the IDs are similar, meaning it would be fine if only the first 1-3 digits are equal. The ID consists of one letter and three numbers, I splitted it in 2 columns. If the letter is wrong but the numbers are equal, it is considered useless.
def similar(a, b):
return SequenceMatcher(None, a, b).ratio()
for ID1, ID2in AB:
enumerate(similar(ID1, ID2))
I think the sequence matcher isn't the best option neither. How would I code it to first check the letter and if that is equal check the numbers as well? I would like to put it into a metric counting 0.5 true for just the letter and 1 true for both being correct
The issue after all was that I was putting the function at a wrong indention in the nested for loop.

Use Sum() function with lists [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 questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
What is the correct syntax for using Sum() function in python for lists?
Say we have a list called list_1. When I tried these two syntax I get the same result:
total_1 = Sum(list_1)
total_2 = list_1.Sum()
What is the best syntax for that?
To sum a list of integers or floats, the correct method is:
list_1 = [1, 2, 3]
total = sum(list_1)
print(total)
which results in the output:
6

for loop find in list with if statement [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 questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have the following question:
considering the given list :
list = ['12','8','3']
why does print('8' in list) returns True
whereas
for i in range(5):
if '8' in list == True:
(code)
doesn't execute my code inside the if loop ?
Could someone explain me why, and how could I make this work ?
Maybe this question has already been asked but I don't see with which keywords I should search for it.
Thanks for the help :-)
You seem to have some logical errors in your code, so I'll try to outline what you should be doing.
You shouldn't name variables str or list or int because they might conflict with Python's built in keywords.
Your check did if '8' in list, but that will test if the string 8 is in the list, not the number. Drop the apostrophes.
You don't have to put if 8 in list in a loop, it'll do the looping and testing for you.
Solution
To check if a number is in a list, you can use python's built in in keyword, your write your own code to do the checking.
Remember not to use keywords like list, so I've changed the name to myList in these examples.
Using in
if 8 in myList: # Note that you don't have to say == True
print('8 is in the list!')
Or using for i in myList)
for i in myList:
if i == 8:
print('8 is in the list!')**
Or using for i in range(len(myList))
for i in range(len(myList)):
if myList[i] == 8:
print('8 is in the list!')

Divisors of a number in Python [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 questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 months ago.
Improve this question
I've tried to write a peice of code in python that prints all the divisors of a number including itself. it doesn't work correctly can anyone see why?
def divisors(num):
for x in range (1, num):
if (num % x) == 0:
print(x)
print("Divisors of 6 are")
print(divisors(6))
range() excludes the upper bound and therefore should read range(1, num+1).
you can also try this
num=int(input("please eenter a number "))
a=[]
for x in range(1,num+1):
if num%x==0:
a.append(x)
print(a)

INVALID SYNTAX ERROR for 'else' statement in python [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 questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am trying to write a quicksort program in python, however I'm getting an invalid syntax error at else statement in the second last line below:
import random
n=int(raw_input("Enter the size of the list: ")) # size of the list
intlist = [0]*n
for num in range(n):
intlist[num]=random.randint(0,10*n)
pivot=random.choice(intlist)
list_1=[] # list of elements smaller than pivot
list_2=[] # list of elements greater than pivot
for num in range(n):
if num<=pivot:
list_1.append(num)
else
list_2.append(num)
This is not a complete program as I am still writing.
add a colon after the else so that it looks like else:. and pick up a good tutorial ;)
Looks like you need a ':' after "else".

Categories

Resources