Created
July 20, 2025 13:58
-
-
Save iharkatkavets/279f56fafdce831f3d6e0d49a906da21 to your computer and use it in GitHub Desktop.
Count words problem, compare Rust/Swift performance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::{collections::HashMap, env, fs, io}; | |
use unicode_general_category::GeneralCategory; | |
use unicode_general_category::get_general_category; | |
fn main() { | |
if env::args().len() != 2 { | |
println!("Use {} <FILE_PATH>", env::args().nth(0).unwrap()); | |
return; | |
} | |
if let Some(file_path) = env::args().nth(1) { | |
count_words(&file_path); | |
} else { | |
println!("Something went wrong"); | |
} | |
} | |
fn count_words(file_path: &str) { | |
if let Ok(content) = read_file(file_path) { | |
let lowercased = content.to_lowercase(); | |
let mut total_words_count = 0; | |
let mut counter = HashMap::new(); | |
let mut most_frequent_word = ""; | |
for word in lowercased.split_whitespace() { | |
let word = word.trim_matches(is_punctuation); | |
if word.is_empty() { | |
continue; | |
} | |
*counter.entry(word).or_insert(0) += 1; | |
total_words_count += 1; | |
if counter.get(most_frequent_word).unwrap_or(&0) < &counter[word] { | |
most_frequent_word = word; | |
} | |
} | |
println!("File {} statistics:", file_path); | |
println!("Total words count: {}", total_words_count); | |
println!( | |
"Most frequent word '{}' repeated {} times", | |
most_frequent_word, &counter[most_frequent_word] | |
); | |
} else { | |
println!("Something went wrong"); | |
} | |
} | |
fn read_file(file_path: &str) -> io::Result<String> { | |
let contents = fs::read_to_string(file_path)?; | |
Ok(contents) | |
} | |
fn is_punctuation(c: char) -> bool { | |
matches!( | |
get_general_category(c), | |
GeneralCategory::ConnectorPunctuation | |
| GeneralCategory::DashPunctuation | |
| GeneralCategory::ClosePunctuation | |
| GeneralCategory::FinalPunctuation | |
| GeneralCategory::InitialPunctuation | |
| GeneralCategory::OtherPunctuation | |
| GeneralCategory::OpenPunctuation | |
) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
let arguments = CommandLine.arguments | |
if arguments.count != 2 { | |
print("Usage: \(arguments[0]) <FILE_PATH>") | |
exit(1) | |
} | |
let filePath = arguments[1] | |
do { | |
let contents = try String(contentsOfFile: filePath, encoding: .utf8) | |
var counter = [String: Int]() | |
var mostFrequentWord = "" | |
var totalWordsCount = 0 | |
let words = contents.lowercased() | |
.components(separatedBy: .whitespacesAndNewlines) | |
.map({ $0.trimmingCharacters(in: .punctuationCharacters) }) | |
.filter({ !$0.isEmpty }) | |
for word in words { | |
totalWordsCount += 1 | |
counter[word, default: 0] += 1 | |
if counter[word]! > counter[mostFrequentWord, default: 0] { | |
mostFrequentWord = word | |
} | |
} | |
print("File \(filePath) statistics:") | |
print("Total words count: \(totalWordsCount)") | |
print("Most frequent word '\(mostFrequentWord)' repeated \(counter[mostFrequentWord]!) times") | |
} catch { | |
print("Failed to read file: \(error.localizedDescription)") | |
exit(1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment