0
Isogram detector : simplify my code
word = input() dict ={} for x in word: if x not in dict: dict[x]=1 else: dict[x]+=1 final=list(dict.values()) if max(final)==1: print("true") else: print("false") Hello, I need help to simplify the end of my code. I had to convert my dictionary to a list to use the max() command. Would you find an easier way to make it works ? (Isogram detector challenge, it has to output true is the word dosent contains 2 time the same letter and false if a word contains 2 times the same letter. Word = true Carrot = false) Thank you :)
4 Antworten
+ 6
Max Lang ,
you can use max() also for dictionaries:
...
if max(dict.values()) == 1:
print("true")
else:
print("false")
allow me some hints for general improofment:
- please use proper indentation of 4 spaces per indentation level
- do not use names of objects / classes like 'dict' as a variable name, because this can cause issues
+ 3
Remember that a set holds only unique values. You could build a set from all the letters in the original word, then compare the set's length with the original word length.
word = input()
s = set(i for i in word)
print(len(s)==len(word))
+ 2
Max Lang ,
see the code how it should be indented:
https://code.sololearn.com/cXG86pIKhyMr/?ref=app
+ 1
Thank you very much both of you for your answers, it amaze me to see its always so easy 🤣😅.
Can you explain me what you mean by using proper indentation of 4 spaces ? Thank you !