char.IsLetter is using Unicode categories, meaning that it considers much more than just Latin letters to be letters. Also you don’t need to call ToCharArray as String implements IEnumerable<char> and therefore you can apply LINQ directly to strings.
Happily. I never really took the time to learn linq, despite using c# for 8 years. Now, I barely use c#, since most of its features (besides linq) exist in other languages… however, c# will always have a special place in my heart.
I come from a Java background but python makes is much easier. We can observe that the lower (or upper) cased input string when filtered for only letters, needs to have 26 unique ones of them/
def is_panagram(input_str):
unique_char_set = { ch_low for ch in input_str if "a" <= (ch_low := ch.lower()) <= "z" }
return len(unique_char_set) == 26
Of course there are more succinct ways of finding the unique set of characters in the string that're alphabetical
unique_char_set = { ch.lower() for ch in input_str if ch.isalpha() }
Or replacing all the non-alphabetical characters with blanks.
import re
unique_car_set = set(re.sub(r'[^(a-zA-Z]','',input_str).lower())
using System;
public static class Kata
{
public static bool IsPangram(string str)
{
//throw new NotImplementedException();
char[] letters = str.ToLower().ToCharArray();
bool a = false, b = false, c = false, d = false, e = false, f = false, g = false, h = false, i = false, j = false, k = false, l = false, m = false, n = false, o = false, p = false, q = false, r = false, s = false, t = false, u = false, v = false, w = false, x = false, y = false, z = false;
for (int number = 0; number < letters.Length; number++)
{
switch (letters[number])
{
case 'a':
a = true;
break;
case 'b':
b = true;
break;
case 'c':
c = true;
break;
case 'd':
d = true;
break;
case 'e':
e = true;
break;
case 'f':
f = true;
break;
case 'g':
g = true;
break;
case 'h':
h = true;
break;
case 'i':
i = true;
break;
case 'j':
j = true;
break;
case 'k':
k = true;
break;
case 'l':
l = true;
break;
case 'm':
m = true;
break;
case 'n':
n = true;
break;
case 'o':
o = true;
break;
case 'p':
p = true;
break;
case 'q':
q = true;
break;
case 'r':
r = true;
break;
case 's':
s = true;
break;
case 't':
t = true;
break;
case 'u':
u = true;
break;
case 'v':
v = true;
break;
case 'w':
w = true;
break;
case 'x':
x = true;
break;
case 'y':
y = true;
break;
case 'z':
z = true;
break;
default:
break;
}
}
if (a == true && b == true && c == true && d == true && e == true && f == true && g == true && h == true && i == true && j == true && k == true && l == true && m == true && n == true && o == true && p == true && q == true && r == true && s == true && t == true && u == true && v == true && w == true && x == true && y == true && z == true)
{
return true;
}
else
{
return false;
}
}
}
it just looks entirely to complex for the simplistic implementation of this entire file thing. I know it wasnt meant to be that way, but it implies more than it declares lol
How I’d do it with Python: def IsPangram(word): word = word.lower() letters = set() for letter in word: letters.add(letter) return len(letters) == 26
I saw some other implementations in the comments as well, but I believe mine is simpler, so I lobbed it out there. I did assume the inputted word is entirely made up of alphabetical characters, but if it’s not u can j add a if isalpha() check before adding it to the set. Takes a very many line solution down to five lines of code, or 6 if u need the isalpha check. Also not sure how to properly format on Reddit lol
I think the most infuriating bit is the commented out throws. It’s generated code that won’t be added back.
DELETE THE DAMN THING! Stop saving scraps of code littered everywhere that slowly but surely lose all context and meaning while cluttering up the line while trying to read the code.
They use a lookup table but then do a mega switch statement on it, when they could've just done a range check.
These are the people developing the OSes we run on our devices, the web platforms we've been duped into depending on.
When will it end?
The only solution is a decentralization of the internet, a new web browser that functions more like a game engine for creating content, developing applications, user interaction, and sharing all of these. It is the next logical evolution what-with the advent of cryptocurrencies, bittorrent, and all-things-hypertext being so horribly antiquated and stale.
We need a new browser, ultimately a new OS, that blurs the line between online devices. Everyone should have their own domain(s) that they can store data on and share apps/content through, accessible seamlessly from any device. It shouldn't require "webstack" skills to create applications and software that makes efficient use of the available hardware. People should be able to create apps from their smartphone without investing months of their life just to get some kinda bare minimum functioning prototype.
The internet of today is a piece of crap compared to what it could be with the networking and hardware that we are all working with.
Ah yes, break the whole internet so that you can have something "better". The internet is getting better slowly, HTML, CSS and JS are constantly getting new functionality. Replacing those entirely is highly unfeasible as it means all current solutions needing to be rebuilt from the ground up.
The web browser can do a lot, it can host full-scale game engines, it doesn't need to operate like a game engine of its own.
OSes are not magic, having an OS with this focus will never see wide adoption. We need a stable OS that can run applications whilst being easy enough to use that people want to use it. Windows and Mac OS fill both those niches. The operations you described belong in the applications domain, not the OS domain.
You make it sound like everyone can't already get a domain if they so desire. Plenty of registrars out there selling affordable domains. If you don't have money, you can get free ones that are less appealing.
It shouldn't require "webstack" skills to create applications...
Yeah no, that is never happening at any scale, there are plenty of WYSIWYG solutions out there, but you'll find there's a reason they never see wide adoption, and that's because as soon as you want to break off from what's implemented, you need to write code. That will not change.
People should be able to create apps from their smartphone
I mostly agree, and heck, it is even possible nowadays, but trust me when I say that it is a horrible experience, keyboard and mouse are the optimal way to do it that you just can't replace with drag, pinch and virtual keyboard.
The internet of today is a piece...
I'm sure many people would agree with my sentiment on this, I'd much rather not have the browser make use of my hardware as I often have it open alongside whatever interactive media I am using/playing at the time, and a browser chewing up all my resources would be unacceptable.
TheFutureIsDecentralized
Probably, but not for a very, very long time
P.S. CodeWars is for people to learn with, so it is unfair to characterise this to everyone working on today's systems.
People often compete for the fastest execution time on websites like that. Others try to minimize space usage, others try to optimize for both at the same time, and still others go for as few lines of code as possible. It's comparing apples to oranges.
The switch is most likely considerably faster as those generally transform into a single jump instruction. Its main disadvantage from a speed point of view is allocating more memory. The int solution could be faster due to less allocation and how cache-friendly it is, but keep in mind it has a branch dependent on two comparisons, one subtraction, one left shift, and one OR every loop compared to a jump and set. Additionally, the int solution isn't likely to take advantage of any pipelining due to the data dependencies between iterations. It's quite possible the switch solution will execute a handful of jumps simultaneously since the order of execution doesn't affect the result.
You should probably move the s.toLowerCase)).toCharArray into a char array out of the loop so that the functions aren’t called repetitively and unnecessarily. Unless I’m stupid, and if so feel free to tell me that.
It would work with as many letters as can be held by a String.
with a variable for each letter you can always add more!
There is not a variable for each letter, there is a single 32-bit integer t using a bit at each indexed location to represent whether the corresponding letter was encountered while looping through the character array. If all of the corresponding bits were set (no matter how many times each), then it returns true.
Just let the compiler optimize them away into a bitfield
You don’t need the compiler to optimize anything into a bitfield, because that’s exactly what the code is already doing.
An int in Java as you mentioned is 32 bits, so technically they're right, your bitfield could support a maximum of 32 different characters. They made it sound like a problem even though it's not however, as in this use case, you only need 26.
with a variable for each letter
They were talking about the solution OP posted. Though they seem to be defending that horrible solution, yours is much more elegant.
let the compiler...
The person I think was still talking about the OP solution, basically saying the compiler would turn that code into basically what you have, which might be true, I'd need to test it to know for sure.
Overall, the person seems to think that a compiler fixing something is an excuse for writing poor code. I don't agree with them, but wanted to highlight that you misinterpreted their comment a little bit.
Yeah you’re probably right, looks like I missed the segue back to where they started talking about OP’s code again. Explains why it sounded like they horribly misunderstood this (Java) solution.
Overall, the person seems to think that a compiler fixing something is an excuse for writing poor code. I don't agree with them, but wanted to highlight that you misinterpreted their comment a little bit.
People often compete for the fastest execution time on websites like that. Others try to minimize space usage, others try to optimize for both at the same time, and still others go for as few lines of code as possible. It's comparing apples to oranges.
The switch is most likely considerably faster as those generally transform into a single jump instruction. Its main disadvantage from a speed point of view is allocating more memory. The int solution could be faster due to less allocation and how cache-friendly it is, but keep in mind it has a branch dependent on two comparisons, one subtraction, one left shift, and one OR every loop compared to a jump and set. Additionally, the int solution isn't likely to take advantage of any pipelining due to the data dependencies between iterations. It's quite possible the switch solution will execute a handful of jumps simultaneously since the order of execution doesn't affect the result.
The person that I was addressing was advocating for the original solution with the discrete variables for each letter, claiming that the compiler would optimise that to a bit field. Which it probably would, but that code is an unmaintainable mess.
I would personally use a set for a solution to this problem as well.
Variable for each letter is a horrible solution due to horrible maintainability, just look at the if statement on the bottom for instance.
Don't worry about 16 bit integers or more than 26 letters as that is clearly outside the problem domain
But if those two things were a concern, variable for each character is still a bad solution, you could solve this with something like a Hash Set pretty easily and much more maintainably.
45
u/Doug_Dimmadab Jun 05 '22
Ohhh I get why it's bad, the
return falsedidn't need to be in an else statement! /s50
u/[deleted] Jun 05 '22
How I would implement it:
At least it was using switch statement I guess, and the runtime isn't bad.
14
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Jun 05 '22 edited Jun 05 '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!
138
u/post-death_wave_core Jun 05 '22
The a == true, etc is the worst part.
22
u/Eyes_and_teeth Jun 05 '22
This causes me physical pain.
10
u/MarkusBerkel Jun 05 '22
That if statement is incredible.
4
u/Hellow2 Jun 05 '22
Well the goal is sus but really the only other option I can think of would be dictionary and idk I really had to think. So that's understandable
3
u/sirtavvi53194 Jun 06 '22
What is codewars?
2
u/cidit_ Jul 02 '22
remember kids
use combinators profusely
129
u/top_of_the_scrote Jun 05 '22
yOu ShOuLd UsE a SwItCh CaSe
8
u/Vigtor_B Jun 05 '22
Sometimes I think I am worthless and trash at coding... Then I see this shit and get a confidence boost, thx OP.
4
u/Elijah629YT-Real Jun 05 '22
Contains
4
u/Bpofficial Jun 05 '22
I would have just left that throw uncommented
4
u/sirthomasofjorge Jun 05 '22
It’s beautiful; poetry really.
1
u/[deleted] Jun 05 '22
that class name!! hattts offf
1
u/Ahtheuncertainty Jun 05 '22
How I’d do it with Python: def IsPangram(word): word = word.lower() letters = set() for letter in word: letters.add(letter) return len(letters) == 26
I saw some other implementations in the comments as well, but I believe mine is simpler, so I lobbed it out there. I did assume the inputted word is entirely made up of alphabetical characters, but if it’s not u can j add a if isalpha() check before adding it to the set. Takes a very many line solution down to five lines of code, or 6 if u need the isalpha check. Also not sure how to properly format on Reddit lol
3
u/Designer-Yam-2430 Jun 05 '22
Cannot read the code but I can feel something is wrong
3
u/aarocka Jun 05 '22
Oh God my eyes
3
u/1d233f73ae3144b0a624 Jun 06 '22
All of you "higher" language programmers disgust me.
3
u/lordheart Aug 14 '22
I think the most infuriating bit is the commented out throws. It’s generated code that won’t be added back.
DELETE THE DAMN THING! Stop saving scraps of code littered everywhere that slowly but surely lose all context and meaning while cluttering up the line while trying to read the code.
2
u/[deleted] Jun 05 '22
Put all letters in an array, if they are not yet in the array and are ‚a‘ <= letter <= ‚z‘. Return letter.size == 26
2
u/fynn34 Jun 05 '22
This got some form of visceral reaction out of me hahaha.
2
u/[deleted] Dec 28 '22
the 99 line killed me💀
1
u/the_monkey_of_lies Jun 05 '22
It's a war alright
1
u/maksytka03 Jun 05 '22
Shitcode
1
u/[deleted] Jun 05 '22
Passed tests?
What is your problem again?
1
u/mario_mario_ Jun 05 '22
Man wtf is that lol
1
u/raka_boy Jun 14 '22
IndexOf() is slightly offended
1
u/edward1234567890 Jun 26 '22
You could use a for loop to scroll through all the letters, but details
1
u/Remarkable_Rush_9378 Jul 06 '22
Wow! This is a real masterpiece! :)
0
u/Throwawaytravis Jun 05 '22
O(1) motherfucker
-13
u/deftware Jun 05 '22
What the hell?
They use a lookup table but then do a mega switch statement on it, when they could've just done a range check.
These are the people developing the OSes we run on our devices, the web platforms we've been duped into depending on.
When will it end?
The only solution is a decentralization of the internet, a new web browser that functions more like a game engine for creating content, developing applications, user interaction, and sharing all of these. It is the next logical evolution what-with the advent of cryptocurrencies, bittorrent, and all-things-hypertext being so horribly antiquated and stale.
We need a new browser, ultimately a new OS, that blurs the line between online devices. Everyone should have their own domain(s) that they can store data on and share apps/content through, accessible seamlessly from any device. It shouldn't require "webstack" skills to create applications and software that makes efficient use of the available hardware. People should be able to create apps from their smartphone without investing months of their life just to get some kinda bare minimum functioning prototype.
The internet of today is a piece of crap compared to what it could be with the networking and hardware that we are all working with.
TheFutureIsDecentralized
36
u/[deleted] Jun 05 '22
[removed] — view removed comment