Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::iter::once;
- use std::mem;
- fn find_offset(bytes: &[u8]) -> usize {
- bytes.iter().position(|b| b == &b'\n').unwrap()
- }
- fn moved<T>(x: T) -> T {x}
- fn hor_mirror(s: String) -> String {
- let mut bytes_vec = s.into_bytes();
- let len = bytes_vec.len();
- let offset = find_offset(&bytes_vec);
- {
- let mut bytes: &mut [u8] = bytes_vec.as_mut();
- for _ in 0..(len / offset) / 2 {
- {
- let (mut slice, mut rest) = bytes.split_at_mut(offset);
- for (slice_byte, rest_byte) in slice.iter_mut().zip(rest.iter_mut().rev()) {
- mem::swap(slice_byte, rest_byte);
- }
- }
- bytes = moved(&mut bytes[offset..]); //WHY
- }
- }
- String::from_utf8(bytes_vec).unwrap()
- }
- fn vert_mirror(s: String) -> String {
- let mut bytes = s.into_bytes();
- for line in bytes.split_mut(|b| b == &b'\n') {
- line.reverse();
- }
- String::from_utf8(bytes).unwrap()
- }
- // first parameter: dots have to be replaced by function of one variable
- fn oper<F: Fn(String) -> String>(f: F, s: String) -> String {
- f(s)
- }
- fn testing1(s: &str, exp: &str) -> () {
- assert_eq!(oper(hor_mirror, s.to_string()), exp)
- }
- fn main() {
- testing1("lVHt\nJVhv\nCSbg\nyeCt", "yeCt\nCSbg\nJVhv\nlVHt");
- testing1("njMK\ndbrZ\nLPKo\ncEYz", "cEYz\nLPKo\ndbrZ\nnjMK");
- testing1("QMxo\ntmFe\nWLUG\nowoq", "owoq\nWLUG\ntmFe\nQMxo");
- }
Add Comment
Please, Sign In to add comment