I'm stuck on finding how many odd digits are in a number for python - python-3.x

So we have to write a function in python to count how many digits of a non-negative integer are odd numbers.
def odd_dig(n):
ans = 0
for i in range(n):
if i in range(n) %2 == 1:
ans += 1
elif n[i]==0:
return None

I don't know python, but for a solution, why not something like the following?
while n > 0 do these two things:
add (n%2) to your count
divide n by 10
because n is defined as non-negative we should be fine with this.

You're using range(n) which creates a list from 1 to n. So when you do range(5), in your for loop, you're iterating over 1,2,3,4,5 instead of the actual digits of n.
If you want to find the number of digits you can do something like len(str(n)) which finds the length of the string form of n.
After that, make sure you're using the right operators on the right variables (string operators on strings, list operators on lists, etc...).

Related

What can I do to add a list and sort out all the prime numbers in the list? Python 3

I am creating a program that
accepts an inputted list
finds all the prime numbers and only displays them.
I tried many different methods, many derived from existing prime filters, but they have hardcoded lists rather user-inputted ones.
I just can't seem to get a filter working with inputting a list, then filtering the prime numbers.
my_list = input("Please type a list")
list(my_list)
prime=[]
for i in my_list:
c=0
for j in range(1,i):
if i%j==0:
c+=1
if c==1:
prime.append(i)
return (prime)
When you get input, you're getting a string. You can't cast a string to a list immediately. Maybe you can request the user to use a separator between the numbers then use split method and cast strings to integers like this:
my_list = input("Please enter the list of numbers and use space seperator")
s_list = my_list.split()
cast_list = [int(num) for num in s_list]
Then, you can work on your prime number task based on your preferred algorithm.
Not sure what your c variable is for, current_number? Your loop returns 'str' object cannot be interpreted as an integer for me. I have used len(my_list) to get the length for the loop.
range() defines as range(start, stop, step) - learn more - it accepts integers and parameters are partially optional.
I copied the code from https://www.codegrepper.com/code-examples/python/how+to+find+prime+numbers+in+list+python
my_list = input("Please type a list")
primes = []
for i in range(0, len(my_list)):
for j in range(2, int(i ** 0.5) + 1):
if i%j == 0:
break
else:
primes.append(i)
print(primes)
More helpful resources from SO: Python function for prime number
I hope this helps.

How to extract numbers with repeating digits within a range

I need to identify the count of numbers with non-repeating digits in the range of two numbers.
Suppose n1=11 and n2=15.
There is the number 11, which has repeated digits, but 12, 13, 14 and 15 have no repeated digits. So, the output is 4.
Wrote this code:
n1=int(input())
n2=int(input())
count=0
for i in range(n1,n2+1):
lst=[]
x=i
while (n1>0):
a=x%10
lst.append(a)
x=x//10
for j in range(0,len(lst)-1):
for k in range(j+1,len(lst)):
if (lst[j]==lst[k]):
break
else:
count=count+1
print (count)
While running the code and after inputting the two numbers, it does not run the code but still accepts input. What did I miss?
The reason your code doesn't run is because it gets stuck in your while loop, it can never exit that condition, since n1 > 0 will never have a chance to be evaluated as False, unless the input itself is <= 0.
Anyway, your approach is over complicated, not quite readable and not exactly pythonic. Here's a simpler, and more readable approach:
from collections import Counter
n1 = int(input())
n2 = int(input())
count = 0
for num in range(n1, n2+1):
num = str(num)
digit_count = Counter(num)
has_repeating_digits = any((True for count in digit_count.values() if count > 1))
if not has_repeating_digits:
count += 1
print(count)
When writing code, in general you should try to avoid nesting too much stuff (in your original example you have 4 nested loops, that's readability and debugging nightmare), and try using self-describing variable names (so a, x, j, k, b... are kind of a no-go).
If in a IPython session you run import this you can also read the "Zen of Python", which kind of sums up the concept of writing proper pythonic code.

I don't know why the correct answer isn't coming up

I'm novice programmer.
I want the smallest of the input values ​​to be output, but I don't know what's wrong.
Input example :
10
10 4 2 3 6 6 7 9 8 5
Output example :
2
n = int(input())
a = input().split()
min=a[0]
for i in range(n) :
if a[i] < min :
min = a[i]
print(min)
what is the problem? please help me
Your code should work (and it does for me).
Nevertheless, min is a reserved Python word. Taking that into consideration, I also recommend the following changes for it to be more idiomatic:
a = input().split()
min_num = a[0]
for element in a:
if element < min :
min = element
print(min)
Variables can be either number or strings. For example "2" is different from 2.
The function split returns an array of strings. You would need to convert each to a number if you want to do number comparison, like:
n = int(input())
a = input().split()
min=int(a[0])
for i in range(n) :
if int(a[i]) < min :
min = int(a[i])
print(min)
Note: you already did that for n (first line in original code), but you did not do the same when you access a.
So, min is actually a python built-in, which would be very useful in this scenario. Also, you are not making the input values in a into integers, so we can do that too:
n = int(input())
a = list(map(int, input().split()))
print(min(a))
We use map to turn all the values from the split list into integers, then turn the map object back into a list. Then, using min, we can find the smallest number very easily, without a for loop.
I think you should convert each element to integers before comparing them.
a = [int(i) for i in input().split()]
Your code should work, but it will compare strings against strings instead of integers against integers.

Is this a right way to check pandigital number upto 9?

While solving problem 32 of project eular, I made this function to check pandigital number, is this a right way to check pandigital number? I think there is some problem, but I can't figure out it myself.
def pandigital(number, digit): # digit: n-digit pandigital number
l = ['{}'.format(j) for j in [i for i in range(1,digit+1)]]
g = list(str(number))
g.sort()
if g == l:
return True
Yes, there is a problem. In fact, three of them.
First, if a number contains more than one digit of the same denomination (duplicates), then g is not equal to l because l has only one copy of each digit. You should convert l and g to sets before the comparison.
Second, your function does not return anything if the number is not pandigital.
Finally, range(1,digit+1) does not include 0 (unless you want zeroless pandigital numbers).
Consider the following solution:
def pandigital(number, digit)
return {f'{i}' for i in range(digit)} == set(str(number))
Replace f'{i}' with '{}'.format(i) if you want to stick to more "classical" Python.

how do I call an element in a list?

a= int(input())
# I input 12345
b = a
list(map(int, b))
print (list[0]*2+list[3]*1)
#can't seem to get 6 as my answer
how do I attain my answer? I can't seem to call the elements in the list. Thank you for your help.
Since you're treating the input as individual digits, you should avoid converting the input to an integer as a whole, but map the individual digits to integers as a sequence of characters:
a= input()
b = list(map(int, a))
print(b[0] * 2 + b[3] * 1)
There are several reasons why your code won't work, including your use of the map function, the fact that you do not assign the result to a variable and the use of list (which is a keyword in Python).
However, consider this code snippet which calculates your desired output:
a = int(input('Enter a number: '))
b = [int(digit) for digit in str(a)]
res = 2 * b[0] + b[3]
print(res)
Basically you have to transform your integer into a string to be able to iterate over it. Afterwards you create your list of digits out of it and can do your calculations.
Generally speaking, you should learn the basics of Python properly. A good starting point would be the official documentation (LINK).

Resources