fix core::decode
This commit is contained in:
parent
91dbec05d3
commit
49d6049a76
3 changed files with 24 additions and 38 deletions
20
src/base4.rs
20
src/base4.rs
|
@ -1,23 +1,3 @@
|
||||||
/*
|
|
||||||
def decode(string):
|
|
||||||
"""Decode a Base 4 encoded string into the number
|
|
||||||
Arguments:
|
|
||||||
- `string`: The encoded string
|
|
||||||
- `alphabet`: The alphabet to use for decoding
|
|
||||||
"""
|
|
||||||
base = len(alphabet)
|
|
||||||
strlen = len(string)
|
|
||||||
num = 0
|
|
||||||
|
|
||||||
idx = 0
|
|
||||||
for char in string:
|
|
||||||
power = (strlen - (idx + 1))
|
|
||||||
num += alphabet.index(char) * (base ** power)
|
|
||||||
idx += 1
|
|
||||||
|
|
||||||
return num
|
|
||||||
*/
|
|
||||||
|
|
||||||
pub fn alphabet() -> Vec<String> {
|
pub fn alphabet() -> Vec<String> {
|
||||||
return vec!["meow", "mrrp", "mreow", "mrow"]
|
return vec!["meow", "mrrp", "mreow", "mrow"]
|
||||||
.into_iter()
|
.into_iter()
|
||||||
|
|
12
src/core.rs
12
src/core.rs
|
@ -1,5 +1,5 @@
|
||||||
//! Handles conversion one word at a time for any base
|
//! Handles conversion one word at a time for any base
|
||||||
|
// based off this SO answer: https://stackoverflow.com/a/1119769
|
||||||
use std::ops::Index;
|
use std::ops::Index;
|
||||||
|
|
||||||
use crate::base4::alphabet;
|
use crate::base4::alphabet;
|
||||||
|
@ -31,20 +31,20 @@ pub fn num_to_cat(num: u32, alphabet: Vec<String>, char_length: u32) -> String {
|
||||||
/// Converts catspeak to a [`u32`]
|
/// Converts catspeak to a [`u32`]
|
||||||
pub fn cat_to_num(text: Vec<String>, alphabet: Vec<String>, char_length: u32) -> u32 {
|
pub fn cat_to_num(text: Vec<String>, alphabet: Vec<String>, char_length: u32) -> u32 {
|
||||||
let mut nums: Vec<u32> = Vec::new();
|
let mut nums: Vec<u32> = Vec::new();
|
||||||
for word in text.clone() {
|
for word in text {
|
||||||
for i in 0..alphabet.len() {
|
for i in 0..alphabet.len() {
|
||||||
if word == alphabet[i] {
|
if word == alphabet[i] {
|
||||||
nums.push(i as u32);
|
nums.push(i as u32);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let base = alphabet.len();
|
let base = alphabet.len();
|
||||||
let idx = 0;
|
|
||||||
let mut num = 0 as u32;
|
let mut num = 0 as u32;
|
||||||
for n in nums {
|
for n in 0..nums.len() {
|
||||||
let power = (text.len() - (idx + 1)) as u32;
|
let power = (char_length - (n as u32 + 1)) as u32;
|
||||||
num += n * (base as u32).pow(power);
|
num += nums[n] * (base as u32).pow(power);
|
||||||
}
|
}
|
||||||
return num;
|
return num;
|
||||||
}
|
}
|
||||||
|
|
30
src/main.rs
30
src/main.rs
|
@ -14,17 +14,6 @@ fn main() {
|
||||||
stdin.read_line(&mut input).unwrap();
|
stdin.read_line(&mut input).unwrap();
|
||||||
let trimmed = input.trim();
|
let trimmed = input.trim();
|
||||||
if trimmed == "1".to_string() {
|
if trimmed == "1".to_string() {
|
||||||
input = "".to_string();
|
|
||||||
stdin.read_line(&mut input).unwrap();
|
|
||||||
println!(
|
|
||||||
"{}",
|
|
||||||
core::cat_to_num(
|
|
||||||
input.trim().to_string().split(" ").into_iter().map(|item| item.to_string()).collect(),
|
|
||||||
base4::alphabet(),
|
|
||||||
base4::char_length()
|
|
||||||
)
|
|
||||||
)
|
|
||||||
} else if trimmed == "2".to_string() {
|
|
||||||
input = "".to_string();
|
input = "".to_string();
|
||||||
stdin.read_line(&mut input).unwrap();
|
stdin.read_line(&mut input).unwrap();
|
||||||
println!(
|
println!(
|
||||||
|
@ -34,7 +23,24 @@ fn main() {
|
||||||
base4::alphabet(),
|
base4::alphabet(),
|
||||||
base4::char_length()
|
base4::char_length()
|
||||||
)
|
)
|
||||||
)
|
);
|
||||||
|
} else if trimmed == "2".to_string() {
|
||||||
|
input = "".to_string();
|
||||||
|
stdin.read_line(&mut input).unwrap();
|
||||||
|
println!(
|
||||||
|
"{}",
|
||||||
|
core::cat_to_num(
|
||||||
|
input
|
||||||
|
.trim()
|
||||||
|
.to_string()
|
||||||
|
.split(" ")
|
||||||
|
.into_iter()
|
||||||
|
.map(|item| item.to_string())
|
||||||
|
.collect(),
|
||||||
|
base4::alphabet(),
|
||||||
|
base4::char_length()
|
||||||
|
)
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
println!("Invalid input, exiting...");
|
println!("Invalid input, exiting...");
|
||||||
break;
|
break;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue