strfry a string

This commit is contained in:
Jan-Erik Rediger 2024-05-16 20:19:07 +02:00
commit f9f8f32674
6 changed files with 106 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

7
Cargo.lock generated Normal file
View file

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "strfry"
version = "0.1.0"

8
Cargo.toml Normal file
View file

@ -0,0 +1,8 @@
[package]
name = "strfry"
version = "0.1.0"
edition = "2021"
description = "randomize a string"
license = "MIT"
[dependencies]

20
LICENSE Normal file
View file

@ -0,0 +1,20 @@
Copyright (c) 2024 Jan-Erik Rediger
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

10
README.md Normal file
View file

@ -0,0 +1,10 @@
# Randomize a string
Randomizes the contents of string by randomly swapping characters in the string.
The result is an anagram of string.
```rust
use strfry::strfry;
let anagram = strfry("i am a weakish speller");
```

60
src/lib.rs Normal file
View file

@ -0,0 +1,60 @@
//! Randomize a string
//!
//! Randomizes the contents of string by randomly swapping characters in the string.
//! The result is an anagram of string.
//!
//! ```rust
//! # use strfry::strfry;
//! let anagram = strfry("i am a weakish speller");
//! ```
use std::collections::hash_map::RandomState;
use std::hash::{BuildHasher, Hasher};
fn random() -> usize {
RandomState::new().build_hasher().finish() as usize
}
/// randomize a string
///
/// Randomizes the contents of string by randomly swapping characters in the string.
/// Panics when the string contains non-ASCII characters.
/// The result is an anagram of string.
pub fn strfry(string: &str) -> String {
assert!(string.is_ascii());
let mut output = string.to_string();
{
// SAFETY: It's all ASCII and we just gonna swap around things.
let string = unsafe { output.as_bytes_mut() };
let len = string.len();
for i in 0..len {
let j = random() % (len - i) + i;
string.swap(i, j);
}
}
output
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn it_works() {
let input = "hello world!";
assert_eq!(input.len(), strfry(&input).len());
}
#[test]
fn randomizes() {
for _ in 0..100 {
assert!(matches!(
&*strfry("abc"),
"abc" | "acb" | "bac" | "bca" | "cab" | "cba"
));
}
}
}