Skip to content

Instantly share code, notes, and snippets.

@jatinsharrma
Last active February 26, 2022 15:30
Show Gist options
  • Select an option

  • Save jatinsharrma/70648a62274c8421e6d9aa2a03861d4b to your computer and use it in GitHub Desktop.

Select an option

Save jatinsharrma/70648a62274c8421e6d9aa2a03861d4b to your computer and use it in GitHub Desktop.
Check double according to the rules
# Write a python function, check_double(number) which accepts a whole number and returns True if it satisfies the given conditions.
# 1. The number and its double should have exactly the same number of digits.
# 2. Both the numbers should have the same digits ,but in different order.
# Otherwise it should return False.
# convert number to list
#O(n)
def numToList(number):
l_st = []
while(number):
l_st.append(number%10);
number //= 10;
return l_st;
#checking no digits is at same place in both numbers
#O(n)
def samePlace(num, dou):
i = len(num) -1
while(i):
if num[i] == dou[i]:
return True
i-=1;
return False
#checking both have same digits
#O(nlogn)
def sameDigits(num,dou):
num = sorted(num)
dou = sorted(dou)
if (num != dou):
return False
return True
# check_double
#O(nlogn)
def check_double(number):
double = number*2
if number == double:
return False
num = numToList(number)
dou = numToList(double)
if (not samePlace(num,dou) and sameDigits(num,dou)):
return True
return False
# main
if __name__ == "__main__":
print(check_double(125874))
@tanya280924

tanya280924 commented Jun 3, 2021

Copy link
Copy Markdown

if temp[i] == numb:
count += 1
can u explain this part

@jatinsharrma

Copy link
Copy Markdown
Author

if temp[i] == numb:
count += 1
can u explain this part

I don't remember what i did 2 years back, but i guess i missed i+=1, i guess i was checking no digit have same place.
Anyway i have updated the code. Now it will be easy to understand.

@vinaykumar-urs

Copy link
Copy Markdown

i don't how you writing a code but suggest me a good way to become a coder like you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment