initial commit
This commit is contained in:
commit
a01da5ca34
42
Makefile
Normal file
42
Makefile
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
.POSIX:
|
||||||
|
|
||||||
|
RUSTC ?= rustc
|
||||||
|
CC ?= cc
|
||||||
|
|
||||||
|
.PHONY: all
|
||||||
|
all: exercism fun
|
||||||
|
|
||||||
|
.PHONY: exercism
|
||||||
|
exercism: exercism-test protein
|
||||||
|
|
||||||
|
fun: fun-test butt
|
||||||
|
|
||||||
|
.PHONY: build
|
||||||
|
build:
|
||||||
|
mkdir -p build
|
||||||
|
|
||||||
|
.PHONY: clean
|
||||||
|
clean:
|
||||||
|
rm -rf build
|
||||||
|
|
||||||
|
.PHONY: butt
|
||||||
|
butt: build/butt
|
||||||
|
build/butt: build fun/butt.rs
|
||||||
|
$(RUSTC) $(RUSTFLAGS) -o $@ fun/butt.rs
|
||||||
|
|
||||||
|
.PHONY: protein
|
||||||
|
protein: build/protein
|
||||||
|
build/protein: build exercism/protein.rs
|
||||||
|
$(RUSTC) $(RUSTFLAGS) -o $@ exercism/protein.rs
|
||||||
|
|
||||||
|
.PHONY: test
|
||||||
|
test: exercism-test fun-test
|
||||||
|
|
||||||
|
.PHONY: exercism-test
|
||||||
|
exercism-test: build /tmp/protein
|
||||||
|
/tmp/protein: exercism/protein.rs
|
||||||
|
$(RUSTC) --test -o $@ exercism/protein.rs
|
||||||
|
$@
|
||||||
|
|
||||||
|
.PHONY: fun-test
|
||||||
|
fun-test:
|
245
exercism/protein.rs
Normal file
245
exercism/protein.rs
Normal file
@ -0,0 +1,245 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2024 Emma Tebibyte <emma@tebibyte.media>
|
||||||
|
*
|
||||||
|
* Copying and distribution of this file, with or without modification, are
|
||||||
|
* permitted in any medium without royalty provided the copyright notice and
|
||||||
|
* this notice are preserved. This file is offered as-is, without any warranty.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* https://exercism.org/tracks/rust/exercises/protein-translation */
|
||||||
|
|
||||||
|
use std::{
|
||||||
|
convert::TryFrom,
|
||||||
|
env::args,
|
||||||
|
fmt::{ Display, Error, Formatter },
|
||||||
|
io,
|
||||||
|
vec::IntoIter,
|
||||||
|
};
|
||||||
|
|
||||||
|
use Codon::*;
|
||||||
|
|
||||||
|
/* codon type */
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
enum Codon {
|
||||||
|
Methionine,
|
||||||
|
Phenylalanine,
|
||||||
|
Leucine,
|
||||||
|
Serine,
|
||||||
|
Tyrosine,
|
||||||
|
Cysteine,
|
||||||
|
Tryptophan,
|
||||||
|
STOP,
|
||||||
|
}
|
||||||
|
|
||||||
|
/* impls ToString for Codon */
|
||||||
|
impl Display for Codon {
|
||||||
|
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
|
||||||
|
match self {
|
||||||
|
Methionine => write!(f, "Methionine"),
|
||||||
|
Phenylalanine => write!(f, "Phenylalanine"),
|
||||||
|
Leucine => write!(f, "Leucine"),
|
||||||
|
Serine => write!(f, "Serine" ),
|
||||||
|
Tyrosine => write!(f, "Tyrosine"),
|
||||||
|
Cysteine => write!(f, "Cysteine"),
|
||||||
|
Tryptophan => write!(f, "Tryptophan"),
|
||||||
|
STOP => write!(f, "STOP"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* try to convert a String to a Codon and return error message otherwise */
|
||||||
|
impl TryFrom<String> for Codon {
|
||||||
|
type Error = String;
|
||||||
|
|
||||||
|
fn try_from(value: String) -> Result<Self, Self::Error> {
|
||||||
|
match value.as_str() {
|
||||||
|
"AUG" => Ok(Methionine),
|
||||||
|
"UUU" | "UUC" => Ok(Phenylalanine),
|
||||||
|
"UUA" | "UUG" => Ok(Leucine),
|
||||||
|
"UCU" | "UCC" | "UCA" | "UCG" => Ok(Serine),
|
||||||
|
"UAU" | "UAC" => Ok(Tyrosine),
|
||||||
|
"UGU" | "UGC" => Ok(Cysteine),
|
||||||
|
"UGG" => Ok(Tryptophan),
|
||||||
|
"UAA" | "UAG" | "UGA" => Ok(STOP),
|
||||||
|
_ => Err(format!("{}: unknown codon", value)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* protein type */
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
struct Protein(Vec<Codon>);
|
||||||
|
|
||||||
|
/* convenience functions for constructing and appending to Proteins */
|
||||||
|
impl Protein {
|
||||||
|
fn new() -> Self { Protein(Vec::new()) }
|
||||||
|
fn push(&mut self, codon: Codon) { self.0.push(codon); }
|
||||||
|
}
|
||||||
|
|
||||||
|
/* implements to_string() for Protein */
|
||||||
|
impl Display for Protein {
|
||||||
|
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
|
||||||
|
write!(f, "{}", self
|
||||||
|
.clone()
|
||||||
|
.into_iter()
|
||||||
|
.map(|x| x.to_string())
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(" "))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* iterator for Proteins that yields Codons */
|
||||||
|
impl IntoIterator for Protein {
|
||||||
|
type Item = Codon;
|
||||||
|
type IntoIter = IntoIter<Self::Item>;
|
||||||
|
fn into_iter(self) -> Self::IntoIter { self.0.into_iter() }
|
||||||
|
}
|
||||||
|
|
||||||
|
/* try to convert a String to a Protein using Codon::try_from() */
|
||||||
|
impl TryFrom<String> for Protein {
|
||||||
|
type Error = String;
|
||||||
|
|
||||||
|
fn try_from(value: String) -> Result<Self, Self::Error> {
|
||||||
|
let mut protein = Protein::new();
|
||||||
|
let codons = value.chars().collect::<Vec<_>>();
|
||||||
|
|
||||||
|
for codon in codons.chunks(3) {
|
||||||
|
match Codon::try_from(codon.iter().collect::<String>())? {
|
||||||
|
STOP => break,
|
||||||
|
c => protein.push(c),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(protein)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() -> io::Result<()> {
|
||||||
|
for arg in args().skip(1) {
|
||||||
|
println!("{}", Protein::try_from(arg).map_err(|x| io::Error::other(x))?);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use std::convert::TryFrom;
|
||||||
|
use Protein;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn examples() {
|
||||||
|
let sequence = String::from("AUGUUUUCUUAAAUG");
|
||||||
|
let protein = Protein::try_from(sequence).unwrap().to_string();
|
||||||
|
|
||||||
|
assert_eq!(protein, "Methionine Phenylalanine Serine");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn methionine() {
|
||||||
|
let sequence = String::from("AUG");
|
||||||
|
let protein = Protein::try_from(sequence).unwrap().to_string();
|
||||||
|
|
||||||
|
assert_eq!(protein, "Methionine");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn phenylalanine() {
|
||||||
|
let sequence = String::from("UUU");
|
||||||
|
let protein = Protein::try_from(sequence).unwrap().to_string();
|
||||||
|
|
||||||
|
assert_eq!(protein, "Phenylalanine");
|
||||||
|
|
||||||
|
let sequence = String::from("UUC");
|
||||||
|
let protein = Protein::try_from(sequence).unwrap().to_string();
|
||||||
|
assert_eq!(protein, "Phenylalanine");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn leucine() {
|
||||||
|
let sequence = String::from("UUA");
|
||||||
|
let protein = Protein::try_from(sequence).unwrap().to_string();
|
||||||
|
|
||||||
|
assert_eq!(protein, "Leucine");
|
||||||
|
|
||||||
|
let sequence = String::from("UUG");
|
||||||
|
let protein = Protein::try_from(sequence).unwrap().to_string();
|
||||||
|
|
||||||
|
assert_eq!(protein, "Leucine");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn serine() {
|
||||||
|
let sequence = String::from("UCU");
|
||||||
|
let protein = Protein::try_from(sequence).unwrap().to_string();
|
||||||
|
|
||||||
|
assert_eq!(protein, "Serine");
|
||||||
|
|
||||||
|
let sequence = String::from("UCC");
|
||||||
|
let protein = Protein::try_from(sequence).unwrap().to_string();
|
||||||
|
|
||||||
|
assert_eq!(protein, "Serine");
|
||||||
|
|
||||||
|
let sequence = String::from("UCA");
|
||||||
|
let protein = Protein::try_from(sequence).unwrap().to_string();
|
||||||
|
|
||||||
|
assert_eq!(protein, "Serine");
|
||||||
|
|
||||||
|
let sequence = String::from("UCG");
|
||||||
|
let protein = Protein::try_from(sequence).unwrap().to_string();
|
||||||
|
|
||||||
|
assert_eq!(protein, "Serine");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn tyrosine() {
|
||||||
|
let sequence = String::from("UAU");
|
||||||
|
let protein = Protein::try_from(sequence).unwrap().to_string();
|
||||||
|
|
||||||
|
assert_eq!(protein, "Tyrosine");
|
||||||
|
|
||||||
|
let sequence = String::from("UAC");
|
||||||
|
let protein = Protein::try_from(sequence).unwrap().to_string();
|
||||||
|
|
||||||
|
assert_eq!(protein, "Tyrosine");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn cysteine() {
|
||||||
|
let sequence = String::from("UGU");
|
||||||
|
let protein = Protein::try_from(sequence).unwrap().to_string();
|
||||||
|
|
||||||
|
assert_eq!(protein, "Cysteine");
|
||||||
|
|
||||||
|
let sequence = String::from("UGC");
|
||||||
|
let protein = Protein::try_from(sequence).unwrap().to_string();
|
||||||
|
|
||||||
|
assert_eq!(protein, "Cysteine");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn tryptophan() {
|
||||||
|
let sequence = String::from("UGG");
|
||||||
|
let protein = Protein::try_from(sequence).unwrap().to_string();
|
||||||
|
|
||||||
|
assert_eq!(protein, "Tryptophan");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn stop() {
|
||||||
|
let sequence = String::from("UAA");
|
||||||
|
let protein = Protein::try_from(sequence).unwrap().to_string();
|
||||||
|
|
||||||
|
assert_eq!(protein, "");
|
||||||
|
|
||||||
|
let sequence = String::from("UAG");
|
||||||
|
let protein = Protein::try_from(sequence).unwrap().to_string();
|
||||||
|
|
||||||
|
assert_eq!(protein, "");
|
||||||
|
|
||||||
|
let sequence = String::from("UGA");
|
||||||
|
let protein = Protein::try_from(sequence).unwrap().to_string();
|
||||||
|
|
||||||
|
assert_eq!(protein, "");
|
||||||
|
}
|
||||||
|
}
|
30
fun/butt.rs
Normal file
30
fun/butt.rs
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
/*
|
||||||
|
* This work by Emma Tebibyte is marked with CC0 1.0
|
||||||
|
* <http://creativecommons.org/publicdomain/zero/1.0>
|
||||||
|
*/
|
||||||
|
|
||||||
|
// https://www.reddit.com/r/learnpython/comments/17pkw1h/why_wont_my_code_run/
|
||||||
|
|
||||||
|
use std::{
|
||||||
|
io::stdin,
|
||||||
|
process::exit,
|
||||||
|
};
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let mut buf = String::new();
|
||||||
|
|
||||||
|
println!("How big is your butt?");
|
||||||
|
stdin().read_line(&mut buf).unwrap();
|
||||||
|
|
||||||
|
let size = buf.trim().parse::<u8>().unwrap_or_else(|e| {
|
||||||
|
eprintln!("Your butt can’t be {}!", buf.trim());
|
||||||
|
exit(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
println!("{}", match size {
|
||||||
|
0 | 1 | 2 => "Your butt is small Im not gonna smack it ",
|
||||||
|
3 => "Your butt is decently sized, if im super horny ill smack and maybe lick it",
|
||||||
|
4 => "Your butt is pretty big there, Why don't you come down and ill show it a good time!",
|
||||||
|
_ => "WOW Your butt is pretty big! you better pull your pants down RIGHT NOW so i can lick it clean",
|
||||||
|
});
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user