Guest User

Untitled

a guest
Apr 26th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. use std::iter::once;
  2. use std::mem;
  3.  
  4. fn find_offset(bytes: &[u8]) -> usize {
  5. bytes.iter().position(|b| b == &b'\n').unwrap()
  6. }
  7.  
  8. fn moved<T>(x: T) -> T {x}
  9. fn hor_mirror(s: String) -> String {
  10.  
  11. let mut bytes_vec = s.into_bytes();
  12. let len = bytes_vec.len();
  13. let offset = find_offset(&bytes_vec);
  14. {
  15. let mut bytes: &mut [u8] = bytes_vec.as_mut();
  16. for _ in 0..(len / offset) / 2 {
  17. {
  18. let (mut slice, mut rest) = bytes.split_at_mut(offset);
  19. for (slice_byte, rest_byte) in slice.iter_mut().zip(rest.iter_mut().rev()) {
  20. mem::swap(slice_byte, rest_byte);
  21. }
  22.  
  23. }
  24. bytes = moved(&mut bytes[offset..]); //WHY
  25. }
  26. }
  27. String::from_utf8(bytes_vec).unwrap()
  28. }
  29.  
  30. fn vert_mirror(s: String) -> String {
  31. let mut bytes = s.into_bytes();
  32. for line in bytes.split_mut(|b| b == &b'\n') {
  33. line.reverse();
  34. }
  35. String::from_utf8(bytes).unwrap()
  36.  
  37. }
  38. // first parameter: dots have to be replaced by function of one variable
  39. fn oper<F: Fn(String) -> String>(f: F, s: String) -> String {
  40. f(s)
  41. }
  42. fn testing1(s: &str, exp: &str) -> () {
  43. assert_eq!(oper(hor_mirror, s.to_string()), exp)
  44. }
  45. fn main() {
  46. testing1("lVHt\nJVhv\nCSbg\nyeCt", "yeCt\nCSbg\nJVhv\nlVHt");
  47. testing1("njMK\ndbrZ\nLPKo\ncEYz", "cEYz\nLPKo\ndbrZ\nnjMK");
  48. testing1("QMxo\ntmFe\nWLUG\nowoq", "owoq\nWLUG\ntmFe\nQMxo");
  49. }
Add Comment
Please, Sign In to add comment