Hello guys. Im trying to find a solution, but im struggling with the asnwer and couldnt figure it out yet. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Hello guys. Im trying to find a solution, but im struggling with the asnwer and couldnt figure it out yet.

##The objective of this program is ask to user to insert a number and make the program check if there is any number repetition of the number user has inserted. Ex: 1223. Any ideias? this is what i have done but still doesnt work. Thanks in advance =) a = int(input("insert a number")) while (a > 0): resto = a // 10 b = a % 10 if b == b: print("the number have repetitions") else: print("the number doesnt have repetitions")

22nd Aug 2018, 10:37 AM
André Costa
André Costa - avatar
15 Answers
+ 6
Try this code.its right. x=input() if(all([x.count(i)==1 for i in x])): print('Not repetation') else: print('repetation')
22nd Aug 2018, 11:48 AM
Maninder $ingh
Maninder $ingh - avatar
+ 4
x=input();print(" Not Repeated" if len(set(x))==len(x) else "Repeated")
22nd Aug 2018, 10:44 PM
Kalyan
Kalyan - avatar
+ 3
x=input();print("Repeated" if any(x.count(i)>1 for i in x) else "Not repeated") #Bad Programming😬
22nd Aug 2018, 10:17 PM
Kalyan
Kalyan - avatar
+ 2
i have to do it only with while and if. cant use length or lists yet
22nd Aug 2018, 11:19 AM
André Costa
André Costa - avatar
+ 2
I like Kaylan's idea of using sets. But an ugly workaround using only if and while could be... num = input("blah blah") checkNum = "" alreadyThere = False x = 0 while x < len(num): if num[x] in checkNum: alreadyThere = True else: checkNum += num[x] x+=1 if alreadyThere == False: print("No repetition.") else: print ("Repetition.") I can't see a way of avoiding len(), but interested to see if someone can. Also, you should look at for loops, which would be good for this.
31st Aug 2018, 9:03 PM
benjamin
+ 2
benjamin check out below code num = input("blah blah:") checkNum = "" alreadyThere = False for x in num: if x in checkNum: alreadyThere = True else: checkNum += x if alreadyThere == False: print("No repetition.") else: print ("Repetition.") 👍
31st Aug 2018, 9:10 PM
Kalyan
Kalyan - avatar
+ 2
Kalyan, I agree a for loop is better here, given André's constraints regarding progress on his course, but still think your set solution is best.
31st Aug 2018, 9:13 PM
benjamin
+ 2
num = input("blah blah:") checkNum = "" alreadyThere = False x='' i=0 while checkNum < num: #for x in num: if num[i] in checkNum: alreadyThere = True break else: checkNum += num[i] i+=1 if alreadyThere == False: print("No repetition.") else: print ("Repetition.") 👍👍👍we can reduce above code😊 benjamin. André Costa try to enhance above code 😉
31st Aug 2018, 9:22 PM
Kalyan
Kalyan - avatar
+ 1
right off the top, you are checking if b==b .. That is always True
22nd Aug 2018, 10:48 AM
LordHill
LordHill - avatar
+ 1
Kalyan num = input("blah blah:") checkNum = "" alreadyThere = False i=0 while checkNum != num: if num[i] in checkNum: alreadyThere = True break else: checkNum += num[i] i+=1 print("Repetition status: " + str(alreadyThere)) x was unused and I've got rid of an if statement. Any more reductions?
31st Aug 2018, 9:40 PM
benjamin
+ 1
Here my solution with some help. thank you guys =) x = int(input("digite um numero ")) resto1 = x % 10 x = x // 10 while x != 0: resto = x % 10 if resto == resto1: print("sim") break resto1 = resto x = x // 10 else: print("nao")
3rd Sep 2018, 3:50 PM
André Costa
André Costa - avatar
+ 1
print(["Duplicate digits","Unique digits"][(lambda x: len(set(x))==len(x))(input())])
6th Sep 2018, 8:41 PM
Kalyan
Kalyan - avatar
0
this is an exercise from coursera curse that im doing. however i only learned, until now, if and while. so i assume that the program only can be done with what i had learned until here.
22nd Aug 2018, 12:33 PM
André Costa
André Costa - avatar
0
Kalyan it is a very good solution that avoids len. Good work! I would question your use of < in the while loop. This works, but it would make more sense to use != What are your thoughts? Also, I'll have a go at condensing the code as you suggested.
31st Aug 2018, 9:33 PM
benjamin
0
Here's an alternate solution, this only uses if and while: def IsDupDigit(num): if num > -10 and num < 10: return False ds, r, f = 0, 0, 0 if num < 0: num = -num while num: r = num % 10 f = 2 ** r if ds & f: return True else: ds |= f num //= 10 return False num = int(input("Enter an integer: ")) print(num) print ("Duplicate digits" if IsDupDigit(num) else "Unique digits")
3rd Sep 2018, 4:34 PM
Ipang