insaner
insaner

Reputation: 1

Python - Count Numbers of Different Digits in String

I'm very new to Python, please help.

I can only use if else statements, no loops or anything special like that.

This program asks the user for one string. It counts the number of different digits in the string. for example a11111a1a1 contains a single digit, while a1s2d1d2d1d2d1 contains 2 digits.

Examples:

% python3 countNumbers.py
enter string: qwerty
there are no digits in string: "qwerty"

% python3 countNumbers.py
enter string: asdf1sdfg
there is one digit in string: "asdf1sdfg"

% python3 countNumbers.py
enter string: as1as2as333333
there are 3 different digits in string: "as1as2as333333"

% python3 countNumbers.py
enter string: a0123456789x
there are 10 different digits in string: "a0123456789x"
num = input("enter string: ")
count = 0

if "0" in num:
    count = count + 1
if "1" in num:
    count = count + 1
if "2" in num:
    count = count + 1
if "3" in num:
    count = count + 1
if "4" in num:
    count = count + 1
if "5" in num:
    count = count + 1
if "6" in num:
    count = count + 1
if "7" in num:
    count = count + 1
if "8" in num:
    count = count + 1
if "9" in num:
    count = count + 1

if count >= 1:
    print("there is one digit in string: ", num)
elif count >= 2:
    print("there are two digits in string: ", num)
else:
    print("there are no digits in string: ", num)

Upvotes: 0

Views: 2675

Answers (6)

Navkant Tyagi
Navkant Tyagi

Reputation: 11

Here it is

count_dict = {}
for i in range(0, 10):
    if i in num and i not in count_dict:
        count_dict[i] = 1 # just adding the integer to a dict
    else:
        continue


print('there are {} different integers in string {}'.format(len(count_dict), num)

Upvotes: -1

Alekh Ranjan
Alekh Ranjan

Reputation: 29

Well if you knew loop it would have been much much easier and would have looked pythonic. However, I have modified your code here and here is the solution using 'if-else' statement and 'sets'

s = set()
if "0" in num:
    s.add(0)
if "1" in num:
    s.add(1)
if "2" in num:
    s.add(2)
if "3" in num:
    s.add(3)
if "4" in num:
    s.add(4)
if "5" in num:
    s.add(5)
if "6" in num:
    s.add(6)
if "7" in num:
    s.add(7)
if "8" in num:
    s.add(8)
if "9" in num:
    s.add(9)
print(len(s))

Upvotes: 0

Nima
Nima

Reputation: 323

num=input()
count=0

for i in num:
    if i in '0123456789':
        count+=1

print(count)

You can use "in" in a single line

Upvotes: -1

drd
drd

Reputation: 605

One liner without re, using plain list and str operations

x='a1s2d1d2d1d2d1'
>>> sum( i.isdigit() for i in list(set(list(x))) )
2
>>>

Breaking it down: convert string to list and then to set and then back to list, will remove duplicates and then count the digits.

>>>
>>> l = list(set(list(x)))
>>> sum( i.isdigit() for i in l )
2

Note: this uses loop inside one liner.

Upvotes: 1

Joan Lara
Joan Lara

Reputation: 1396

You can do it this way:

len(set(a).intersection(map(str, range(10))))

Upvotes: 1

AER
AER

Reputation: 1531

This is using regex, so import the regex library:

import re

Use the following Python to find individual digits:

a = re.findall('\d', string)

If you want more than one digit together use:

a = re.findall('\d+', string)

Then use the following to count (len) the set (which makes it unique):

len(set(a))

Upvotes: 2

Related Questions