Reputation: 25
I'm trying to make a program that will check if two numbers have the same digits but in different order. For example, 232 and 223 will print "true" but 123 and 223 will print "false". Now I have no mistakes but the answer should be "true" and it's not:
my code:
a=322
b=223
list_a = list(str(a))
list_b = list(str(b))
c=len(str(a))
d=len(str(b))
j=0
if c != d:
print "false"
else:
for i in range(len(list_a)):
while j<d:
if i == list_b[j]:
list_b.remove(list_b[j])
break
j=j+1
j=0
if list_b==[]:
print "true"
Upvotes: 0
Views: 16629
Reputation: 11
Python Comparison Operators In fact, this is the first and easiest practice about the Python Comparison Operators. Let's Get Code !
first_Number = input('Enter firstNumber: ') #Prompt first number from user
second_Number = input('Enter secondNumber: ') #Prompt second number from user
# Pay Attention that the numbers we got from users are integers in the string Format
first_Number_int = int(first_Number) # Then we need to convert the string to integer ( with int() Function)
second_Number_int = int(second_Number) # Same conversion for second number
#We could write in this way to optimize and legiblely write code instead of the top 4 lines of code in summary.
# first_Number = int(input('Enter firstNumber: '))
# second_Number = int(input('Enter secondNumber: '))
# We need conditional statment to measure and compare between two numbers.
# We'll have three different results.
if (first_Number_int > second_Number_int): # First : first number is bigger than second number
print('firstNumber is Bigger than secondNumber')
elif (first_Number_int < second_Number_int)) : # Second : second number is bigger than first number
print('secondNumber is Bigger than firstNumber')
else: # Third: Equal ( Some people only take two conclusions that only numbers are bigger than each other )
print('firstNumber and secondNumber are Euqal to Eachother')
I hope I've been able to help you, my dear friends.
The problem that my mind is busy with, is there another way to compare two numbers in python? If you, my dear friends, find another solution, I'd be happy to learn.
Thank you so much ,
Sepehr Tajbakhsh
2 November , 2022
Upvotes: 0
Reputation: 1
Comparing two numbers with different digits using sort():
def check_different_numbers_having_same_digits(num1,num2):
num1=[int(i) for i in str(num1)] #converting to int to list
num1.sort() #sorting the list
num2=[int(i) for i in str(num2)]
num2.sort()
if(num1==num2):
return True
else:
return False
print(check_different_numbers_having_same_digits(234,324))
Upvotes: 0
Reputation: 25908
Something like this seems like an obvious way:
#!/usr/bin/python
def same_digits(a, b):
if sorted(str(a)) == sorted(str(b)):
print "{0} and {1} contain the same digits".format(a, b)
else:
print "{0} and {1} do not contain the same digits".format(a, b)
same_digits(232, 232)
same_digits(232, 223)
same_digits(232, 233)
Output:
paul@local:~/src/python/scratch$ ./testnum.py
232 and 232 contain the same digits
232 and 223 contain the same digits
232 and 233 do not contain the same digits
paul@local:~/src/python/scratch$
If you want to match true regardless of the number of each digit, then use set
to eliminate duplicates:
#!/usr/bin/python
def same_digits(a, b):
if sorted(set(str(a))) == sorted(set(str(b))):
print "{0} and {1} contain the same digits".format(a, b)
else:
print "{0} and {1} do not contain the same digits".format(a, b)
same_digits(232, 232)
same_digits(232, 223)
same_digits(232, 233)
same_digits(232, 2333332232)
same_digits(232, 2)
same_digits(232, 234)
Output:
paul@local:~/src/python/scratch$ ./testnum2.py
232 and 232 contain the same digits
232 and 223 contain the same digits
232 and 233 contain the same digits
232 and 2333332232 contain the same digits
232 and 2 do not contain the same digits
232 and 234 do not contain the same digits
paul@local:~/src/python/scratch$
If you really must do it the hard way, then this replicates the first example without using sorted()
:
#!/usr/bin/python
def same_digits_loop(a, b):
a_alpha = str(a)
b_alpha = str(b)
if len(a_alpha) != len(b_alpha):
return False
for c in a_alpha:
b_alpha = b_alpha.replace(c, "", 1)
return False if len(b_alpha) else True
def same_digits(a, b):
if same_digits_loop(a, b):
print "{0} and {1} contain the same digits".format(a, b)
else:
print "{0} and {1} do not contain the same digits".format(a, b)
same_digits(232, 23)
same_digits(232, 232)
same_digits(232, 223)
same_digits(232, 233)
same_digits(232, 2333)
and outputs:
paul@local:~/src/python/scratch$ ./testnum3.py
232 and 23 do not contain the same digits
232 and 232 contain the same digits
232 and 223 contain the same digits
232 and 233 do not contain the same digits
232 and 2333 do not contain the same digits
paul@local:~/src/python/scratch$
For the code you have in your question in your latest edit, just change:
if i == list_b[j]:
to:
if list_a[i] == list_b[j]:
and it'll work. That being said, it won't always work, because when you do this:
while j<d:
every time you remove an element from list_b
, the length of list_b
will change, but d
will not. You'll be going out of bounds when the digits are not the same unless you update d
to equal the new length each time, and check if list_b
has become empty before you've reached the end of list_a
.
Upvotes: 6
Reputation: 29416
i
variable in your snippet is string, not integer, that's why you have TypeError
. Change
if list_a[i] == list_b[j]:
to
if i == list_b[j]:
If you wan to use i
like index, change for
loop like so:
for i in range(len(list_a)):
Also, indices in python start from 0, not from 1, so change
j = 1
to
j = 0
One more correction:
while j <= d:
to
while j < d:
So you won't have index-out-of-range.
Also this one:
list_a = list(str(a))
list_b = list(str(b))
Upvotes: 0
Reputation: 161
You can use the Counter object to obtain the fingerprint of each digits string, and then just compare the two fingerprint, that's all of it:
In [1]: n1 = 123
In [2]: n2 = 312
In [3]: n1, n2 = map(str, [n1, n2])
In [4]: n1,n2
Out[4]: ('123', '312')
In [5]: from collections import Counter
In [6]: c1 = Counter(n1)
In [7]: c2 = Counter(n2)
In [8]: c1 == c2
Out[8]: True
In [9]: c1
Out[9]: Counter({'1': 1, '3': 1, '2': 1})
In [10]: c2
Out[10]: Counter({'1': 1, '3': 1, '2': 1})
If you are not care about the number of digits in the string, you can use the set builtin type to obtain the fingerprint:
In [13]: n1 = 112
In [14]: n2 = 212
In [15]: n1, n2 = map(str, [n1, n2])
In [16]: s1 = set(n1)
In [17]: s2 = set(n2)
In [18]: s1
Out[18]: set(['1', '2'])
In [19]: s2
Out[19]: set(['1', '2'])
In [20]: s1 == s2
Out[20]: True
The only work you should do is just find some kind of Fingerprint!
Upvotes: 1
Reputation: 387
I think it helps
a = 323
b = 233
al = [c for c in str(a)]
bl = [c for c in str(b)]
print sorted(al) == sorted(bl)
Upvotes: -1