+ 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")
15 Respostas
+ 6
Try this code.its right.
x=input()
if(all([x.count(i)==1 for i in x])):
print('Not repetation')
else:
print('repetation')
+ 4
x=input();print(" Not Repeated" if len(set(x))==len(x) else "Repeated")
+ 3
x=input();print("Repeated" if any(x.count(i)>1 for i in x) else "Not repeated") #Bad Programming😬
+ 2
i have to do it only with while and if. cant use length or lists yet
+ 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.
+ 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.")
👍
+ 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.
+ 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 😉
+ 1
right off the top, you are checking if b==b .. That is always True
+ 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?
+ 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")
+ 1
print(["Duplicate digits","Unique digits"][(lambda x: len(set(x))==len(x))(input())])
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.
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.
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")