They don’t do anything to check for a period and the only indicator is it’s variable name. Could be paragraphs or a whole poem. You are right they could split the string into sentences and their variable make a lot more sense, but as is I just see it as a string
First off, filter() takes a function that returns true/false and a list, and returns the elements in the list that return true in that function. Str.isalpha returns true if a string or character is a letter. So filter(str.isalpha, sentence.upper()) returns the letters in sentence (after capitalization); for example, if sentence is "I'm 20 years old.", this returns "IMYEARSOLD".
Then applying set() turns this list into a set; this is another data collection like a list, but with some different qualities, including not accepting repeated values. If you convert a list with duplicates into a set, it only contains the distinct values. Thus, this contains a list of all the distinct letters in the sequence; ie, if the original sentence was "to be or not to be", the set would contain entries of t, o, b, e, r, and n.
Then len() returns the number of entries in this list. If it's 26, then the set contains all 26 letters, so all 26 letters had to appear somewhere in the original string; if not, some letter was missing.
Don't forget to decide whether letters with diacritics count as the English letter or not, and then normalize the input so letters with combining diacritics are treated the same as precomposed characters.
def isatoz(sentence: str):
sentence = sentence.upper()
sentence = sentence.replace(" ", "")
print(sentence)
i = 0
if sentence.count("A") > 0:
i += 1
if sentence.count("B") > 0:
i += 1
if sentence.count("C") > 0:
i += 1
if sentence.count("D") > 0:
i += 1
if sentence.count("E") > 0:
i += 1
if sentence.count("F") > 0:
i += 1
if sentence.count("G") > 0:
i += 1
if sentence.count("H") > 0:
i += 1
if sentence.count("I") > 0:
i += 1
if sentence.count("J") > 0:
i += 1
if sentence.count("K") > 0:
i += 1
if sentence.count("L") > 0:
i += 1
if sentence.count("M") > 0:
i += 1
if sentence.count("N") > 0:
i += 1
if sentence.count("O") > 0:
i += 1
if sentence.count("P") > 0:
i += 1
if sentence.count("Q") > 0:
i += 1
if sentence.count("R") > 0:
i += 1
if sentence.count("S") > 0:
i += 1
if sentence.count("T") > 0:
i += 1
if sentence.count("U") > 0:
i += 1
if sentence.count("V") > 0:
i += 1
if sentence.count("W") > 0:
i += 1
if sentence.count("X") > 0:
i += 1
if sentence.count("Y") > 0:
i += 1
if sentence.count("Z") > 0:
i += 1
print(i)
print(i == 26)
No I looped through the letters and checked if the count of said letter is > 0 and if it is increment i. i kept spitting out 36 no matter what sentence I used
I don’t have the old version, sorry. It’s of no matter now, since I probably did something very wrong in that loop. I’m a beginner and this is only like my second function lol
While it does ‘work’, which nobody is arguing against, it’s still bad code. The function purpose is unclear or misleading, and the whole thing could be done much more succinctly.
i would have done something like i = 0; i += n for n in [sentence.count(c) for c in string.ascii_uppercase] personally. still not the greatest or most readable, but far more clean imo
I don't think this is the thing they are trying to achieve, this counts the number of characters in the string and adds is to I, correct me if I'm wrong
Use a hash table with ord(), map() and filter() then check to see if there’s a 1 in every position. If I wasn’t on my phone I would whip something up but that’s a one liner.
61
u/checkmatemypipi Dec 02 '22
It's so convoluted I don't even understand it, is it counting the characters in a string
3
u/v_maria Dec 02 '22
ok why
51
u/[deleted] Dec 02 '22
But I'm still not sure why.
13
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Dec 02 '22
Image Transcription: Code
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
16
u/Ok-Mulberry-4600 Dec 02 '22
This is actually simpler than using RegEx
2
u/Steve_OH Dec 02 '22 edited Dec 02 '22
I’m on mobile so forgive my pseudocode, but surely the simplest method for this niche requirement would be something like this.
4
u/Temporary-Data-102 Dec 02 '22
Why… just why
1
u/HoneydewPuzzled1914 Dec 19 '22
I see so this subreddit is about people who forgot about loops
Btw I am new
4
u/IvarRagnarssson Dec 02 '22
2
u/samurai_ka Dec 02 '22
If it works, it's not wrong
3
u/mMykros Dec 02 '22
You could technically just sort it and remove the values that repeat themselves
3
u/[deleted] Dec 02 '22
i would have done something like
i = 0; i += n for n in [sentence.count(c) for c in string.ascii_uppercase]personally. still not the greatest or most readable, but far more clean imo1
u/[deleted] Dec 02 '22
Use an array
1
u/the-software-man Dec 02 '22
"The quick brown fox jumps over the lazy dog."
1
u/ReddRobben Dec 02 '22
Use a hash table with ord(), map() and filter() then check to see if there’s a 1 in every position. If I wasn’t on my phone I would whip something up but that’s a one liner.
1
u/-Konrad- Dec 02 '22
Nvm says you’re a beginner, I forgot this wasn’t /r/programminghorror :D
1
u/MotivatedMommy Dec 02 '22
You could sort the string, then iterate through it once and increment a counter whenever the value changes
1
u/Desperate_Depth_9334 Dec 07 '22
hhhhhhhhhhhhhhhhhhhhhh it's impossible
1
u/-Konrad- Dec 02 '22
Come on really :’(
3
u/[deleted] Dec 02 '22
[removed] — view removed comment