Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exercises(isogram): example: refactor #331

Merged
merged 3 commits into from
Oct 4, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 8 additions & 19 deletions exercises/practice/isogram/.meta/example.zig
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
pub fn isIsogram(str: []const u8) bool {
var mask: u32 = 0;
for (str) |value| {
switch (value) {
'a'...'z' => {
if (mask & @as(u32, 1) << @as(u5, @truncate(value - 'a')) == 0) {
mask |= @as(u32, 1) << @as(u5, @truncate(value - 'a'));
} else {
return false;
}
},
'A'...'Z' => {
if (mask & @as(u32, 1) << @as(u5, @truncate(value - 'A')) == 0) {
mask |= @as(u32, 1) << @as(u5, @truncate(value - 'A'));
} else {
return false;
}
},
pub fn isIsogram(s: []const u8) bool {
var letters = [_]bool{false} ** 26;
for (s) |c| {
const i = switch (c) {
'A'...'Z' => c - 'A',
'a'...'z' => c - 'a',
else => continue,
}
};
if (letters[i]) return false else letters[i] = true;
ee7 marked this conversation as resolved.
Show resolved Hide resolved
}
return true;
}